Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add path to assertion parsing #515

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions pkg/core/assertion/assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@
}

func Parse(assertion any, compiler compilers.Compilers) (Assertion, error) {
out, err := parse(nil, assertion, compiler)
if err != nil {
return nil, err
}
return out, nil
}

func parse(path *field.Path, assertion any, compiler compilers.Compilers) (Assertion, *field.Error) {
switch reflectutils.GetKind(assertion) {
case reflect.Slice:
return parseSlice(assertion, compiler)
return parseSlice(path, assertion, compiler)

Check warning on line 30 in pkg/core/assertion/assertion.go

View check run for this annotation

Codecov / codecov/patch

pkg/core/assertion/assertion.go#L30

Added line #L30 was not covered by tests
case reflect.Map:
return parseMap(assertion, compiler)
return parseMap(path, assertion, compiler)
default:
return parseScalar(assertion, compiler)
return parseScalar(path, assertion, compiler)
}
}

Expand Down Expand Up @@ -55,11 +63,12 @@
return errs, nil
}

func parseSlice(assertion any, compiler compilers.Compilers) (sliceNode, error) {
func parseSlice(path *field.Path, assertion any, compiler compilers.Compilers) (sliceNode, *field.Error) {

Check warning on line 66 in pkg/core/assertion/assertion.go

View check run for this annotation

Codecov / codecov/patch

pkg/core/assertion/assertion.go#L66

Added line #L66 was not covered by tests
var assertions sliceNode
valueOf := reflect.ValueOf(assertion)
for i := 0; i < valueOf.Len(); i++ {
sub, err := Parse(valueOf.Index(i).Interface(), compiler)
path := path.Index(i)
sub, err := parse(path, valueOf.Index(i).Interface(), compiler)

Check warning on line 71 in pkg/core/assertion/assertion.go

View check run for this annotation

Codecov / codecov/patch

pkg/core/assertion/assertion.go#L70-L71

Added lines #L70 - L71 were not covered by tests
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -138,13 +147,14 @@
return errs, nil
}

func parseMap(assertion any, compiler compilers.Compilers) (mapNode, error) {
func parseMap(path *field.Path, assertion any, compiler compilers.Compilers) (mapNode, *field.Error) {
assertions := mapNode{}
iter := reflect.ValueOf(assertion).MapRange()
for iter.Next() {
key := iter.Key().Interface()
value := iter.Value().Interface()
assertion, err := Parse(value, compiler)
path := path.Child(fmt.Sprint(key))
assertion, err := parse(path, value, compiler)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -173,12 +183,12 @@
return errs, nil
}

func parseScalar(in any, compiler compilers.Compilers) (scalarNode, error) {
func parseScalar(path *field.Path, in any, compiler compilers.Compilers) (scalarNode, *field.Error) {
proj, err := projection.ParseScalar(in, compiler)
if err != nil {
return nil, err
return nil, field.InternalError(path, err)
}
return proj, err
return proj, nil

Check warning on line 191 in pkg/core/assertion/assertion.go

View check run for this annotation

Codecov / codecov/patch

pkg/core/assertion/assertion.go#L191

Added line #L191 was not covered by tests
}

func expectValueMessage(value any) string {
Expand Down
29 changes: 29 additions & 0 deletions pkg/core/assertion/assertion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,32 @@ func TestAssert(t *testing.T) {
})
}
}

func TestParse(t *testing.T) {
tests := []struct {
name string
assertion any
want field.ErrorList
wantErr bool
}{{
name: "bad scalar",
assertion: map[string]any{
"foo": map[string]any{
"bar": "~.(`42`)",
},
},
wantErr: true,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
compiler := compilers.DefaultCompilers
parsed, err := Parse(tt.assertion, compiler)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.NotNil(t, parsed)
}
})
}
}