~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/mattn/go-runewidth/runewidth_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package runewidth
 
2
 
 
3
import (
 
4
        "testing"
 
5
)
 
6
 
 
7
var runewidthtests = []struct {
 
8
        in  rune
 
9
        out int
 
10
}{
 
11
        {'世', 2},
 
12
        {'界', 2},
 
13
        {'セ', 1},
 
14
        {'カ', 1},
 
15
        {'イ', 1},
 
16
        {'☆', 2}, // double width in ambiguous
 
17
        {'\x00', 0},
 
18
        {'\x01', 1},
 
19
        {'\u0300', 0},
 
20
}
 
21
 
 
22
func TestRuneWidth(t *testing.T) {
 
23
        c := NewCondition()
 
24
        c.EastAsianWidth = true
 
25
        for _, tt := range runewidthtests {
 
26
                if out := c.RuneWidth(tt.in); out != tt.out {
 
27
                        t.Errorf("Width(%q) = %q, want %q", tt.in, out, tt.out)
 
28
                }
 
29
        }
 
30
}
 
31
 
 
32
var isambiguouswidthtests = []struct {
 
33
        in  rune
 
34
        out bool
 
35
}{
 
36
        {'世', false},
 
37
        {'■', true},
 
38
        {'界', false},
 
39
        {'○', true},
 
40
        {'㈱', false},
 
41
        {'①', true},
 
42
        {'②', true},
 
43
        {'③', true},
 
44
        {'④', true},
 
45
        {'⑤', true},
 
46
        {'⑥', true},
 
47
        {'⑦', true},
 
48
        {'⑧', true},
 
49
        {'⑨', true},
 
50
        {'⑩', true},
 
51
        {'⑪', true},
 
52
        {'⑫', true},
 
53
        {'⑬', true},
 
54
        {'⑭', true},
 
55
        {'⑮', true},
 
56
        {'⑯', true},
 
57
        {'⑰', true},
 
58
        {'⑱', true},
 
59
        {'⑲', true},
 
60
        {'⑳', true},
 
61
        {'☆', true},
 
62
}
 
63
 
 
64
func TestIsAmbiguousWidth(t *testing.T) {
 
65
        for _, tt := range isambiguouswidthtests {
 
66
                if out := IsAmbiguousWidth(tt.in); out != tt.out {
 
67
                        t.Errorf("IsAmbiguousWidth(%q) = %q, want %q", tt.in, out, tt.out)
 
68
                }
 
69
        }
 
70
}
 
71
 
 
72
var stringwidthtests = []struct {
 
73
        in  string
 
74
        out int
 
75
}{
 
76
        {"■㈱の世界①", 12},
 
77
        {"スター☆", 8},
 
78
}
 
79
 
 
80
func TestStringWidth(t *testing.T) {
 
81
        c := NewCondition()
 
82
        c.EastAsianWidth = true
 
83
        for _, tt := range stringwidthtests {
 
84
                if out := c.StringWidth(tt.in); out != tt.out {
 
85
                        t.Errorf("StringWidth(%q) = %q, want %q", tt.in, out, tt.out)
 
86
                }
 
87
        }
 
88
}
 
89
 
 
90
func TestStringWidthInvalid(t *testing.T) {
 
91
        s := "こんにちわ\x00世界"
 
92
        if out := StringWidth(s); out != 14 {
 
93
                t.Errorf("StringWidth(%q) = %q, want %q", s, out, 14)
 
94
        }
 
95
}
 
96
 
 
97
func TestTruncate(t *testing.T) {
 
98
        s := "あいうえおあいうえおえおおおおおおおおおおおおおおおおおおおおおおおおおおおおおお"
 
99
        expected := "あいうえおあいうえおえおおおおおおおおおおおおおおおおおおおおおおおおおおお..."
 
100
 
 
101
        if out := Truncate(s, 80, "..."); out != expected {
 
102
                t.Errorf("Truncate(%q) = %q, want %q", s, out, expected)
 
103
        }
 
104
}
 
105
 
 
106
func TestWrap(t *testing.T) {
 
107
        s := `東京特許許可局局長はよく柿喰う客だ/東京特許許可局局長はよく柿喰う客だ
 
108
123456789012345678901234567890
 
109
 
 
110
END`
 
111
        expected := `東京特許許可局局長はよく柿喰う
 
112
客だ/東京特許許可局局長はよく
 
113
柿喰う客だ
 
114
123456789012345678901234567890
 
115
 
 
116
END`
 
117
 
 
118
        if out := Wrap(s, 30); out != expected {
 
119
                t.Errorf("Wrap(%q) = %q, want %q", s, out, expected)
 
120
        }
 
121
}
 
122
 
 
123
func TestTruncateNoNeeded(t *testing.T) {
 
124
        s := "あいうえおあい"
 
125
        expected := "あいうえおあい"
 
126
 
 
127
        if out := Truncate(s, 80, "..."); out != expected {
 
128
                t.Errorf("Truncate(%q) = %q, want %q", s, out, expected)
 
129
        }
 
130
}
 
131
 
 
132
var isneutralwidthtests = []struct {
 
133
        in  rune
 
134
        out bool
 
135
}{
 
136
        {'→', false},
 
137
        {'┊', false},
 
138
        {'┈', false},
 
139
        {'~', false},
 
140
        {'└', false},
 
141
        {'⣀', true},
 
142
        {'⣀', true},
 
143
}
 
144
 
 
145
func TestIsNeutralWidth(t *testing.T) {
 
146
        for _, tt := range isneutralwidthtests {
 
147
                if out := IsNeutralWidth(tt.in); out != tt.out {
 
148
                        t.Errorf("IsNeutralWidth(%q) = %q, want %q", tt.in, out, tt.out)
 
149
                }
 
150
        }
 
151
}
 
152
 
 
153
func TestFillLeft(t *testing.T) {
 
154
        s := "あxいうえお"
 
155
        expected := "    あxいうえお"
 
156
 
 
157
        if out := FillLeft(s, 15); out != expected {
 
158
                t.Errorf("FillLeft(%q) = %q, want %q", s, out, expected)
 
159
        }
 
160
}
 
161
 
 
162
func TestFillRight(t *testing.T) {
 
163
        s := "あxいうえお"
 
164
        expected := "あxいうえお    "
 
165
 
 
166
        if out := FillRight(s, 15); out != expected {
 
167
                t.Errorf("FillRight(%q) = %q, want %q", s, out, expected)
 
168
        }
 
169
}