diff options
| author | Shulhan <ms@kilabit.info> | 2022-08-03 22:27:33 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2022-08-03 22:27:33 +0700 |
| commit | ec98ea095fa85b5a4b64bd7ad37414c30d14310e (patch) | |
| tree | a0d0af0ad27852b1644972ee2b11e870ea5acc03 /lib/strings/string_test.go | |
| parent | 39d9c20c35a11c44cdd27783e407c1225027d820 (diff) | |
| download | pakakeh.go-ec98ea095fa85b5a4b64bd7ad37414c30d14310e.tar.xz | |
lib/strings: clean up test codes
Changes,
* Use test.Data for test that require longer text input and output
* Replace variable declaration ":=" with explicit one.
* Use literal string
Diffstat (limited to 'lib/strings/string_test.go')
| -rw-r--r-- | lib/strings/string_test.go | 215 |
1 files changed, 99 insertions, 116 deletions
diff --git a/lib/strings/string_test.go b/lib/strings/string_test.go index 62433dd2..b3b68535 100644 --- a/lib/strings/string_test.go +++ b/lib/strings/string_test.go @@ -5,83 +5,57 @@ package strings import ( + "strings" "testing" "github.com/shuLhan/share/lib/test" ) func TestCleanURI(t *testing.T) { - cases := []struct { - text string - exp string - }{{ - // Empty - }, { - text: `ftp://test.com/123 The [[United States]] has regularly voted alone and against international consensus, using its [[United Nations Security Council veto power|veto power]] to block the adoption of proposed UN Security Council resolutions supporting the [[PLO]] and calling for a two-state solution to the [[Israeli-Palestinian conflict]].<ref>[http://books.google.ca/books?id=CHL5SwGvobQC&pg=PA168&dq=US+veto+Israel+regularly#v=onepage&q=US%20veto%20Israel%20regularly&f=false Pirates and emperors, old and new: international terrorism in the real world], [[Noam Chomsky]], p. 168.</ref><ref>The US has also used its veto to block resolutions that are critical of Israel.[https://books.google.ca/books?id=yzmpDAz7ZAwC&pg=PT251&dq=US+veto+Israel+regularly&lr=#v=onepage&q=US%20veto%20Israel%20regularly&f=false Uneasy neighbors], David T. Jones and David Kilgour, p. 235.</ref> The United States responded to the frequent criticism from UN organs by adopting the [[Negroponte doctrine]].`, - exp: ` The [[United States]] has regularly voted alone and against international consensus, using its [[United Nations Security Council veto power|veto power]] to block the adoption of proposed UN Security Council resolutions supporting the [[PLO]] and calling for a two-state solution to the [[Israeli-Palestinian conflict]].<ref>[ Pirates and emperors, old and new: international terrorism in the real world], [[Noam Chomsky]], p. 168.</ref><ref>The US has also used its veto to block resolutions that are critical of Israel.[ Uneasy neighbors], David T. Jones and David Kilgour, p. 235.</ref> The United States responded to the frequent criticism from UN organs by adopting the [[Negroponte doctrine]].`, - }} - - for _, c := range cases { - got := CleanURI(c.text) + var ( + tdata *test.Data + err error + exp string + got string + ) - test.Assert(t, "", c.exp, got) + tdata, err = test.LoadData(`testdata/clean_uri_test.txt`) + if err != nil { + t.Fatal(err) } -} - -func TestCleanWikiMarkup(t *testing.T) { - cases := []struct { - text string - exp string - }{{ - text: `==External links== -*[http://www.bigfinish.com/24-Doctor-Who-The-Eye-of-the-Scorpion Big Finish Productions - ''The Eye of the Scorpion''] -*{{Doctor Who RG | id=who_bf24 | title=The Eye of the Scorpion}} -===Reviews=== -* Test image [[Image:fileto.png]]. -* Test file [[File:fileto.png]]. -*{{OG review | id=bf-24 | title=The Eye of the Scorpion}} -*{{DWRG | id=eyes | title=The Eye of the Scorpion}} - -<br clear="all"> -{{Fifthdoctoraudios}} - -{{DEFAULTSORT:Eye of the Scorpion, The}} -[[Category:Fifth Doctor audio plays]] -[[Category:Fifth Doctor audio plays]] -[[:Category:2001 audio plays]] -{{DoctorWho-stub}}`, - exp: `==External links== -*[http://www.bigfinish.com/24-Doctor-Who-The-Eye-of-the-Scorpion Big Finish Productions - ''The Eye of the Scorpion''] -*{{Doctor Who RG | id=who_bf24 | title=The Eye of the Scorpion}} -===Reviews=== -* Test image . -* Test file . -*{{OG review | id=bf-24 | title=The Eye of the Scorpion}} -*{{DWRG | id=eyes | title=The Eye of the Scorpion}} - -<br clear="all"> -{{Fifthdoctoraudios}} - + exp = string(tdata.Output[`default`]) + got = CleanURI(string(tdata.Input[`default`])) + test.Assert(t, ``, exp, got) +} +func TestCleanWikiMarkup(t *testing.T) { + var ( + tdata *test.Data + err error + exp string + got string + ) -{{DoctorWho-stub}}`, - }} + tdata, err = test.LoadData(`testdata/clean_wiki_markup_test.txt`) + if err != nil { + t.Fatal(err) + } - for _, c := range cases { - got := CleanWikiMarkup(c.text) + exp = string(tdata.Output[`default`]) + got = CleanWikiMarkup(string(tdata.Input[`default`])) - test.Assert(t, "", c.exp, got) - } + test.Assert(t, ``, exp, got) } func TestMergeSpaces(t *testing.T) { - cases := []struct { + type testCase struct { text string - withline bool exp string - }{{ + withline bool + } + var cases = []testCase{{ text: " a\n\nb c d\n\n", exp: " a\n\nb c d\n\n", }, { @@ -93,95 +67,104 @@ func TestMergeSpaces(t *testing.T) { exp: " a\nb c d\n", }} - for _, c := range cases { - got := MergeSpaces(c.text, c.withline) - - test.Assert(t, "", c.exp, got) + var ( + c testCase + got string + ) + for _, c = range cases { + got = MergeSpaces(c.text, c.withline) + test.Assert(t, ``, c.exp, got) } } func TestReverse(t *testing.T) { - cases := []struct { + type testCase struct { input string exp string - }{{ - input: "The quick bròwn 狐 jumped over the lazy 犬", - exp: "犬 yzal eht revo depmuj 狐 nwòrb kciuq ehT", + } + var cases = []testCase{{ + input: `The quick bròwn 狐 jumped over the lazy 犬`, + exp: `犬 yzal eht revo depmuj 狐 nwòrb kciuq ehT`, }} - for _, c := range cases { - got := Reverse(c.input) - - test.Assert(t, "Reverse", c.exp, got) + var ( + c testCase + got string + ) + for _, c = range cases { + got = Reverse(c.input) + test.Assert(t, `Reverse`, c.exp, got) } } func TestSingleSpace(t *testing.T) { - cases := []struct { + type testCase struct { in string exp string - }{{ - in: "", + } + + var cases = []testCase{{ + // Empty input. }, { in: " \t\v\r\n\r\n\fa \t\v\r\n\r\n\f", exp: " a ", }} - for _, c := range cases { - got := SingleSpace(c.in) + + var ( + c testCase + got string + ) + for _, c = range cases { + got = SingleSpace(c.in) test.Assert(t, c.in, c.exp, got) } } func TestSplit(t *testing.T) { - cases := []struct { - text string - exp []string - }{{ - text: `// Copyright 2016-2018 Shulhan <ms@kilabit.info>. All rights reserved.`, - exp: []string{"Copyright", "2016-2018", "Shulhan", - "ms@kilabit.info", "All", "rights", "reserved"}, - }, { - text: `The [[United States]] has regularly voted alone and - against international consensus, using its [[United Nations - Security Council veto power|veto power]] to block the adoption - of proposed UN Security Council resolutions supporting the - [[PLO]] and calling for a two-state solution to the - [[Israeli-Palestinian conflict]].`, - exp: []string{"The", "United", "States", "has", "regularly", - "voted", "alone", "and", "against", "international", - "consensus", "using", "its", "Nations", "Security", - "Council", "veto", "power|veto", "power", "to", - "block", "adoption", "of", "proposed", "UN", - "resolutions", "supporting", "PLO", "calling", - "for", "a", "two-state", "solution", - "Israeli-Palestinian", "conflict", - }, - }} + var ( + tdata *test.Data + err error + name string + got []string + exp []string + raw []byte + ) - for _, c := range cases { - got := Split(c.text, true, true) - test.Assert(t, "", c.exp, got) + tdata, err = test.LoadData(`testdata/split_test.txt`) + if err != nil { + t.Fatal(err) + } + + for name, raw = range tdata.Input { + got = Split(string(raw), true, true) + raw = tdata.Output[name] + exp = strings.Fields(string(raw)) + test.Assert(t, name, exp, got) } } func TestTrimNonAlnum(t *testing.T) { - cases := []struct { + type testCase struct { text string exp string - }{ - {"[[alpha]]", "alpha"}, - {"[[alpha", "alpha"}, - {"alpha]]", "alpha"}, - {"alpha", "alpha"}, - {"alpha0", "alpha0"}, - {"1alpha", "1alpha"}, - {"1alpha0", "1alpha0"}, - {"[][][]", ""}, + } + var cases = []testCase{ + {`[[alpha]]`, `alpha`}, + {`[[alpha`, `alpha`}, + {`alpha]]`, `alpha`}, + {`alpha`, `alpha`}, + {`alpha0`, `alpha0`}, + {`1alpha`, `1alpha`}, + {`1alpha0`, `1alpha0`}, + {`[][][]`, ``}, } - for _, c := range cases { - got := TrimNonAlnum(c.text) - - test.Assert(t, "", c.exp, got) + var ( + c testCase + got string + ) + for _, c = range cases { + got = TrimNonAlnum(c.text) + test.Assert(t, ``, c.exp, got) } } |
