-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonpointer_test.go
140 lines (128 loc) · 3.94 KB
/
jsonpointer_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
// Copyright 2022 Chance Dinkins
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// The License can be found in the LICENSE file.
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package jsonpointer_test
import (
"fmt"
"testing"
"github.com/chanced/jsonpointer"
"github.com/stretchr/testify/require"
)
// func TestError(t *testing.T) {
// assert := require.New(t)
// var err error = &jsonpointer.FormatError{}
// err2 := &jsonpointer.FormatError{}
// assert.ErrorIs(err, err2)
// }
type strval string
func (str strval) String() string { return string(str) }
func TestNew(t *testing.T) {
assert := require.New(t)
tests := []struct {
pointer jsonpointer.Pointer
expectedstring string
}{
{jsonpointer.New(""), "/"},
{jsonpointer.New("foo", "bar", "baz"), "/foo/bar/baz"},
{jsonpointer.New("/foo/bar/baz"), "/~1foo~1bar~1baz"},
{jsonpointer.New("/"), "/~1"},
{jsonpointer.New(), ""},
{jsonpointer.New("~"), "/~0"},
}
for _, e := range tests {
assert.Equal(e.expectedstring, e.pointer.String())
}
}
func TestFrom(t *testing.T) {
assert := require.New(t)
p, err := jsonpointer.From("#/foo")
assert.NoError(err)
assert.Equal("/foo", p.String())
_, err = jsonpointer.From("#invalid")
assert.Error(err)
}
func TestJSONPointerValidate(t *testing.T) {
assert := require.New(t)
tests := []struct {
ptr jsonpointer.Pointer
err error
}{
{"", nil},
{"/", nil},
{"/foo~", jsonpointer.ErrMalformedEncoding},
{"/foo~1", nil},
{"/foo~1bar", nil},
{"foo/bar", jsonpointer.ErrMalformedStart},
{"/foo/bar", nil},
{"/~/", jsonpointer.ErrMalformedEncoding},
}
for i, test := range tests {
fmt.Printf("=== RUN TestJSONPointerValidate #%d, pointer %s\n", i+1, test.ptr)
err := test.ptr.Validate()
if test.err != nil {
assert.Error(err)
assert.ErrorIs(err, test.err)
} else {
assert.NoError(err)
}
fmt.Println("--- PASS")
}
}
func TestNewFromStrings(t *testing.T) {
assert := require.New(t)
p := jsonpointer.NewFromStrings([]string{"foo", "bar", "baz"})
assert.Equal("/foo/bar/baz", p.String())
}
func TestJSONPointerNext(t *testing.T) {
assert := require.New(t)
tests := []struct {
pointer jsonpointer.Pointer
expectedpointer jsonpointer.Pointer
expectedtoken jsonpointer.Token
expectedok bool
}{
{"", "", "", false},
{"/", "", "", true},
{"/foo", "", "foo", true},
{"/foo/bar", "/bar", "foo", true},
{"/~1foo~1bar~1baz", "", "~1foo~1bar~1baz", true},
{"malformed", "", "", false},
{"/foo/bar/baz", "/bar/baz", "foo", true},
}
for i, e := range tests {
np, nt, ok := e.pointer.Next()
assert.Equal(e.expectedpointer, np, "test[%v]: expected pointer to equal %v, got %v", i, e.expectedpointer, np)
assert.Equal(e.expectedtoken, nt, "test[%v]: expected token to equal %v, got %v", i, e.expectedtoken, nt)
assert.Equal(e.expectedok, ok, "test[%v]: expected ok to equal %v, got %v", i, e.expectedok, ok)
}
}
func TestJSONPointerPop(t *testing.T) {
assert := require.New(t)
pt := []struct {
pointer jsonpointer.Pointer
expectedpointer jsonpointer.Pointer
expectedtoken jsonpointer.Token
expectedok bool
}{
{"/foo/bar/baz", "/foo/bar", "baz", true},
{"/foo/bar", "/foo", "bar", true},
{"/", "", "", true},
{"", "", "", false},
{"malformed", "", "", false},
}
for _, e := range pt {
np, tok, ok := e.pointer.Pop()
assert.Equal(e.expectedtoken, tok, "expected token to equal %v, got %v", e.expectedtoken, tok)
assert.Equal(e.expectedpointer, np, "expected pointer to equal %v, got %v", e.expectedpointer, np)
assert.Equal(e.expectedok, ok, "expected ok to equal %v, got %v", e.expectedok, ok)
}
}