From ec98ea095fa85b5a4b64bd7ad37414c30d14310e Mon Sep 17 00:00:00 2001 From: Shulhan Date: Wed, 3 Aug 2022 22:27:33 +0700 Subject: 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 --- lib/strings/row_example_test.go | 20 +- lib/strings/row_test.go | 84 +++--- lib/strings/statistic_example_test.go | 66 +++-- lib/strings/statistic_test.go | 362 +++++++++++++++--------- lib/strings/string_example_test.go | 98 ++++--- lib/strings/string_test.go | 215 +++++++------- lib/strings/strings_example_test.go | 106 ++++--- lib/strings/strings_test.go | 344 +++++++++++++--------- lib/strings/table_example_test.go | 43 +-- lib/strings/table_test.go | 156 +++++----- lib/strings/testdata/clean_uri_test.txt | 31 ++ lib/strings/testdata/clean_wiki_markup_test.txt | 37 +++ lib/strings/testdata/split_test.txt | 23 ++ lib/strings/to_example_test.go | 32 ++- lib/strings/to_test.go | 39 +-- 15 files changed, 1003 insertions(+), 653 deletions(-) create mode 100644 lib/strings/testdata/clean_uri_test.txt create mode 100644 lib/strings/testdata/clean_wiki_markup_test.txt create mode 100644 lib/strings/testdata/split_test.txt diff --git a/lib/strings/row_example_test.go b/lib/strings/row_example_test.go index 45292e94..0b4acfc0 100644 --- a/lib/strings/row_example_test.go +++ b/lib/strings/row_example_test.go @@ -9,12 +9,12 @@ import ( ) func ExampleRow_IsEqual() { - row := Row{{"a"}, {"b", "c"}} - fmt.Println(row.IsEqual(Row{{"a"}, {"b", "c"}})) - fmt.Println(row.IsEqual(Row{{"a"}, {"c", "b"}})) - fmt.Println(row.IsEqual(Row{{"c", "b"}, {"a"}})) - fmt.Println(row.IsEqual(Row{{"b", "c"}, {"a"}})) - fmt.Println(row.IsEqual(Row{{"a"}, {"b"}})) + var row = Row{{`a`}, {`b`, `c`}} + fmt.Println(row.IsEqual(Row{{`a`}, {`b`, `c`}})) + fmt.Println(row.IsEqual(Row{{`a`}, {`c`, `b`}})) + fmt.Println(row.IsEqual(Row{{`c`, `b`}, {`a`}})) + fmt.Println(row.IsEqual(Row{{`b`, `c`}, {`a`}})) + fmt.Println(row.IsEqual(Row{{`a`}, {`b`}})) // Output: // true // true @@ -24,11 +24,11 @@ func ExampleRow_IsEqual() { } func ExampleRow_Join() { - row := Row{{"a"}, {"b", "c"}} - fmt.Println(row.Join(";", ",")) + var row = Row{{`a`}, {`b`, `c`}} + fmt.Println(row.Join(`;`, `,`)) - row = Row{{"a"}, {}} - fmt.Println(row.Join(";", ",")) + row = Row{{`a`}, {}} + fmt.Println(row.Join(`;`, `,`)) // Output: // a;b,c // a; diff --git a/lib/strings/row_test.go b/lib/strings/row_test.go index b3ac097d..2d63f501 100644 --- a/lib/strings/row_test.go +++ b/lib/strings/row_test.go @@ -11,56 +11,70 @@ import ( ) func TestRowIsEqual(t *testing.T) { - cases := []struct { - a, b Row - exp bool - }{{ - a: Row{{"a"}, {"b", "c"}}, - b: Row{{"a"}, {"b", "c"}}, + type testCase struct { + a Row + b Row + exp bool + } + + var cases = []testCase{{ + a: Row{{`a`}, {`b`, `c`}}, + b: Row{{`a`}, {`b`, `c`}}, exp: true, }, { - a: Row{{"a"}, {"b", "c"}}, - b: Row{{"a"}, {"c", "b"}}, + a: Row{{`a`}, {`b`, `c`}}, + b: Row{{`a`}, {`c`, `b`}}, exp: true, }, { - a: Row{{"a"}, {"b", "c"}}, - b: Row{{"c", "b"}, {"a"}}, + a: Row{{`a`}, {`b`, `c`}}, + b: Row{{`c`, `b`}, {`a`}}, exp: true, }, { - a: Row{{"a"}, {"b", "c"}}, - b: Row{{"a"}, {"b", "a"}}, + a: Row{{`a`}, {`b`, `c`}}, + b: Row{{`a`}, {`b`, `a`}}, }} - for _, c := range cases { - got := c.a.IsEqual(c.b) - test.Assert(t, "", c.exp, got) + var ( + c testCase + got bool + ) + for _, c = range cases { + got = c.a.IsEqual(c.b) + test.Assert(t, ``, c.exp, got) } } func TestRowJoin(t *testing.T) { - cases := []struct { - row Row - lsep, ssep string - exp string - }{{ - // - lsep: ";", - ssep: ",", - exp: "", + type testCase struct { + lsep string + ssep string + exp string + row Row + } + var cases = []testCase{{ + // Empty input. + }, { + lsep: `;`, + ssep: `,`, + exp: ``, }, { - row: Row{{"a"}, {}}, - lsep: ";", - ssep: ",", - exp: "a;", + row: Row{{`a`}, {}}, + lsep: `;`, + ssep: `,`, + exp: `a;`, }, { - row: Row{{"a"}, {"b", "c"}}, - lsep: ";", - ssep: ",", - exp: "a;b,c", + row: Row{{`a`}, {`b`, `c`}}, + lsep: `;`, + ssep: `,`, + exp: `a;b,c`, }} - for _, c := range cases { - got := c.row.Join(c.lsep, c.ssep) - test.Assert(t, "", c.exp, got) + var ( + c testCase + got string + ) + for _, c = range cases { + got = c.row.Join(c.lsep, c.ssep) + test.Assert(t, ``, c.exp, got) } } diff --git a/lib/strings/statistic_example_test.go b/lib/strings/statistic_example_test.go index e3df7d56..be989d64 100644 --- a/lib/strings/statistic_example_test.go +++ b/lib/strings/statistic_example_test.go @@ -9,89 +9,107 @@ import ( ) func ExampleCountAlnum() { - fmt.Println(CountAlnum("// A b c 1 2 3")) + fmt.Println(CountAlnum(`// A b c 1 2 3`)) // Output: 6 } func ExampleCountAlnumDistribution() { - chars, counts := CountAlnumDistribution("// A b c A b") - fmt.Printf("%c %v\n", chars, counts) + var ( + chars []rune + counts []int + ) + + chars, counts = CountAlnumDistribution(`// A b c A b`) + + fmt.Printf(`%c %v`, chars, counts) // Output: [A b c] [2 2 1] } func ExampleCountCharSequence() { - text := "aaa abcdee ffgf" + var ( + text = `aaa abcdee ffgf` + chars []rune + counts []int + ) - chars, counts := CountCharSequence(text) + chars, counts = CountCharSequence(text) // 'a' is not counted as 4 because its breaked by another character, // space ' '. - fmt.Printf("%c %v\n", chars, counts) + fmt.Printf(`%c %v`, chars, counts) + // Output: // [a e f] [3 2 2] } func ExampleCountDigit() { - text := "// Copyright 2018 Mhd Sulhan . All rights reserved." + var text = `// Copyright 2018 Mhd Sulhan . All rights reserved.` fmt.Println(CountDigit(text)) // Output: 4 } func ExampleCountUniqChar() { - fmt.Println(CountUniqChar("abc abc")) - fmt.Println(CountUniqChar("abc ABC")) + fmt.Println(CountUniqChar(`abc abc`)) + fmt.Println(CountUniqChar(`abc ABC`)) // Output: // 4 // 7 } func ExampleCountUpperLower() { - fmt.Println(CountUpperLower("// A B C d e f g h I J K")) + fmt.Println(CountUpperLower(`// A B C d e f g h I J K`)) // Output: 6 5 } func ExampleMaxCharSequence() { - c, n := MaxCharSequence("aaa abcdee ffgf") + var ( + c rune + n int + ) + + c, n = MaxCharSequence(`aaa abcdee ffgf`) - fmt.Printf("%c %d\n", c, n) + fmt.Printf(`%c %d`, c, n) // Output: a 3 } func ExampleRatioAlnum() { - fmt.Println(RatioAlnum("//A1")) + fmt.Println(RatioAlnum(`//A1`)) // Output: 0.5 } func ExampleRatioDigit() { - fmt.Println(RatioDigit("// A b 0 1")) + fmt.Println(RatioDigit(`// A b 0 1`)) // Output: 0.2 } func ExampleRatioNonAlnum() { - fmt.Println(RatioNonAlnum("// A1", false)) - fmt.Println(RatioNonAlnum("// A1", true)) + fmt.Println(RatioNonAlnum(`// A1`, false)) + fmt.Println(RatioNonAlnum(`// A1`, true)) // Output: // 0.4 // 0.6 } func ExampleRatioUpper() { - fmt.Println(RatioUpper("// A b c d")) + fmt.Println(RatioUpper(`// A b c d`)) // Output: 0.25 } func ExampleRatioUpperLower() { - fmt.Println(RatioUpperLower("// A b c d e")) + fmt.Println(RatioUpperLower(`// A b c d e`)) // Output: 0.25 } func ExampleTextSumCountTokens() { - text := "[[aa]] [[AA]]" + var ( + text = `[[aa]] [[AA]]` + tokens = []string{`[[`} + ) - tokens := []string{"[["} fmt.Println(TextSumCountTokens(text, tokens, false)) - tokens = []string{"aa"} + tokens = []string{`aa`} fmt.Println(TextSumCountTokens(text, tokens, false)) fmt.Println(TextSumCountTokens(text, tokens, true)) @@ -103,10 +121,10 @@ func ExampleTextSumCountTokens() { } func ExampleTextFrequencyOfTokens() { - text := "a b c d A B C D 1 2" + var text = `a b c d A B C D 1 2` - fmt.Println(TextFrequencyOfTokens(text, []string{"a"}, false)) - fmt.Println(TextFrequencyOfTokens(text, []string{"a"}, true)) + fmt.Println(TextFrequencyOfTokens(text, []string{`a`}, false)) + fmt.Println(TextFrequencyOfTokens(text, []string{`a`}, true)) // Output: // 0.2 // 0.1 diff --git a/lib/strings/statistic_test.go b/lib/strings/statistic_test.go index c7d401b0..f8187ffc 100644 --- a/lib/strings/statistic_test.go +++ b/lib/strings/statistic_test.go @@ -11,73 +11,87 @@ import ( ) func TestCountAlnum(t *testing.T) { - cases := []struct { + type testCase struct { text string exp int - }{{ - // Empty + } + + var cases = []testCase{{ + // Empty. }, { - text: "// 123", + text: `// 123`, exp: 3, }, { - text: "// A B C", + text: `// A B C`, exp: 3, }, { - text: "// A b c 1 2 3", + text: `// A b c 1 2 3`, exp: 6, }} - for _, c := range cases { - got := CountAlnum(c.text) - test.Assert(t, "", c.exp, got) + var ( + c testCase + got int + ) + for _, c = range cases { + got = CountAlnum(c.text) + test.Assert(t, ``, c.exp, got) } } func TestCountAlnumDistribution(t *testing.T) { - cases := []struct { + type testCase struct { text string expChars []rune expCounts []int - }{{ - // Empty + } + + var cases = []testCase{{ + // Empty input. }, { - text: "// 123", + text: `// 123`, expChars: []rune{'1', '2', '3'}, expCounts: []int{1, 1, 1}, }, { - text: "// A B C", + text: `// A B C`, expChars: []rune{'A', 'B', 'C'}, expCounts: []int{1, 1, 1}, }, { - text: "// A B C A B C", + text: `// A B C A B C`, expChars: []rune{'A', 'B', 'C'}, expCounts: []int{2, 2, 2}, }} - for _, c := range cases { - gotChars, gotCounts := CountAlnumDistribution(c.text) - test.Assert(t, "chars", c.expChars, gotChars) - test.Assert(t, "counts", c.expCounts, gotCounts) + var ( + c testCase + gotChars []rune + gotCounts []int + ) + for _, c = range cases { + gotChars, gotCounts = CountAlnumDistribution(c.text) + test.Assert(t, `chars`, c.expChars, gotChars) + test.Assert(t, `counts`, c.expCounts, gotCounts) } } func TestCountCharSequence(t *testing.T) { - cases := []struct { + type testCase struct { text string expChars []rune expCounts []int - }{{ - text: "// Copyright 2016 Mhd Sulhan . All rights reserved.", + } + var cases = []testCase{{ + text: `// Copyright 2016 Mhd Sulhan . All rights reserved.`, expChars: []rune{'/', 'l'}, expCounts: []int{2, 2}, }, { - text: "Use of this source code is governed by a BSD-style", + text: `Use of this source code is governed by a BSD-style`, }, { - text: "aaa abcdee ffgf", + text: `aaa abcdee ffgf`, expChars: []rune{'a', 'e', 'f'}, expCounts: []int{3, 2, 2}, }, { - text: " | image name = {{legend|#0080FF|Areas affected by flooding}}{{legend|#002255|Death(s) affected by flooding}}{{legend|#C83737|Areas affected by flooding and strong winds}}{{legend|#550000|Death(s) affected by flooding and strong winds}}", + text: ` | image name = {{legend|#0080FF|Areas affected by flooding}}{{legend|#002255|Death(s) affected by flooding}}{{legend|#C83737|Areas affected by flooding and strong winds}}{{legend|#550000|Death(s) affected by flooding and strong winds}}`, expChars: []rune{ '{', '0', 'F', 'f', 'o', '}', '{', '0', '2', '5', @@ -94,302 +108,378 @@ func TestCountCharSequence(t *testing.T) { }, }} - for _, c := range cases { - gotChars, gotCounts := CountCharSequence(c.text) + var ( + c testCase + gotChars []rune + gotCounts []int + ) + for _, c = range cases { + gotChars, gotCounts = CountCharSequence(c.text) - test.Assert(t, "", c.expChars, gotChars) - test.Assert(t, "", c.expCounts, gotCounts) + test.Assert(t, ``, c.expChars, gotChars) + test.Assert(t, ``, c.expCounts, gotCounts) } } func TestCountDigit(t *testing.T) { - cases := []struct { + type testCase struct { text string exp int - }{{ + } + + var cases = []testCase{{ // Empty. }, { - text: "// Copyright 2018 Mhd Sulhan . All rights reserved.", + text: `// 2018 `, exp: 4, }} - for _, c := range cases { - got := CountDigit(c.text) + var ( + c testCase + got int + ) + for _, c = range cases { + got = CountDigit(c.text) - test.Assert(t, "", c.exp, got) + test.Assert(t, ``, c.exp, got) } } func TestCountNonAlnum(t *testing.T) { - cases := []struct { + type testCase struct { text string - withspace bool exp int - }{{ - // Empty + withspace bool + } + var cases = []testCase{{ + // Empty. }, { - text: "// 123", + text: `// 123`, exp: 2, }, { - text: "// 123", + text: `// 123`, withspace: true, exp: 3, }, { - text: "// A B C", + text: `// A B C`, exp: 2, }, { - text: "// A B C", + text: `// A B C`, withspace: true, exp: 5, }, { - text: "// A b c 1 2 3", + text: `// A b c 1 2 3`, exp: 2, }, { - text: "// A b c 1 2 3", + text: `// A b c 1 2 3`, withspace: true, exp: 8, }} - for _, c := range cases { - got := CountNonAlnum(c.text, c.withspace) - test.Assert(t, "", c.exp, got) + var ( + c testCase + got int + ) + for _, c = range cases { + got = CountNonAlnum(c.text, c.withspace) + test.Assert(t, ``, c.exp, got) } } func TestCountUniqChar(t *testing.T) { - cases := []struct { + type testCase struct { text string exp int - }{{ + } + + var cases = []testCase{{ // Empty. }, { - text: "abc abc", + text: `abc abc`, exp: 4, }, { - text: "abc ABC", + text: `abc ABC`, exp: 7, }} - for _, c := range cases { - got := CountUniqChar(c.text) - test.Assert(t, "", c.exp, got) + var ( + c testCase + got int + ) + for _, c = range cases { + got = CountUniqChar(c.text) + test.Assert(t, ``, c.exp, got) } } func TestCountUpperLower(t *testing.T) { - cases := []struct { + type testCase struct { text string expUpper int expLower int - }{{ - text: "// Copyright 2016 Mhd Sulhan . All rights reserved.", + } + + var cases = []testCase{{ + text: `// Copyright 2016 Mhd Sulhan . All rights reserved.`, expUpper: 4, expLower: 44, }} - for _, c := range cases { - gotup, gotlo := CountUpperLower(c.text) + var ( + c testCase + gotup int + gotlo int + ) + for _, c = range cases { + gotup, gotlo = CountUpperLower(c.text) - test.Assert(t, "", c.expUpper, gotup) - test.Assert(t, "", c.expLower, gotlo) + test.Assert(t, ``, c.expUpper, gotup) + test.Assert(t, ``, c.expLower, gotlo) } } func TestMaxCharSequence(t *testing.T) { - cases := []struct { + type testCase struct { text string char rune count int - }{{ - text: "// Copyright 2016 Mhd Sulhan . All rights reserved.", + } + + var cases = []testCase{{ + text: `// Copyright 2016 Mhd Sulhan . All rights reserved.`, char: '/', count: 2, }, { - text: "Use of this source code is governed by a BSD-style", + text: `Use of this source code is governed by a BSD-style`, }, { - text: "aaa abcdee ffgf", + text: `aaa abcdee ffgf`, char: 'a', count: 3, }, { - text: " | image name = {{legend|#0080FF|Areas affected by flooding}}{{legend|#002255|Death(s) affected by flooding}}{{legend|#C83737|Areas affected by flooding and strong winds}}{{legend|#550000|Death(s) affected by flooding and strong winds}}", + text: ` | image name = {{legend|#0080FF|Areas affected by flooding}}{{legend|#002255|Death(s) affected by flooding}}{{legend|#C83737|Areas affected by flooding and strong winds}}{{legend|#550000|Death(s) affected by flooding and strong winds}}`, char: '0', count: 4, }} - for _, c := range cases { - gotv, gotc := MaxCharSequence(c.text) + var ( + c testCase + gotv rune + gotc int + ) + for _, c = range cases { + gotv, gotc = MaxCharSequence(c.text) - test.Assert(t, "", c.char, gotv) - test.Assert(t, "", c.count, gotc) + test.Assert(t, ``, c.char, gotv) + test.Assert(t, ``, c.count, gotc) } } func TestRatioAlnum(t *testing.T) { - cases := []struct { + type testCase struct { text string exp float64 - }{{ + } + + var cases = []testCase{{ // Empty. }, { - text: "// A b c d", + text: `// A b c d`, exp: 0.4, }, { - text: "// A123b", + text: `// A123b`, exp: 0.625, }} - for _, c := range cases { - got := RatioAlnum(c.text) - test.Assert(t, "", c.exp, got) + var ( + c testCase + got float64 + ) + for _, c = range cases { + got = RatioAlnum(c.text) + test.Assert(t, ``, c.exp, got) } } func TestRatioDigit(t *testing.T) { - cases := []struct { + type testCase struct { text string exp float64 - }{{ + } + var cases = []testCase{{ // Empty. }, { - text: "// A b c d", + text: `// A b c d`, exp: 0, }, { - text: "// A123b", + text: `// A123b`, exp: 0.375, }} - for _, c := range cases { - got := RatioDigit(c.text) - test.Assert(t, "", c.exp, got) + var ( + c testCase + got float64 + ) + for _, c = range cases { + got = RatioDigit(c.text) + test.Assert(t, ``, c.exp, got) } } func TestRatioNonAlnum(t *testing.T) { - cases := []struct { + type testCase struct { text string - withspace bool exp float64 - }{{ + withspace bool + } + + var cases = []testCase{{ // Empty. }, { - text: "// A b c d", + text: `// A b c d`, exp: 0.2, }, { - text: "// A b c d", + text: `// A b c d`, withspace: true, exp: 0.6, }, { - text: "// A123b", + text: `// A123b`, exp: 0.25, }, { - text: "// A123b", + text: `// A123b`, withspace: true, exp: 0.375, }} - for _, c := range cases { - got := RatioNonAlnum(c.text, c.withspace) - test.Assert(t, "", c.exp, got) + var ( + c testCase + got float64 + ) + for _, c = range cases { + got = RatioNonAlnum(c.text, c.withspace) + test.Assert(t, ``, c.exp, got) } } func TestRatioUpper(t *testing.T) { - cases := []struct { + type testCase struct { text string exp float64 - }{{ + } + + var cases = []testCase{{ // Empty. }, { - text: "// A b c d", + text: `// A b c d`, exp: 0.25, }} - for _, c := range cases { - got := RatioUpper(c.text) - test.Assert(t, "", c.exp, got) + var ( + c testCase + got float64 + ) + for _, c = range cases { + got = RatioUpper(c.text) + test.Assert(t, ``, c.exp, got) } } func TestRatioUpperLower(t *testing.T) { - cases := []struct { + type testCase struct { text string exp float64 - }{{ - // Empty + } + var cases = []testCase{{ + // Empty. }, { - text: "// 134234", + text: `// 134234`, }, { - text: "// A B C", + text: `// A B C`, exp: 3, }, { - text: "// A b c d e", + text: `// A b c d e`, exp: 0.25, }} - for _, c := range cases { - got := RatioUpperLower(c.text) - test.Assert(t, "", c.exp, got) + var ( + c testCase + got float64 + ) + for _, c = range cases { + got = RatioUpperLower(c.text) + test.Assert(t, ``, c.exp, got) } } func TestTextSumCountTokens(t *testing.T) { - cases := []struct { + type testCase struct { text string tokens []string - sensitive bool exp int - }{{ + sensitive bool + } + + var cases = []testCase{{ // Empty. }, { - text: "[[aa]] [[AA]]", - tokens: []string{"[["}, + text: `[[aa]] [[AA]]`, + tokens: []string{`[[`}, exp: 2, }, { - text: "[[aa]] [[AA]]", - tokens: []string{"]]"}, + text: `[[aa]] [[AA]]`, + tokens: []string{`]]`}, exp: 2, }, { - text: "[[aa]] [[AA]]", - tokens: []string{"[[", "]]"}, + text: `[[aa]] [[AA]]`, + tokens: []string{`[[`, `]]`}, exp: 4, }, { - text: "[[aa]] [[AA]]", - tokens: []string{"aa"}, + text: `[[aa]] [[AA]]`, + tokens: []string{`aa`}, exp: 2, }, { - text: "[[aa]] [[AA]]", - tokens: []string{"aa"}, + text: `[[aa]] [[AA]]`, + tokens: []string{`aa`}, sensitive: true, exp: 1, }} - for _, c := range cases { - got := TextSumCountTokens(c.text, c.tokens, c.sensitive) - test.Assert(t, "", c.exp, got) + var ( + c testCase + got int + ) + for _, c = range cases { + got = TextSumCountTokens(c.text, c.tokens, c.sensitive) + test.Assert(t, ``, c.exp, got) } } func TestTextFrequencyOfTokens(t *testing.T) { - cases := []struct { + type testCase struct { text string tokens []string sensitive bool exp float64 - }{{ + } + + var cases = []testCase{{ // Empty. }, { - text: "a b c d A B C D", - tokens: []string{"a"}, + text: `a b c d A B C D`, + tokens: []string{`a`}, exp: 0.25, }, { - text: "a b c d A B C D", - tokens: []string{"a"}, + text: `a b c d A B C D`, + tokens: []string{`a`}, sensitive: true, exp: 0.125, }} - for _, c := range cases { - got := TextFrequencyOfTokens(c.text, c.tokens, c.sensitive) - test.Assert(t, "", c.exp, got) + var ( + c testCase + got float64 + ) + for _, c = range cases { + got = TextFrequencyOfTokens(c.text, c.tokens, c.sensitive) + test.Assert(t, ``, c.exp, got) } } diff --git a/lib/strings/string_example_test.go b/lib/strings/string_example_test.go index eba21f76..2cf2b889 100644 --- a/lib/strings/string_example_test.go +++ b/lib/strings/string_example_test.go @@ -9,42 +9,47 @@ import ( ) func ExampleAlnum() { - cases := []struct { + type testCase struct { text string withSpace bool - }{ - {"A, b.c", false}, - {"A, b.c", true}, - {"A1 b", false}, - {"A1 b", true}, } - for _, c := range cases { - fmt.Printf("%s\n", Alnum(c.text, c.withSpace)) + var cases = []testCase{ + {`A, b.c`, false}, + {`A, b.c`, true}, + {`A1 b`, false}, + {`A1 b`, true}, } - //Output: - //Abc - //A bc - //A1b - //A1 b + + var ( + c testCase + ) + for _, c = range cases { + fmt.Println(Alnum(c.text, c.withSpace)) + } + // Output: + // Abc + // A bc + // A1b + // A1 b } func ExampleCleanURI() { - text := `You can visit ftp://hostname or https://hostname/link%202 for more information` + var text = `You can visit ftp://hostname or https://hostname/link%202 for more information` - fmt.Printf("%s\n", CleanURI(text)) + fmt.Println(CleanURI(text)) // Output: You can visit or for more information } func ExampleCleanWikiMarkup() { - text := `* Test image [[Image:fileto.png]].` + var text = `* Test image [[Image:fileto.png]].` - fmt.Printf("%s\n", CleanWikiMarkup(text)) + fmt.Println(CleanWikiMarkup(text)) // Output: * Test image . } func ExampleMergeSpaces() { - line := " a\n\nb c d\n\n" + var line = " a\n\nb c d\n\n" fmt.Printf("Without merging newline: '%s'\n", MergeSpaces(line, false)) fmt.Printf("With merging newline: '%s'\n", MergeSpaces(line, true)) // Output: @@ -59,11 +64,11 @@ func ExampleMergeSpaces() { } func ExampleSplit() { - line := `a b c [A] B C` - fmt.Printf("%s\n", Split(line, false, false)) - fmt.Printf("%s\n", Split(line, true, false)) - fmt.Printf("%s\n", Split(line, false, true)) - fmt.Printf("%s\n", Split(line, true, true)) + var line string = `a b c [A] B C` + fmt.Println(Split(line, false, false)) + fmt.Println(Split(line, true, false)) + fmt.Println(Split(line, false, true)) + fmt.Println(Split(line, true, true)) // Output: // [a b c [A] B C] // [a b c A B C] @@ -72,29 +77,32 @@ func ExampleSplit() { } func ExampleTrimNonAlnum() { - inputs := []string{ - "[[alpha]]", - "[[alpha", - "alpha]]", - "alpha", - "alpha0", - "1alpha", - "1alpha0", - "[a][b][c]", - "[][][]", - } + var ( + inputs = []string{ + `[[alpha]]`, + `[[alpha`, + `alpha]]`, + `alpha`, + `alpha0`, + `1alpha`, + `1alpha0`, + `[a][b][c]`, + `[][][]`, + } + in string + ) - for _, in := range inputs { - fmt.Printf("'%s'\n", TrimNonAlnum(in)) + for _, in = range inputs { + fmt.Println(TrimNonAlnum(in)) } // Output: - // 'alpha' - // 'alpha' - // 'alpha' - // 'alpha' - // 'alpha0' - // '1alpha' - // '1alpha0' - // 'a][b][c' - // '' + // alpha + // alpha + // alpha + // alpha + // alpha0 + // 1alpha + // 1alpha0 + // a][b][c + // } 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]].[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.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. 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]].[ Pirates and emperors, old and new: international terrorism in the real world], [[Noam Chomsky]], p. 168.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. 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}} - -
-{{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}} - -
-{{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 . 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 + ) + + tdata, err = test.LoadData(`testdata/split_test.txt`) + if err != nil { + t.Fatal(err) + } - for _, c := range cases { - got := Split(c.text, true, true) - test.Assert(t, "", c.exp, got) + 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) } } diff --git a/lib/strings/strings_example_test.go b/lib/strings/strings_example_test.go index d6508cd7..c69e52c5 100644 --- a/lib/strings/strings_example_test.go +++ b/lib/strings/strings_example_test.go @@ -9,8 +9,10 @@ import ( ) func ExampleAppendUniq() { - in := []string{"a", "", "b", "c"} - vals := []string{"b", "", "C", "d"} + var ( + in = []string{`a`, ``, `b`, `c`} + vals = []string{`b`, ``, `C`, `d`} + ) in = AppendUniq(in, vals...) @@ -20,24 +22,27 @@ func ExampleAppendUniq() { } func ExampleCountMissRate() { - src := []string{"A", "B", "C", "D"} - tgt := []string{"A", "B", "C", "D"} + var ( + src = []string{`A`, `B`, `C`, `D`} + tgt = []string{`A`, `B`, `C`, `D`} + ) + fmt.Println(CountMissRate(src, tgt)) - src = []string{"A", "B", "C", "D"} - tgt = []string{"B", "B", "C", "D"} + src = []string{`A`, `B`, `C`, `D`} + tgt = []string{`B`, `B`, `C`, `D`} fmt.Println(CountMissRate(src, tgt)) - src = []string{"A", "B", "C", "D"} - tgt = []string{"B", "C", "C", "D"} + src = []string{`A`, `B`, `C`, `D`} + tgt = []string{`B`, `C`, `C`, `D`} fmt.Println(CountMissRate(src, tgt)) - src = []string{"A", "B", "C", "D"} - tgt = []string{"B", "C", "D", "D"} + src = []string{`A`, `B`, `C`, `D`} + tgt = []string{`B`, `C`, `D`, `D`} fmt.Println(CountMissRate(src, tgt)) - src = []string{"A", "B", "C", "D"} - tgt = []string{"C", "D", "D", "E"} + src = []string{`A`, `B`, `C`, `D`} + tgt = []string{`C`, `D`, `D`, `E`} fmt.Println(CountMissRate(src, tgt)) // Output: @@ -49,17 +54,23 @@ func ExampleCountMissRate() { } func ExampleCountToken() { - words := []string{"A", "B", "C", "a", "b", "c"} - fmt.Println(CountToken(words, "C", false)) - fmt.Println(CountToken(words, "C", true)) + var ( + words = []string{`A`, `B`, `C`, `a`, `b`, `c`} + ) + + fmt.Println(CountToken(words, `C`, false)) + fmt.Println(CountToken(words, `C`, true)) // Output: // 2 // 1 } func ExampleCountTokens() { - words := []string{"A", "B", "C", "a", "b", "c"} - tokens := []string{"A", "B"} + var ( + words = []string{`A`, `B`, `C`, `a`, `b`, `c`} + tokens = []string{`A`, `B`} + ) + fmt.Println(CountTokens(words, tokens, false)) fmt.Println(CountTokens(words, tokens, true)) // Output: @@ -68,17 +79,23 @@ func ExampleCountTokens() { } func ExampleFrequencyOfToken() { - words := []string{"A", "B", "C", "a", "b", "c"} - fmt.Println(FrequencyOfToken(words, "C", false)) - fmt.Println(FrequencyOfToken(words, "C", true)) + var ( + words = []string{`A`, `B`, `C`, `a`, `b`, `c`} + ) + + fmt.Println(FrequencyOfToken(words, `C`, false)) + fmt.Println(FrequencyOfToken(words, `C`, true)) // Output: // 0.3333333333333333 // 0.16666666666666666 } func ExampleFrequencyOfTokens() { - words := []string{"A", "B", "C", "a", "b", "c"} - tokens := []string{"A", "B"} + var ( + words = []string{`A`, `B`, `C`, `a`, `b`, `c`} + tokens = []string{`A`, `B`} + ) + fmt.Println(FrequencyOfTokens(words, tokens, false)) fmt.Println(FrequencyOfTokens(words, tokens, true)) // Output: @@ -87,10 +104,10 @@ func ExampleFrequencyOfTokens() { } func ExampleIsEqual() { - fmt.Println(IsEqual([]string{"a", "b"}, []string{"a", "b"})) - fmt.Println(IsEqual([]string{"a", "b"}, []string{"b", "a"})) - fmt.Println(IsEqual([]string{"a", "b"}, []string{"a"})) - fmt.Println(IsEqual([]string{"a", "b"}, []string{"b", "b"})) + fmt.Println(IsEqual([]string{`a`, `b`}, []string{`a`, `b`})) + fmt.Println(IsEqual([]string{`a`, `b`}, []string{`b`, `a`})) + fmt.Println(IsEqual([]string{`a`, `b`}, []string{`a`})) + fmt.Println(IsEqual([]string{`a`, `b`}, []string{`b`, `b`})) // Output: // true // true @@ -99,14 +116,20 @@ func ExampleIsEqual() { } func ExampleLongest() { - words := []string{"a", "bb", "ccc", "d", "eee"} + var ( + words = []string{`a`, `bb`, `ccc`, `d`, `eee`} + ) + fmt.Println(Longest(words)) // Output: ccc 2 } func ExampleMostFrequentTokens() { - words := []string{"a", "b", "B", "B", "a"} - tokens := []string{"a", "b"} + var ( + words = []string{`a`, `b`, `B`, `B`, `a`} + tokens = []string{`a`, `b`} + ) + fmt.Println(MostFrequentTokens(words, tokens, false)) fmt.Println(MostFrequentTokens(words, tokens, true)) // Output: @@ -115,8 +138,10 @@ func ExampleMostFrequentTokens() { } func ExampleSortByIndex() { - dat := []string{"Z", "X", "C", "V", "B", "N", "M"} - ids := []int{4, 2, 6, 5, 3, 1, 0} + var ( + dat = []string{`Z`, `X`, `C`, `V`, `B`, `N`, `M`} + ids = []int{4, 2, 6, 5, 3, 1, 0} + ) fmt.Println(dat) SortByIndex(&dat, ids) @@ -127,7 +152,10 @@ func ExampleSortByIndex() { } func ExampleSwap() { - ss := []string{"a", "b", "c"} + var ( + ss = []string{`a`, `b`, `c`} + ) + Swap(ss, -1, 1) fmt.Println(ss) Swap(ss, 1, -1) @@ -147,8 +175,11 @@ func ExampleSwap() { } func ExampleTotalFrequencyOfTokens() { - words := []string{"A", "B", "C", "a", "b", "c"} - tokens := []string{"A", "B"} + var ( + words = []string{`A`, `B`, `C`, `a`, `b`, `c`} + tokens = []string{`A`, `B`} + ) + fmt.Println(TotalFrequencyOfTokens(words, tokens, false)) fmt.Println(TotalFrequencyOfTokens(words, tokens, true)) // Output: @@ -157,9 +188,12 @@ func ExampleTotalFrequencyOfTokens() { } func ExampleUniq() { - words := []string{"a", "", "A"} + var ( + words = []string{`a`, ``, `A`} + ) + fmt.Printf("%s %s\n", Uniq(words, false), words) - words = []string{"a", "", "A"} + words = []string{`a`, ``, `A`} fmt.Printf("%s %s\n", Uniq(words, true), words) // Output: // [a] [a ] diff --git a/lib/strings/strings_test.go b/lib/strings/strings_test.go index 6984b7c1..304ff6fa 100644 --- a/lib/strings/strings_test.go +++ b/lib/strings/strings_test.go @@ -12,286 +12,352 @@ import ( ) func TestCountMissRate(t *testing.T) { - cases := []struct { + type testCase struct { src []string target []string exp float64 - }{{ + } + + var cases = []testCase{{ // Empty. }, { - src: []string{"A", "B", "C", "D"}, - target: []string{"A", "B", "C"}, + src: []string{`A`, `B`, `C`, `D`}, + target: []string{`A`, `B`, `C`}, exp: 0, }, { - src: []string{"A", "B", "C", "D"}, - target: []string{"A", "B", "C", "D"}, + src: []string{`A`, `B`, `C`, `D`}, + target: []string{`A`, `B`, `C`, `D`}, exp: 0, }, { - src: []string{"A", "B", "C", "D"}, - target: []string{"B", "B", "C", "D"}, + src: []string{`A`, `B`, `C`, `D`}, + target: []string{`B`, `B`, `C`, `D`}, exp: 0.25, }, { - src: []string{"A", "B", "C", "D"}, - target: []string{"B", "C", "C", "D"}, + src: []string{`A`, `B`, `C`, `D`}, + target: []string{`B`, `C`, `C`, `D`}, exp: 0.5, }, { - src: []string{"A", "B", "C", "D"}, - target: []string{"B", "C", "D", "D"}, + src: []string{`A`, `B`, `C`, `D`}, + target: []string{`B`, `C`, `D`, `D`}, exp: 0.75, }, { - src: []string{"A", "B", "C", "D"}, - target: []string{"C", "D", "D", "E"}, + src: []string{`A`, `B`, `C`, `D`}, + target: []string{`C`, `D`, `D`, `E`}, exp: 1.0, }} - for _, c := range cases { - got, _, _ := CountMissRate(c.src, c.target) - test.Assert(t, "", c.exp, got) + var ( + c testCase + got float64 + ) + for _, c = range cases { + got, _, _ = CountMissRate(c.src, c.target) + test.Assert(t, ``, c.exp, got) } } func TestCountTokens(t *testing.T) { - cases := []struct { + type testCase struct { words []string tokens []string - sensitive bool exp []int - }{{ + sensitive bool + } + + var cases = []testCase{{ // Empty. }, { - words: []string{"A", "B", "C", "a", "b", "c"}, - tokens: []string{"A", "B"}, + words: []string{`A`, `B`, `C`, `a`, `b`, `c`}, + tokens: []string{`A`, `B`}, exp: []int{2, 2}, }, { - words: []string{"A", "B", "C", "a", "b", "c"}, - tokens: []string{"A", "B"}, + words: []string{`A`, `B`, `C`, `a`, `b`, `c`}, + tokens: []string{`A`, `B`}, sensitive: true, exp: []int{1, 1}, }} - for _, c := range cases { - got := CountTokens(c.words, c.tokens, c.sensitive) - - test.Assert(t, "", c.exp, got) + var ( + c testCase + got []int + ) + for _, c = range cases { + got = CountTokens(c.words, c.tokens, c.sensitive) + test.Assert(t, ``, c.exp, got) } } func TestDelete(t *testing.T) { - value := "b" - exp := []string{"a", "c"} - - cases := []struct { + type testCase struct { in []string - }{{ - in: []string{"b", "a", "c"}, + } + + var cases = []testCase{{ + in: []string{`b`, `a`, `c`}, }, { - in: []string{"a", "b", "c"}, + in: []string{`a`, `b`, `c`}, }, { - in: []string{"a", "c", "b"}, + in: []string{`a`, `c`, `b`}, }} - for _, c := range cases { - var ok bool + var ( + value = `b` + exp = []string{`a`, `c`} + + c testCase + ok bool + ) + for _, c = range cases { c.in, ok = Delete(c.in, value) - test.Assert(t, "Delete OK?", true, ok) - test.Assert(t, "Delete", exp, c.in) + test.Assert(t, `Delete OK?`, true, ok) + test.Assert(t, `Delete`, exp, c.in) } } func TestFrequencyOfTokens(t *testing.T) { - cases := []struct { - words, tokens []string - exp []float64 - sensitive bool - }{{ + type testCase struct { + words []string + tokens []string + exp []float64 + sensitive bool + } + var cases = []testCase{{ // Empty. }, { - words: []string{"a", "b", "a", "b", "a", "c"}, - tokens: []string{"a", "b"}, + words: []string{`a`, `b`, `a`, `b`, `a`, `c`}, + tokens: []string{`a`, `b`}, exp: []float64{0.5, 0.3333333333333333}, }} - for _, c := range cases { - got := FrequencyOfTokens(c.words, c.tokens, c.sensitive) - test.Assert(t, "", c.exp, got) + var ( + c testCase + got []float64 + ) + for _, c = range cases { + got = FrequencyOfTokens(c.words, c.tokens, c.sensitive) + test.Assert(t, ``, c.exp, got) } } func TestIsContain(t *testing.T) { - ss := []string{"a", "b", "c", "d"} + var ( + ss = []string{`a`, `b`, `c`, `d`} - got := IsContain(ss, "a") - test.Assert(t, "", true, got) + got bool + ) - got = IsContain(ss, "e") - test.Assert(t, "", false, got) + got = IsContain(ss, `a`) + test.Assert(t, ``, true, got) + + got = IsContain(ss, `e`) + test.Assert(t, ``, false, got) } func TestIsEqual(t *testing.T) { - cases := []struct { - a, b []string - exp bool - }{{ - a: []string{"a", "b"}, - b: []string{"a", "b"}, + type testCase struct { + a []string + b []string + exp bool + } + + var cases = []testCase{{ + a: []string{`a`, `b`}, + b: []string{`a`, `b`}, exp: true, }, { - a: []string{"a", "b"}, - b: []string{"b", "a"}, + a: []string{`a`, `b`}, + b: []string{`b`, `a`}, exp: true, }, { - a: []string{"a", "b"}, - b: []string{"a"}, + a: []string{`a`, `b`}, + b: []string{`a`}, }, { - a: []string{"a"}, - b: []string{"b", "a"}, + a: []string{`a`}, + b: []string{`b`, `a`}, }, { - a: []string{"a", "b"}, - b: []string{"a", "c"}, + a: []string{`a`, `b`}, + b: []string{`a`, `c`}, }} - for _, c := range cases { - test.Assert(t, "", c.exp, IsEqual(c.a, c.b)) + var ( + c testCase + ) + for _, c = range cases { + test.Assert(t, ``, c.exp, IsEqual(c.a, c.b)) } } func TestLongest(t *testing.T) { - cases := []struct { - words []string + type testCase struct { exp string + words []string expIdx int - }{{ + } + + var cases = []testCase{{ // Empty. expIdx: -1, }, { - words: []string{"a", "bb", "ccc", "d", "eee"}, - exp: "ccc", + words: []string{`a`, `bb`, `ccc`, `d`, `eee`}, + exp: `ccc`, expIdx: 2, }, { - words: []string{"a", "bb", "ccc", "dddd", "eee"}, - exp: "dddd", + words: []string{`a`, `bb`, `ccc`, `dddd`, `eee`}, + exp: `dddd`, expIdx: 3, }} - for _, c := range cases { - got, idx := Longest(c.words) + var ( + c testCase + got string + idx int + ) + for _, c = range cases { + got, idx = Longest(c.words) - test.Assert(t, "word", c.exp, got) - test.Assert(t, "idx", c.expIdx, idx) + test.Assert(t, `word`, c.exp, got) + test.Assert(t, `idx`, c.expIdx, idx) } } func TestMostFrequentTokens(t *testing.T) { - cases := []struct { - words, tokens []string - sensitive bool - exp string - }{{ - // Empty + type testCase struct { + exp string + words []string + tokens []string + sensitive bool + } + + var cases = []testCase{{ + // Empty. }, { - words: []string{"a", "b", "A"}, - tokens: []string{"a", "b"}, - exp: "a", + words: []string{`a`, `b`, `A`}, + tokens: []string{`a`, `b`}, + exp: `a`, }, { - words: []string{"a", "b", "A", "b"}, - tokens: []string{"a", "b"}, + words: []string{`a`, `b`, `A`, `b`}, + tokens: []string{`a`, `b`}, sensitive: true, - exp: "b", + exp: `b`, }, { - words: []string{"a", "b", "A", "B"}, - tokens: []string{"a", "b"}, - exp: "a", + words: []string{`a`, `b`, `A`, `B`}, + tokens: []string{`a`, `b`}, + exp: `a`, }} - for _, c := range cases { - got := MostFrequentTokens(c.words, c.tokens, c.sensitive) - test.Assert(t, "", c.exp, got) + var ( + c testCase + got string + ) + for _, c = range cases { + got = MostFrequentTokens(c.words, c.tokens, c.sensitive) + test.Assert(t, ``, c.exp, got) } } func TestSortByIndex(t *testing.T) { - dat := []string{"Z", "X", "C", "V", "B", "N", "M"} - exp := []string{"B", "C", "M", "N", "V", "X", "Z"} - ids := []int{4, 2, 6, 5, 3, 1, 0} + var ( + dat = []string{`Z`, `X`, `C`, `V`, `B`, `N`, `M`} + exp = []string{`B`, `C`, `M`, `N`, `V`, `X`, `Z`} + ids = []int{4, 2, 6, 5, 3, 1, 0} + ) SortByIndex(&dat, ids) - test.Assert(t, "", exp, dat) + test.Assert(t, ``, exp, dat) } func TestSwap(t *testing.T) { - ss := []string{"a", "b", "c"} + type testCase struct { + exp []string + x int + y int + } - cases := []struct { - x, y int - exp []string - }{{ + var cases = []testCase{{ x: -1, - exp: []string{"a", "b", "c"}, + exp: []string{`a`, `b`, `c`}, }, { y: -1, - exp: []string{"a", "b", "c"}, + exp: []string{`a`, `b`, `c`}, }, { x: 4, - exp: []string{"a", "b", "c"}, + exp: []string{`a`, `b`, `c`}, }, { y: 4, - exp: []string{"a", "b", "c"}, + exp: []string{`a`, `b`, `c`}, }, { x: 1, y: 1, - exp: []string{"a", "b", "c"}, + exp: []string{`a`, `b`, `c`}, }, { x: 1, y: 2, - exp: []string{"a", "c", "b"}, + exp: []string{`a`, `c`, `b`}, }} - for _, c := range cases { + + var ( + ss = []string{`a`, `b`, `c`} + + c testCase + ) + for _, c = range cases { Swap(ss, c.x, c.y) - test.Assert(t, "", c.exp, ss) + test.Assert(t, ``, c.exp, ss) } } func TestTotalFrequencyOfTokens(t *testing.T) { - cases := []struct { + type testCase struct { words, tokens []string - sensitive bool exp float64 - }{{ + sensitive bool + } + + var cases = []testCase{{ // Empty. }, { - words: []string{"a", "b", "a", "b", "a", "c"}, - tokens: []string{"a", "b"}, + words: []string{`a`, `b`, `a`, `b`, `a`, `c`}, + tokens: []string{`a`, `b`}, exp: numbers.Float64Round((3.0/6)+(2.0/6), 3), }} - for _, c := range cases { - got := TotalFrequencyOfTokens(c.words, c.tokens, c.sensitive) - - test.Assert(t, "", c.exp, numbers.Float64Round(got, 3)) + var ( + c testCase + got float64 + ) + for _, c = range cases { + got = TotalFrequencyOfTokens(c.words, c.tokens, c.sensitive) + test.Assert(t, ``, c.exp, numbers.Float64Round(got, 3)) } } func TestUniq(t *testing.T) { - cases := []struct { + type testCase struct { words []string - sensitive bool expReturn []string expWords []string - }{{ - words: []string{"a", "A"}, + sensitive bool + } + + var cases = []testCase{{ + words: []string{`a`, `A`}, sensitive: true, - expReturn: []string{"a", "A"}, - expWords: []string{"a", "A"}, + expReturn: []string{`a`, `A`}, + expWords: []string{`a`, `A`}, }, { - words: []string{"a", "A"}, - expReturn: []string{"a"}, - expWords: []string{"a", ""}, + words: []string{`a`, `A`}, + expReturn: []string{`a`}, + expWords: []string{`a`, ``}, }} - for _, c := range cases { - got := Uniq(c.words, c.sensitive) - test.Assert(t, "unique", c.expReturn, got) - test.Assert(t, "words", c.expWords, c.words) + var ( + c testCase + got []string + ) + for _, c = range cases { + got = Uniq(c.words, c.sensitive) + test.Assert(t, `unique`, c.expReturn, got) + test.Assert(t, `words`, c.expWords, c.words) } } diff --git a/lib/strings/table_example_test.go b/lib/strings/table_example_test.go index 4ddccf3d..57160171 100644 --- a/lib/strings/table_example_test.go +++ b/lib/strings/table_example_test.go @@ -9,11 +9,11 @@ import ( ) func ExamplePartition() { - ss := []string{"a", "b", "c"} + var ss = []string{`a`, `b`, `c`} - fmt.Println("Partition k=1:", Partition(ss, 1)) - fmt.Println("Partition k=2:", Partition(ss, 2)) - fmt.Println("Partition k=3:", Partition(ss, 3)) + fmt.Println(`Partition k=1:`, Partition(ss, 1)) + fmt.Println(`Partition k=2:`, Partition(ss, 2)) + fmt.Println(`Partition k=3:`, Partition(ss, 3)) // Output: // Partition k=1: [[[a b c]]] @@ -22,30 +22,29 @@ func ExamplePartition() { } func ExampleSinglePartition() { - ss := []string{"a", "b", "c"} + var ss = []string{`a`, `b`, `c`} fmt.Println(SinglePartition(ss)) - // Output: - // [[[a] [b] [c]]] + // Output: [[[a] [b] [c]]] } func ExampleTable_IsEqual() { - table := Table{ - {{"a"}, {"b", "c"}}, - {{"b"}, {"a", "c"}}, - {{"c"}, {"a", "b"}}, + var table = Table{ + {{`a`}, {`b`, `c`}}, + {{`b`}, {`a`, `c`}}, + {{`c`}, {`a`, `b`}}, } fmt.Println(table.IsEqual(table)) - other := Table{ - {{"c"}, {"a", "b"}}, - {{"a"}, {"b", "c"}}, - {{"b"}, {"a", "c"}}, + var other = Table{ + {{`c`}, {`a`, `b`}}, + {{`a`}, {`b`, `c`}}, + {{`b`}, {`a`, `c`}}, } fmt.Println(table.IsEqual(other)) other = Table{ - {{"a"}, {"b", "c"}}, - {{"b"}, {"a", "c"}}, + {{`a`}, {`b`, `c`}}, + {{`b`}, {`a`, `c`}}, } fmt.Println(table.IsEqual(other)) @@ -56,10 +55,12 @@ func ExampleTable_IsEqual() { } func ExampleTable_JoinCombination() { - table := Table{ - {{"a"}, {"b"}, {"c"}}, - } - s := "X" + var ( + table = Table{ + {{`a`}, {`b`}, {`c`}}, + } + s = `X` + ) fmt.Println(table.JoinCombination(s)) // Output: diff --git a/lib/strings/table_test.go b/lib/strings/table_test.go index 7998b441..326f1171 100644 --- a/lib/strings/table_test.go +++ b/lib/strings/table_test.go @@ -11,143 +11,169 @@ import ( ) func TestPartition(t *testing.T) { - cases := []struct { + type testCase struct { + exp Table ss []string k int - exp Table - }{{ - ss: []string{"a", "b"}, + } + + var cases = []testCase{{ + ss: []string{`a`, `b`}, k: 1, exp: Table{ - {{"a", "b"}}, + {{`a`, `b`}}, }, }, { - ss: []string{"a", "b"}, + ss: []string{`a`, `b`}, k: 2, exp: Table{ - {{"a"}, {"b"}}, + {{`a`}, {`b`}}, }, }, { - ss: []string{"a", "b", "c"}, + ss: []string{`a`, `b`, `c`}, k: 1, exp: Table{ - {{"a", "b", "c"}}, + {{`a`, `b`, `c`}}, }, }, { - ss: []string{"a", "b", "c"}, + ss: []string{`a`, `b`, `c`}, k: 2, exp: Table{ - {{"b", "a"}, {"c"}}, - {{"b"}, {"c", "a"}}, - {{"b", "c"}, {"a"}}, + {{`b`, `a`}, {`c`}}, + {{`b`}, {`c`, `a`}}, + {{`b`, `c`}, {`a`}}, }, }, { - ss: []string{"a", "b", "c"}, + ss: []string{`a`, `b`, `c`}, k: 3, exp: Table{ - {{"a"}, {"b"}, {"c"}}, + {{`a`}, {`b`}, {`c`}}, }, }} - for _, c := range cases { - t.Logf("Partition: %d\n", c.k) + var ( + c testCase + got Table + ) + + for _, c = range cases { + t.Logf(`Partition: %d`, c.k) - got := Partition(c.ss, c.k) + got = Partition(c.ss, c.k) - test.Assert(t, "", c.exp, got) + test.Assert(t, ``, c.exp, got) } } func TestSinglePartition(t *testing.T) { - cases := []struct { - ss []string + type testCase struct { exp Table - }{{ - ss: []string{"a", "b", "c"}, + ss []string + } + + var cases = []testCase{{ + ss: []string{`a`, `b`, `c`}, exp: Table{ - {{"a"}, {"b"}, {"c"}}, + {{`a`}, {`b`}, {`c`}}, }, }} - for _, c := range cases { - got := SinglePartition(c.ss) - test.Assert(t, "", c.exp, got) + var ( + c testCase + got Table + ) + for _, c = range cases { + got = SinglePartition(c.ss) + test.Assert(t, ``, c.exp, got) } } func TestTable_IsEqual(t *testing.T) { - table := Table{ - {{"a"}, {"b", "c"}}, - {{"b"}, {"a", "c"}}, - {{"c"}, {"a", "b"}}, - } - - cases := []struct { + type testCase struct { tcmp Table exp bool - }{{ + } + + var table = Table{ + {{`a`}, {`b`, `c`}}, + {{`b`}, {`a`, `c`}}, + {{`c`}, {`a`, `b`}}, + } + + var cases = []testCase{{ // Empty. }, { tcmp: table, exp: true, }, { tcmp: Table{ - {{"c"}, {"a", "b"}}, - {{"a"}, {"b", "c"}}, - {{"b"}, {"a", "c"}}, + {{`c`}, {`a`, `b`}}, + {{`a`}, {`b`, `c`}}, + {{`b`}, {`a`, `c`}}, }, exp: true, }, { tcmp: Table{ - {{"c"}, {"a", "b"}}, - {{"a"}, {"b", "c"}}, + {{`c`}, {`a`, `b`}}, + {{`a`}, {`b`, `c`}}, }, }, { tcmp: Table{ - {{"b"}, {"a", "b"}}, - {{"c"}, {"a", "b"}}, - {{"a"}, {"b", "c"}}, + {{`b`}, {`a`, `b`}}, + {{`c`}, {`a`, `b`}}, + {{`a`}, {`b`, `c`}}, }, }} - for _, c := range cases { - got := table.IsEqual(c.tcmp) - test.Assert(t, "", c.exp, got) + var ( + c testCase + got bool + ) + + for _, c = range cases { + got = table.IsEqual(c.tcmp) + test.Assert(t, ``, c.exp, got) } } func TestTable_JoinCombination(t *testing.T) { - cases := []struct { - table Table + type testCase struct { s string + table Table exp Table - }{{ + } + + var cases = []testCase{{ table: Table{ - {{"a"}, {"b"}, {"c"}}, + {{`a`}, {`b`}, {`c`}}, }, - s: "X", + s: `X`, exp: Table{ - {{"a", "X"}, {"b"}, {"c"}}, - {{"a"}, {"b", "X"}, {"c"}}, - {{"a"}, {"b"}, {"c", "X"}}, + {{`a`, `X`}, {`b`}, {`c`}}, + {{`a`}, {`b`, `X`}, {`c`}}, + {{`a`}, {`b`}, {`c`, `X`}}, }, }, { table: Table{ - {{"a"}, {"b"}, {"c"}}, - {{"g"}, {"h"}}, + {{`a`}, {`b`}, {`c`}}, + {{`g`}, {`h`}}, }, - s: "X", + s: `X`, exp: Table{ - {{"a", "X"}, {"b"}, {"c"}}, - {{"a"}, {"b", "X"}, {"c"}}, - {{"a"}, {"b"}, {"c", "X"}}, - {{"g", "X"}, {"h"}}, - {{"g"}, {"h", "X"}}, + {{`a`, `X`}, {`b`}, {`c`}}, + {{`a`}, {`b`, `X`}, {`c`}}, + {{`a`}, {`b`}, {`c`, `X`}}, + {{`g`, `X`}, {`h`}}, + {{`g`}, {`h`, `X`}}, }, }} - for _, c := range cases { - got := c.table.JoinCombination(c.s) - test.Assert(t, "", c.exp, got) + var ( + c testCase + got Table + ) + for _, c = range cases { + got = c.table.JoinCombination(c.s) + test.Assert(t, ``, c.exp, got) } } diff --git a/lib/strings/testdata/clean_uri_test.txt b/lib/strings/testdata/clean_uri_test.txt new file mode 100644 index 00000000..fd85d251 --- /dev/null +++ b/lib/strings/testdata/clean_uri_test.txt @@ -0,0 +1,31 @@ +This test remove any URL from the strings. + +>>> +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]].[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.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. The United +States responded to the frequent criticism from UN organs by adopting the +[[Negroponte doctrine]]. + +<<< + 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]].[ +Pirates and emperors, old and new: international terrorism in the real world], +[[Noam Chomsky]], p. 168.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. The United +States responded to the frequent criticism from UN organs by adopting the +[[Negroponte doctrine]]. diff --git a/lib/strings/testdata/clean_wiki_markup_test.txt b/lib/strings/testdata/clean_wiki_markup_test.txt new file mode 100644 index 00000000..852d52e4 --- /dev/null +++ b/lib/strings/testdata/clean_wiki_markup_test.txt @@ -0,0 +1,37 @@ +>>> +==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}} + +
+{{Fifthdoctoraudios}} + +{{DEFAULTSORT:Eye of the Scorpion, The}} +[[Category:Fifth Doctor audio plays]] +[[Category:Fifth Doctor audio plays]] +[[:Category:2001 audio plays]] +{{DoctorWho-stub}} + +<<< +==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}} + +
+{{Fifthdoctoraudios}} + + + + + +{{DoctorWho-stub}} diff --git a/lib/strings/testdata/split_test.txt b/lib/strings/testdata/split_test.txt new file mode 100644 index 00000000..630738be --- /dev/null +++ b/lib/strings/testdata/split_test.txt @@ -0,0 +1,23 @@ +Test Split with clean and unique parameters. + +>>> Single-line +// Copyright 2016-2018 Shulhan . All rights reserved. + +<<< Single-line +Copyright 2016-2018 Shulhan ms@kilabit.info All rights reserved + +>>> Multi-line +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]]. + +<<< Multi-line +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 diff --git a/lib/strings/to_example_test.go b/lib/strings/to_example_test.go index a84e92c1..096548fd 100644 --- a/lib/strings/to_example_test.go +++ b/lib/strings/to_example_test.go @@ -9,29 +9,41 @@ import ( ) func ExampleToBytes() { - ss := []string{"This", "is", "a", "string"} - fmt.Printf("%s\n", ToBytes(ss)) - // Output: - // [This is a string] + var ( + ss = []string{`This`, `is`, `a`, `string`} + sbytes [][]byte = ToBytes(ss) + ) + + fmt.Printf(`%s`, sbytes) + // Output: [This is a string] } func ExampleToFloat64() { - in := []string{"0", "1.1", "e", "3"} + var ( + in = []string{`0`, `1.1`, `e`, `3`} + sf64 []float64 = ToFloat64(in) + ) - fmt.Println(ToFloat64(in)) + fmt.Println(sf64) // Output: [0 1.1 0 3] } func ExampleToInt64() { - in := []string{"0", "1", "e", "3.3"} + var ( + in = []string{`0`, `1`, `e`, `3.3`} + si64 []int64 = ToInt64(in) + ) - fmt.Println(ToInt64(in)) + fmt.Println(si64) // Output: [0 1 0 3] } func ExampleToStrings() { - i64 := []interface{}{0, 1.99, 2, 3} + var ( + i64 = []interface{}{0, 1.99, 2, 3} + ss []string = ToStrings(i64) + ) - fmt.Println(ToStrings(i64)) + fmt.Println(ss) // Output: [0 1.99 2 3] } diff --git a/lib/strings/to_test.go b/lib/strings/to_test.go index f04cb9b4..4112b66d 100644 --- a/lib/strings/to_test.go +++ b/lib/strings/to_test.go @@ -11,33 +11,40 @@ import ( ) func TestToFloat64(t *testing.T) { - in := []string{"0", "1.1", "e", "3"} - exp := []float64{0, 1.1, 0, 3} + var ( + in = []string{`0`, `1.1`, `e`, `3`} + exp = []float64{0, 1.1, 0, 3} + got = ToFloat64(in) + ) - got := ToFloat64(in) - - test.Assert(t, "", exp, got) + test.Assert(t, ``, exp, got) } func TestToInt64(t *testing.T) { - in := []string{"0", "1", "e", "3.3"} - exp := []int64{0, 1, 0, 3} - - got := ToInt64(in) + var ( + in = []string{`0`, `1`, `e`, `3.3`} + exp = []int64{0, 1, 0, 3} + got = ToInt64(in) + ) - test.Assert(t, "", exp, got) + test.Assert(t, ``, exp, got) } func TestToStrings(t *testing.T) { - is := make([]interface{}, 0) - i64 := []int64{0, 1, 2, 3} - exp := []string{"0", "1", "2", "3"} + var ( + is = make([]interface{}, 0) + i64 = []int64{0, 1, 2, 3} + exp = []string{`0`, `1`, `2`, `3`} + + v int64 + got []string + ) - for _, v := range i64 { + for _, v = range i64 { is = append(is, v) } - got := ToStrings(is) + got = ToStrings(is) - test.Assert(t, "", exp, got) + test.Assert(t, ``, exp, got) } -- cgit v1.3