-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathjsonpath_edgecase_test.go
More file actions
95 lines (81 loc) · 1.94 KB
/
jsonpath_edgecase_test.go
File metadata and controls
95 lines (81 loc) · 1.94 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
// Copyright 2015, 2021; oliver, DoltHub Authors
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
package jsonpath
import (
"encoding/json"
"regexp"
"testing"
)
// Edge case tests - null handling, number comparisons, regex operations
func Test_jsonpath_null_in_the_middle(t *testing.T) {
data := `{
"head_commit": null,
}
`
var j interface{}
json.Unmarshal([]byte(data), &j)
res, err := JsonPathLookup(j, "$.head_commit.author.username")
t.Log(res, err)
}
func Test_jsonpath_num_cmp(t *testing.T) {
data := `{
"books": [
{ "name": "My First Book", "price": 10 },
{ "name": "My Second Book", "price": 20 }
]
}`
var j interface{}
json.Unmarshal([]byte(data), &j)
res, err := JsonPathLookup(j, "$.books[?(@.price > 100)].name")
if err != nil {
t.Fatal(err)
}
arr := res.([]interface{})
if len(arr) != 0 {
t.Fatal("should return [], got: ", arr)
}
}
func TestReg(t *testing.T) {
r := regexp.MustCompile(`(?U).*REES`)
t.Log(r)
t.Log(r.Match([]byte(`Nigel Rees`)))
res, err := JsonPathLookup(json_data, "$.store.book[?(@.author =~ /(?i).*REES/ )].author")
t.Log(err, res)
author := res.([]interface{})[0].(string)
t.Log(author)
if author != "Nigel Rees" {
t.Fatal("should be `Nigel Rees` but got: ", author)
}
}
var tcases_reg_op = []struct {
Line string
Exp string
Err bool
}{
{``, ``, true},
{`xxx`, ``, true},
{`/xxx`, ``, true},
{`xxx/`, ``, true},
{`'/xxx/'`, ``, true},
{`"/xxx/"`, ``, true},
{`/xxx/`, `xxx`, false},
{`/π/`, `π`, false},
}
func TestRegOp(t *testing.T) {
for idx, tcase := range tcases_reg_op {
t.Logf("idx: %v, tcase: %v", idx, tcase)
res, err := regFilterCompile(tcase.Line)
if tcase.Err == true {
if err == nil {
t.Fatal("expect err but got nil")
}
} else {
if res == nil || res.String() != tcase.Exp {
t.Fatal("different. res:", res)
}
}
}
}