Skip to content

Commit

Permalink
Merge pull request #28 from elliotwutingfeng/coverage
Browse files Browse the repository at this point in the history
Improve test coverage to 100%
  • Loading branch information
rivo authored Aug 19, 2023
2 parents a54496b + ef94a4d commit a353c27
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
19 changes: 19 additions & 0 deletions grapheme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,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) {
Expand Down Expand Up @@ -191,6 +194,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) {
Expand Down Expand Up @@ -425,6 +431,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.
Expand Down
26 changes: 26 additions & 0 deletions line_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

var hasTrailingLineBreakTestCases = []struct {
Expand Down
20 changes: 20 additions & 0 deletions sentence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
26 changes: 26 additions & 0 deletions step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions width_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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)
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions word_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit a353c27

Please sign in to comment.