test_func.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright 2021 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 mock
  15. import (
  16. "github.com/lf-edge/ekuiper/sdk/go/api"
  17. "reflect"
  18. "testing"
  19. )
  20. type FuncTest struct {
  21. Args []interface{}
  22. Result interface{}
  23. Ok bool
  24. }
  25. func TestFuncExec(f api.Function, tests []FuncTest, t *testing.T) {
  26. ctx := newMockFuncContext(newMockContext("rule1", "op1"), 1)
  27. for i, tt := range tests {
  28. r, o := f.Exec(tt.Args, ctx)
  29. if o != tt.Ok {
  30. t.Errorf("%d ok mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.Ok, o)
  31. } else if !reflect.DeepEqual(tt.Result, r) {
  32. t.Errorf("%d result mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.Result, r)
  33. }
  34. }
  35. }