mirror of
https://github.com/retailcrm/mg-transport-core.git
synced 2024-11-22 05:06:04 +03:00
better tests, ability to create only dynamic or only static renderer
This commit is contained in:
parent
17b69ea3a3
commit
6cab6169b1
@ -17,8 +17,23 @@ type Renderer struct {
|
||||
|
||||
// NewRenderer is a Renderer constructor
|
||||
func NewRenderer(funcMap template.FuncMap) Renderer {
|
||||
return newRendererWithMultitemplate(funcMap, multitemplate.NewRenderer())
|
||||
}
|
||||
|
||||
// NewStaticRenderer is a Renderer constructor with multitemplate.Render
|
||||
func NewStaticRenderer(funcMap template.FuncMap) Renderer {
|
||||
return newRendererWithMultitemplate(funcMap, multitemplate.New())
|
||||
}
|
||||
|
||||
// NewStaticRenderer is a Renderer constructor with multitemplate.DynamicRender
|
||||
func NewDynamicRenderer(funcMap template.FuncMap) Renderer {
|
||||
return newRendererWithMultitemplate(funcMap, multitemplate.NewDynamic())
|
||||
}
|
||||
|
||||
// newRendererWithMultitemplate initializes Renderer with provided multitemplate.Renderer instance
|
||||
func newRendererWithMultitemplate(funcMap template.FuncMap, renderer multitemplate.Renderer) Renderer {
|
||||
return Renderer{
|
||||
Renderer: multitemplate.NewRenderer(),
|
||||
Renderer: renderer,
|
||||
FuncMap: funcMap,
|
||||
alreadyAdded: map[string]*template.Template{},
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-contrib/multitemplate"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/stretchr/testify/suite"
|
||||
@ -20,12 +21,14 @@ var (
|
||||
|
||||
type TemplateTest struct {
|
||||
suite.Suite
|
||||
renderer Renderer
|
||||
static Renderer
|
||||
dynamic Renderer
|
||||
}
|
||||
|
||||
func (t *TemplateTest) SetupSuite() {
|
||||
t.initTestData()
|
||||
t.renderer = t.initRenderer()
|
||||
t.static = t.initStatic()
|
||||
t.dynamic = t.initDynamic()
|
||||
}
|
||||
|
||||
func (t *TemplateTest) initTestData() {
|
||||
@ -41,8 +44,20 @@ func (t *TemplateTest) initTestData() {
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TemplateTest) initRenderer() Renderer {
|
||||
return NewRenderer(template.FuncMap{
|
||||
func (t *TemplateTest) initStatic() Renderer {
|
||||
return NewStaticRenderer(template.FuncMap{
|
||||
"trans": func(data string) string {
|
||||
if data == "test" {
|
||||
return "ok"
|
||||
}
|
||||
|
||||
return "fail"
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (t *TemplateTest) initDynamic() Renderer {
|
||||
return NewDynamicRenderer(template.FuncMap{
|
||||
"trans": func(data string) string {
|
||||
if data == "test" {
|
||||
return "ok"
|
||||
@ -54,8 +69,10 @@ func (t *TemplateTest) initRenderer() Renderer {
|
||||
}
|
||||
|
||||
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()))
|
||||
tplStatic := t.static.Push("index", fmt.Sprintf(testTemplatesFile, 1), fmt.Sprintf(testTemplatesFile, 2))
|
||||
tplDynamic := t.dynamic.Push("index", fmt.Sprintf(testTemplatesFile, 1), fmt.Sprintf(testTemplatesFile, 2))
|
||||
assert.Equal(t.T(), 3, len(tplStatic.Templates()))
|
||||
assert.Equal(t.T(), 3, len(tplDynamic.Templates()))
|
||||
}
|
||||
|
||||
func (t *TemplateTest) Test_PushAlreadyExists() {
|
||||
@ -63,20 +80,49 @@ func (t *TemplateTest) Test_PushAlreadyExists() {
|
||||
assert.Nil(t.T(), recover())
|
||||
}()
|
||||
|
||||
tpl := t.renderer.Push("index", fmt.Sprintf(testTemplatesFile, 1), fmt.Sprintf(testTemplatesFile, 2))
|
||||
assert.Equal(t.T(), 3, len(tpl.Templates()))
|
||||
tplStatic := t.static.Push("index", fmt.Sprintf(testTemplatesFile, 1), fmt.Sprintf(testTemplatesFile, 2))
|
||||
tplDynamic := t.dynamic.Push("index", fmt.Sprintf(testTemplatesFile, 1), fmt.Sprintf(testTemplatesFile, 2))
|
||||
assert.Equal(t.T(), 3, len(tplStatic.Templates()))
|
||||
assert.Equal(t.T(), 3, len(tplDynamic.Templates()))
|
||||
}
|
||||
|
||||
func (t *TemplateTest) Test_PushNewInstance() {
|
||||
func (t *TemplateTest) Test_PushNewInstanceStatic() {
|
||||
defer func() {
|
||||
assert.Nil(t.T(), recover())
|
||||
}()
|
||||
|
||||
newInstance := t.initRenderer()
|
||||
newInstance := t.initStatic()
|
||||
tpl := newInstance.Push("index", fmt.Sprintf(testTemplatesFile, 1), fmt.Sprintf(testTemplatesFile, 2))
|
||||
assert.Equal(t.T(), 3, len(tpl.Templates()))
|
||||
}
|
||||
|
||||
func (t *TemplateTest) Test_PushNewInstanceDynamic() {
|
||||
defer func() {
|
||||
assert.Nil(t.T(), recover())
|
||||
}()
|
||||
|
||||
newInstance := t.initDynamic()
|
||||
tpl := newInstance.Push("index", fmt.Sprintf(testTemplatesFile, 1), fmt.Sprintf(testTemplatesFile, 2))
|
||||
assert.Equal(t.T(), 3, len(tpl.Templates()))
|
||||
}
|
||||
|
||||
func TestTemplate_NewRenderer(t *testing.T) {
|
||||
r := NewRenderer(template.FuncMap{})
|
||||
assert.NotNil(t, r)
|
||||
}
|
||||
|
||||
func TestTemplate_NewStaticRenderer(t *testing.T) {
|
||||
r := NewStaticRenderer(template.FuncMap{})
|
||||
assert.NotNil(t, r)
|
||||
assert.IsType(t, multitemplate.New(), r.Renderer)
|
||||
}
|
||||
|
||||
func TestTemplate_NewDynamicRenderer(t *testing.T) {
|
||||
r := NewDynamicRenderer(template.FuncMap{})
|
||||
assert.NotNil(t, r)
|
||||
assert.IsType(t, multitemplate.NewDynamic(), r.Renderer)
|
||||
}
|
||||
|
||||
func TestTemplate_Suite(t *testing.T) {
|
||||
suite.Run(t, new(TemplateTest))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user