template_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 TestDuplicateTags(t *testing.T) {
  64. template := "[foo]bar[foo][foo]baz"
  65. tpl := New(template, "[", "]")
  66. s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
  67. result := "111bar111111baz"
  68. if s != result {
  69. t.Fatalf("unexpected template value %q. Expected %q", s, result)
  70. }
  71. }
  72. func TestMultipleTags(t *testing.T) {
  73. template := "foo[foo]aa[aaa]ccc"
  74. tpl := New(template, "[", "]")
  75. s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
  76. result := "foo111aabbbccc"
  77. if s != result {
  78. t.Fatalf("unexpected template value %q. Expected %q", s, result)
  79. }
  80. }
  81. func TestLongDelimiter(t *testing.T) {
  82. template := "foo{{{foo}}}bar"
  83. tpl := New(template, "{{{", "}}}")
  84. s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
  85. result := "foo111bar"
  86. if s != result {
  87. t.Fatalf("unexpected template value %q. Expected %q", s, result)
  88. }
  89. }
  90. func TestIdenticalDelimiter(t *testing.T) {
  91. template := "foo@foo@foo@aaa@"
  92. tpl := New(template, "@", "@")
  93. s := tpl.ExecuteString(map[string]interface{}{"foo": "111", "aaa": "bbb"})
  94. result := "foo111foobbb"
  95. if s != result {
  96. t.Fatalf("unexpected template value %q. Expected %q", s, result)
  97. }
  98. }
  99. func TestDlimitersWithDistinctSize(t *testing.T) {
  100. template := "foo<?phpaaa?>bar<?phpzzz?>"
  101. tpl := New(template, "<?php", "?>")
  102. s := tpl.ExecuteString(map[string]interface{}{"zzz": "111", "aaa": "bbb"})
  103. result := "foobbbbar111"
  104. if s != result {
  105. t.Fatalf("unexpected template value %q. Expected %q", s, result)
  106. }
  107. }
  108. func TestEmptyValue(t *testing.T) {
  109. template := "foobar[foo]"
  110. tpl := New(template, "[", "]")
  111. s := tpl.ExecuteString(map[string]interface{}{"foo": "", "aaa": "bbb"})
  112. result := "foobar"
  113. if s != result {
  114. t.Fatalf("unexpected template value %q. Expected %q", s, result)
  115. }
  116. }
  117. func TestNoValue(t *testing.T) {
  118. template := "foobar[foo]x[aaa]"
  119. tpl := New(template, "[", "]")
  120. s := tpl.ExecuteString(map[string]interface{}{"aaa": "bbb"})
  121. result := "foobarxbbb"
  122. if s != result {
  123. t.Fatalf("unexpected template value %q. Expected %q", s, result)
  124. }
  125. }
  126. func TestNoEndDelimiter(t *testing.T) {
  127. template := "foobar[foo"
  128. _, err := NewTemplate(template, "[", "]")
  129. if err == nil {
  130. t.Fatalf("expected non-nil error. got nil")
  131. }
  132. expectPanic(t, func() { New(template, "[", "]") })
  133. }
  134. func TestUnsupportedValue(t *testing.T) {
  135. template := "foobar[foo]"
  136. tpl := New(template, "[", "]")
  137. expectPanic(t, func() {
  138. tpl.ExecuteString(map[string]interface{}{"foo": 123, "aaa": "bbb"})
  139. })
  140. }
  141. func TestMixedValues(t *testing.T) {
  142. template := "foo[foo]bar[bar]baz[baz]"
  143. tpl := New(template, "[", "]")
  144. s := tpl.ExecuteString(map[string]interface{}{
  145. "foo": "111",
  146. "bar": []byte("bbb"),
  147. "baz": TagFunc(func(w io.Writer, tag string) (int, error) { return w.Write([]byte(tag)) }),
  148. })
  149. result := "foo111barbbbbazbaz"
  150. if s != result {
  151. t.Fatalf("unexpected template value %q. Expected %q", s, result)
  152. }
  153. }
  154. func expectPanic(t *testing.T, f func()) {
  155. defer func() {
  156. if r := recover(); r == nil {
  157. t.Fatalf("missing panic")
  158. }
  159. }()
  160. f()
  161. }