funcs_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package templates
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "github.com/emqx/kuiper/common"
  6. "reflect"
  7. "testing"
  8. )
  9. func TestBase64Encode(t *testing.T) {
  10. var tests = []struct {
  11. para interface{}
  12. expect string
  13. err string
  14. }{
  15. {
  16. para: 1,
  17. expect: "1",
  18. },
  19. {
  20. para: float32(3.14),
  21. expect: "3.14",
  22. },
  23. {
  24. para: float64(3.1415),
  25. expect: "3.1415",
  26. },
  27. {
  28. para: "hello",
  29. expect: "hello",
  30. },
  31. {
  32. para: "{\"hello\" : 3}",
  33. expect: "{\"hello\" : 3}",
  34. },
  35. {
  36. para: map[string]interface{}{
  37. "temperature": 30,
  38. "humidity": 20,
  39. },
  40. expect: `{"humidity":20,"temperature":30}`,
  41. },
  42. }
  43. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  44. for i, tt := range tests {
  45. result, err := Base64Encode(tt.para)
  46. r, _ := base64.StdEncoding.DecodeString(result)
  47. if !reflect.DeepEqual(tt.err, common.Errstring(err)) {
  48. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.para, tt.err, err)
  49. } else if tt.err == "" && !reflect.DeepEqual(tt.expect, string(r)) {
  50. t.Errorf("%d. %q\n\n mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.para, tt.expect, string(r))
  51. }
  52. }
  53. }