Repository navigation
Expand file tree
/
Copy pathparser_test.go
More file actions
297 lines (274 loc) · 8.13 KB
/
Copy pathparser_test.go
File metadata and controls
297 lines (274 loc) · 8.13 KB
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
package dbgp
import (
"os"
"strings"
"testing"
"unicode/utf8"
)
func TestFormatSimpleValue(t *testing.T) {
tests := []struct {
typ string
content string
classname string
want string
}{
{"string", "hello", "", `"hello"`},
{"string", "", "", `""`},
{"int", "42", "", "42"},
{"bool", "1", "", "true"},
{"bool", "0", "", "false"},
{"null", "", "", "null"},
{"array", "", "App\\Foo", "array[]"},
{"array", "", "", "array[]"},
{"object", "", "App\\Service", "App\\Service{}"},
}
for _, tt := range tests {
got := formatSimpleValue(tt.typ, tt.content, tt.classname)
if got != tt.want {
t.Errorf("formatSimpleValue(%q, %q, %q) = %q, want %q", tt.typ, tt.content, tt.classname, got, tt.want)
}
}
}
func TestFormatPropertyValue(t *testing.T) {
tests := []struct {
name string
prop Property
want string
}{
{
name: "base64 string",
prop: Property{Type: "string", Encoding: "base64", Value: "SGVsbG8sIFdvcmxkIQ=="},
want: `"Hello, World!"`,
},
{
name: "int",
prop: Property{Type: "int", Value: "42"},
want: "42",
},
{
name: "bool true",
prop: Property{Type: "bool", Value: "1"},
want: "true",
},
{
name: "array with children",
prop: Property{Type: "array", Children: 5},
want: "array[5]",
},
{
name: "object with classname",
prop: Property{Type: "object", ClassName: "App\\Service", Children: 2},
want: "App\\Service",
},
{
name: "string starting with angle bracket",
prop: Property{Type: "string", Value: "<html>"},
want: `"<html>"`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := formatPropertyValue(tt.prop)
if got != tt.want {
t.Errorf("formatPropertyValue() = %q, want %q", got, tt.want)
}
})
}
}
func TestParseVariablesFromRealXML(t *testing.T) {
data, err := os.ReadFile("testdata/context_get_complex.xml")
if err != nil {
t.Fatalf("read test file: %v", err)
}
vars := ParseVariables(string(data))
findVar := func(name string) *Variable {
for i := range vars {
if vars[i].Name == name {
return &vars[i]
}
}
return nil
}
tests := []struct {
name string
wantType string
wantValue string
wantLevel int
}{
{"$count", "string", `"Hello, World!"`, 0},
{"$name", "string", `"World"`, 0},
{"$sum", "int", "15", 0},
{"$numbers", "array", "array[5]", 0},
{"$numbers[0]", "int", "1", 1},
{"$user", "array", "array[3]", 0},
{"$obj", "object", `App\Service\MyService`, 0},
{"$obj->id", "int", "42", 1},
{"$emptyArray", "array", "array[]", 0},
{"$nullVal", "null", "null", 0},
{"$boolVal", "bool", "true", 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := findVar(tt.name)
if v == nil {
t.Fatalf("variable %s not found", tt.name)
}
if v.Type != tt.wantType {
t.Errorf("type = %q, want %q", v.Type, tt.wantType)
}
if v.Value != tt.wantValue {
t.Errorf("value = %q, want %q", v.Value, tt.wantValue)
}
if v.Level != tt.wantLevel {
t.Errorf("level = %d, want %d", v.Level, tt.wantLevel)
}
})
}
}
func TestParseAllVariablesFromRealXML(t *testing.T) {
data, err := os.ReadFile("testdata/context_get_complex.xml")
if err != nil {
t.Fatalf("read test file: %v", err)
}
vars := ParseAllVariables(string(data))
findVar := func(name string) *Variable {
for i := range vars {
if vars[i].Name == name {
return &vars[i]
}
}
return nil
}
tests := []struct {
name string
wantType string
wantValue string
wantLevel int
}{
{"$obj", "object", `App\Service\MyService`, 0},
{"$obj->cache", "object", `App\Cache`, 1},
{"$obj->cache->items", "array", "array[3]", 2},
{"$obj->cache->items[0]", "string", `"item1"`, 3},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := findVar(tt.name)
if v == nil {
t.Fatalf("variable %s not found", tt.name)
}
if v.Type != tt.wantType {
t.Errorf("type = %q, want %q", v.Type, tt.wantType)
}
if v.Value != tt.wantValue {
t.Errorf("value = %q, want %q", v.Value, tt.wantValue)
}
if v.Level != tt.wantLevel {
t.Errorf("level = %d, want %d", v.Level, tt.wantLevel)
}
})
}
}
// Variables from a recorded context_get (max_children=10).
func TestParseVariablesFromXdebugFixture(t *testing.T) {
vars := ParseVariables(fixture(t, "context_get_main"))
got := map[string]Variable{}
for _, v := range vars {
got[v.Name] = v
}
want := []Variable{
{Name: "$float", Type: "float", Value: "1.5"},
{Name: "$greeting", Type: "string", Value: `"Héllo, wörld"`},
{Name: "$i", Type: "uninitialized", Value: "null"},
{Name: "$long", Type: "string", Value: `"` + strings.Repeat("abcdefghij", 6)[:55] + `..."`},
{Name: "$nullVal", Type: "null", Value: "null"},
{Name: "$numbers", Type: "array", Value: "array[40]"},
{Name: "$numbers[9]", Type: "int", Value: "10", Level: 1},
{Name: "$obj", Type: "object", Value: "Service"},
{Name: "$obj->id", Type: "int", Value: "42", Level: 1},
{Name: "$obj->cache", Type: "object", Value: "Cache", Level: 1},
{Name: "$obj->name", Type: "string", Value: `"svc"`, Level: 1},
{Name: `$user["active"]`, Type: "bool", Value: "true", Level: 1},
}
for _, w := range want {
if g, ok := got[w.Name]; !ok || g != w {
t.Errorf("%s = %+v, want %+v", w.Name, g, w)
}
}
// Only the first page of children (pagesize 10) is present.
if _, ok := got["$numbers[10]"]; ok {
t.Error("$numbers[10] present beyond the first page")
}
}
func TestParseVariablesFromPropertyGet(t *testing.T) {
vars := ParseVariables(fixture(t, "property_get_page"))
if len(vars) != 11 || vars[0].Value != "array[40]" || vars[1].Name != "$numbers[10]" || vars[1].Value != "11" {
t.Errorf("vars = %+v", vars)
}
}
func TestParseVariablesMalformed(t *testing.T) {
for _, xml := range []string{"", "<response", "<init/>", "not xml"} {
if vars := ParseVariables(xml); vars != nil {
t.Errorf("ParseVariables(%q) = %+v, want nil", xml, vars)
}
if vars := ParseAllVariables(xml); vars != nil {
t.Errorf("ParseAllVariables(%q) = %+v, want nil", xml, vars)
}
}
}
func TestFormatSimpleValueEdgeCases(t *testing.T) {
tests := []struct{ typ, content, want string }{
{"bool", "true", "true"},
{"bool", "", "false"},
{"uninitialized", "", "null"},
{"float", "1.5", "1.5"},
{"object", "", "object{}"},
{"resource", "", "<resource>"},
{"resource", "resource id='5' type='stream'", "resource id='5' type='stream'"},
{"resource", strings.Repeat("r", 70), strings.Repeat("r", 57) + "..."},
{"string", strings.Repeat("s", 60), `"` + strings.Repeat("s", 60) + `"`},
{"string", strings.Repeat("s", 61), `"` + strings.Repeat("s", 55) + `..."`},
}
for _, tt := range tests {
if got := formatSimpleValue(tt.typ, tt.content, ""); got != tt.want {
t.Errorf("formatSimpleValue(%q, %q) = %q, want %q", tt.typ, tt.content, got, tt.want)
}
}
}
func TestFormatSimpleValueTruncatesOnRuneBoundary(t *testing.T) {
got := formatSimpleValue("string", strings.Repeat("é", 40), "")
if !utf8.ValidString(got) {
t.Errorf("truncated value is not valid UTF-8: %q", got)
}
}
func TestFormatPropertyValueInvalidBase64(t *testing.T) {
got := formatPropertyValue(Property{Type: "string", Encoding: "base64", Value: "not base64!"})
if got != `"not base64!"` {
t.Errorf("got %q", got)
}
}
func TestFormatVariable(t *testing.T) {
tests := []struct {
v Variable
want string
}{
{Variable{Name: "$a", Type: "int", Value: "1"}, "$a" + strings.Repeat(" ", 29) + "int = 1"},
{Variable{Name: "$a[0]", Type: "int", Value: "1", Level: 1}, " $a[0]" + strings.Repeat(" ", 24) + "int = 1"},
{Variable{Name: "$x", Type: "int", Value: "1", Level: 20}, strings.Repeat(" ", 20) + "$x int = 1"},
}
for _, tt := range tests {
if got := FormatVariable(tt.v); got != tt.want {
t.Errorf("FormatVariable(%+v) = %q, want %q", tt.v, got, tt.want)
}
}
}
// Eval results have no fullname; their children are shown by name.
func TestParseVariablesFallsBackToName(t *testing.T) {
vars := ParseVariables(fixture(t, "eval_array"))
var names []string
for _, v := range vars {
names = append(names, v.Name)
}
if got := strings.Join(names, ","); got != ",0,1,k" {
t.Errorf("names = %q, want %q", got, ",0,1,k")
}
}