funcs_test.go 1.2 KB

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