template_test.go 5.5 KB

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