funcs_str_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package function
  15. import (
  16. "fmt"
  17. "testing"
  18. "github.com/stretchr/testify/require"
  19. "github.com/lf-edge/ekuiper/internal/conf"
  20. kctx "github.com/lf-edge/ekuiper/internal/topo/context"
  21. "github.com/lf-edge/ekuiper/internal/topo/state"
  22. "github.com/lf-edge/ekuiper/pkg/api"
  23. )
  24. func TestStrFuncNil(t *testing.T) {
  25. contextLogger := conf.Log.WithField("rule", "testExec")
  26. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  27. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  28. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  29. oldBuiltins := builtins
  30. defer func() {
  31. builtins = oldBuiltins
  32. }()
  33. builtins = map[string]builtinFunc{}
  34. registerStrFunc()
  35. for name, function := range builtins {
  36. switch name {
  37. case "concat":
  38. r, b := function.exec(fctx, []interface{}{"1", nil, "2"})
  39. require.True(t, b, fmt.Sprintf("%v failed", name))
  40. require.Equal(t, "12", r)
  41. case "endswith", "regexp_matches", "startswith":
  42. r, b := function.check([]interface{}{nil})
  43. require.True(t, b, fmt.Sprintf("%v failed", name))
  44. require.Equal(t, false, r)
  45. case "indexof":
  46. r, b := function.exec(fctx, []interface{}{nil})
  47. require.True(t, b, fmt.Sprintf("%v failed", name))
  48. require.Equal(t, -1, r)
  49. case "length", "numbytes":
  50. r, b := function.check([]interface{}{nil})
  51. require.True(t, b, fmt.Sprintf("%v failed", name))
  52. require.Equal(t, 0, r)
  53. default:
  54. r, b := function.check([]interface{}{nil})
  55. require.True(t, b, fmt.Sprintf("%v failed", name))
  56. require.Nil(t, r, fmt.Sprintf("%v failed", name))
  57. }
  58. }
  59. }