aboutsummaryrefslogtreecommitdiffstats
path: root/util/text/text_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'util/text/text_test.go')
-rw-r--r--util/text/text_test.go70
1 files changed, 70 insertions, 0 deletions
diff --git a/util/text/text_test.go b/util/text/text_test.go
index c70d2ccd..f5b15a43 100644
--- a/util/text/text_test.go
+++ b/util/text/text_test.go
@@ -203,3 +203,73 @@ func TestWordLen(t *testing.T) {
}
}
}
+
+func TestSplitWord(t *testing.T) {
+ cases := []struct {
+ Input string
+ Length int
+ Result, Leftover string
+ }{
+ // A simple word passes through.
+ {
+ "foo",
+ 4,
+ "foo", "",
+ },
+ // Cut at the right place
+ {
+ "foobarHoy",
+ 4,
+ "foob", "arHoy",
+ },
+ // A simple word passes through with colors
+ {
+ "\x1b[31mbar\x1b[0m",
+ 4,
+ "\x1b[31mbar\x1b[0m", "",
+ },
+ // Cut at the right place with colors
+ {
+ "\x1b[31mfoobarHoy\x1b[0m",
+ 4,
+ "\x1b[31mfoob", "arHoy\x1b[0m",
+ },
+ // Handle prefix and suffix properly
+ {
+ "foo\x1b[31mfoobarHoy\x1b[0mbaaar",
+ 4,
+ "foo\x1b[31mf", "oobarHoy\x1b[0mbaaar",
+ },
+ // Cut properly with length = 0
+ {
+ "foo",
+ 0,
+ "", "foo",
+ },
+ // Handle chinese
+ {
+ "快檢什麼望對",
+ 4,
+ "快檢", "什麼望對",
+ },
+ {
+ "快檢什麼望對",
+ 5,
+ "快檢", "什麼望對",
+ },
+ // Handle chinese with colors
+ {
+ "快\x1b[31m檢什麼\x1b[0m望對",
+ 4,
+ "快\x1b[31m檢", "什麼\x1b[0m望對",
+ },
+ }
+
+ for i, tc := range cases {
+ result, leftover := splitWord(tc.Input, tc.Length)
+ if result != tc.Result || leftover != tc.Leftover {
+ t.Fatalf("Case %d Input:\n\n`%s`\n\nExpected Output:\n\n`%s` - `%s`\n\nActual Output:\n\n`%s` - `%s`",
+ i, tc.Input, tc.Result, tc.Leftover, result, leftover)
+ }
+ }
+}