funcs_array_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. "reflect"
  18. "testing"
  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 TestArrayFunctions(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. var tests = []struct {
  30. name string
  31. args []interface{}
  32. result interface{}
  33. }{
  34. {
  35. name: "array_create",
  36. args: []interface{}{
  37. 1, "2", 3,
  38. },
  39. result: []interface{}{
  40. 1, "2", 3,
  41. },
  42. },
  43. }
  44. for i, tt := range tests {
  45. f, ok := builtins[tt.name]
  46. if !ok {
  47. t.Fatal(fmt.Sprintf("builtin %v not found", tt.name))
  48. }
  49. result, _ := f.exec(fctx, tt.args)
  50. if !reflect.DeepEqual(result, tt.result) {
  51. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  52. }
  53. }
  54. }