Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Merge branch 'development' into test-individual-methods
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates authored Nov 27, 2017
2 parents c54fe1d + 5e03e0a commit ad053c7
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 38 deletions.
14 changes: 9 additions & 5 deletions render/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/gobuffalo/buffalo/render"
"github.com/gobuffalo/packr"
"github.com/stretchr/testify/require"
)

Expand All @@ -29,9 +30,11 @@ func Test_HTML(t *testing.T) {
t.Run("without a layout", func(st *testing.T) {
r := require.New(st)

j := render.New(render.Options{}).HTML
j := render.New(render.Options{
TemplatesBox: packr.NewBox(tmpDir),
}).HTML

re := j(tmpFile.Name())
re := j(filepath.Base(tmpFile.Name()))
r.Equal("text/html", re.ContentType())
bb := &bytes.Buffer{}
err = re.Render(bb, map[string]interface{}{"name": "Mark"})
Expand All @@ -50,12 +53,13 @@ func Test_HTML(t *testing.T) {
r.NoError(err)

re := render.New(render.Options{
HTMLLayout: layout.Name(),
TemplatesBox: packr.NewBox(tmpDir),
HTMLLayout: filepath.Base(layout.Name()),
})

st.Run("using just the HTMLLayout", func(sst *testing.T) {
r := require.New(sst)
h := re.HTML(tmpFile.Name())
h := re.HTML(filepath.Base(tmpFile.Name()))

r.Equal("text/html", h.ContentType())
bb := &bytes.Buffer{}
Expand All @@ -72,7 +76,7 @@ func Test_HTML(t *testing.T) {

_, err = nlayout.Write([]byte("<html><%= yield %></html>"))
r.NoError(err)
h := re.HTML(tmpFile.Name(), nlayout.Name())
h := re.HTML(filepath.Base(tmpFile.Name()), filepath.Base(nlayout.Name()))

r.Equal("text/html", h.ContentType())
bb := &bytes.Buffer{}
Expand Down
13 changes: 8 additions & 5 deletions render/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ func Test_JavaScript(t *testing.T) {
t.Run("without a layout", func(st *testing.T) {
r := require.New(st)

j := render.New(render.Options{}).JavaScript
j := render.New(render.Options{
TemplatesBox: packr.NewBox(tmpDir),
}).JavaScript

re := j(tmpFile.Name())
re := j(filepath.Base(tmpFile.Name()))
r.Equal("application/javascript", re.ContentType())
bb := &bytes.Buffer{}
err = re.Render(bb, map[string]interface{}{"name": "Mark"})
Expand All @@ -50,12 +52,13 @@ func Test_JavaScript(t *testing.T) {
r.NoError(err)

re := render.New(render.Options{
JavaScriptLayout: layout.Name(),
JavaScriptLayout: filepath.Base(layout.Name()),
TemplatesBox: packr.NewBox(tmpDir),
})

st.Run("using just the JavaScriptLayout", func(sst *testing.T) {
r := require.New(sst)
h := re.JavaScript(tmpFile.Name())
h := re.JavaScript(filepath.Base(tmpFile.Name()))

r.Equal("application/javascript", h.ContentType())
bb := &bytes.Buffer{}
Expand All @@ -71,7 +74,7 @@ func Test_JavaScript(t *testing.T) {

_, err = nlayout.Write([]byte("<html><%= yield %></html>"))
r.NoError(err)
h := re.JavaScript(tmpFile.Name(), nlayout.Name())
h := re.JavaScript(filepath.Base(tmpFile.Name()), filepath.Base(nlayout.Name()))

r.Equal("application/javascript", h.ContentType())
bb := &bytes.Buffer{}
Expand Down
14 changes: 9 additions & 5 deletions render/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/gobuffalo/buffalo/render"
"github.com/gobuffalo/packr"
"github.com/stretchr/testify/require"
)

Expand All @@ -30,11 +31,13 @@ func Test_Markdown(t *testing.T) {
r := require.New(st)

table := []ji{
render.New(render.Options{}).HTML,
render.New(render.Options{
TemplatesBox: packr.NewBox(tmpDir),
}).HTML,
}

for _, j := range table {
re := j(tmpFile.Name())
re := j(filepath.Base(tmpFile.Name()))
r.Equal("text/html", re.ContentType())
bb := &bytes.Buffer{}
err = re.Render(bb, map[string]interface{}{"name": "Mark"})
Expand All @@ -46,16 +49,17 @@ func Test_Markdown(t *testing.T) {
t.Run("with a layout", func(st *testing.T) {
r := require.New(st)

layout, err := os.Create(filepath.Join("", "test.html"))
layout, err := os.Create(filepath.Join(tmpDir, "test.html"))
r.NoError(err)
defer os.Remove(layout.Name())

_, err = layout.Write([]byte("<body><%= yield %></body>"))
r.NoError(err)

re := render.New(render.Options{
HTMLLayout: layout.Name(),
}).HTML(tmpFile.Name())
HTMLLayout: filepath.Base(layout.Name()),
TemplatesBox: packr.NewBox(tmpDir),
}).HTML(filepath.Base(tmpFile.Name()))

r.Equal("text/html", re.ContentType())
bb := &bytes.Buffer{}
Expand Down
15 changes: 12 additions & 3 deletions render/plain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@ import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/gobuffalo/buffalo/render"
"github.com/gobuffalo/packr"
"github.com/stretchr/testify/require"
)

func Test_Plain(t *testing.T) {
r := require.New(t)

tmpFile, err := ioutil.TempFile("", "test")
tDir, err := ioutil.TempDir("", "templates")
if err != nil {
r.Fail("Could not set the templates dir")
}

tmpFile, err := os.Create(filepath.Join(tDir, "test"))
r.NoError(err)
defer os.Remove(tmpFile.Name())

Expand All @@ -22,9 +29,11 @@ func Test_Plain(t *testing.T) {

type ji func(...string) render.Renderer

j := render.New(render.Options{}).Plain
j := render.New(render.Options{
TemplatesBox: packr.NewBox(tDir),
}).Plain

re := j(tmpFile.Name())
re := j(filepath.Base(tmpFile.Name()))
r.Equal("text/plain; charset=utf-8", re.ContentType())
var examples = []string{"Mark", "Jém"}
for _, example := range examples {
Expand Down
66 changes: 46 additions & 20 deletions render/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import (
func Test_Template(t *testing.T) {
r := require.New(t)

tmpFile, err := ioutil.TempFile("", "test")
tPath, err := ioutil.TempDir("", "")
r.NoError(err)
defer os.Remove(tPath)

tmpFile, err := os.Create(filepath.Join(tPath, "test"))
r.NoError(err)
defer os.Remove(tmpFile.Name())

Expand All @@ -26,11 +30,13 @@ func Test_Template(t *testing.T) {
type ji func(string, ...string) render.Renderer

table := []ji{
render.New(render.Options{}).Template,
render.New(render.Options{
TemplatesBox: packr.NewBox(tPath),
}).Template,
}

for _, j := range table {
re := j("foo/bar", tmpFile.Name())
re := j("foo/bar", filepath.Base(tmpFile.Name()))
r.Equal("foo/bar", re.ContentType())
bb := &bytes.Buffer{}
err = re.Render(bb, render.Data{"name": "Mark"})
Expand Down Expand Up @@ -114,28 +120,34 @@ func Test_AssetPath(t *testing.T) {
"application.css": "/assets/application.aabbc123.css",
}

tdir, err := ioutil.TempDir("", "test")
tDir, err := ioutil.TempDir("", "templates")
if err != nil {
r.Fail("Could not set the Temp dir")
r.Fail("Could not set the templates dir")
}

aDir, err := ioutil.TempDir("", "assets")
if err != nil {
r.Fail("Could not set the assets dir")
}

re := render.New(render.Options{
AssetsBox: packr.NewBox(tdir),
TemplatesBox: packr.NewBox(tDir),
AssetsBox: packr.NewBox(aDir),
}).Template

ioutil.WriteFile(filepath.Join(tdir, "manifest.json"), []byte(`{
ioutil.WriteFile(filepath.Join(aDir, "manifest.json"), []byte(`{
"application.css": "application.aabbc123.css"
}`), 0644)

for original, expected := range cases {

tmpFile, err := os.Create(filepath.Join(tdir, "test.html"))
tmpFile, err := os.Create(filepath.Join(tDir, "test.html"))
r.NoError(err)

_, err = tmpFile.Write([]byte("<%= assetPath(\"" + original + "\") %>"))
r.NoError(err)

result := re("text/html", tmpFile.Name())
result := re("text/html", filepath.Base(tmpFile.Name()))

bb := &bytes.Buffer{}
err = result.Render(bb, render.Data{})
Expand All @@ -153,24 +165,30 @@ func Test_AssetPathNoManifest(t *testing.T) {
"something.txt": "/assets/something.txt",
}

tdir, err := ioutil.TempDir("", "test")
tDir, err := ioutil.TempDir("", "templates")
if err != nil {
r.Fail("Could not set the templates dir")
}

aDir, err := ioutil.TempDir("", "assets")
if err != nil {
r.Fail("Could not set the Temp dir")
r.Fail("Could not set the assets dir")
}

re := render.New(render.Options{
AssetsBox: packr.NewBox(tdir),
TemplatesBox: packr.NewBox(tDir),
AssetsBox: packr.NewBox(aDir),
}).Template

for original, expected := range cases {

tmpFile, err := os.Create(filepath.Join(tdir, "test.html"))
tmpFile, err := os.Create(filepath.Join(tDir, "test.html"))
r.NoError(err)

_, err = tmpFile.Write([]byte("<%= assetPath(\"" + original + "\") %>"))
r.NoError(err)

result := re("text/html", tmpFile.Name())
result := re("text/html", filepath.Base(tmpFile.Name()))

bb := &bytes.Buffer{}
err = result.Render(bb, render.Data{})
Expand All @@ -188,24 +206,32 @@ func Test_AssetPathManifestCorrupt(t *testing.T) {
"other.txt": "manifest.json is not correct",
}

tdir, err := ioutil.TempDir("", "test")
r.NoError(err)
tDir, err := ioutil.TempDir("", "templates")
if err != nil {
r.Fail("Could not set the templates dir")
}

aDir, err := ioutil.TempDir("", "assets")
if err != nil {
r.Fail("Could not set the assets dir")
}

ioutil.WriteFile(filepath.Join(tdir, "manifest.json"), []byte(`//shdnn Corrupt!`), 0644)
ioutil.WriteFile(filepath.Join(aDir, "manifest.json"), []byte(`//shdnn Corrupt!`), 0644)

re := render.New(render.Options{
AssetsBox: packr.NewBox(tdir),
TemplatesBox: packr.NewBox(tDir),
AssetsBox: packr.NewBox(aDir),
}).Template

for original, expected := range cases {

tmpFile, err := os.Create(filepath.Join(tdir, "test.html"))
tmpFile, err := os.Create(filepath.Join(tDir, "test.html"))
r.NoError(err)

_, err = tmpFile.Write([]byte("<%= assetPath(\"" + original + "\") %>"))
r.NoError(err)

result := re("text/html", tmpFile.Name())
result := re("text/html", filepath.Base(tmpFile.Name()))

bb := &bytes.Buffer{}
err = result.Render(bb, render.Data{})
Expand Down

0 comments on commit ad053c7

Please sign in to comment.