Skip to content

Commit

Permalink
Test error cases
Browse files Browse the repository at this point in the history
  • Loading branch information
zix99 committed Nov 25, 2019
1 parent 5ea4f42 commit f3ba3a3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pkg/expressions/funcsArithmatic.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "strconv"
func arithmaticHelperi(equation func(int, int) int) KeyBuilderFunction {
return KeyBuilderFunction(func(args []KeyBuilderStage) KeyBuilderStage {
if len(args) < 2 {
return stageError(ErrorArgCount)
return stageLiteral(ErrorArgCount)
}
return KeyBuilderStage(func(context KeyBuilderContext) string {
final, err := strconv.Atoi(args[0](context))
Expand All @@ -31,7 +31,7 @@ func arithmaticHelperi(equation func(int, int) int) KeyBuilderFunction {
func arithmaticHelperf(equation func(float64, float64) float64) KeyBuilderFunction {
return KeyBuilderFunction(func(args []KeyBuilderStage) KeyBuilderStage {
if len(args) < 2 {
return stageError(ErrorArgCount)
return stageLiteral(ErrorArgCount)
}
return KeyBuilderStage(func(context KeyBuilderContext) string {
final, err := strconv.ParseFloat(args[0](context), 64)
Expand Down
12 changes: 12 additions & 0 deletions pkg/expressions/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,20 @@ func TestArithmatic(t *testing.T) {
assert.Equal(t, "122 200 50 90", key)
}

func TestArithmaticError(t *testing.T) {
kb, _ := NewKeyBuilder().Compile("{sumi 1} {sumi 1 a} {sumi a 1} {sumi 1 1 b}")
key := kb.BuildKey(&testFuncContext)
assert.Equal(t, "<ARGN> <BAD-TYPE> <BAD-TYPE> <BAD-TYPE>", key)
}

func TestArithmaticf(t *testing.T) {
kb, _ := NewKeyBuilder().Compile("{sumf {1} {4}} {multf {1} 2} {divf {1} 2} {subf {1} 10}")
key := kb.BuildKey(&testFuncContext)
assert.Equal(t, "122 200 50 90", key)
}

func TestArithmaticfError(t *testing.T) {
kb, _ := NewKeyBuilder().Compile("{sumf 1} {sumf 1 a} {sumf a 1} {sumf 1 2 a}")
key := kb.BuildKey(&testFuncContext)
assert.Equal(t, "<ARGN> <BAD-TYPE> <BAD-TYPE> <BAD-TYPE>", key)
}
5 changes: 4 additions & 1 deletion pkg/expressions/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ func stageLiteral(s string) KeyBuilderStage {
}

func stageSimpleVariable(s string) KeyBuilderStage {
index, _ := strconv.Atoi(s)
index, err := strconv.Atoi(s)
if err != nil {
return stageLiteral(ErrorType)
}
return KeyBuilderStage(func(context KeyBuilderContext) string {
return context.GetMatch(index)
})
Expand Down

0 comments on commit f3ba3a3

Please sign in to comment.