funcs_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 template
  15. import (
  16. "encoding/base64"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/internal/testx"
  19. "reflect"
  20. "testing"
  21. )
  22. func TestBase64Encode(t *testing.T) {
  23. var tests = []struct {
  24. para interface{}
  25. expect string
  26. err string
  27. }{
  28. {
  29. para: 1,
  30. expect: "1",
  31. },
  32. {
  33. para: float32(3.14),
  34. expect: "3.14",
  35. },
  36. {
  37. para: float64(3.1415),
  38. expect: "3.1415",
  39. },
  40. {
  41. para: "hello",
  42. expect: "hello",
  43. },
  44. {
  45. para: "{\"hello\" : 3}",
  46. expect: "{\"hello\" : 3}",
  47. },
  48. {
  49. para: map[string]interface{}{
  50. "temperature": 30,
  51. "humidity": 20,
  52. },
  53. expect: `{"humidity":20,"temperature":30}`,
  54. },
  55. }
  56. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  57. for i, tt := range tests {
  58. result, err := Base64Encode(tt.para)
  59. r, _ := base64.StdEncoding.DecodeString(result)
  60. if !reflect.DeepEqual(tt.err, testx.Errstring(err)) {
  61. t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.para, tt.err, err)
  62. } else if tt.err == "" && !reflect.DeepEqual(tt.expect, string(r)) {
  63. t.Errorf("%d. %q\n\n mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.para, tt.expect, string(r))
  64. }
  65. }
  66. }