git.lukeshu.com/go/lowmemjson@v0.3.9-0.20230723050957-72f6d13f6fb2/internal/jsonparse/parse_test.go (about) 1 // Copyright (C) 2023 Luke Shumaker <lukeshu@lukeshu.com> 2 // 3 // SPDX-License-Identifier: GPL-2.0-or-later 4 5 package jsonparse 6 7 import ( 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestParserHandleRune(t *testing.T) { 14 t.Parallel() 15 type testcase struct { 16 Input string 17 ExpStack []string 18 } 19 testcases := map[string]testcase{ 20 // Keep these test-cases in-sync with the examples in parse.go. 21 "object": { 22 Input: `{"x":"y","a":"b"}`, 23 ExpStack: []string{ 24 // st,// processed 25 `?`, 26 `{`, // { 27 `:"`, // {" 28 `:"`, // {"x 29 `:`, // {"x" 30 `o?`, // {"x": 31 `o"`, // {"x":" 32 `o"`, // {"x":"y 33 `o`, // {"x":"y" 34 `}`, // {"x":"y", 35 `:"`, // {"x":"y"," 36 `:"`, // {"x":"y","a 37 `:`, // {"x":"y","a" 38 `o?`, // {"x":"y","a": 39 `o"`, // {"x":"y","a":" 40 `o"`, // {"x":"y","a":"b 41 `o`, // {"x":"y","a":"b" 42 ``, // {"x":"y","a":"b"} 43 }, 44 }, 45 "array": { 46 Input: `["x","y"]`, 47 ExpStack: []string{ 48 // st,// processed 49 `?`, 50 `[`, // [ 51 `a"`, // [" 52 `a"`, // ["x 53 `a`, // ["x" 54 `a?`, // ["x", 55 `a"`, // ["x"," 56 `a"`, // ["x","y 57 `a`, // ["x","y" 58 ``, // ["x","y"] 59 }, 60 }, 61 } 62 for tcName, tc := range testcases { 63 tc := tc 64 t.Run(tcName, func(t *testing.T) { 65 t.Parallel() 66 var par Parser 67 if !assert.Equal(t, len(tc.Input)+1, len(tc.ExpStack)) { 68 return 69 } 70 for i, r := range tc.Input { 71 assert.Equal(t, tc.ExpStack[i], par.stackString()) 72 _, err := par.HandleRune(r, true) 73 assert.NoError(t, err) 74 assert.Equal(t, tc.ExpStack[i+1], par.stackString()) 75 } 76 }) 77 } 78 }