templates: Add tests for funcInclude and funcImport (#4357)

* Update tplcontext.go

Add {{ render "/path/to/file.ext" $data }} via funcRender

* Update tplcontext.go

* Refactor funcInclude, add funcImport to enable {{block}} and {{template}}

* Fix funcImport return of nil showing up in html

* Update godocs for  and

* Add tests for funcInclude

* Add tests for funcImport

* os.RemoveAll -> os.Remove for TestFuncInclude and TestFuncImport
This commit is contained in:
Tim Culverhouse 2021-09-20 13:29:37 -05:00 committed by GitHub
parent 0a5f7a677f
commit 16f752125f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -91,6 +91,140 @@ func TestCookie(t *testing.T) {
}
}
func TestImport(t *testing.T) {
for i, test := range []struct {
fileContent string
fileName string
shouldErr bool
expect string
}{
{
// file exists, template is defined
fileContent: `{{ define "imported" }}text{{end}}`,
fileName: "file1",
shouldErr: false,
expect: `"imported"`,
},
{
// file does not exit
fileContent: "",
fileName: "",
shouldErr: true,
},
} {
context := getContextOrFail(t)
var absFilePath string
// create files for test case
if test.fileName != "" {
absFilePath := filepath.Join(fmt.Sprintf("%s", context.Root), test.fileName)
if err := ioutil.WriteFile(absFilePath, []byte(test.fileContent), os.ModePerm); err != nil {
os.Remove(absFilePath)
t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
}
}
// perform test
context.NewTemplate("parent")
actual, err := context.funcImport(test.fileName)
templateWasDefined := strings.Contains(context.tpl.DefinedTemplates(), test.expect)
if err != nil {
if !test.shouldErr {
t.Errorf("Test %d: Expected no error, got: '%s'", i, err)
}
} else if test.shouldErr {
t.Errorf("Test %d: Expected error but had none", i)
} else if !templateWasDefined && actual != "" {
// template should be defined, return value should be an empty string
t.Errorf("Test %d: Expected template %s to be define but got %s", i, test.expect, context.tpl.DefinedTemplates())
}
if absFilePath != "" {
if err := os.Remove(absFilePath); err != nil && !os.IsNotExist(err) {
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
}
}
}
}
func TestInclude(t *testing.T) {
for i, test := range []struct {
fileContent string
fileName string
shouldErr bool
expect string
args string
}{
{
// file exists, content is text only
fileContent: "text",
fileName: "file1",
shouldErr: false,
expect: "text",
},
{
// file exists, content is template
fileContent: "{{ if . }}text{{ end }}",
fileName: "file1",
shouldErr: false,
expect: "text",
},
{
// file does not exit
fileContent: "",
fileName: "",
shouldErr: true,
},
{
// args
fileContent: "{{ index .Args 0 }}",
fileName: "file1",
shouldErr: false,
args: "text",
expect: "text",
},
{
// args, reference arg out of range
fileContent: "{{ index .Args 1 }}",
fileName: "file1",
shouldErr: true,
args: "text",
},
} {
context := getContextOrFail(t)
var absFilePath string
// create files for test case
if test.fileName != "" {
absFilePath := filepath.Join(fmt.Sprintf("%s", context.Root), test.fileName)
if err := ioutil.WriteFile(absFilePath, []byte(test.fileContent), os.ModePerm); err != nil {
os.Remove(absFilePath)
t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
}
}
// perform test
actual, err := context.funcInclude(test.fileName, test.args)
if err != nil {
if !test.shouldErr {
t.Errorf("Test %d: Expected no error, got: '%s'", i, err)
}
} else if test.shouldErr {
t.Errorf("Test %d: Expected error but had none", i)
} else if actual != test.expect {
t.Errorf("Test %d: Expected %s but got %s", i, test.expect, actual)
}
if absFilePath != "" {
if err := os.Remove(absFilePath); err != nil && !os.IsNotExist(err) {
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
}
}
}
}
func TestCookieMultipleCookies(t *testing.T) {
context := getContextOrFail(t)