From 35c3fcd655318c293a51c47cc5495851e7fb5305 Mon Sep 17 00:00:00 2001 From: Wu Tingfeng Date: Fri, 3 Feb 2023 07:20:29 +0800 Subject: [PATCH 01/12] Add instructions and gitignore for test coverage command. --- .gitignore | 2 ++ README.md | 6 ++++++ 2 files changed, 8 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e09deb7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.html +*.out diff --git a/README.md b/README.md index 25e9346..d30da50 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,12 @@ Most terminals or text displays / text editors using a monospace font (for examp go get github.com/rivo/uniseg ``` +## Testing + +```bash +go test -v -coverprofile coverage.out && go tool cover -html coverage.out -o coverage.html +``` + ## Examples ### Counting Characters in a String From f51ba0c88dc8fa23b23188cc25290f44dfafb496 Mon Sep 17 00:00:00 2001 From: Wu Tingfeng Date: Fri, 3 Feb 2023 07:46:01 +0800 Subject: [PATCH 02/12] Add tests for FirstWord and FirstWordInString. --- word_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/word_test.go b/word_test.go index 822039d..6997cf0 100644 --- a/word_test.go +++ b/word_test.go @@ -61,6 +61,16 @@ func TestWordCasesBytes(t *testing.T) { len(testCase.expected)) } } + word, rest, newState := FirstWord([]byte{}, -1) + if len(word) > 0 { + t.Errorf(`Expected word to be empty byte slice, got %q`, word) + } + if len(rest) > 0 { + t.Errorf(`Expected rest to be empty byte slice, got %q`, rest) + } + if newState != 0 { + t.Errorf(`Expected newState to be 0, got %d`, newState) + } } // Test all official Unicode test cases for word boundaries using the string @@ -122,6 +132,16 @@ func TestWordCasesString(t *testing.T) { len(testCase.expected)) } } + word, rest, newState := FirstWordInString("", -1) + if len(word) > 0 { + t.Errorf(`Expected word to be empty string, got %q`, word) + } + if len(rest) > 0 { + t.Errorf(`Expected rest to be empty string, got %q`, rest) + } + if newState != 0 { + t.Errorf(`Expected newState to be 0, got %d`, newState) + } } // Benchmark the use of the word break function for byte slices. From 10e8f0b8202bd841b79eba92b3be50750c3239ae Mon Sep 17 00:00:00 2001 From: Wu Tingfeng Date: Fri, 3 Feb 2023 07:46:32 +0800 Subject: [PATCH 03/12] Add tests for Width and FirstGraphemeClusterInString. --- width_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/width_test.go b/width_test.go index dcfab3c..3aedb7f 100644 --- a/width_test.go +++ b/width_test.go @@ -359,6 +359,9 @@ func TestWidthGraphemes(t *testing.T) { for index, testCase := range widthTestCases { var actual int graphemes := NewGraphemes(testCase.original) + if w := graphemes.Width(); w != 0 { + t.Errorf("Expected initial Width to be 0, got %d", w) + } for graphemes.Next() { actual += graphemes.Width() } @@ -397,6 +400,19 @@ func TestWidthGraphemesFunctionString(t *testing.T) { if actual != testCase.expected { t.Errorf("Width of %q is %d, expected %d (test case %d)", testCase.original, actual, testCase.expected, index) } + cluster, rest, width, newState := FirstGraphemeClusterInString(text, -1) + if len(cluster) > 0 { + t.Errorf(`Expected cluster to be empty string, got %q`, cluster) + } + if len(rest) > 0 { + t.Errorf(`Expected rest to be empty string, got %q`, rest) + } + if width != 0 { + t.Errorf(`Expected width to be 0, got %d`, width) + } + if newState != 0 { + t.Errorf(`Expected newState to be 0, got %d`, newState) + } } } From 234877125b606c648e510c0ee12d9ca3e4d171ff Mon Sep 17 00:00:00 2001 From: Wu Tingfeng Date: Fri, 3 Feb 2023 07:47:43 +0800 Subject: [PATCH 04/12] Add tests for Step and StepString. --- step_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/step_test.go b/step_test.go index a1344d3..79f1ae8 100644 --- a/step_test.go +++ b/step_test.go @@ -66,6 +66,19 @@ func TestStepBytesGrapheme(t *testing.T) { len(testCase.expected)) } } + cluster, rest, boundaries, newState := Step([]byte{}, -1) + if len(cluster) > 0 { + t.Errorf(`Expected cluster to be empty byte slice, got %q`, cluster) + } + if len(rest) > 0 { + t.Errorf(`Expected rest to be empty byte slice, got %q`, rest) + } + if boundaries != 0 { + t.Errorf(`Expected width to be 0, got %d`, boundaries) + } + if newState != 0 { + t.Errorf(`Expected newState to be 0, got %d`, newState) + } } // Test official word boundaries Unicode test cases for grapheme clusters using @@ -283,6 +296,19 @@ func TestStepStringGrapheme(t *testing.T) { len(testCase.expected)) } } + cluster, rest, boundaries, newState := StepString("", -1) + if len(cluster) > 0 { + t.Errorf(`Expected cluster to be empty string, got %q`, cluster) + } + if len(rest) > 0 { + t.Errorf(`Expected rest to be empty string, got %q`, rest) + } + if boundaries != 0 { + t.Errorf(`Expected width to be 0, got %d`, boundaries) + } + if newState != 0 { + t.Errorf(`Expected newState to be 0, got %d`, newState) + } } // Test official word boundaries Unicode test cases for grapheme clusters using From 5a4faf229269700fbe69e97feccd4199d5162f9f Mon Sep 17 00:00:00 2001 From: Wu Tingfeng Date: Fri, 3 Feb 2023 07:50:50 +0800 Subject: [PATCH 05/12] Add tests for FirstSentence and FirstSentenceInString. --- sentence_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/sentence_test.go b/sentence_test.go index 77f37a1..f423e52 100644 --- a/sentence_test.go +++ b/sentence_test.go @@ -63,6 +63,16 @@ func TestSentenceCasesBytes(t *testing.T) { len(testCase.expected)) } } + sentence, rest, newState := FirstSentence([]byte{}, -1) + if len(sentence) > 0 { + t.Errorf(`Expected sentence to be empty byte slice, got %q`, sentence) + } + if len(rest) > 0 { + t.Errorf(`Expected rest to be empty byte slice, got %q`, rest) + } + if newState != 0 { + t.Errorf(`Expected newState to be 0, got %d`, newState) + } } // Test all official Unicode test cases for sentence boundaries using the string @@ -124,6 +134,16 @@ func TestSentenceCasesString(t *testing.T) { len(testCase.expected)) } } + sentence, rest, newState := FirstSentenceInString("", -1) + if len(sentence) > 0 { + t.Errorf(`Expected sentence to be empty string, got %q`, sentence) + } + if len(rest) > 0 { + t.Errorf(`Expected rest to be empty string, got %q`, rest) + } + if newState != 0 { + t.Errorf(`Expected newState to be 0, got %d`, newState) + } } // Benchmark the use of the sentence break function for byte slices. From ba789bceb2e9f6a15ca2d739a90e58eacb05e430 Mon Sep 17 00:00:00 2001 From: Wu Tingfeng Date: Fri, 3 Feb 2023 07:52:16 +0800 Subject: [PATCH 06/12] Add tests for IsWordBoundary, IsSentenceBoundary, and FirstGraphemeCluster. --- grapheme_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/grapheme_test.go b/grapheme_test.go index 89b35d0..eb8bdc1 100644 --- a/grapheme_test.go +++ b/grapheme_test.go @@ -126,6 +126,9 @@ func TestGraphemesClassWord(t *testing.T) { index int cluster []rune ) + if !gr.IsWordBoundary() { + t.Error("Expected initial IsWordBoundary to be true, got false") + } GraphemeLoop: for gr.Next() { if index >= len(testCase.expected) { @@ -188,6 +191,9 @@ func TestGraphemesClassSentence(t *testing.T) { index int cluster []rune ) + if !gr.IsSentenceBoundary() { + t.Error("Expected initial IsSentenceBoundary to be true, got false") + } GraphemeLoop: for gr.Next() { if index >= len(testCase.expected) { @@ -422,6 +428,19 @@ func TestGraphemesFunctionBytes(t *testing.T) { len(testCase.expected)) } } + cluster, rest, width, newState := FirstGraphemeCluster([]byte{}, 0) + if len(cluster) > 0 { + t.Errorf(`Expected cluster to be empty byte slice, got %q`, cluster) + } + if len(rest) > 0 { + t.Errorf(`Expected rest to be empty byte slice, got %q`, rest) + } + if width != 0 { + t.Errorf(`Expected width to be 0, got %d`, width) + } + if newState != 0 { + t.Errorf(`Expected newState to be 0, got %d`, newState) + } } // Run all lists of test cases using the Graphemes function for strings. From ef1861bc1f0ca0912088450b46617f18a5b659ae Mon Sep 17 00:00:00 2001 From: Wu Tingfeng Date: Fri, 3 Feb 2023 07:54:51 +0800 Subject: [PATCH 07/12] Add tests for FirstLineSegment and FirstLineSegmentInString. --- line_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/line_test.go b/line_test.go index 368f033..07a70eb 100644 --- a/line_test.go +++ b/line_test.go @@ -61,6 +61,19 @@ func TestLineCasesBytes(t *testing.T) { len(testCase.expected)) } } + segment, rest, mustBreak, newState := FirstLineSegment([]byte{}, -1) + if len(segment) > 0 { + t.Errorf(`Expected segment to be empty byte slice, got %q`, segment) + } + if len(rest) > 0 { + t.Errorf(`Expected rest to be empty byte slice, got %q`, rest) + } + if mustBreak { + t.Error(`Expected mustBreak to be false, got true`) + } + if newState != 0 { + t.Errorf(`Expected newState to be 0, got %d`, newState) + } } // Test all official Unicode test cases for line breaks using the string @@ -122,6 +135,19 @@ func TestLineCasesString(t *testing.T) { len(testCase.expected)) } } + segment, rest, mustBreak, newState := FirstLineSegmentInString("", -1) + if len(segment) > 0 { + t.Errorf(`Expected segment to be empty string, got %q`, segment) + } + if len(rest) > 0 { + t.Errorf(`Expected rest to be empty string, got %q`, rest) + } + if mustBreak { + t.Error(`Expected mustBreak to be false, got true`) + } + if newState != 0 { + t.Errorf(`Expected newState to be 0, got %d`, newState) + } } // Benchmark the use of the line break function for byte slices. From f493054633523ffc54ff78008f3155b1a78ae45c Mon Sep 17 00:00:00 2001 From: Wu Tingfeng Date: Fri, 3 Feb 2023 07:55:36 +0800 Subject: [PATCH 08/12] Add tests for HasTrailingLineBreak and HasTrailingLineBreakInString. --- line_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/line_test.go b/line_test.go index 07a70eb..c4f86d9 100644 --- a/line_test.go +++ b/line_test.go @@ -2,6 +2,24 @@ package uniseg import "testing" +// Test the HasTrailingLineBreak function. +func TestHasTrailingLineBreak(t *testing.T) { + for _, last := range []byte{'\n', '\r'} { + if !HasTrailingLineBreak([]byte{last}) { + t.Error("Expected true") + } + } +} + +// Test the HasTrailingLineBreakInString function. +func TestHasTrailingLineBreakInString(t *testing.T) { + for _, last := range []string{"\n", "\r"} { + if !HasTrailingLineBreakInString(last) { + t.Error("Expected true") + } + } +} + // Test all official Unicode test cases for line breaks using the byte slice // function. func TestLineCasesBytes(t *testing.T) { From ad1b90bf88247160298987f2362a4a4c1e8a5932 Mon Sep 17 00:00:00 2001 From: Wu Tingfeng Date: Fri, 3 Feb 2023 07:57:45 +0800 Subject: [PATCH 09/12] Add tests for LineBreak. --- examples_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/examples_test.go b/examples_test.go index 19fcecd..c6618b4 100644 --- a/examples_test.go +++ b/examples_test.go @@ -312,8 +312,17 @@ func ExampleGraphemes_lineBreaking() { fmt.Print("‖") } } + if g.LineBreak() == uniseg.LineMustBreak { + fmt.Print("\nNo clusters left. LineMustBreak") + } + g.Reset() + if g.LineBreak() == uniseg.LineDontBreak { + fmt.Print("\nIterator has been reset. LineDontBreak") + } // Output: First |line. //‖Second |line.‖ + //No clusters left. LineMustBreak + //Iterator has been reset. LineDontBreak } func ExampleStringWidth() { From 700d40a9f7942375321d8454ab03a24a25447dfa Mon Sep 17 00:00:00 2001 From: Wu Tingfeng Date: Sat, 5 Aug 2023 11:28:55 +0800 Subject: [PATCH 10/12] remove duplicate tests --- line_test.go | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/line_test.go b/line_test.go index 8ba173e..cde13e3 100644 --- a/line_test.go +++ b/line_test.go @@ -2,24 +2,6 @@ package uniseg import "testing" -// Test the HasTrailingLineBreak function. -func TestHasTrailingLineBreak(t *testing.T) { - for _, last := range []byte{'\n', '\r'} { - if !HasTrailingLineBreak([]byte{last}) { - t.Error("Expected true") - } - } -} - -// Test the HasTrailingLineBreakInString function. -func TestHasTrailingLineBreakInString(t *testing.T) { - for _, last := range []string{"\n", "\r"} { - if !HasTrailingLineBreakInString(last) { - t.Error("Expected true") - } - } -} - // Test all official Unicode test cases for line breaks using the byte slice // function. func TestLineCasesBytes(t *testing.T) { From 93f40b4481d38a1d7507c595b935cef06dbb5525 Mon Sep 17 00:00:00 2001 From: Wu Tingfeng Date: Sat, 19 Aug 2023 18:08:35 +0800 Subject: [PATCH 11/12] Removed test coverage material --- .gitignore | 2 -- README.md | 8 +------- 2 files changed, 1 insertion(+), 9 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index e09deb7..0000000 --- a/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -*.html -*.out diff --git a/README.md b/README.md index d30da50..87ec38a 100644 --- a/README.md +++ b/README.md @@ -41,12 +41,6 @@ Most terminals or text displays / text editors using a monospace font (for examp go get github.com/rivo/uniseg ``` -## Testing - -```bash -go test -v -coverprofile coverage.out && go tool cover -html coverage.out -o coverage.html -``` - ## Examples ### Counting Characters in a String @@ -160,4 +154,4 @@ This package does not depend on any packages outside the standard library. ## Your Feedback -Add your issue here on GitHub, preferably before submitting any PR's. Feel free to get in touch if you have any questions. \ No newline at end of file +Add your issue here on GitHub, preferably before submitting any PR's. Feel free to get in touch if you have any questions. From ef94a4da15aeff0ca275665047f3e666ed1a479c Mon Sep 17 00:00:00 2001 From: Wu Tingfeng Date: Sat, 19 Aug 2023 18:14:27 +0800 Subject: [PATCH 12/12] Revert README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 87ec38a..25e9346 100644 --- a/README.md +++ b/README.md @@ -154,4 +154,4 @@ This package does not depend on any packages outside the standard library. ## Your Feedback -Add your issue here on GitHub, preferably before submitting any PR's. Feel free to get in touch if you have any questions. +Add your issue here on GitHub, preferably before submitting any PR's. Feel free to get in touch if you have any questions. \ No newline at end of file