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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
package jlexer
import (
"bytes"
"encoding/json"
"reflect"
"testing"
)
func TestString(t *testing.T) {
for i, test := range []struct {
toParse string
want string
wantError bool
}{
{toParse: `"simple string"`, want: "simple string"},
{toParse: " \r\r\n\t " + `"test"`, want: "test"},
{toParse: `"\n\t\"\/\\\f\r"`, want: "\n\t\"/\\\f\r"},
{toParse: `"\u0020"`, want: " "},
{toParse: `"\u0020-\t"`, want: " -\t"},
{toParse: `"\ufffd\uFFFD"`, want: "\ufffd\ufffd"},
{toParse: `"\ud83d\ude00"`, want: "😀"},
{toParse: `"\ud83d\ude08"`, want: "😈"},
{toParse: `"\ud8"`, wantError: true},
{toParse: `"test"junk`, want: "test"},
{toParse: `5`, wantError: true}, // not a string
{toParse: `"\x"`, wantError: true}, // invalid escape
{toParse: `"\ud800"`, want: "�"}, // invalid utf-8 char; return replacement char
} {
{
l := Lexer{Data: []byte(test.toParse)}
got := l.String()
if got != test.want {
t.Errorf("[%d, %q] String() = %v; want %v", i, test.toParse, got, test.want)
}
err := l.Error()
if err != nil && !test.wantError {
t.Errorf("[%d, %q] String() error: %v", i, test.toParse, err)
} else if err == nil && test.wantError {
t.Errorf("[%d, %q] String() ok; want error", i, test.toParse)
}
}
{
l := Lexer{Data: []byte(test.toParse)}
got := l.StringIntern()
if got != test.want {
t.Errorf("[%d, %q] String() = %v; want %v", i, test.toParse, got, test.want)
}
err := l.Error()
if err != nil && !test.wantError {
t.Errorf("[%d, %q] String() error: %v", i, test.toParse, err)
} else if err == nil && test.wantError {
t.Errorf("[%d, %q] String() ok; want error", i, test.toParse)
}
}
}
}
func TestStringIntern(t *testing.T) {
data := []byte(`"string interning test"`)
var l Lexer
allocsPerRun := testing.AllocsPerRun(1000, func() {
l = Lexer{Data: data}
_ = l.StringIntern()
})
if allocsPerRun != 0 {
t.Fatalf("expected 0 allocs, got %f", allocsPerRun)
}
allocsPerRun = testing.AllocsPerRun(1000, func() {
l = Lexer{Data: data}
_ = l.String()
})
if allocsPerRun != 1 {
t.Fatalf("expected 1 allocs, got %f", allocsPerRun)
}
}
func TestBytes(t *testing.T) {
for i, test := range []struct {
toParse string
want string
wantError bool
}{
{toParse: `"c2ltcGxlIHN0cmluZw=="`, want: "simple string"},
{toParse: " \r\r\n\t " + `"dGVzdA=="`, want: "test"},
{toParse: `"c3ViamVjdHM\/X2Q9MQ=="`, want: "subjects?_d=1"}, // base64 with forward slash escaped
{toParse: `5`, wantError: true}, // not a JSON string
{toParse: `"foobar"`, wantError: true}, // not base64 encoded
{toParse: `"c2ltcGxlIHN0cmluZw="`, wantError: true}, // invalid base64 padding
} {
l := Lexer{Data: []byte(test.toParse)}
got := l.Bytes()
if bytes.Compare(got, []byte(test.want)) != 0 {
t.Errorf("[%d, %q] Bytes() = %v; want: %v", i, test.toParse, got, []byte(test.want))
}
err := l.Error()
if err != nil && !test.wantError {
t.Errorf("[%d, %q] Bytes() error: %v", i, test.toParse, err)
} else if err == nil && test.wantError {
t.Errorf("[%d, %q] Bytes() ok; want error", i, test.toParse)
}
}
}
func TestNumber(t *testing.T) {
for i, test := range []struct {
toParse string
want string
wantError bool
}{
{toParse: "123", want: "123"},
{toParse: "-123", want: "-123"},
{toParse: "\r\n12.35", want: "12.35"},
{toParse: "12.35e+1", want: "12.35e+1"},
{toParse: "12.35e-15", want: "12.35e-15"},
{toParse: "12.35E-15", want: "12.35E-15"},
{toParse: "12.35E15", want: "12.35E15"},
{toParse: `"a"`, wantError: true},
{toParse: "123junk", wantError: true},
{toParse: "1.2.3", wantError: true},
{toParse: "1e2e3", wantError: true},
{toParse: "1e2.3", wantError: true},
} {
l := Lexer{Data: []byte(test.toParse)}
got := l.number()
if got != test.want {
t.Errorf("[%d, %q] number() = %v; want %v", i, test.toParse, got, test.want)
}
err := l.Error()
if err != nil && !test.wantError {
t.Errorf("[%d, %q] number() error: %v", i, test.toParse, err)
} else if err == nil && test.wantError {
t.Errorf("[%d, %q] number() ok; want error", i, test.toParse)
}
}
}
func TestBool(t *testing.T) {
for i, test := range []struct {
toParse string
want bool
wantError bool
}{
{toParse: "true", want: true},
{toParse: "false", want: false},
{toParse: "1", wantError: true},
{toParse: "truejunk", wantError: true},
{toParse: `false"junk"`, wantError: true},
{toParse: "True", wantError: true},
{toParse: "False", wantError: true},
} {
l := Lexer{Data: []byte(test.toParse)}
got := l.Bool()
if got != test.want {
t.Errorf("[%d, %q] Bool() = %v; want %v", i, test.toParse, got, test.want)
}
err := l.Error()
if err != nil && !test.wantError {
t.Errorf("[%d, %q] Bool() error: %v", i, test.toParse, err)
} else if err == nil && test.wantError {
t.Errorf("[%d, %q] Bool() ok; want error", i, test.toParse)
}
}
}
func TestSkipRecursive(t *testing.T) {
for i, test := range []struct {
toParse string
left string
wantError bool
}{
{toParse: "5, 4", left: ", 4"},
{toParse: "[5, 6], 4", left: ", 4"},
{toParse: "[5, [7,8]]: 4", left: ": 4"},
{toParse: `{"a":1}, 4`, left: ", 4"},
{toParse: `{"a":1, "b":{"c": 5}, "e":[12,15]}, 4`, left: ", 4"},
// array start/end chars in a string
{toParse: `[5, "]"], 4`, left: ", 4"},
{toParse: `[5, "\"]"], 4`, left: ", 4"},
{toParse: `[5, "["], 4`, left: ", 4"},
{toParse: `[5, "\"["], 4`, left: ", 4"},
// object start/end chars in a string
{toParse: `{"a}":1}, 4`, left: ", 4"},
{toParse: `{"a\"}":1}, 4`, left: ", 4"},
{toParse: `{"a{":1}, 4`, left: ", 4"},
{toParse: `{"a\"{":1}, 4`, left: ", 4"},
// object with double slashes at the end of string
{toParse: `{"a":"hey\\"}, 4`, left: ", 4"},
// make sure skipping an invalid json results in an error
{toParse: `{"a": [ ##invalid json## ]}, 4`, wantError: true},
{toParse: `{"a": [ [1], [ ##invalid json## ]]}, 4`, wantError: true},
} {
l := Lexer{Data: []byte(test.toParse)}
l.SkipRecursive()
got := string(l.Data[l.pos:])
if got != test.left {
t.Errorf("[%d, %q] SkipRecursive() left = %v; want %v", i, test.toParse, got, test.left)
}
err := l.Error()
if err != nil && !test.wantError {
t.Errorf("[%d, %q] SkipRecursive() error: %v", i, test.toParse, err)
} else if err == nil && test.wantError {
t.Errorf("[%d, %q] SkipRecursive() ok; want error", i, test.toParse)
}
}
}
func TestInterface(t *testing.T) {
for i, test := range []struct {
toParse string
want interface{}
wantError bool
}{
{toParse: "null", want: nil},
{toParse: "true", want: true},
{toParse: `"a"`, want: "a"},
{toParse: "5", want: float64(5)},
{toParse: `{}`, want: map[string]interface{}{}},
{toParse: `[]`, want: []interface{}{}},
{toParse: `{"a": "b"}`, want: map[string]interface{}{"a": "b"}},
{toParse: `[5]`, want: []interface{}{float64(5)}},
{toParse: `{"a":5 , "b" : "string"}`, want: map[string]interface{}{"a": float64(5), "b": "string"}},
{toParse: `["a", 5 , null, true]`, want: []interface{}{"a", float64(5), nil, true}},
{toParse: `{"a" "b"}`, wantError: true},
{toParse: `{"a": "b",}`, wantError: true},
{toParse: `{"a":"b","c" "b"}`, wantError: true},
{toParse: `{"a": "b","c":"d",}`, wantError: true},
{toParse: `{,}`, wantError: true},
{toParse: `[1, 2,]`, wantError: true},
{toParse: `[1 2]`, wantError: true},
{toParse: `[,]`, wantError: true},
} {
l := Lexer{Data: []byte(test.toParse)}
got := l.Interface()
if !reflect.DeepEqual(got, test.want) {
t.Errorf("[%d, %q] Interface() = %v; want %v", i, test.toParse, got, test.want)
}
err := l.Error()
if err != nil && !test.wantError {
t.Errorf("[%d, %q] Interface() error: %v", i, test.toParse, err)
} else if err == nil && test.wantError {
t.Errorf("[%d, %q] Interface() ok; want error", i, test.toParse)
}
}
}
func TestConsumed(t *testing.T) {
for i, test := range []struct {
toParse string
wantError bool
}{
{toParse: "", wantError: false},
{toParse: " ", wantError: false},
{toParse: "\r\n", wantError: false},
{toParse: "\t\t", wantError: false},
{toParse: "{", wantError: true},
} {
l := Lexer{Data: []byte(test.toParse)}
l.Consumed()
err := l.Error()
if err != nil && !test.wantError {
t.Errorf("[%d, %q] Consumed() error: %v", i, test.toParse, err)
} else if err == nil && test.wantError {
t.Errorf("[%d, %q] Consumed() ok; want error", i, test.toParse)
}
}
}
func TestJsonNumber(t *testing.T) {
for i, test := range []struct {
toParse string
want json.Number
wantLexerError bool
wantValue interface{}
wantValueError bool
}{
{toParse: `10`, want: json.Number("10"), wantValue: int64(10)},
{toParse: `0`, want: json.Number("0"), wantValue: int64(0)},
{toParse: `0.12`, want: json.Number("0.12"), wantValue: 0.12},
{toParse: `25E-4`, want: json.Number("25E-4"), wantValue: 25e-4},
{toParse: `"10"`, want: json.Number("10"), wantValue: int64(10)},
{toParse: `"0"`, want: json.Number("0"), wantValue: int64(0)},
{toParse: `"0.12"`, want: json.Number("0.12"), wantValue: 0.12},
{toParse: `"25E-4"`, want: json.Number("25E-4"), wantValue: 25e-4},
{toParse: `"foo"`, want: json.Number("foo"), wantValueError: true},
{toParse: `null`, want: json.Number(""), wantValueError: true},
{toParse: `"a""`, want: json.Number("a"), wantValueError: true},
{toParse: `[1]`, want: json.Number(""), wantLexerError: true, wantValueError: true},
{toParse: `{}`, want: json.Number(""), wantLexerError: true, wantValueError: true},
{toParse: `a`, want: json.Number(""), wantLexerError: true, wantValueError: true},
} {
l := Lexer{Data: []byte(test.toParse)}
got := l.JsonNumber()
if got != test.want {
t.Errorf("[%d, %q] JsonNumber() = %v; want %v", i, test.toParse, got, test.want)
}
err := l.Error()
if err != nil && !test.wantLexerError {
t.Errorf("[%d, %q] JsonNumber() lexer error: %v", i, test.toParse, err)
} else if err == nil && test.wantLexerError {
t.Errorf("[%d, %q] JsonNumber() ok; want lexer error", i, test.toParse)
}
var valueErr error
var gotValue interface{}
switch test.wantValue.(type) {
case float64:
gotValue, valueErr = got.Float64()
default:
gotValue, valueErr = got.Int64()
}
if !reflect.DeepEqual(gotValue, test.wantValue) && !test.wantLexerError && !test.wantValueError {
t.Errorf("[%d, %q] JsonNumber() = %v; want %v", i, test.toParse, gotValue, test.wantValue)
}
if valueErr != nil && !test.wantValueError {
t.Errorf("[%d, %q] JsonNumber() value error: %v", i, test.toParse, valueErr)
} else if valueErr == nil && test.wantValueError {
t.Errorf("[%d, %q] JsonNumber() ok; want value error", i, test.toParse)
}
}
}
func TestFetchStringUnterminatedString(t *testing.T) {
for _, test := range []struct {
data []byte
}{
{data: []byte(`"sting without trailing quote`)},
{data: []byte(`"\"`)},
{data: []byte{'"'}},
} {
l := Lexer{Data: test.data}
l.fetchString()
if l.pos > len(l.Data) {
t.Errorf("fetchString(%s): pos=%v should not be greater than length of Data = %v", test.data, l.pos, len(l.Data))
}
if l.Error() == nil {
t.Errorf("fetchString(%s): should add parsing error", test.data)
}
}
}
|