-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
- Loading branch information
1 parent
211203c
commit 39e28a1
Showing
3 changed files
with
93 additions
and
169 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package projection | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/jmespath-community/go-jmespath/pkg/binding" | ||
tassert "github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestProjection(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
key any | ||
value any | ||
bindings binding.Bindings | ||
want any | ||
wantErr bool | ||
}{{ | ||
name: "map index not found", | ||
key: "foo", | ||
value: map[string]any{ | ||
"bar": 42, | ||
}, | ||
bindings: nil, | ||
want: nil, | ||
wantErr: false, | ||
}, { | ||
name: "map index found", | ||
key: "bar", | ||
value: map[string]any{ | ||
"bar": 42, | ||
}, | ||
bindings: nil, | ||
want: 42, | ||
wantErr: false, | ||
}, { | ||
name: "map index found (and nil)", | ||
key: "bar", | ||
value: map[string]any{ | ||
"bar": nil, | ||
}, | ||
bindings: nil, | ||
want: nil, | ||
wantErr: false, | ||
}, { | ||
name: "non string key (not found)", | ||
key: 3, | ||
value: map[int]any{ | ||
2: "foo", | ||
}, | ||
bindings: nil, | ||
want: nil, | ||
wantErr: false, | ||
}, { | ||
name: "non string key (found)", | ||
key: 2, | ||
value: map[int]any{ | ||
2: "foo", | ||
}, | ||
bindings: nil, | ||
want: "foo", | ||
wantErr: false, | ||
}, { | ||
name: "non string key (found and nil)", | ||
key: 2, | ||
value: map[int]any{ | ||
2: nil, | ||
}, | ||
bindings: nil, | ||
want: nil, | ||
wantErr: false, | ||
}, { | ||
name: "nil value", | ||
key: "foo", | ||
value: nil, | ||
bindings: nil, | ||
want: nil, | ||
wantErr: false, | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
proj := Parse(tt.key) | ||
got, err := proj.Handler(context.TODO(), tt.value, tt.bindings) | ||
if tt.wantErr { | ||
tassert.Error(t, err) | ||
} else { | ||
tassert.NoError(t, err) | ||
} | ||
tassert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} |