Improve test coverage, fix various error handling bugs (#40)
- [x] Bring test coverage back up to around 90%. - [x] Fix various bugs in error handling - [x] Remove panic behaviour when templates can't be found Closes #39 Co-authored-by: Nicolas Herry <beastieboy@beastieboy.net> Reviewed-on: #40master 0.1.1
parent
ee8290bf49
commit
ba3e2eaa6f
@ -0,0 +1,98 @@ |
||||
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)) |
||||
} |
||||
|
||||
} |
@ -0,0 +1 @@ |
||||
This template is broken and should generate an error. {{ .NonExistentField }} |
Loading…
Reference in new issue