-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue_test.go
180 lines (159 loc) · 3.99 KB
/
value_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package parse
import (
"fmt"
"testing"
)
type sTst struct {
input, result string
ok bool
}
var sTests = []sTst{
{"`abc`", "abc", true},
{"`\\n\n\\n`", "\\n\n\\n", true},
{"\"\\\"\"", "\"", true},
{`"Hello, world!\n"`, "Hello, world!\n", true},
{`"日本語"`, "日本語", true},
{"\"\\u65e5本\\U00008a9e\"", "\u65e5本\U00008a9e", true},
{"\"\\xff\\u00FF\"", "\xff\u00FF", true},
{"\"\\uD800\"", "", false}, // illegal: surrogate half
{"\"\\U00110000\"", "", false}, // illegal: invalid Unicode code point
{"\"日本語\"", "日本語", true}, // UTF-8 input text
{"`日本語`", "日本語", true}, // UTF-8 input text as a raw literal
{"\"\\u65e5\\u672c\\u8a9e\"", "日本語", true}, // the explicit Unicode code points
{"\"\\U000065e5\\U0000672c\\U00008a9e\"", "日本語", true}, // the explicit Unicode code points
{"\"\\xe6\\x97\\xa5\\xe6\\x9c\\xac\\xe8\\xaa\\x9e\"", "日本語", true}, // the explicit UTF-8 bytes
{"\"\\xzz\"", "", false},
{"\"....", "", false},
{"`.......", "", false},
}
func TestString(t *testing.T) {
fmt.Println("Test string parsers")
for i, t := range sTests {
var s string
fmt.Printf("TEST [%d] ", i)
l, err := Parse(&s, []byte(t.input), nil)
if err != nil {
if t.ok {
fmt.Printf("ERROR: `%s` %s\n", t.input, err.Error())
} else {
fmt.Printf("OK (%d): %s\n", l, err.Error())
}
} else {
if !t.ok {
fmt.Printf("ERROR (%d): parsed %s\n", l, s)
} else {
if s == t.result {
fmt.Printf("OK (%d): %s\n", l, s)
} else {
fmt.Printf("ERROR (%d): '%s' != '%s'\n", l, s, t.result)
}
}
}
}
}
type iTst struct {
input string
sresult int64
uresult uint64
ok bool
}
var iTests = []iTst{
{"0", 0, 0, true},
{"1233", 1233, 0, true},
{"-5", -5, 0, true},
{"0x666", 0, 0x666, true},
{"077", 0, 077, true},
{"-abc", 0, 0, false},
}
func TestInt(t *testing.T) {
fmt.Println("Test int parsers")
for i, t := range iTests {
var iv int64
var uv uint64
var ok bool
var err error
var l int
fmt.Printf("TEST [%d] ", i)
if t.sresult == 0 {
l, err = Parse(&uv, []byte(t.input), nil)
ok = uv == t.uresult
} else {
l, err = Parse(&iv, []byte(t.input), nil)
ok = iv == t.sresult
}
if err != nil {
if t.ok {
fmt.Printf("ERROR: `%s` %s\n", t.input, err.Error())
} else {
fmt.Printf("OK (%d): %s\n", l, err.Error())
}
} else {
if !t.ok {
fmt.Printf("ERROR (%d): parsed %s\n", l, t.input)
} else {
if ok {
fmt.Printf("OK (%d): %s\n", l, t.input)
} else {
fmt.Printf("ERROR (%d): %s != %d or %d\n", l, t.input, uv, iv)
}
}
}
}
}
func TestBool(t *testing.T) {
var b bool
_, err := Parse(&b, []byte("false"), nil)
if err != nil {
fmt.Printf("ERROR: %v\n", err)
} else {
fmt.Printf("OK: parsed\n")
}
_, err = Parse(&b, []byte("true"), nil)
if err != nil {
fmt.Printf("ERROR: %v\n", err)
} else {
fmt.Printf("OK: parsed\n")
}
_, err = Parse(&b, []byte("YES"), nil)
if err != nil {
fmt.Printf("OK: %v\n", err)
} else {
fmt.Printf("ERROR: parsed invalid boolean\n")
}
}
type fTst struct {
input string
result float64
ok bool
}
var fTests = []fTst{
{"0.1", 0.1, true},
{"-0.1", -0.1, true},
{"0.1e2", 0.1e2, true},
{"0.1e-4", 0.1e-4, true},
{"-.1", -.1, true},
{"100", 100.0, true},
{"-100e-2", -100e-2, true},
{".", 0, false},
}
func TestFloat(t *testing.T) {
fmt.Println("Test float parsers")
for i, t := range fTests {
var f float64
fmt.Printf("TEST [%d] ", i)
l, err := Parse(&f, []byte(t.input), nil)
if err != nil {
if t.ok {
fmt.Printf("ERROR: `%s` %s\n", t.input, err.Error())
} else {
fmt.Printf("OK (%d): %s\n", l, err.Error())
}
} else {
if !t.ok {
fmt.Printf("ERROR (%d): parsed %s == %f\n", l, t.input, f)
} else {
fmt.Printf("OK (%d): %s == %f\n", l, t.input, f)
}
}
}
}