dati

A Go library/binary to parse & execute data against template langauges.
git clone git://src.gearsix.net/dati
Log | Files | Refs | Atom | README | LICENSE

commit 254722853f24e3f347e1e78adca03ac5f65aabf6
parent 1c4e682fbe7551798a246541dfd65a7713b7b318
Author: gearsix <gearsix@tuta.io>
Date:   Tue,  9 Mar 2021 20:26:20 +0000

added TestExecuteTemplate

Diffstat:
Msrc/template_test.go | 48++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 40 insertions(+), 8 deletions(-)

diff --git a/src/template_test.go b/src/template_test.go @@ -1,18 +1,20 @@ package main import ( + "bytes" "testing" + "strings" ) -const tmplRootGood = "hello {{ template \"partial\" . }}" -const tmplPartialGood = "{{ .language }}" -const tmplRootBad = "hello {{{ template \"partial\" . }}" -const tmplPartialBad = "{{{ .language }}" +const tmplRootGood = "{{ .example1 }} {{ template \"tmplPartialGood\" . }}" +const tmplPartialGood = "{{range .data}} .example2 {{ end }}" +const tmplRootBad = "{{ example1 }} {{{ template \"partial\" . }}" +const tmplPartialBad = "{{{ .example }}" -const hmplRootGood = "hello {{ template \"partial\" . }}" -const hmplPartialGood = "<b>{{ .language }}</b>" -const hmplRootBad = "hello {{{ template \"partial\" . }}" -const hmplPartialBad = "<b>{{{ .language }}</b>" +const hmplRootGood = "<!DOCTYPE html><html>{{ .example1 }} {{ template \"hmplPartialGood\" . }}</html>" +const hmplPartialGood = "{{range .data}}<b>.example2</b>{{ end }}" +const hmplRootBad = "{{ example1 }} {{{ template \"partial\" . }}" +const hmplPartialBad = "<b>{{{ .example2 }}</b>" func TestLoadTemplateFile(t *testing.T) { var e error @@ -75,3 +77,33 @@ func TestLoadTemplateFile(t *testing.T) { } } } + +func TestExecuteTemplate(t *testing.T) { + var e error + var sd, gd, d Data + var tmpl, hmpl Template + var results bytes.Buffer + + if gd, e = LoadData("json", strings.NewReader(goodJson1)); e != nil { + t.Skip("setup failure:", e) + } + if d, e = LoadData("json", strings.NewReader(goodJson2)); e != nil { + t.Skip("setup failure:", e) + } + data := make([]Data, 1) + data = append(data, d) + sd = GenerateSuperData("", data, gd) + if tmpl, e = LoadTemplateFile(tmplRootGood, tmplPartialGood); e != nil { + t.Skip("setup failure:", e) + } + if hmpl, e = LoadTemplateFile(hmplRootGood, hmplPartialGood); e != nil { + t.Skip("setup failure:", e) + } + + if results, e = ExecuteTemplate(tmpl, sd); e != nil { + t.Error(e) + } + if results, e = ExecuteTemplate(hmpl, sd); e != nil { + t.Error(e) + } +}