diff --git a/pkg/engine/assert/expression_test.go b/pkg/engine/assert/expression_test.go index 401ea183..20e8c1a3 100644 --- a/pkg/engine/assert/expression_test.go +++ b/pkg/engine/assert/expression_test.go @@ -2,8 +2,9 @@ package assert import ( "context" - "reflect" "testing" + + tassert "github.com/stretchr/testify/assert" ) func Test_parseExpressionRegex(t *testing.T) { @@ -170,9 +171,8 @@ func Test_parseExpressionRegex(t *testing.T) { }} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := parseExpressionRegex(context.Background(), tt.in); !reflect.DeepEqual(got, tt.want) { - t.Errorf("parseExpressionRegex() = %v, want %v", got, tt.want) - } + got := parseExpressionRegex(context.Background(), tt.in) + tassert.Equal(t, tt.want, got) }) } } diff --git a/pkg/engine/assert/project.go b/pkg/engine/assert/project.go index 788934c8..38fd1fbb 100644 --- a/pkg/engine/assert/project.go +++ b/pkg/engine/assert/project.go @@ -2,7 +2,6 @@ package assert import ( "context" - "fmt" "reflect" "github.com/jmespath-community/go-jmespath/pkg/binding" @@ -33,26 +32,28 @@ func project(ctx context.Context, key interface{}, value interface{}, bindings b }, nil } else { if reflectutils.GetKind(value) == reflect.Map { - projected := reflect.ValueOf(value).MapIndex(reflect.ValueOf(expression.statement)) - if !projected.IsValid() { - return nil, fmt.Errorf("failed to find the map index `%s`", expression.statement) + mapValue := reflect.ValueOf(value).MapIndex(reflect.ValueOf(expression.statement)) + var value interface{} + if mapValue.IsValid() { + value = mapValue.Interface() } return &projection{ foreach: expression.foreach, foreachName: expression.foreachName, binding: expression.binding, - result: projected.Interface(), + result: value, }, nil } } } if reflectutils.GetKind(value) == reflect.Map { - projected := reflect.ValueOf(value).MapIndex(reflect.ValueOf(key)) - if !projected.IsValid() { - return nil, fmt.Errorf("failed to find the map index `%v`", key) + mapValue := reflect.ValueOf(value).MapIndex(reflect.ValueOf(key)) + var value interface{} + if mapValue.IsValid() { + value = mapValue.Interface() } return &projection{ - result: projected.Interface(), + result: value, }, nil } // TODO is this an error ? diff --git a/pkg/engine/assert/project_test.go b/pkg/engine/assert/project_test.go new file mode 100644 index 00000000..ca164c10 --- /dev/null +++ b/pkg/engine/assert/project_test.go @@ -0,0 +1,40 @@ +package assert + +import ( + "context" + "testing" + + "github.com/jmespath-community/go-jmespath/pkg/binding" + tassert "github.com/stretchr/testify/assert" +) + +func Test_project(t *testing.T) { + tests := []struct { + name string + key interface{} + value interface{} + bindings binding.Bindings + want *projection + wantErr bool + }{{ + name: "map index not found", + key: "foo", + value: map[string]interface{}{ + "bar": 42, + }, + bindings: nil, + want: nil, + wantErr: false, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := project(context.TODO(), tt.key, tt.value, tt.bindings) + if tt.wantErr { + tassert.Error(t, err) + tassert.Equal(t, tt.want, got) + } else { + tassert.NoError(t, err) + } + }) + } +}