github.com/webdestroya/awsmocker@v0.2.6/jmespath_test.go (about) 1 package awsmocker 2 3 import ( 4 "encoding/json" 5 "reflect" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestJmesPathMatching(t *testing.T) { 12 var j = []byte(`{"foo": {"bar": {"baz": [0, 1, 2, 3, 4, 4.5, true, false, null, "hello"]}}}`) 13 var d interface{} 14 err := json.Unmarshal(j, &d) 15 require.NoError(t, err) 16 17 t.Run("test generic expressions", func(t *testing.T) { 18 tables := []struct { 19 expr string 20 val interface{} 21 }{ 22 {"foo.bar.baz[99]", nil}, 23 {"foo.bar.baz[2]", int(2)}, 24 {"foo.bar.baz[2]", int8(2)}, 25 {"foo.bar.baz[2]", int16(2)}, 26 {"foo.bar.baz[2]", int32(2)}, 27 {"foo.bar.baz[2]", int64(2)}, 28 29 {"foo.bar.baz[2]", uint(2)}, 30 {"foo.bar.baz[2]", uint8(2)}, 31 {"foo.bar.baz[2]", uint16(2)}, 32 {"foo.bar.baz[2]", uint32(2)}, 33 {"foo.bar.baz[2]", uint64(2)}, 34 35 {"foo.bar.baz[2]", float32(2.0)}, 36 {"foo.bar.baz[2]", float64(2.0)}, 37 {"foo.bar.baz[2]", func(v any) bool { return reflect.DeepEqual(v, float64(2.0)) }}, 38 } 39 40 for _, table := range tables { 41 require.Truef(t, JMESMatch(d, table.expr, table.val), "expected true for %T", table.val) 42 } 43 }) 44 45 t.Run("bad expected values", func(t *testing.T) { 46 tables := []struct { 47 expr string 48 val interface{} 49 }{ 50 {"some goofy expression", []int{2}}, 51 52 {"foo.bar.baz[2]", []int{2}}, 53 {"foo.bar.baz[2]", []int8{2}}, 54 {"foo.bar.baz[2]", []int16{2}}, 55 {"foo.bar.baz[2]", []int32{2}}, 56 {"foo.bar.baz[2]", []int64{2}}, 57 58 {"foo.bar.baz[2]", []uint{2}}, 59 {"foo.bar.baz[2]", []uint8{2}}, 60 {"foo.bar.baz[2]", []uint16{2}}, 61 {"foo.bar.baz[2]", []uint32{2}}, 62 {"foo.bar.baz[2]", []uint64{2}}, 63 64 {"foo.bar.baz[2]", []float32{2.0}}, 65 {"foo.bar.baz[2]", []float64{2.0}}, 66 {"foo.bar.baz[2]", func(v float64) bool { return reflect.DeepEqual(v, float64(2.0)) }}, 67 } 68 69 for _, table := range tables { 70 require.Panicsf(t, func() { JMESMatch(d, table.expr, table.val) }, "expected panic for %T", table.val) 71 } 72 }) 73 74 }