| 1 |
// Copyright 2015 Huan Du. All rights reserved. |
| 2 |
// Licensed under the MIT license that can be found in the LICENSE file. |
| 3 |
|
| 4 |
package xstrings |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
func TestLen(t *testing.T) { |
| 12 |
runner := func(str string) string { |
| 13 |
return fmt.Sprint(Len(str)) |
| 14 |
} |
| 15 |
|
| 16 |
runTestCases(t, runner, _M{ |
| 17 |
"abcdef": "6", |
| 18 |
"中文": "2", |
| 19 |
"中yin文hun排": "9", |
| 20 |
"": "0", |
| 21 |
}) |
| 22 |
} |
| 23 |
|
| 24 |
func TestWordCount(t *testing.T) { |
| 25 |
runner := func(str string) string { |
| 26 |
return fmt.Sprint(WordCount(str)) |
| 27 |
} |
| 28 |
|
| 29 |
runTestCases(t, runner, _M{ |
| 30 |
"one word: λ": "3", |
| 31 |
"中文": "0", |
| 32 |
"你好,sekai!": "1", |
| 33 |
"oh, it's super-fancy!!a": "4", |
| 34 |
"": "0", |
| 35 |
"-": "0", |
| 36 |
"it's-'s": "1", |
| 37 |
}) |
| 38 |
} |
| 39 |
|
| 40 |
func TestWidth(t *testing.T) { |
| 41 |
runner := func(str string) string { |
| 42 |
return fmt.Sprint(Width(str)) |
| 43 |
} |
| 44 |
|
| 45 |
runTestCases(t, runner, _M{ |
| 46 |
"abcd\t0123\n7890": "12", |
| 47 |
"中zh英eng文混排": "15", |
| 48 |
"": "0", |
| 49 |
}) |
| 50 |
} |
| 51 |
|
| 52 |
func TestRuneWidth(t *testing.T) { |
| 53 |
runner := func(str string) string { |
| 54 |
return fmt.Sprint(RuneWidth([]rune(str)[0])) |
| 55 |
} |
| 56 |
|
| 57 |
runTestCases(t, runner, _M{ |
| 58 |
"a": "1", |
| 59 |
"中": "2", |
| 60 |
"\x11": "0", |
| 61 |
}) |
| 62 |
} |