You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
2.0 KiB
98 lines
2.0 KiB
package gopher
|
|
|
|
|
|
import (
|
|
"testing"
|
|
"os"
|
|
"time"
|
|
"bytes"
|
|
"text/template"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
)
|
|
|
|
func TestRunTemplate(t *testing.T) {
|
|
d, _ := os.Getwd()
|
|
c := Context { Root: d + "/../testdata" }
|
|
|
|
p := "/attic/hello.gt"
|
|
|
|
rq, _ := NewRequest(c, p)
|
|
rs := NewResponse(c, *rq)
|
|
|
|
dc := DynamicContext {
|
|
ServerContext: c,
|
|
RequestInfo: *rq,
|
|
ResponseInfo: *rs,
|
|
ErrorInfo: GopherErrorInfo{},
|
|
CurrentTime: time.Now(),
|
|
}
|
|
|
|
lines, _ := os.ReadFile(rq.FullPath)
|
|
|
|
tmpl, err := template.New("test").Parse(string(lines))
|
|
var b bytes.Buffer
|
|
err = tmpl.Execute(&b, dc)
|
|
expected := []string{b.String()}
|
|
|
|
got, err := RunTemplate(&dc, rq.FullPath);
|
|
|
|
if err != nil {
|
|
t.Errorf("Received an unexpected error when running a template with path %s; %s", p, err.Error())
|
|
}
|
|
|
|
if ! cmp.Equal(got, expected) {
|
|
t.Errorf("Unexpected return value when generating a template: %s", cmp.Diff(expected, got))
|
|
}
|
|
|
|
p = "/attic/broken.gt"
|
|
|
|
rq, _ = NewRequest(c, p)
|
|
rs = NewResponse(c, *rq)
|
|
|
|
dc = DynamicContext {
|
|
ServerContext: c,
|
|
RequestInfo: *rq,
|
|
ResponseInfo: *rs,
|
|
ErrorInfo: GopherErrorInfo{},
|
|
CurrentTime: time.Now(),
|
|
}
|
|
|
|
expected = []string{}
|
|
|
|
got, err = RunTemplate(&dc, rq.FullPath);
|
|
|
|
if err == nil {
|
|
t.Errorf("Failed to received an error when running a broken template with path %s", p)
|
|
}
|
|
|
|
if ! cmp.Equal(got, expected) {
|
|
t.Errorf("Unexpected return value when generating a broken template: %s", cmp.Diff(expected, got))
|
|
}
|
|
|
|
p = "/attic/non-existent.gt"
|
|
|
|
rq, _ = NewRequest(c, p)
|
|
rs = NewResponse(c, *rq)
|
|
|
|
dc = DynamicContext {
|
|
ServerContext: c,
|
|
RequestInfo: *rq,
|
|
ResponseInfo: *rs,
|
|
ErrorInfo: GopherErrorInfo{},
|
|
CurrentTime: time.Now(),
|
|
}
|
|
|
|
expected = []string{}
|
|
|
|
got, err = RunTemplate(&dc, rq.FullPath);
|
|
|
|
if err == nil {
|
|
t.Errorf("Failed to received an error when running a non-existent template with path %s", p)
|
|
}
|
|
|
|
if ! cmp.Equal(got, expected) {
|
|
t.Errorf("Unexpected return value when generating a non-existent template: %s", cmp.Diff(expected, got))
|
|
}
|
|
|
|
}
|
|
|