mg-transport-core/core/template_test.go

57 lines
1.3 KiB
Go
Raw Normal View History

2019-09-19 14:16:52 +03:00
package core
import (
"fmt"
"html/template"
"io/ioutil"
"os"
"path"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
var (
testTemplatesDir = path.Join(os.TempDir(), "templates_test_dir")
testTemplatesFile = path.Join(testTemplatesDir, "tpl%d.html")
)
type TemplateTest struct {
suite.Suite
renderer Renderer
}
2019-10-18 13:18:36 +03:00
func (t *TemplateTest) SetupSuite() {
2019-09-19 14:16:52 +03:00
if _, err := os.Stat(testTemplatesDir); err != nil && os.IsNotExist(err) {
err := os.Mkdir(testTemplatesDir, os.ModePerm)
require.Nil(t.T(), err)
data1 := []byte(`data {{template "body" .}}`)
data2 := []byte(`{{define "body"}}test {{"test" | trans}}{{end}}`)
err1 := ioutil.WriteFile(fmt.Sprintf(testTemplatesFile, 1), data1, os.ModePerm)
err2 := ioutil.WriteFile(fmt.Sprintf(testTemplatesFile, 2), data2, os.ModePerm)
require.Nil(t.T(), err1)
require.Nil(t.T(), err2)
}
t.renderer = NewRenderer(template.FuncMap{
"trans": func(data string) string {
if data == "test" {
return "ok"
}
return "fail"
},
})
}
func (t *TemplateTest) Test_Push() {
tpl := t.renderer.Push("index", fmt.Sprintf(testTemplatesFile, 1), fmt.Sprintf(testTemplatesFile, 2))
assert.Equal(t.T(), 3, len(tpl.Templates()))
}
func TestTemplate_Suite(t *testing.T) {
suite.Run(t, new(TemplateTest))
}