github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/render/template_test.go (about) 1 package render 2 3 import ( 4 "bytes" 5 "strings" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 ) 10 11 func Test_Template(t *testing.T) { 12 r := require.New(t) 13 14 e := NewEngine() 15 box := e.TemplatesBox 16 r.NoError(box.AddString(htmlTemplate, `<%= name %>`)) 17 18 re := e.Template("foo/bar", htmlTemplate) 19 r.Equal("foo/bar", re.ContentType()) 20 21 bb := &bytes.Buffer{} 22 r.NoError(re.Render(bb, Data{"name": "Mark"})) 23 } 24 25 func Test_AssetPath(t *testing.T) { 26 r := require.New(t) 27 28 e := NewEngine() 29 30 abox := e.AssetsBox 31 r.NoError(abox.AddString("manifest.json", `{ 32 "application.css": "application.aabbc123.css" 33 }`)) 34 35 cases := map[string]string{ 36 "something.txt": "/assets/something.txt", 37 "images/something.png": "/assets/images/something.png", 38 "/images/something.png": "/assets/images/something.png", 39 "application.css": "/assets/application.aabbc123.css", 40 } 41 42 for original, expected := range cases { 43 tbox := e.TemplatesBox 44 r.NoError(tbox.AddString(htmlTemplate, "<%= assetPath(\""+original+"\") %>")) 45 46 re := e.Template("text/html; charset=utf-8", htmlTemplate) 47 48 bb := &bytes.Buffer{} 49 r.NoError(re.Render(bb, Data{})) 50 r.Equal(expected, strings.TrimSpace(bb.String())) 51 } 52 53 } 54 55 func Test_AssetPathNoManifest(t *testing.T) { 56 r := require.New(t) 57 58 e := NewEngine() 59 60 cases := map[string]string{ 61 "something.txt": "/assets/something.txt", 62 } 63 64 for original, expected := range cases { 65 tbox := e.TemplatesBox 66 r.NoError(tbox.AddString(htmlTemplate, "<%= assetPath(\""+original+"\") %>")) 67 68 re := e.Template("text/html; charset=utf-8", htmlTemplate) 69 70 bb := &bytes.Buffer{} 71 r.NoError(re.Render(bb, Data{})) 72 r.Equal(expected, strings.TrimSpace(bb.String())) 73 } 74 } 75 76 func Test_AssetPathNoManifestCorrupt(t *testing.T) { 77 r := require.New(t) 78 79 e := NewEngine() 80 81 abox := e.AssetsBox 82 r.NoError(abox.AddString("manifest.json", "//shdnn Corrupt!")) 83 84 cases := map[string]string{ 85 "something.txt": "manifest.json is not correct", 86 "other.txt": "manifest.json is not correct", 87 } 88 89 for original, expected := range cases { 90 tbox := e.TemplatesBox 91 r.NoError(tbox.AddString(htmlTemplate, "<%= assetPath(\""+original+"\") %>")) 92 93 re := e.Template("text/html; charset=utf-8", htmlTemplate) 94 95 bb := &bytes.Buffer{} 96 r.Error(re.Render(bb, Data{})) 97 r.NotEqual(expected, strings.TrimSpace(bb.String())) 98 } 99 }