-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharset_test.go
More file actions
225 lines (198 loc) · 5.43 KB
/
charset_test.go
File metadata and controls
225 lines (198 loc) · 5.43 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package xterm
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestCharsetDECSpecialGraphics(t *testing.T) {
t.Parallel()
type TestCase struct {
Name string
Input byte
Expected rune
}
tests := []TestCase{
{"diamond", '`', '\u25c6'},
{"checkerboard", 'a', '\u2592'},
{"degree", 'f', '\u00b0'},
{"lower-right corner", 'j', '\u2518'},
{"upper-right corner", 'k', '\u2510'},
{"upper-left corner", 'l', '\u250c'},
{"lower-left corner", 'm', '\u2514'},
{"crossing", 'n', '\u253c'},
{"horizontal line", 'q', '\u2500'},
{"vertical line", 'x', '\u2502'},
{"pi", '{', '\u03c0'},
{"not-equal", '|', '\u2260'},
{"pound", '}', '\u00a3'},
{"middle dot", '~', '\u00b7'},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
type Expectation struct {
Rune rune
Found bool
}
r, ok := CharsetDECSpecialGraphics[tc.Input]
got := Expectation{Rune: r, Found: ok}
expected := Expectation{Rune: tc.Expected, Found: true}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
}
func TestCHARSETSMap(t *testing.T) {
t.Parallel()
type TestCase struct {
Name string
Designator byte
IsNil bool
SampleKey byte
SampleVal rune
}
tests := []TestCase{
{"US (B) is nil", 'B', true, 0, 0},
{"DEC special (0)", '0', false, 'q', '\u2500'},
{"British (A)", 'A', false, '#', '\u00a3'},
{"Finnish (C)", 'C', false, '[', '\u00c4'},
{"Finnish alias (5)", '5', false, '[', '\u00c4'},
{"German (K)", 'K', false, '~', '\u00df'},
{"Swedish (H)", 'H', false, '@', '\u00c9'},
{"Swedish alias (7)", '7', false, '@', '\u00c9'},
{"Swiss (=)", '=', false, '#', '\u00f9'},
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
type Expectation struct {
Found bool
IsNil bool
SampleVal rune
}
cs, found := CHARSETS[tc.Designator]
var sampleVal rune
if cs != nil && tc.SampleKey != 0 {
sampleVal = cs[tc.SampleKey]
}
got := Expectation{Found: found, IsNil: cs == nil, SampleVal: sampleVal}
expected := Expectation{Found: true, IsNil: tc.IsNil, SampleVal: tc.SampleVal}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
})
}
}
func TestCharsetServiceReset(t *testing.T) {
t.Parallel()
type Expectation struct {
CharsetNil bool
GLevel int
NumSlots int
}
cs := NewCharsetService()
cs.SetgCharset(0, CharsetDECSpecialGraphics)
cs.SetgLevel(0)
cs.Reset()
got := Expectation{
CharsetNil: cs.Charset == nil,
GLevel: cs.GLevel,
NumSlots: len(cs.Charsets()),
}
expected := Expectation{CharsetNil: true, GLevel: 0, NumSlots: 4}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestCharsetServiceSetgCharset(t *testing.T) {
t.Parallel()
type Expectation struct {
ActiveCharsetNil bool
G1CharsetNil bool
MappedRune rune
}
cs := NewCharsetService()
cs.SetgCharset(1, CharsetBritish)
// Active charset should still be nil (GLevel=0, G0 is nil)
got := Expectation{
ActiveCharsetNil: cs.Charset == nil,
G1CharsetNil: cs.Charsets()[1] == nil,
}
expected := Expectation{ActiveCharsetNil: true, G1CharsetNil: false}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
// Now switch to G1
cs.SetgLevel(1)
r := cs.Charset['#']
got2 := Expectation{
ActiveCharsetNil: cs.Charset == nil,
MappedRune: r,
}
expected2 := Expectation{ActiveCharsetNil: false, MappedRune: '\u00a3'}
if diff := cmp.Diff(expected2, got2); diff != "" {
t.Errorf("after SetgLevel (-want +got):\n%s", diff)
}
}
func TestCharsetServiceSetgLevel(t *testing.T) {
t.Parallel()
type Expectation struct {
GLevel int
CharsetIsNil bool
}
cs := NewCharsetService()
cs.SetgCharset(0, CharsetDECSpecialGraphics)
cs.SetgCharset(2, CharsetGerman)
cs.SetgLevel(2)
got := Expectation{GLevel: cs.GLevel, CharsetIsNil: cs.Charset == nil}
expected := Expectation{GLevel: 2, CharsetIsNil: false}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
// Verify the active charset is German
type Expectation2 struct {
Rune rune
}
got2 := Expectation2{Rune: cs.Charset['~']}
expected2 := Expectation2{Rune: '\u00df'}
if diff := cmp.Diff(expected2, got2); diff != "" {
t.Errorf("charset mapping (-want +got):\n%s", diff)
}
}
func TestCharsetServiceSetgCharsetUpdatesActive(t *testing.T) {
t.Parallel()
// When setting a charset on the active GLevel, the active charset should update.
type Expectation struct {
CharsetNil bool
MappedRune rune
}
cs := NewCharsetService()
// GLevel defaults to 0
cs.SetgCharset(0, CharsetFrench)
got := Expectation{
CharsetNil: cs.Charset == nil,
MappedRune: cs.Charset['#'],
}
expected := Expectation{CharsetNil: false, MappedRune: '\u00a3'}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}
func TestCharsetServiceSetgCharsetExpandsSlots(t *testing.T) {
t.Parallel()
// Setting G3 should expand the charsets slice if needed.
type Expectation struct {
MinSlots int
G3IsNil bool
}
cs := NewCharsetService()
cs.SetgCharset(3, CharsetSwiss)
got := Expectation{
MinSlots: len(cs.Charsets()),
G3IsNil: cs.Charsets()[3] == nil,
}
expected := Expectation{MinSlots: 4, G3IsNil: false}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("(-want +got):\n%s", diff)
}
}