1 package ast
2
3 import (
4 "encoding/json"
5 "testing"
6
7 "github.com/stretchr/testify/require"
8 )
9
10 func TestPath_String(t *testing.T) {
11 type Spec struct {
12 Value Path
13 Expected string
14 }
15 specs := []*Spec{
16 {
17 Value: Path{PathName("a"), PathIndex(2), PathName("c")},
18 Expected: "a[2].c",
19 },
20 {
21 Value: Path{},
22 Expected: ``,
23 },
24 {
25 Value: Path{PathIndex(1), PathName("b")},
26 Expected: `[1].b`,
27 },
28 }
29
30 for _, spec := range specs {
31 t.Run(spec.Value.String(), func(t *testing.T) {
32 require.Equal(t, spec.Expected, spec.Value.String())
33 })
34 }
35 }
36
37 func TestPath_MarshalJSON(t *testing.T) {
38 type Spec struct {
39 Value Path
40 Expected string
41 }
42 specs := []*Spec{
43 {
44 Value: Path{PathName("a"), PathIndex(2), PathName("c")},
45 Expected: `["a",2,"c"]`,
46 },
47 {
48 Value: Path{},
49 Expected: `[]`,
50 },
51 {
52 Value: Path{PathIndex(1), PathName("b")},
53 Expected: `[1,"b"]`,
54 },
55 }
56
57 for _, spec := range specs {
58 t.Run(spec.Value.String(), func(t *testing.T) {
59 b, err := json.Marshal(spec.Value)
60 require.Nil(t, err)
61
62 require.Equal(t, spec.Expected, string(b))
63 })
64 }
65 }
66
67 func TestPath_UnmarshalJSON(t *testing.T) {
68 type Spec struct {
69 Value string
70 Expected Path
71 }
72 specs := []*Spec{
73 {
74 Value: `["a",2,"c"]`,
75 Expected: Path{PathName("a"), PathIndex(2), PathName("c")},
76 },
77 {
78 Value: `[]`,
79 Expected: Path{},
80 },
81 {
82 Value: `[1,"b"]`,
83 Expected: Path{PathIndex(1), PathName("b")},
84 },
85 }
86
87 for _, spec := range specs {
88 t.Run(spec.Value, func(t *testing.T) {
89 var path Path
90 err := json.Unmarshal([]byte(spec.Value), &path)
91 require.Nil(t, err)
92
93 require.Equal(t, spec.Expected, path)
94 })
95 }
96 }
97
View as plain text