-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalized_path_test.go
More file actions
76 lines (68 loc) · 2.34 KB
/
normalized_path_test.go
File metadata and controls
76 lines (68 loc) · 2.34 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
package jsonpath
import "testing"
func TestNormalizedPathGeneration(t *testing.T) {
tests := []struct {
name string
segments []interface{}
expected string
}{
{"Root", []interface{}{}, "$"},
{"Simple member", []interface{}{"store"}, "$['store']"},
{"Nested members", []interface{}{"store", "book"}, "$['store']['book']"},
{"Array index", []interface{}{"store", "book", 0}, "$['store']['book'][0]"},
{"Member with single quote", []interface{}{"it's"}, "$['it\\'s']"},
{"Member with backslash", []interface{}{"back\\slash"}, "$['back\\\\slash']"},
{"Empty member name", []interface{}{""}, "$['']"},
{"Member with control char", []interface{}{"tab\there"}, "$['tab\\u0009here']"},
{"Numeric index", []interface{}{42}, "$[42]"},
{"Negative index", []interface{}{-1}, "$[-1]"},
{"Mixed segments", []interface{}{"a", 0, "b", 1}, "$['a'][0]['b'][1]"},
{"Member with special chars", []interface{}{"key with spaces"}, "$['key with spaces']"},
{"Member with unicode", []interface{}{"名前"}, "$['名前']"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := GenerateNormalizedPath(tt.segments)
if result != tt.expected {
t.Errorf("GenerateNormalizedPath(%v) = %q, want %q", tt.segments, result, tt.expected)
}
})
}
}
func TestNormalizedPathGeneratorMethods(t *testing.T) {
npg := NewNormalizedPathGenerator()
if npg.String() != "$" {
t.Errorf("NewNormalizedPathGenerator().String() = %q, want %q", npg.String(), "$")
}
npg.AddMember("test")
if npg.String() != "$['test']" {
t.Errorf("After AddMember(\"test\") = %q, want %q", npg.String(), "$['test']")
}
npg.AddIndex(0)
if npg.String() != "$['test'][0]" {
t.Errorf("After AddIndex(0) = %q, want %q", npg.String(), "$['test'][0]")
}
}
func TestEscapeMemberName(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"No escaping", "simple", "simple"},
{"Single quote", "it's", "it\\'s"},
{"Backslash", "back\\slash", "back\\\\slash"},
{"Tab", "\t", "\\u0009"},
{"Newline", "\n", "\\u000a"},
{"Carriage return", "\r", "\\u000d"},
{"Mixed", "a'b\\c", "a\\'b\\\\c"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := escapeMemberName(tt.input)
if result != tt.expected {
t.Errorf("escapeMemberName(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}