Repository navigation
Expand file tree
/
Copy pathsplit_test.go
More file actions
71 lines (65 loc) · 1.89 KB
/
Copy pathsplit_test.go
File metadata and controls
71 lines (65 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"reflect"
"testing"
"unicode/utf8"
)
func TestSentenceSplitterSplitPreservesEndingPunctuation(t *testing.T) {
splitter := NewSentenceSplitter()
tests := []struct {
name string
text string
want []string
}{
{
name: "sentence punctuation",
text: "First sentence. Is this working? It is!",
want: []string{"First sentence.", "Is this working?", "It is!"},
},
{
name: "punctuation followed by closing quote",
text: "He said \"Hello.\" Then he left.",
want: []string{"He said \"Hello.\"", "Then he left."},
},
{
name: "abbreviations",
text: "Dr. Smith arrived. Mr. Jones left.",
want: []string{"Dr. Smith arrived.", "Mr. Jones left."},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := splitter.Split(tt.text); !reflect.DeepEqual(got, tt.want) {
t.Fatalf("Split(%q) = %#v, want %#v", tt.text, got, tt.want)
}
})
}
}
func TestCustomDelimitersAndLiteralTokens(t *testing.T) {
s := NewSentenceSplitter()
for _, tc := range []struct {
text, delimiters string
want []string
}{
{"One; Two.", ";", []string{"One;", "Two."}},
{"One- Two.", "-", []string{"One-", "Two."}},
{"__ABBR1__ is literal. Dr. Smith left.", "", []string{"__ABBR1__ is literal.", "Dr. Smith left."}},
{"\uE000 is literal. Next!", "", []string{"\uE000 is literal.", "Next!"}},
} {
if got := s.SplitWithDelimiters(tc.text, tc.delimiters); !reflect.DeepEqual(got, tc.want) {
t.Fatalf("got %#v, want %#v", got, tc.want)
}
}
}
func FuzzSentenceDelimiters(f *testing.F) {
for _, chars := range []string{"-", "]", "\\", "[a-z]", ".!?"} {
f.Add(chars)
}
f.Fuzz(func(t *testing.T, chars string) {
// The HTTP layer accepts only bounded, valid UTF-8 delimiter strings.
if len(chars) > 128 || !utf8.ValidString(chars) {
return
}
NewSentenceSplitter().SplitWithDelimiters("A sentence. Another sentence!", chars)
})
}