template_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package fasttemplate
  2. import (
  3. "io"
  4. "testing"
  5. )
  6. func TestEmptyTemplate(t *testing.T) {
  7. tpl, err := NewTemplate("", "[", "]")
  8. if err != nil {
  9. t.Fatalf("unexpected error: %s", err)
  10. }
  11. s := tpl.ExecuteString(map[string]interface{}{"foo": "bar", "aaa": "bbb"})
  12. if s != "" {
  13. t.Fatalf("unexpected string returned %q. Expected empty string", s)
  14. }
  15. }
  16. func TestEmptyTagStart(t *testing.T) {
  17. expectPanic(t, func() { NewTemplate("foobar", "", "]") })
  18. }
  19. func TestEmptyTagEnd(t *testing.T) {
  20. expectPanic(t, func() { NewTemplate("foobar", "[", "") })
  21. }
  22. func TestNoTags(t *testing.T) {
  23. template := "foobar"
  24. tpl, err := NewTemplate(template, "[", "]")
  25. if err != nil {
  26. t.Fatalf("unexpected error: %s", err)
  27. }
  28. s := tpl.ExecuteString(map[string]interface{}{"foo": "bar", "aaa": "bbb"})
  29. if s != template {
  30. t.Fatalf("unexpected template value %q. Expected %q", s, template)
  31. }
  32. }
  33. func TestEmptyTagName(t *testing.T) {
  34. template := "foo[]bar"
  35. tpl, err := NewTemplate(template, "[", "]")
  36. if err != nil {
  37. t.Fatalf("unexpected error: %s", err)
  38. }
  39. s := tpl.ExecuteString(map[string]interface{}{"": "111", "aaa": "bbb"})
  40. result := "foo111bar"
  41. if s != result {
  42. t.Fatalf("unexpected template value %q. Expected %q", s, result)
  43. }
  44. }
  45. func TestOnlyTag(t *testing.T) {
  46. template := "[foo]"
  47. tpl, err := NewTemplate(template, "[", "]")
  48. if err != nil {
  49. t.Fatalf("unexpected error: %s", err)
  50. }
  51. s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
  52. result := "111"
  53. if s != result {
  54. t.Fatalf("unexpected template value %q. Expected %q", s, result)
  55. }
  56. }
  57. func TestStartWithTag(t *testing.T) {
  58. template := "[foo]barbaz"
  59. tpl, err := NewTemplate(template, "[", "]")
  60. if err != nil {
  61. t.Fatalf("unexpected error: %s", err)
  62. }
  63. s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
  64. result := "111barbaz"
  65. if s != result {
  66. t.Fatalf("unexpected template value %q. Expected %q", s, result)
  67. }
  68. }
  69. func TestEndWithTag(t *testing.T) {
  70. template := "foobar[foo]"
  71. tpl, err := NewTemplate(template, "[", "]")
  72. if err != nil {
  73. t.Fatalf("unexpected error: %s", err)
  74. }
  75. s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
  76. result := "foobar111"
  77. if s != result {
  78. t.Fatalf("unexpected template value %q. Expected %q", s, result)
  79. }
  80. }
  81. func TestDuplicateTags(t *testing.T) {
  82. template := "[foo]bar[foo][foo]baz"
  83. tpl, err := NewTemplate(template, "[", "]")
  84. if err != nil {
  85. t.Fatalf("unexpected error: %s", err)
  86. }
  87. s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
  88. result := "111bar111111baz"
  89. if s != result {
  90. t.Fatalf("unexpected template value %q. Expected %q", s, result)
  91. }
  92. }
  93. func TestMultipleTags(t *testing.T) {
  94. template := "foo[foo]aa[aaa]ccc"
  95. tpl, err := NewTemplate(template, "[", "]")
  96. if err != nil {
  97. t.Fatalf("unexpected error: %s", err)
  98. }
  99. s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
  100. result := "foo111aabbbccc"
  101. if s != result {
  102. t.Fatalf("unexpected template value %q. Expected %q", s, result)
  103. }
  104. }
  105. func TestLongDelimiter(t *testing.T) {
  106. template := "foo{{{foo}}}bar"
  107. tpl, err := NewTemplate(template, "{{{", "}}}")
  108. if err != nil {
  109. t.Fatalf("unexpected error: %s", err)
  110. }
  111. s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
  112. result := "foo111bar"
  113. if s != result {
  114. t.Fatalf("unexpected template value %q. Expected %q", s, result)
  115. }
  116. }
  117. func TestIdenticalDelimiter(t *testing.T) {
  118. template := "foo@foo@foo@aaa@"
  119. tpl, err := NewTemplate(template, "@", "@")
  120. if err != nil {
  121. t.Fatalf("unexpected error: %s", err)
  122. }
  123. s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
  124. result := "foo111foobbb"
  125. if s != result {
  126. t.Fatalf("unexpected template value %q. Expected %q", s, result)
  127. }
  128. }
  129. func TestDlimitersWithDistinctSize(t *testing.T) {
  130. template := "foo<?phpaaa?>bar<?phpzzz?>"
  131. tpl, err := NewTemplate(template, "<?php", "?>")
  132. if err != nil {
  133. t.Fatalf("unexpected error: %s", err)
  134. }
  135. s := tpl.ExecuteString(map[string]interface{}{"zzz": "111", "aaa": "bbb"})
  136. result := "foobbbbar111"
  137. if s != result {
  138. t.Fatalf("unexpected template value %q. Expected %q", s, result)
  139. }
  140. }
  141. func TestEmptyValue(t *testing.T) {
  142. template := "foobar[foo]"
  143. tpl, err := NewTemplate(template, "[", "]")
  144. if err != nil {
  145. t.Fatalf("unexpected error: %s", err)
  146. }
  147. s := tpl.ExecuteString(map[string]interface{}{"foo": "", "aaa": "bbb"})
  148. result := "foobar"
  149. if s != result {
  150. t.Fatalf("unexpected template value %q. Expected %q", s, result)
  151. }
  152. }
  153. func TestNoValue(t *testing.T) {
  154. template := "foobar[foo]x[aaa]"
  155. tpl, err := NewTemplate(template, "[", "]")
  156. if err != nil {
  157. t.Fatalf("unexpected error: %s", err)
  158. }
  159. s := tpl.ExecuteString(map[string]interface{}{"aaa": "bbb"})
  160. result := "foobarxbbb"
  161. if s != result {
  162. t.Fatalf("unexpected template value %q. Expected %q", s, result)
  163. }
  164. }
  165. func TestNoEndDelimiter(t *testing.T) {
  166. template := "foobar[foo"
  167. _, err := NewTemplate(template, "[", "]")
  168. if err == nil {
  169. t.Fatalf("expected non-nil error. got nil")
  170. }
  171. }
  172. func TestUnsupportedValue(t *testing.T) {
  173. template := "foobar[foo]"
  174. tpl, err := NewTemplate(template, "[", "]")
  175. if err != nil {
  176. t.Fatalf("unexpected error: %s", err)
  177. }
  178. expectPanic(t, func() {
  179. tpl.ExecuteString(map[string]interface{}{"foo": 123, "aaa": "bbb"})
  180. })
  181. }
  182. func TestMixedValues(t *testing.T) {
  183. template := "foo[foo]bar[bar]baz[baz]"
  184. tpl, err := NewTemplate(template, "[", "]")
  185. if err != nil {
  186. t.Fatalf("unexpected error: %s", err)
  187. }
  188. s := tpl.ExecuteString(map[string]interface{}{
  189. "foo": "111",
  190. "bar": []byte("bbb"),
  191. "baz": TagFunc(func(w io.Writer, tag string) (int, error) { return w.Write([]byte(tag)) }),
  192. })
  193. result := "foo111barbbbbazbaz"
  194. if s != result {
  195. t.Fatalf("unexpected template value %q. Expected %q", s, result)
  196. }
  197. }
  198. func expectPanic(t *testing.T, f func()) {
  199. defer func() {
  200. if r := recover(); r == nil {
  201. t.Fatalf("missing panic")
  202. }
  203. }()
  204. f()
  205. }