-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathutil_test.go
109 lines (103 loc) · 2.28 KB
/
util_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
package jsonschema
import (
"hash/maphash"
"testing"
)
func TestQuote(t *testing.T) {
tests := []struct{ input, want string }{
{`abc"def'ghi`, `'abc"def\'ghi'`},
}
for _, test := range tests {
got := quote(test.input)
if got != test.want {
t.Errorf("quote(%q): got %q, want %q", test.input, got, test.want)
}
}
}
func TestFragment(t *testing.T) {
tests := []struct {
input string
want any
}{
{"#", jsonPointer("")},
{"#/a/b", jsonPointer("/a/b")},
{"#abcd", anchor("abcd")},
{"#%61%62%63%64", anchor("abcd")},
{"#%2F%61%62%63%64%2fef", jsonPointer("/abcd/ef")}, // '/' is enchoded
{"#abcd+ef", anchor("abcd+ef")}, // '+' should not traslate to space
}
for _, test := range tests {
_, frag, err := splitFragment(test.input)
if err != nil {
t.Errorf("splitFragment(%q): %v", test.input, err)
continue
}
got := frag.convert()
if got != test.want {
t.Errorf("splitFragment(%q): got %q, want %q", test.input, got, test.want)
}
}
}
func TestUnescape(t *testing.T) {
tests := []struct {
input, want string
ok bool
}{
{"bar~0", "bar~", true},
{"bar~1", "bar/", true},
{"bar~01", "bar~1", true},
{"bar~", "", false},
{"bar~~", "", false},
}
for _, test := range tests {
got, ok := unescape(test.input)
if ok != test.ok {
t.Errorf("unescape(%q).ok: got %v, want %v", test.input, ok, test.ok)
continue
}
if got != test.want {
t.Errorf("unescape(%q): got %q, want %q", test.input, got, test.want)
}
}
}
func TestEquals(t *testing.T) {
tests := []struct {
v1, v2 any
want bool
}{
{1.0, 1, true},
{-1.0, -1, true},
}
for _, test := range tests {
got, k := equals(test.v1, test.v2)
if k != nil {
t.Error("got error:", k)
continue
}
if got != test.want {
t.Errorf("equals(%v, %v): got %v, want %v", test.v1, test.v2, got, test.want)
}
}
}
func TestHashEquals(t *testing.T) {
tests := []struct {
v1, v2 any
want bool
}{
{1.0, 1, true},
{-1.0, -1, true},
}
hash := new(maphash.Hash)
for _, test := range tests {
hash.Reset()
writeHash(test.v1, hash)
h1 := hash.Sum64()
hash.Reset()
writeHash(test.v2, hash)
h2 := hash.Sum64()
got := h1 == h2
if got != test.want {
t.Errorf("hashEquals(%v, %v): got %v, want %v", test.v1, test.v2, got, test.want)
}
}
}