summaryrefslogtreecommitdiff
path: root/internal/parse_test.go
blob: 91cd277fce4b13396915a9f2f5c4665f30b76b59 (plain)
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
// Copyright (C) 2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package internal

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestParserHandleRune(t *testing.T) {
	t.Parallel()
	type testcase struct {
		Input    string
		ExpStack []string
	}
	testcases := map[string]testcase{
		// Keep these test-cases in-sync with the examples in parse.go.
		"object": {
			Input: `{"x":"y","a":"b"}`,
			ExpStack: []string{
				// st,// processed
				`?`,
				`{`,  // {
				`»"`, // {"
				`»"`, // {"x
				`»`,  // {"x"
				`o?`, // {"x":
				`o"`, // {"x":"
				`o"`, // {"x":"y
				`o`,  // {"x":"y"
				`{`,  // {"x":"y",
				`»"`, // {"x":"y","
				`»"`, // {"x":"y","a
				`»`,  // {"x":"y","a"
				`o?`, // {"x":"y","a":
				`o"`, // {"x":"y","a":"
				`o"`, // {"x":"y","a":"b
				`o`,  // {"x":"y","a":"b"
				``,   // {"x":"y","a":"b"}
			},
		},
		"array": {
			Input: `["x","y"]`,
			ExpStack: []string{
				// st,// processed
				`?`,
				`[`,  // [
				`a"`, // ["
				`a"`, // ["x
				`a`,  // ["x"
				`]`,  // ["x",
				`a"`, // ["x","
				`a"`, // ["x","y
				`a`,  // ["x","y"
				``,   // ["x","y"]
			},
		},
	}
	for tcName, tc := range testcases {
		tc := tc
		t.Run(tcName, func(t *testing.T) {
			t.Parallel()
			var par Parser
			if !assert.Equal(t, len(tc.Input)+1, len(tc.ExpStack)) {
				return
			}
			for i, r := range tc.Input {
				assert.Equal(t, tc.ExpStack[i], par.stackString())
				_, err := par.HandleRune(r)
				assert.NoError(t, err)
				assert.Equal(t, tc.ExpStack[i+1], par.stackString())
			}
		})
	}
}