funcs_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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\n mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.para, tt.expect, string(r))
  50. }
  51. }
  52. }
  53. func TestAdd(t *testing.T) {
  54. var tests = []struct {
  55. para1 interface{}
  56. para2 interface{}
  57. expect interface{}
  58. err string
  59. }{
  60. {
  61. para1: int(3),
  62. para2: int(3),
  63. expect: int64(6),
  64. },
  65. {
  66. para1: int64(3),
  67. para2: int64(3),
  68. expect: int64(6),
  69. },
  70. {
  71. para1: int64(3),
  72. para2: int64(-3),
  73. expect: int64(0),
  74. },
  75. {
  76. para1: int(3),
  77. para2: float32(3),
  78. expect: int64(6),
  79. },
  80. {
  81. para1: int(3),
  82. para2: float32(3.14),
  83. expect: int64(6),
  84. },
  85. {
  86. para1: int64(3),
  87. para2: float64(3.14),
  88. expect: int64(6),
  89. },
  90. {
  91. para1: int64(3),
  92. para2: uint(1),
  93. expect: nil,
  94. err: "Unsupported data type uint of para2 for Add function when para1 type is int64.",
  95. },
  96. {
  97. para1: uint64(3),
  98. para2: uint64(1),
  99. expect: uint64(4),
  100. err: "",
  101. },
  102. {
  103. para1: uint64(3),
  104. para2: int64(1),
  105. expect: nil,
  106. err: "Unsupported data type int64 of para2 for Add function when para1 type is uint64.",
  107. },
  108. {
  109. para1: float32(3.1),
  110. para2: float32(1.1),
  111. expect: float64(4.199999928474426),
  112. err: "",
  113. },
  114. {
  115. para1: float64(3.1),
  116. para2: float64(1.1),
  117. expect: float64(4.2),
  118. err: "",
  119. },
  120. }
  121. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  122. for i, tt := range tests {
  123. result, err := Add(tt.para1, tt.para2)
  124. if !reflect.DeepEqual(tt.err, errstring(err)) {
  125. t.Errorf("%d. %q %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.para1, tt.para2, tt.err, err)
  126. } else if tt.err == "" && !reflect.DeepEqual(tt.expect, result) {
  127. t.Errorf("%d. %q %q \n\n mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.para1, tt.para2, tt.expect, result)
  128. }
  129. }
  130. }
  131. // errstring returns the string representation of an error.
  132. func errstring(err error) string {
  133. if err != nil {
  134. return err.Error()
  135. }
  136. return ""
  137. }