-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
167 lines (164 loc) · 4.31 KB
/
parser_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
package main
import (
"reflect"
"testing"
)
func TestParser(t *testing.T) {
tests := []struct {
name string
input string
expectedValue interface{}
expectError bool
}{
{
name: "Empty Input",
input: "",
expectedValue: nil,
expectError: true,
},
{
name: "String",
input: `"hello"`,
expectedValue: "hello",
expectError: false,
},
{
name: "Number",
input: `42`,
expectedValue: float64(42),
expectError: false,
},
{
name: "Invalid Number",
input: `42..5`,
expectedValue: float64(0),
expectError: true,
},
{
name: "Boolean",
input: `true`,
expectedValue: true,
expectError: false,
},
{
name: "Empty Object",
input: "{}",
expectedValue: make(map[string]interface{}),
expectError: false,
},
{
name: "Invalid Object (Unbalanced Brace)",
input: "{",
expectedValue: make(map[string]interface{}),
expectError: true,
},
{
name: "Invalid Object (Unbalanced Extra Brace)",
input: "{}}",
expectedValue: make(map[string]interface{}),
expectError: true,
},
{
name: "Simple Object with String Value",
input: `{"key":"value"}`,
expectedValue: map[string]interface{}{"key": "value"},
expectError: false,
},
{
name: "Invalid Simple Object with String Value",
input: `{"key':"value"}`,
expectedValue: make(map[string]interface{}),
expectError: true,
},
{
name: "Simple Object with Numeric, Boolean and Null Values",
input: `{ "keyA": "value", "keyB": 42.5, "keyC": true, "keyD": null }`,
expectedValue: map[string]interface{}{"keyA": "value", "keyB": float64(42.5), "keyC": true, "keyD": nil},
expectError: false,
},
{
name: "Simple Object Containing Nested Objects",
input: `{"key": {"key2": "value"}, "key3": []}`,
expectedValue: map[string]interface{}{"key": map[string]interface{}{"key2": "value"}, "key3": []interface{}{}},
expectError: false,
},
{
name: "Empty Array",
input: "[]",
expectedValue: make([]interface{}, 0),
expectError: false,
},
{
name: "Invalid Array (Unbalanced Brace)",
input: "[",
expectedValue: make([]interface{}, 0),
expectError: true,
},
{
name: "Invalid Array (Unbalanced Extra Brace)",
input: "[]]",
expectedValue: make([]interface{}, 0),
expectError: true,
},
{
name: "Simple Array with String Values",
input: `[ "a", "b", "c" ]`,
expectedValue: []interface{}{"a", "b", "c"},
expectError: false,
},
{
name: "Simple Array with Numeric, Boolean, and Null Values",
input: `[ true, false, null, -42.5, "a" ]`,
expectedValue: []interface{}{true, false, nil, float64(-42.5), "a"},
expectError: false,
},
{
name: "Array with Object Values",
input: `[ { "key": "a" }, { "key": "b"}, {"key": "c"} ]`,
expectedValue: []interface{}{
map[string]interface{}{"key": "a"},
map[string]interface{}{"key": "b"},
map[string]interface{}{"key": "c"},
},
expectError: false,
},
{
name: "Array with Nested Arrays",
input: `[ ["a"], ["b"], ["c"] ]`,
expectedValue: []interface{}{
[]interface{}{"a"},
[]interface{}{"b"},
[]interface{}{"c"},
},
expectError: false,
},
{
name: "Complex Object",
input: `{"key": {"key2": "value", "key3": { "key4": [-84.25] } }, "key5": [{"key6": false, "key7": null }] }`,
expectedValue: map[string]interface{}{
"key": map[string]interface{}{
"key2": "value",
"key3": map[string]interface{}{
"key4": []interface{}{float64(-84.25)},
},
},
"key5": []interface{}{
map[string]interface{}{"key6": false, "key7": nil},
},
},
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := NewParser(tt.input)
actualValue, actualError := p.Parse()
if actualError != nil && !tt.expectError {
t.Fatalf("Expected no error but received: %v", actualError)
}
if !reflect.DeepEqual(actualValue, tt.expectedValue) {
t.Fatalf("Expected %T but received %T", tt.expectedValue, actualValue)
}
})
}
}