forked from gizak/termui
-
Notifications
You must be signed in to change notification settings - Fork 2
/
helper_test.go
70 lines (58 loc) · 1.69 KB
/
helper_test.go
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
// Copyright 2017 Zack Guo <[email protected]>. All rights reserved.
// Use of this source code is governed by a MIT license that can
// be found in the LICENSE file.
package termui
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestStr2Rune(t *testing.T) {
s := "你好,世界."
rs := str2runes(s)
if len(rs) != 6 {
t.Error(t)
}
}
func TestWidth(t *testing.T) {
s0 := "つのだ☆HIRO"
s1 := "11111111111"
// above not align for setting East Asian Ambiguous to wide!!
if strWidth(s0) != strWidth(s1) {
t.Error("str len failed")
}
len1 := []rune{'a', '2', '&', '「', 'オ', '。'} //will false: 'ᆵ', 'ᄚ', 'ᄒ'
for _, v := range len1 {
if charWidth(v) != 1 {
t.Error("len1 failed")
}
}
len2 := []rune{'漢', '字', '한', '자', '你', '好', 'だ', '。', '%', 's', 'E', 'ョ', '、', 'ヲ'}
for _, v := range len2 {
if charWidth(v) != 2 {
t.Error("len2 failed")
}
}
}
func TestTrim(t *testing.T) {
s := "つのだ☆HIRO"
if string(trimStr2Runes(s, 10)) != "つのだ☆HI"+dot {
t.Error("trim failed")
}
if string(trimStr2Runes(s, 11)) != "つのだ☆HIRO" {
t.Error("avoid tail trim failed")
}
if string(trimStr2Runes(s, 15)) != "つのだ☆HIRO" {
t.Error("avoid trim failed")
}
}
func TestTrimStrIfAppropriate_NoTrim(t *testing.T) {
assert.Equal(t, "hello", TrimStrIfAppropriate("hello", 5))
}
func TestTrimStrIfAppropriate(t *testing.T) {
assert.Equal(t, "hel…", TrimStrIfAppropriate("hello", 4))
assert.Equal(t, "h…", TrimStrIfAppropriate("hello", 2))
}
func TestStringToAttribute(t *testing.T) {
assert.Equal(t, ColorRed, StringToAttribute("ReD"))
assert.Equal(t, ColorRed|AttrBold, StringToAttribute("RED, bold"))
}