1package jmespath 2 3import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8) 9 10func TestValidUncompiledExpressionSearches(t *testing.T) { 11 assert := assert.New(t) 12 var j = []byte(`{"foo": {"bar": {"baz": [0, 1, 2, 3, 4]}}}`) 13 var d interface{} 14 err := json.Unmarshal(j, &d) 15 assert.Nil(err) 16 result, err := Search("foo.bar.baz[2]", d) 17 assert.Nil(err) 18 assert.Equal(2.0, result) 19} 20 21func TestValidPrecompiledExpressionSearches(t *testing.T) { 22 assert := assert.New(t) 23 data := make(map[string]interface{}) 24 data["foo"] = "bar" 25 precompiled, err := Compile("foo") 26 assert.Nil(err) 27 result, err := precompiled.Search(data) 28 assert.Nil(err) 29 assert.Equal("bar", result) 30} 31 32func TestInvalidPrecompileErrors(t *testing.T) { 33 assert := assert.New(t) 34 _, err := Compile("not a valid expression") 35 assert.NotNil(err) 36} 37 38func TestInvalidMustCompilePanics(t *testing.T) { 39 defer func() { 40 r := recover() 41 assert.NotNil(t, r) 42 }() 43 MustCompile("not a valid expression") 44} 45