diff --git a/core/template.go b/core/template.go index 986ed39..6e43619 100644 --- a/core/template.go +++ b/core/template.go @@ -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{}, } diff --git a/core/template_test.go b/core/template_test.go index c89fd7c..21ab439 100644 --- a/core/template_test.go +++ b/core/template_test.go @@ -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)) }