diff options
author | Yang Zhang <yang_zhang@iapcm.ac.cn> | 2018-12-28 23:09:20 +0800 |
---|---|---|
committer | Yang Zhang <yang_zhang@iapcm.ac.cn> | 2018-12-30 08:38:31 +0800 |
commit | c0ce41540e775ec0a86770d10d5c3eb75bce0939 (patch) | |
tree | 9d89d8b2889be165160225d9f9c457530cd2e173 /util/text/text_test.go | |
parent | 52419165350a2b36706a6fb67acda862d80d6730 (diff) | |
download | git-bug-c0ce41540e775ec0a86770d10d5c3eb75bce0939.tar.gz |
Add back removed text functions
Diffstat (limited to 'util/text/text_test.go')
-rw-r--r-- | util/text/text_test.go | 70 |
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) + } + } +} |