-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobject_test.go
More file actions
63 lines (57 loc) · 1.39 KB
/
object_test.go
File metadata and controls
63 lines (57 loc) · 1.39 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
package libjson
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestObjectAtom(t *testing.T) {
input := []struct {
inp string
path string
expected any
}{
{"12", ".", 12},
{`"str"`, ".", "str"},
{"true", ".", true},
{"false", ".", false},
{"null", ".", nil},
{`{"key": "value"}`, ".key", "value"},
{`{ "hello": {"world": ["hi"] } }`, ".hello.world.0", "hi"},
}
for _, i := range input {
t.Run(i.inp+i.path, func(t *testing.T) {
obj, err := New([]byte(i.inp))
assert.NoError(t, err)
assert.NotNil(t, obj)
out, err := obj.get(i.path)
assert.NoError(t, err)
assert.EqualValues(t, i.expected, out)
})
}
}
// This tests the example in the readme, always copy from here to the readme
func TestObjectReadme(t *testing.T) {
input := `{ "hello": {"world": ["hi"] } }`
jsonObj, _ := New([]byte(input)) // or libjson.NewReader(r io.Reader)
// accessing values
// fmt.Println(Get[string](&jsonObj, ".hello.world.0")) // hi
val, err := Get[string](&jsonObj, ".hello.world.0")
assert.NoError(t, err)
assert.EqualValues(t, "hi", val)
}
func TestStandardFail(t *testing.T) {
input := []string{
`{"a":"b"}/**/`,
`{"a":"b"}/**//`,
`{"a":"b"}//`,
`{"a":"b"}/`,
`{"a":"b"}#`,
}
for _, i := range input {
t.Run(i, func(t *testing.T) {
in := []byte(i)
p := parser{l: lexer{data: in}}
_, err := p.parse(in)
assert.Error(t, err)
})
}
}