File: path_test.go

package info (click to toggle)
golang-github-vektah-gqlparser 2.5.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,748 kB
  • sloc: javascript: 164; sh: 46; makefile: 10
file content (96 lines) | stat: -rw-r--r-- 1,706 bytes parent folder | download
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
package ast

import (
	"encoding/json"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestPath_String(t *testing.T) {
	type Spec struct {
		Value    Path
		Expected string
	}
	specs := []*Spec{
		{
			Value:    Path{PathName("a"), PathIndex(2), PathName("c")},
			Expected: "a[2].c",
		},
		{
			Value:    Path{},
			Expected: ``,
		},
		{
			Value:    Path{PathIndex(1), PathName("b")},
			Expected: `[1].b`,
		},
	}

	for _, spec := range specs {
		t.Run(spec.Value.String(), func(t *testing.T) {
			require.Equal(t, spec.Expected, spec.Value.String())
		})
	}
}

func TestPath_MarshalJSON(t *testing.T) {
	type Spec struct {
		Value    Path
		Expected string
	}
	specs := []*Spec{
		{
			Value:    Path{PathName("a"), PathIndex(2), PathName("c")},
			Expected: `["a",2,"c"]`,
		},
		{
			Value:    Path{},
			Expected: `[]`,
		},
		{
			Value:    Path{PathIndex(1), PathName("b")},
			Expected: `[1,"b"]`,
		},
	}

	for _, spec := range specs {
		t.Run(spec.Value.String(), func(t *testing.T) {
			b, err := json.Marshal(spec.Value)
			require.NoError(t, err)

			require.Equal(t, spec.Expected, string(b))
		})
	}
}

func TestPath_UnmarshalJSON(t *testing.T) {
	type Spec struct {
		Value    string
		Expected Path
	}
	specs := []*Spec{
		{
			Value:    `["a",2,"c"]`,
			Expected: Path{PathName("a"), PathIndex(2), PathName("c")},
		},
		{
			Value:    `[]`,
			Expected: Path{},
		},
		{
			Value:    `[1,"b"]`,
			Expected: Path{PathIndex(1), PathName("b")},
		},
	}

	for _, spec := range specs {
		t.Run(spec.Value, func(t *testing.T) {
			var path Path
			err := json.Unmarshal([]byte(spec.Value), &path)
			require.NoError(t, err)

			require.Equal(t, spec.Expected, path)
		})
	}
}