forked from rjkroege/edwood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
95 lines (90 loc) · 2.7 KB
/
util_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
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
package main
import (
"github.com/rjkroege/edwood/internal/util"
"path/filepath"
"reflect"
"testing"
)
func TestCvttorunes(t *testing.T) {
testCases := []struct {
p []byte
n int
r []rune
nb int
nulls bool
}{
{[]byte("Hello world"), 11, []rune("Hello world"), 11, false},
{[]byte("Hello \x00\x00world"), 13, []rune("Hello world"), 13, true},
{[]byte("Hello 世界"), 6 + 3 + 3, []rune("Hello 世界"), 6 + 3 + 3, false},
{[]byte("Hello 世界"), 6 + 3 + 1, []rune("Hello 世界"), 6 + 3 + 3, false},
{[]byte("Hello 世界"), 6 + 3 + 2, []rune("Hello 世界"), 6 + 3 + 3, false},
{[]byte("Hello 世\xe7\x95"), 6 + 3 + 1, []rune("Hello 世\uFFFD"), 6 + 3 + 1, false},
{[]byte("Hello 世\xe7\x95"), 6 + 3 + 2, []rune("Hello 世\uFFFD\uFFFD"), 6 + 3 + 2, false},
{[]byte("\xe4\xb8\x96界 hello"), 3 + 3 + 6, []rune("世界 hello"), 3 + 3 + 6, false},
{[]byte("\xb8\x96界 hello"), 2 + 3 + 6, []rune("\uFFFD\uFFFD界 hello"), 2 + 3 + 6, false},
{[]byte("\x96界 hello"), 1 + 3 + 6, []rune("\uFFFD界 hello"), 1 + 3 + 6, false},
}
for _, tc := range testCases {
r, nb, nulls := util.Cvttorunes(tc.p, tc.n)
if !reflect.DeepEqual(r, tc.r) || nb != tc.nb || nulls != tc.nulls {
t.Errorf("util.Cvttorunes of (%q, %v) returned %q, %v, %v; expected %q, %v, %v\n",
tc.p, tc.n, r, nb, nulls, tc.r, tc.nb, tc.nulls)
}
}
}
func TestErrorwin1Name(t *testing.T) {
tt := []struct {
dir, name string
}{
{"", "+Errors"},
{".", "+Errors"},
{"/", "/+Errors"},
{"/home/gopher", "/home/gopher/+Errors"},
{"/home/gopher/", "/home/gopher/+Errors"},
{"C:/Users/gopher", "C:/Users/gopher/+Errors"},
{"C:/Users/gopher/", "C:/Users/gopher/+Errors"},
{"C:/", "C:/+Errors"},
}
for _, tc := range tt {
name := filepath.ToSlash(errorwin1Name(filepath.FromSlash(tc.dir)))
if name != tc.name {
t.Errorf("errorwin1Name(%q) is %q; expected %q", tc.dir, name, tc.name)
}
}
}
func TestQuote(t *testing.T) {
var testCases = []struct {
s, q string
}{
{"", "''"},
{"Edwood", "Edwood"},
{"Plan 9", "'Plan 9'"},
{"Don't", "'Don''t'"},
{"Don't worry!", "'Don''t worry!'"},
}
for _, tc := range testCases {
q := quote(tc.s)
if q != tc.q {
t.Errorf("%q quoted is %q; expected %q\n", tc.s, q, tc.q)
}
}
}
func TestSkipbl(t *testing.T) {
tt := []struct {
s []rune
q []rune
}{
{nil, nil},
{[]rune(" \t\n"), nil},
{[]rune(" \t\nabc"), []rune("abc")},
{[]rune(" \t\n \t\nabc"), []rune("abc")},
{[]rune(" \t\nabc \t\nabc"), []rune("abc \t\nabc")},
{[]rune(" \t\nαβγ \t\nαβγ"), []rune("αβγ \t\nαβγ")},
}
for _, tc := range tt {
q := skipbl(tc.s)
if !reflect.DeepEqual(q, tc.q) {
t.Errorf("skipbl(%v) returned %v; expected %v", tc.s, q, tc.q)
}
}
}