commit 71f0d3dba246487873e445ec38cabdc53fe87c13
parent 48e975183de8afbe9e38d825bc21a07c65f6d122
Author: gearsix <gearsix@tuta.io>
Date: Fri, 1 Oct 2021 22:53:39 +0100
added test for LoadTemplateStringTmpl
Diffstat:
2 files changed, 43 insertions(+), 1 deletion(-)
diff --git a/template.go b/template.go
@@ -229,7 +229,7 @@ func LoadTemplateStringTmpl(name string, root string, partials ...string) (t Tem
if e == nil {
template := tmpl.Must(tmpl.New(name).Parse(root))
for i, p := range partials {
- if template, e = template.New(name + "-partial" + strconv.Itoa(i)).Parse(p); e != nil {
+ if _, e = template.New(name + "-partial" + strconv.Itoa(i)).Parse(p); e != nil {
break
}
}
diff --git a/template_test.go b/template_test.go
@@ -73,6 +73,37 @@ const mstResult = "0 00"
const mstRootBad = "{{> badPartial.mst}}{{#doesnt-exist}}{{/exit}}"
const mstPartialBad = "p{{$}{{ > noexist}}"
+func validateTemplate(t *testing.T, template Template, templateType string, rootName string, partialNames ...string) {
+ types := map[string]string{
+ "tmpl": "*template.Template",
+ "gotmpl": "*template.Template",
+ "hmpl": "*template.Template",
+ "gohmpl": "*template.Template",
+ "mst": "*mustache.Template",
+ "mustache": "*mustache.Template",
+ }
+
+ if reflect.TypeOf(template.Template).String() != types[templateType] {
+ t.Fatal("invalid template loaded")
+ }
+
+ if types[templateType] == "*template.Template" {
+ var rv []reflect.Value
+ for _, p := range partialNames {
+ rv := reflect.ValueOf(template.Template).MethodByName("Lookup").Call([]reflect.Value{reflect.ValueOf(p)})
+ if rv[0].IsNil() {
+ t.Fatalf("missing defined template '%s'", p)
+ rv = reflect.ValueOf(template.Template).MethodByName("DefinedTemplates").Call([]reflect.Value{})
+ t.Log(rv)
+ }
+ }
+ rv = reflect.ValueOf(template.Template).MethodByName("Name").Call([]reflect.Value{})
+ if rv[0].String() != rootName {
+ t.Fatalf("invalid template name: %s does not match %s", rv[0].String(), rootName)
+ }
+ }
+}
+
func validateTemplateFile(t *testing.T, template Template, root string, partials ...string) {
types := map[string]string{
"tmpl": "*template.Template",
@@ -162,6 +193,17 @@ func TestLoadTemplateFile(t *testing.T) {
}
}
+func TestLoadTemplateStringTmpl(t *testing.T) {
+ t.Parallel()
+
+ name := "tmplGood"
+ tmpl, err := LoadTemplateStringTmpl(name, tmplRootGood, tmplPartialGood)
+ if err != nil {
+ t.Fatal(err)
+ }
+ validateTemplate(t, tmpl, "tmpl", name, name + "-partial0")
+}
+
func validateExecute(t *testing.T, results string, expect string, e error) {
if e != nil {
t.Fatal(e)