1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-02 14:37:50 +01:00
gojekyll/pages/page_test.go

73 lines
1.8 KiB
Go
Raw Normal View History

2017-07-04 23:36:06 +02:00
package pages
import (
"bytes"
2017-08-22 16:47:34 +02:00
"io/ioutil"
2017-08-18 16:55:34 +02:00
"os"
2017-08-22 16:47:34 +02:00
"path/filepath"
2017-07-04 23:36:06 +02:00
"testing"
2017-07-07 01:31:36 +02:00
"github.com/osteele/gojekyll/config"
2017-08-22 16:47:34 +02:00
"github.com/osteele/gojekyll/utils"
2017-07-04 23:36:06 +02:00
"github.com/stretchr/testify/require"
)
2017-08-18 16:55:34 +02:00
func TestPage_TemplateContext(t *testing.T) {
s := siteFake{t, config.Default()}
f := file{site: s}
p := page{file: f}
os.Setenv("JEKYLL_ENV", "") // nolint: errcheck
tc := p.TemplateContext()
j := tc["jekyll"].(map[string]string)
require.Equal(t, "development", j["environment"])
require.Contains(t, j["version"], "gojekyll")
os.Setenv("JEKYLL_ENV", "production") // nolint: errcheck
tc = p.TemplateContext()
j = tc["jekyll"].(map[string]string)
require.Equal(t, "production", j["environment"])
}
2017-07-13 18:28:02 +02:00
func TestPage_Categories(t *testing.T) {
s := siteFake{t, config.Default()}
2017-09-03 18:21:55 +02:00
fm := FrontMatter{"categories": "b a"}
f := file{site: s, fm: fm}
2017-07-13 18:28:02 +02:00
p := page{file: f}
require.Equal(t, []string{"a", "b"}, p.Categories())
}
2017-07-04 23:36:06 +02:00
2017-07-13 18:28:02 +02:00
func TestPage_Write(t *testing.T) {
2017-08-22 16:47:34 +02:00
t.Run("rendering", func(t *testing.T) {
p := requirePageFromFile(t, "page_with_layout.md")
buf := new(bytes.Buffer)
require.NoError(t, p.Write(buf))
require.Contains(t, buf.String(), "page with layout")
})
t.Run("rendering error", func(t *testing.T) {
p := requirePageFromFile(t, "liquid_error.md")
err := p.Write(ioutil.Discard)
require.NotNil(t, err)
require.Contains(t, err.Error(), "render error")
pe, ok := err.(utils.PathError)
require.True(t, ok)
require.Equal(t, "testdata/liquid_error.md", pe.Path())
})
}
func fakePageFromFile(t *testing.T, file string) (Document, error) {
return NewFile(
siteFake{t, config.Default()},
filepath.Join("testdata", file),
file,
2017-09-03 18:21:55 +02:00
FrontMatter{},
2017-08-22 16:47:34 +02:00
)
}
func requirePageFromFile(t *testing.T, file string) Document {
p, err := fakePageFromFile(t, file)
2017-07-04 23:36:06 +02:00
require.NoError(t, err)
require.NotNil(t, p)
2017-08-22 16:47:34 +02:00
return p
2017-07-04 23:36:06 +02:00
}