-
Notifications
You must be signed in to change notification settings - Fork 14
/
functions_test.go
48 lines (38 loc) · 1.08 KB
/
functions_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package goql
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
type fn struct {
}
//
func (f *fn) Execute(v ...Getter) (Getter, error) {
if len(v) != 2 {
return nil, fmt.Errorf("test function need two arg")
}
switch t := v[0].Get().(type) {
case string:
if r, ok := v[1].Get().(string); ok {
return String{String: t + r}, nil
}
}
return nil, fmt.Errorf("invalid types %T, %T", v[0].Get(), v[1].Get())
}
func TestRegister(t *testing.T) {
concat := &fn{}
assert.NotPanics(t, func() { RegisterFunction("fnconcat", concat) })
assert.Panics(t, func() { RegisterFunction("fnconcat", concat) })
assert.True(t, hasFunction("fnconcat"))
assert.False(t, hasFunction("invalid-func"))
res, err := executeFunction("fnconcat", String{}, Bool{})
assert.Error(t, err)
assert.Nil(t, res)
res, err = executeFunction("fnconcat", String{String: "Hello"}, String{String: "World"})
assert.NoError(t, err)
assert.IsType(t, String{}, res)
assert.Equal(t, "HelloWorld", res.Get().(string))
res, err = executeFunction("notexists")
assert.Error(t, err)
assert.Nil(t, res)
}