summaryrefslogtreecommitdiff
path: root/jsonpath/parse_test.go
blob: db41efeefe9c165db4896a26c5d1d21189144e4f (plain)
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
package jsonpath

import (
	"strings"
	"testing"

	"src.userspace.com.au/query/json"
)

func TestParse(t *testing.T) {
	tests := []struct {
		path, src, expect string
	}{
		{
			path:   "$.test",
			src:    `{"test":"one"}`,
			expect: "one",
		},
	}

	p := Parser{}

	for _, tt := range tests {
		doc, err := json.Parse(strings.NewReader(tt.src))
		if err != nil {
			t.Errorf("json.Parse(%q) failed: %s", tt.src, err)
		}

		sel, err := p.Parse(tt.path)
		if err != nil {
			t.Errorf("Parse(%q) failed: %s", tt.path, err)
		}
		actual := sel.MatchFirst(doc)
		actualText := actual.InnerText()
		if actualText != tt.expect {
			t.Errorf("MatchFirst(%s) => %s, expected %s", tt.src, actualText, tt.expect)
		}
	}
}