-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonpath_test.go
54 lines (50 loc) · 1008 Bytes
/
jsonpath_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
package jsonpath
import (
"encoding/json"
"testing"
)
func TestGetPathValueKeyWithSpaces(t *testing.T) {
jp := MustParsePath(`$["key with spaces"]`)
data := map[string]interface{}{
"key with spaces": "v",
}
value, err := GetPathValue(data, jp)
if err != nil {
t.Error(err)
return
}
stringVal, ok := value.(string)
if !ok {
t.Error("value was not string")
return
}
if stringVal != "v" {
t.Error("value is incorrect")
return
}
}
func TestJsonUnmarshal(t *testing.T) {
jsonEncodedExpression := []byte(`"$[\"key with spaces\"]"`)
var target JsonPath
err := json.Unmarshal(jsonEncodedExpression, &target)
if err != nil {
t.Error(err)
return
}
if target.str != `$["key with spaces"]` {
t.Error("parsing error")
return
}
}
func TestJsonMarshal(t *testing.T) {
jp := MustParsePath(`$["key with spaces"]`)
b, err := json.Marshal(jp)
if err != nil {
t.Error(err)
return
}
if string(b) != `"$[\"key with spaces\"]"` {
t.Error("value is incorrect")
return
}
}