Skip to content

Commit

Permalink
feat: add test
Browse files Browse the repository at this point in the history
Signed-off-by: carlclone <906561974@qq.com>
  • Loading branch information
carlclone committed Jan 3, 2024
1 parent f3afd63 commit 6c688a3
Showing 1 changed file with 79 additions and 2 deletions.
81 changes: 79 additions & 2 deletions internal/binder/function/funcs_str_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ package function
import (
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"reflect"
"testing"

"github.com/stretchr/testify/require"

"github.com/lf-edge/ekuiper/internal/conf"
kctx "github.com/lf-edge/ekuiper/internal/topo/context"
"github.com/lf-edge/ekuiper/internal/topo/state"
"github.com/lf-edge/ekuiper/pkg/api"
"github.com/lf-edge/ekuiper/pkg/ast"
)

func TestStrFuncNil(t *testing.T) {
Expand Down Expand Up @@ -290,3 +291,79 @@ func testFormatLocale(t *testing.T, fctx *kctx.DefaultFuncContext) {
}
}
}

func TestStringFuncVal(t *testing.T) {
tests := []struct {
name string
funcName string
args []ast.Expr
err error
}{
{
name: "format failure",
funcName: "format",
args: []ast.Expr{
&ast.StringLiteral{Val: "1"},
},
err: fmt.Errorf("At least has 2 argument but found 1."),
},
{
name: "format failure",
funcName: "format",
args: []ast.Expr{
&ast.StringLiteral{Val: "1"},
&ast.IntegerLiteral{Val: 1},
},
err: fmt.Errorf("Expect numeric type for parameter 1"),
},
{
name: "format failure",
funcName: "format",
args: []ast.Expr{
&ast.IntegerLiteral{Val: 1},
&ast.StringLiteral{Val: "1"},
},
err: fmt.Errorf("Expect integer type for parameter 2"),
},
{
name: "format success",
funcName: "format",
args: []ast.Expr{
&ast.IntegerLiteral{Val: 1},
&ast.IntegerLiteral{Val: 0},
},
err: nil,
},
{
name: "format failure",
funcName: "format",
args: []ast.Expr{
&ast.IntegerLiteral{Val: 1},
&ast.IntegerLiteral{Val: 0},
&ast.IntegerLiteral{Val: 0},
},
err: fmt.Errorf("Expect string type for parameter 3"),
},
{
name: "format success",
funcName: "format",
args: []ast.Expr{
&ast.IntegerLiteral{Val: 1},
&ast.IntegerLiteral{Val: 0},
&ast.StringLiteral{Val: "en"},
},
err: nil,
},
}

registerStrFunc()

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f, ok := builtins[tt.funcName]
assert.True(t, ok)
err := f.val(nil, tt.args)
assert.Equal(t, tt.err, err)
})
}
}

0 comments on commit 6c688a3

Please sign in to comment.