File: query_test.go

package info (click to toggle)
golang-github-antchfx-jsonquery 1.1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 112 kB
  • sloc: makefile: 2
file content (133 lines) | stat: -rw-r--r-- 3,086 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package jsonquery

import (
	"strings"
	"testing"

	"github.com/antchfx/xpath"
)

func BenchmarkSelectorCache(b *testing.B) {
	DisableSelectorCache = false
	for i := 0; i < b.N; i++ {
		getQuery("/AAA/BBB/DDD/CCC/EEE/ancestor::*")
	}
}

func BenchmarkDisableSelectorCache(b *testing.B) {
	DisableSelectorCache = true
	for i := 0; i < b.N; i++ {
		getQuery("/AAA/BBB/DDD/CCC/EEE/ancestor::*")
	}
}

func TestNavigator(t *testing.T) {
	s := `{
		"name":"John",
		"age":30,
		"cars": [
			{ "name":"Ford", "models":[ "Fiesta", "Focus", "Mustang" ] },
			{ "name":"BMW", "models":[ "320", "X3", "X5" ] },
			{ "name":"Fiat", "models":[ "500", "Panda" ] }
		]
	 }`
	doc, _ := parseString(s)
	/**
	<age>30</age>
	<cars>
		<element>
			<models>...</models>
			<name>Ford</name>
		</element>
		<element>
			<models>...</models>
			<name>BMW</name>
		</element>
		<element>
			<models>...</models>
			<name>Fiat</name>
		</element>
	</cars>
	<name>John</name>
	*/
	nav := CreateXPathNavigator(doc)
	nav.MoveToRoot()
	if nav.NodeType() != xpath.RootNode {
		t.Fatal("node type is not RootNode")
	}
	// Move to first child(age).
	if e, g := true, nav.MoveToChild(); e != g {
		t.Fatalf("expected %v but %v", e, g)
	}
	if e, g := "age", nav.Current().Data; e != g {
		t.Fatalf("expected %v but %v", e, g)
	}
	if e, g := "30", nav.Value(); e != g {
		t.Fatalf("expected %v but %v", e, g)
	}
	// Move to next sibling node(cars).
	if e, g := true, nav.MoveToNext(); e != g {
		t.Fatalf("expected %v but %v", e, g)
	}
	if e, g := "cars", nav.Current().Data; e != g {
		t.Fatalf("expected %v but %v", e, g)
	}
	m := make(map[string][]string)
	// Move to cars child node.
	cur := nav.Copy()
	for ok := nav.MoveToChild(); ok; ok = nav.MoveToNext() {
		// Move to <element> node.
		// <element><models>...</models><name>Ford</name></element>
		cur1 := nav.Copy()
		var name string
		var models []string
		// name || models
		for ok := nav.MoveToChild(); ok; ok = nav.MoveToNext() {
			cur2 := nav.Copy()
			n := nav.Current()
			if n.Data == "name" {
				name = n.InnerText()
			} else {
				for ok := nav.MoveToChild(); ok; ok = nav.MoveToNext() {
					cur3 := nav.Copy()
					models = append(models, nav.Value())
					nav.MoveTo(cur3)
				}
			}
			nav.MoveTo(cur2)
		}
		nav.MoveTo(cur1)
		m[name] = models
	}
	expected := []struct {
		name, value string
	}{
		{"Ford", "Fiesta,Focus,Mustang"},
		{"BMW", "320,X3,X5"},
		{"Fiat", "500,Panda"},
	}
	for _, v := range expected {
		if e, g := v.value, strings.Join(m[v.name], ","); e != g {
			t.Fatalf("expected %v=%v,but %v=%v", v.name, e, v.name, g)
		}
	}
	nav.MoveTo(cur)
	// move to name.
	if e, g := true, nav.MoveToNext(); e != g {
		t.Fatalf("expected %v but %v", e, g)
	}
	// move to cars
	nav.MoveToPrevious()
	if e, g := "cars", nav.Current().Data; e != g {
		t.Fatalf("expected %v but %v", e, g)
	}
	// move to age.
	nav.MoveToFirst()
	if e, g := "age", nav.Current().Data; e != g {
		t.Fatalf("expected %v but %v", e, g)
	}
	nav.MoveToParent()
	if g := nav.Current().Type; g != DocumentNode {
		t.Fatalf("node type is not DocumentNode")
	}
}