This repository has been archived by the owner on Jun 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
textutils_test.go
164 lines (144 loc) · 3.52 KB
/
textutils_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
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
package ion
import (
"strings"
"testing"
"time"
)
func TestParseTimestamp(t *testing.T) {
test := func(str string, eval string) {
t.Run(str, func(t *testing.T) {
val, err := parseTimestamp(str)
if err != nil {
t.Fatal(err)
}
et, err := time.Parse(time.RFC3339Nano, eval)
if err != nil {
t.Fatal(err)
}
if !val.Equal(et) {
t.Errorf("expected %v, got %v", eval, val)
}
})
}
test("1234T", "1234-01-01T00:00:00Z")
test("1234-05T", "1234-05-01T00:00:00Z")
test("1234-05-06", "1234-05-06T00:00:00Z")
test("1234-05-06T", "1234-05-06T00:00:00Z")
test("1234-05-06T07:08Z", "1234-05-06T07:08:00Z")
test("1234-05-06T07:08:09Z", "1234-05-06T07:08:09Z")
test("1234-05-06T07:08:09.100Z", "1234-05-06T07:08:09.100Z")
test("1234-05-06T07:08:09.100100Z", "1234-05-06T07:08:09.100100Z")
test("1234-05-06T07:08+09:10", "1234-05-06T07:08:00+09:10")
test("1234-05-06T07:08:09-10:11", "1234-05-06T07:08:09-10:11")
}
func TestWriteSymbol(t *testing.T) {
test := func(sym, expected string) {
t.Run(expected, func(t *testing.T) {
buf := strings.Builder{}
if err := writeSymbol(sym, &buf); err != nil {
t.Fatal(err)
}
actual := buf.String()
if actual != expected {
t.Errorf("expected \"%v\", got \"%v\"", expected, actual)
}
})
}
test("", "''")
test("null", "'null'")
test("null.null", "'null.null'")
test("basic", "basic")
test("_basic_", "_basic_")
test("$basic$", "$basic$")
test("$123", "$123")
test("123", "'123'")
test("abc'def", "'abc\\'def'")
test("abc\"def", "'abc\"def'")
}
func TestSymbolNeedsQuoting(t *testing.T) {
test := func(sym string, expected bool) {
t.Run(sym, func(t *testing.T) {
actual := symbolNeedsQuoting(sym)
if actual != expected {
t.Errorf("expected %v, got %v", expected, actual)
}
})
}
test("", true)
test("null", true)
test("true", true)
test("false", true)
test("nan", true)
test("basic", false)
test("_basic_", false)
test("basic$123", false)
test("$", false)
test("$basic", false)
test("$123", false)
test("123", true)
test("abc.def", true)
test("abc,def", true)
test("abc:def", true)
test("abc{def", true)
test("abc}def", true)
test("abc[def", true)
test("abc]def", true)
test("abc'def", true)
test("abc\"def", true)
}
func TestIsSymbolRef(t *testing.T) {
test := func(sym string, expected bool) {
t.Run(sym, func(t *testing.T) {
actual := isSymbolRef(sym)
if actual != expected {
t.Errorf("expected %v, got %v", expected, actual)
}
})
}
test("", false)
test("1", false)
test("a", false)
test("$", false)
test("$1", true)
test("$1234567890", true)
test("$a", false)
test("$1234a567890", false)
}
func TestWriteEscapedSymbol(t *testing.T) {
test := func(sym, expected string) {
t.Run(expected, func(t *testing.T) {
buf := strings.Builder{}
if err := writeEscapedSymbol(sym, &buf); err != nil {
t.Fatal(err)
}
actual := buf.String()
if actual != expected {
t.Errorf("bad encoding of \"%v\": \"%v\"",
expected, actual)
}
})
}
test("basic", "basic")
test("\"basic\"", "\"basic\"")
test("o'clock", "o\\'clock")
test("c:\\", "c:\\\\")
}
func TestWriteEscapedChar(t *testing.T) {
test := func(c byte, expected string) {
t.Run(expected, func(t *testing.T) {
buf := strings.Builder{}
if err := writeEscapedChar(c, &buf); err != nil {
t.Fatal(err)
}
actual := buf.String()
if actual != expected {
t.Errorf("bad encoding of '%v': \"%v\"",
expected, actual)
}
})
}
test(0, "\\0")
test('\n', "\\n")
test(1, "\\x01")
test('\xFF', "\\xFF")
}