templates_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package templates_test
  2. import (
  3. "bytes"
  4. "fmt"
  5. "os"
  6. "testing"
  7. "testing/iotest"
  8. "cdop.pt/go/free/platepipe/templates"
  9. . "cdop.pt/go/open/assertive"
  10. )
  11. func TestHTMLTemplateFiles(t *testing.T) {
  12. t.Run("non existent html template", func(t *testing.T) {
  13. _, _, err := templates.FromFile("non-existent-file.html")
  14. Need(t, err != nil)
  15. Want(t, err.Error() ==
  16. "open non-existent-file.html: no such file or directory")
  17. })
  18. t.Run("bad html template", func(t *testing.T) {
  19. f := mkTestFile(t, "bad-html-template*.html", "<p>malformed {{")
  20. defer os.Remove(f)
  21. tpl, data, err := templates.FromFile(f)
  22. Need(t, err != nil)
  23. errmsg := err.Error()
  24. Want(t, errmsg[:10] == "template: ")
  25. Want(t, errmsg[len(errmsg)-15:] == "unclosed action")
  26. Want(t, len(data) == 0)
  27. Want(t, tpl == nil)
  28. })
  29. t.Run("good html template", func(t *testing.T) {
  30. f := mkTestFile(t, "good-html-template-*.html",
  31. "key = 'value'\n\n<p>text {{ .key }}</p>")
  32. defer os.Remove(f)
  33. tpl, data, err := templates.FromFile(f)
  34. Need(t, err == nil)
  35. Want(t, len(data) == 1)
  36. Want(t, tpl != nil)
  37. })
  38. }
  39. func TestHTMLTemplateStreams(t *testing.T) {
  40. t.Run("html stream error", func(t *testing.T) {
  41. msg := "error reading html template"
  42. r := iotest.ErrReader(fmt.Errorf(msg))
  43. _, _, err := templates.HTMLTemplateFromStream(r)
  44. Need(t, err != nil)
  45. Want(t, err.Error() == msg)
  46. })
  47. t.Run("html stream template error", func(t *testing.T) {
  48. r := bytes.NewBuffer([]byte("<p>malformed {{"))
  49. tpl, data, err := templates.HTMLTemplateFromStream(r)
  50. Need(t, err != nil)
  51. errmsg := err.Error()
  52. Want(t, errmsg[:10] == "template: ")
  53. Want(t, errmsg[len(errmsg)-15:] == "unclosed action")
  54. Want(t, len(data) == 0)
  55. Want(t, tpl == nil)
  56. })
  57. t.Run("html stream success", func(t *testing.T) {
  58. r := bytes.NewBuffer([]byte("key = 'value'\n\n<p>text {{ .key }}</p>"))
  59. tpl, data, err := templates.HTMLTemplateFromStream(r)
  60. Need(t, err == nil)
  61. Want(t, len(data) == 1)
  62. Want(t, tpl != nil)
  63. })
  64. }
  65. func TestTextTemplateFiles(t *testing.T) {
  66. t.Run("non existent text template", func(t *testing.T) {
  67. _, _, err := templates.FromFile("non-existent-file.txt")
  68. Need(t, err != nil)
  69. Want(t, err.Error() ==
  70. "open non-existent-file.txt: no such file or directory")
  71. })
  72. t.Run("bad text template", func(t *testing.T) {
  73. f := mkTestFile(t, "bad-text-template*.txt", "malformed {{")
  74. defer os.Remove(f)
  75. tpl, data, err := templates.FromFile(f)
  76. Need(t, err != nil)
  77. errmsg := err.Error()
  78. Want(t, errmsg[:10] == "template: ")
  79. Want(t, errmsg[len(errmsg)-15:] == "unclosed action")
  80. Want(t, len(data) == 0)
  81. Want(t, tpl == nil)
  82. })
  83. t.Run("good text template", func(t *testing.T) {
  84. f := mkTestFile(t, "good-text-template-*.txt", "key = 'value'\n\ntext {{ .key }}")
  85. defer os.Remove(f)
  86. tpl, data, err := templates.FromFile(f)
  87. Need(t, err == nil)
  88. Want(t, len(data) == 1)
  89. Want(t, tpl != nil)
  90. })
  91. }
  92. func TestTextTemplateStreams(t *testing.T) {
  93. t.Run("text stream error", func(t *testing.T) {
  94. msg := "error reading text template"
  95. r := iotest.ErrReader(fmt.Errorf(msg))
  96. _, _, err := templates.TextTemplateFromStream(r)
  97. Need(t, err != nil)
  98. Want(t, err.Error() == msg)
  99. })
  100. t.Run("text stream template error", func(t *testing.T) {
  101. r := bytes.NewBuffer([]byte("malformed {{"))
  102. tpl, data, err := templates.TextTemplateFromStream(r)
  103. Need(t, err != nil)
  104. errmsg := err.Error()
  105. Want(t, errmsg[:10] == "template: ")
  106. Want(t, errmsg[len(errmsg)-15:] == "unclosed action")
  107. Want(t, len(data) == 0)
  108. Want(t, tpl == nil)
  109. })
  110. t.Run("text stream success", func(t *testing.T) {
  111. r := bytes.NewBuffer([]byte("key = 'value'\n\ntext {{ .key }}"))
  112. tpl, data, err := templates.TextTemplateFromStream(r)
  113. Need(t, err == nil)
  114. Want(t, len(data) == 1)
  115. Want(t, tpl != nil)
  116. })
  117. }
  118. func TestTemplateApplication(t *testing.T) {
  119. t.Run("apply text template", func(t *testing.T) {
  120. f := mkTestFile(t, "template-*.txt",
  121. "tkey0 = 'v0'\ntkey1 = 1\n\nhere are all the keys:\n\n"+
  122. "template key 0 is {{.tkey0}}\n"+
  123. "template key 1 is {{.tkey1}}\n"+
  124. "var key 0 is {{.vkey0}}\n"+
  125. "var key 1 is {{.vkey1}}\n")
  126. defer os.Remove(f)
  127. tpl, data, err := templates.FromFile(f)
  128. Need(t, err == nil)
  129. Need(t, tpl != nil)
  130. data["vkey0"] = 2.43
  131. data["vkey1"] = []int{1, 2, 3}
  132. buf := bytes.NewBuffer([]byte{})
  133. err = tpl.Apply(buf, data)
  134. Need(t, err == nil)
  135. Want(t, buf.String() == "here are all the keys:\n\n"+
  136. "template key 0 is v0\n"+
  137. "template key 1 is 1\n"+
  138. "var key 0 is 2.43\n"+
  139. "var key 1 is [1 2 3]\n")
  140. })
  141. t.Run("apply html template", func(t *testing.T) {
  142. f := mkTestFile(t, "template-*.html",
  143. "tkey0 = 'v0'\ntkey1 = 1\n\n<p>here are all the keys:<ul>"+
  144. "<li>template key 0 is {{.tkey0}}</li>"+
  145. "<li>template key 1 is {{.tkey1}}</li>"+
  146. "<li>var key 0 is {{.vkey0}}</li>"+
  147. "<li>var key 1 is {{.vkey1}}</li>"+
  148. "</ul></p>")
  149. defer os.Remove(f)
  150. tpl, data, err := templates.FromFile(f)
  151. Need(t, err == nil)
  152. Need(t, tpl != nil)
  153. data["vkey0"] = 2.43
  154. data["vkey1"] = []int{1, 2, 3}
  155. buf := bytes.NewBuffer([]byte{})
  156. err = tpl.Apply(buf, data)
  157. Need(t, err == nil)
  158. Want(t, buf.String() == "<p>here are all the keys:"+
  159. "<ul><li>template key 0 is v0</li><li>template key 1 is 1</li>"+
  160. "<li>var key 0 is 2.43</li><li>var key 1 is [1 2 3]</li></ul></p>")
  161. })
  162. }
  163. func mkTestFile(t *testing.T, pattern, content string) string {
  164. f, err := os.CreateTemp("", pattern)
  165. Need(t, err == nil)
  166. name := f.Name()
  167. _, err = f.WriteString(content)
  168. Need(t, err == nil)
  169. err = f.Sync()
  170. Need(t, err == nil)
  171. err = f.Close()
  172. Need(t, err == nil)
  173. return name
  174. }