mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-14 06:46:27 +03:00
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:
parent
0a5f7a677f
commit
16f752125f
1 changed files with 134 additions and 0 deletions
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue