generated from milosgajdos/go-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update: add splitter tests * fix: bugfixing sadness Signed-off-by: Milos Gajdos <[email protected]> --------- Signed-off-by: Milos Gajdos <[email protected]>
- Loading branch information
1 parent
eacb758
commit 817f998
Showing
6 changed files
with
295 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package text | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestCharSplitter(t *testing.T) { | ||
t.Parallel() | ||
var testCases = []struct { | ||
size int | ||
overlap int | ||
trim bool | ||
keepSep bool | ||
sep Sep | ||
input string | ||
exp []string | ||
}{ | ||
{ | ||
size: 7, | ||
overlap: 3, | ||
sep: Sep{Value: " "}, | ||
input: "foo bar baz 123", | ||
exp: []string{"foo bar", "bar baz", "baz 123"}, | ||
}, | ||
{ | ||
size: 2, | ||
overlap: 0, | ||
sep: Sep{Value: " "}, | ||
input: "foo bar", | ||
exp: []string{"foo", "bar"}, | ||
}, | ||
{ | ||
size: 3, | ||
overlap: 1, | ||
sep: Sep{Value: " "}, | ||
input: "foo bar baz a a", | ||
exp: []string{"foo", "bar", "baz", "a a"}, | ||
}, | ||
{ | ||
size: 3, | ||
overlap: 1, | ||
sep: Sep{Value: " "}, | ||
input: "a a foo bar baz", | ||
exp: []string{"a a", "foo", "bar", "baz"}, | ||
}, | ||
{ | ||
size: 1, | ||
overlap: 1, | ||
sep: Sep{Value: " "}, | ||
input: "foo bar baz 123", | ||
exp: []string{"foo", "bar", "baz", "123"}, | ||
}, | ||
{ | ||
size: 1, | ||
overlap: 0, | ||
keepSep: true, | ||
sep: Sep{Value: ".", IsRegexp: false}, | ||
input: "foo.bar.baz.123", | ||
exp: []string{"foo", ".bar", ".baz", ".123"}, | ||
}, | ||
{ | ||
size: 1, | ||
overlap: 0, | ||
keepSep: true, | ||
sep: Sep{Value: `\.`, IsRegexp: true}, | ||
input: "foo.bar.baz.123", | ||
exp: []string{"foo", ".bar", ".baz", ".123"}, | ||
}, | ||
{ | ||
size: 1, | ||
overlap: 0, | ||
sep: Sep{Value: ".", IsRegexp: false}, | ||
input: "foo.bar.baz.123", | ||
exp: []string{"foo", "bar", "baz", "123"}, | ||
}, | ||
{ | ||
size: 1, | ||
overlap: 0, | ||
sep: Sep{Value: `\.`, IsRegexp: true}, | ||
input: "foo.bar.baz.123", | ||
exp: []string{"foo", "bar", "baz", "123"}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
s := NewSplitterWithConfig(Config{ | ||
ChunkSize: tc.size, | ||
ChunkOverlap: tc.overlap, | ||
TrimSpace: tc.trim, | ||
KeepSep: tc.keepSep, | ||
LenFunc: DefaultLenFunc, | ||
}) | ||
cs := NewCharSplitter(). | ||
WithSplitter(s). | ||
WithSep(tc.sep) | ||
|
||
t.Run(fmt.Sprintf("sep=%#v,size=%d,overlap=%d,trim=%v,keepSep=%v", | ||
tc.sep, tc.size, tc.overlap, tc.trim, tc.keepSep), | ||
func(t *testing.T) { | ||
t.Parallel() | ||
splits := cs.Split(tc.input) | ||
if !reflect.DeepEqual(splits, tc.exp) { | ||
t.Errorf("expected: %#v, got: %#v", tc.exp, splits) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package text | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestRecursiveCharSplitter(t *testing.T) { | ||
t.Parallel() | ||
var testCases = []struct { | ||
size int | ||
overlap int | ||
trim bool | ||
keepSep bool | ||
seps []Sep | ||
input string | ||
exp []string | ||
}{ | ||
{ | ||
size: 10, | ||
overlap: 1, | ||
trim: true, | ||
keepSep: true, | ||
seps: DefaultSeparators, | ||
input: `Hi.` + "\n\n" + `I'm Harrison.` + "\n\n" + `How? Are? You?` + "\n" + `Okay then f f f f. | ||
This is a weird text to write, but gotta test the splittingggg some how. | ||
Bye!` + "\n\n" + `-H.`, | ||
exp: []string{ | ||
"Hi.", | ||
"I'm", | ||
"Harrison.", | ||
"How? Are?", | ||
"You?", | ||
"Okay then", | ||
"f f f f.", | ||
"This is a", | ||
"weird", | ||
"text to", | ||
"write,", | ||
"but gotta", | ||
"test the", | ||
"splitting", | ||
"gggg", | ||
"some how.", | ||
"Bye!", | ||
"-H.", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
s := NewSplitterWithConfig(Config{ | ||
ChunkSize: tc.size, | ||
ChunkOverlap: tc.overlap, | ||
TrimSpace: tc.trim, | ||
KeepSep: tc.keepSep, | ||
LenFunc: DefaultLenFunc, | ||
}) | ||
cs := NewRecursiveCharSplitter(). | ||
WithSplitter(s). | ||
WithSeps(tc.seps) | ||
|
||
t.Run(fmt.Sprintf("sep=%#v,size=%d,overlap=%d,trim=%v,keepSep=%v", | ||
tc.seps, tc.size, tc.overlap, tc.trim, tc.keepSep), | ||
func(t *testing.T) { | ||
t.Parallel() | ||
splits := cs.Split(tc.input) | ||
if !reflect.DeepEqual(splits, tc.exp) { | ||
t.Errorf("expected: %#v, got: %#v", tc.exp, splits) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.