github.com/bketelsen/buffalo@v0.9.5/render/html_test.go (about) 1 package render_test 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "os" 7 "strings" 8 "testing" 9 10 "github.com/gobuffalo/buffalo/render" 11 "github.com/gobuffalo/plush" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func Test_HTML(t *testing.T) { 16 r := require.New(t) 17 18 tmpFile, err := ioutil.TempFile("", "test") 19 r.NoError(err) 20 defer os.Remove(tmpFile.Name()) 21 22 _, err = tmpFile.Write([]byte("<%= name %>")) 23 r.NoError(err) 24 25 type ji func(...string) render.Renderer 26 t.Run("without a layout", func(st *testing.T) { 27 r := require.New(st) 28 29 j := render.New(render.Options{ 30 TemplateEngine: plush.BuffaloRenderer, 31 }).HTML 32 33 re := j(tmpFile.Name()) 34 r.Equal("text/html", re.ContentType()) 35 bb := &bytes.Buffer{} 36 err = re.Render(bb, map[string]interface{}{"name": "Mark"}) 37 r.NoError(err) 38 r.Equal("Mark", strings.TrimSpace(bb.String())) 39 }) 40 41 t.Run("with a layout", func(st *testing.T) { 42 r := require.New(st) 43 44 layout, err := ioutil.TempFile("", "test") 45 r.NoError(err) 46 defer os.Remove(layout.Name()) 47 48 _, err = layout.Write([]byte("<body><%= yield %></body>")) 49 r.NoError(err) 50 51 re := render.New(render.Options{ 52 HTMLLayout: layout.Name(), 53 TemplateEngine: plush.BuffaloRenderer, 54 }) 55 56 st.Run("using just the HTMLLayout", func(sst *testing.T) { 57 r := require.New(sst) 58 h := re.HTML(tmpFile.Name()) 59 60 r.Equal("text/html", h.ContentType()) 61 bb := &bytes.Buffer{} 62 err = h.Render(bb, map[string]interface{}{"name": "Mark"}) 63 r.NoError(err) 64 r.Equal("<body>Mark</body>", strings.TrimSpace(bb.String())) 65 }) 66 67 st.Run("overriding the HTMLLayout", func(sst *testing.T) { 68 r := require.New(sst) 69 nlayout, err := ioutil.TempFile("", "test-layout2") 70 r.NoError(err) 71 defer os.Remove(nlayout.Name()) 72 73 _, err = nlayout.Write([]byte("<html><%= yield %></html>")) 74 r.NoError(err) 75 h := re.HTML(tmpFile.Name(), nlayout.Name()) 76 77 r.Equal("text/html", h.ContentType()) 78 bb := &bytes.Buffer{} 79 err = h.Render(bb, map[string]interface{}{"name": "Mark"}) 80 r.NoError(err) 81 r.Equal("<html>Mark</html>", strings.TrimSpace(bb.String())) 82 }) 83 84 }) 85 }