Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
miroslav-debug committed Apr 12, 2019
1 parent 0c383fe commit 69835f2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
24 changes: 11 additions & 13 deletions evaluable.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,27 +138,25 @@ func variable(path Evaluables) Evaluable {
vvElem = vvElem.Elem()
}

v = vvElem.Interface()
if vvElem.IsValid() {
v = vvElem.Interface()

continue
continue
}
}

if vvElem.Kind() == reflect.Slice {
i, err := strconv.Atoi(k)
if i, err := strconv.Atoi(k); err == nil && i >= 0 && vv.Len() > i {
vvElem = vv.Index(i)

if err != nil {
continue
}
if vvElem.Kind() == reflect.Ptr {
vvElem = vvElem.Elem()
}

vvElem = vv.Index(i)
v = vvElem.Interface()

if vvElem.Kind() == reflect.Ptr {
vvElem = vvElem.Elem()
continue
}

v = vvElem.Interface()

continue
}

if vvElem.Kind() != reflect.Struct {
Expand Down
12 changes: 12 additions & 0 deletions gval_evaluationFailure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,18 @@ func TestModifierTyping(test *testing.T) {
},
wantErr: unknownParameter,
},
{
name: "Nested slice call index out of bound",
expression: `foo.Nested.Slice[10]`,
parameter: map[string]interface{}{"foo": foo},
wantErr: unknownParameter,
},
{
name: "Nested map call missing key",
expression: `foo.Nested.Map["d"]`,
parameter: map[string]interface{}{"foo": foo},
wantErr: unknownParameter,
},
{
name: "invalid selector",
expression: "hello[world()]",
Expand Down
12 changes: 12 additions & 0 deletions gval_parameterized_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,18 @@ func TestParameterized(t *testing.T) {
parameter: map[string]interface{}{"foo": foo},
want: "funkalicious",
},
{
name: "Nested map call",
expression: `foo.Nested.Map["a"]`,
parameter: map[string]interface{}{"foo": foo},
want: 1,
},
{
name: "Nested slice call",
expression: `foo.Nested.Slice[1]`,
parameter: map[string]interface{}{"foo": foo},
want: 2,
},
{

name: "Parameter call with + modifier",
Expand Down
8 changes: 6 additions & 2 deletions gval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ func (d dummyParameter) AlwaysFail() (interface{}, error) {
}

type dummyNestedParameter struct {
Funk string
Funk string
Map map[string]int
Slice []int
}

func (d dummyNestedParameter) Dunk(arg1 string) string {
Expand All @@ -88,7 +90,9 @@ var foo = dummyParameter{
BoolFalse: false,
Nil: nil,
Nested: dummyNestedParameter{
Funk: "funkalicious",
Funk: "funkalicious",
Map: map[string]int{"a": 1, "b": 2, "c": 3},
Slice: []int{1, 2, 3},
},
}

Expand Down

0 comments on commit 69835f2

Please sign in to comment.