Ejemplo n.º 1
0
func testSel(t *testing.T, sel Selector, jsstr string, val ...interface{}) {
	out := make(chan *jsontree.JsonTree, 2)
	_js := jsontree.New()
	err := _js.UnmarshalJSON([]byte(jsstr))
	if err != nil {
		yt.Nil(t, err)
	}
	go sel(out, _js)
	i := 0
	for js := range out {
		if js == nil {
			break
		}
		if i < len(val) {
			if err := js.Err(); err != nil {
				_, ok := val[i].(TestError)
				y.Assert(t, ok, "unexpected error: ", err)
				i++
				continue
			}
			v, _ := js.Interface()
			yt.Equal(t, val[i], v, jsstr)
		}
		i++
	}
	yt.Equal(t, len(val), i, "wrong number of tests ", jsstr)
}
Ejemplo n.º 2
0
func TestParse(t *testing.T) {
	sel, err := Parse(".test")
	yt.Nil(t, err)
	testSel(t, sel, `{"test":"ok"}`, "ok")
	testSel(t, sel, `{"no":"negative"}`, TestError(""))

	sel, err = Parse(".test.nesting")
	yt.Nil(t, err)
	testSel(t, sel, `{"test":{"nesting":"good"}}`, "good")
	testSel(t, sel, `{"test":{"bad":"miss"}}`, TestError(""))
	testSel(t, sel, `{"test":"miss"}`, TestError(""))
	testSel(t, sel, `{"something":{"nesting":"miss"}}`)

	sel, err = Parse(".test.*")
	yt.Nil(t, err)
	testSel(t, sel, `{"test":{"foo1":{"bar":true}, "foo2":{"bar":true}},"bar":{"foo":false}}`,
		map[string]interface{}{"bar": true},
		map[string]interface{}{"bar": true})
	sel, err = Parse(".test.*.bar")
	yt.Nil(t, err)
	testSel(t, sel, `{"test":{"foo1":{"bar":true}, "foo2":{"bar":true}},"bar":{"foo":false}}`,
		true, true)
	testSel(t, sel, `{"test":{"foo1":{"bar":true}, "foo2":"bar"},"bar":{"foo":false}}`,
		true, TestError(""))

	sel, err = Parse(".test.**")
	yt.Nil(t, err)
	testSel(t, sel, `{"test":{"foo1":{"bar":true}, "foo2":{"bar":true}},"bar":{"foo":false}}`,
		map[string]interface{}{
			"foo1": map[string]interface{}{"bar": true},
			"foo2": map[string]interface{}{"bar": true},
		},
		map[string]interface{}{"bar": true},
		true,
		map[string]interface{}{"bar": true},
		true)
	sel, err = Parse(".test.**.qux")
	yt.Nil(t, err)
	//testSel(t, sel, `{"test":{"foo1":{"bar":{"qux":true}}, "foo2":{"bar":{"qux":true}}},"bar":{"qux":true}}`,
	//	true, true, true)
}