forked from foolin/goview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstance_test.go
64 lines (57 loc) · 1.54 KB
/
instance_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package goview
import (
"html/template"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func TestDefault(t *testing.T) {
recorder := httptest.NewRecorder()
err := Render(recorder, http.StatusOK, "index", M{})
//expect like this info:
// "error: ViewEngine render read name:layouts/master, path:/foolin/goview/views/layouts/master.html, error: open /foolin/goview/views/layouts/master.html: The system cannot find the path specified"
if err == nil {
t.Error("render is ok?")
} else {
t.Logf("expect error: %v", err)
}
}
func TestRender(t *testing.T) {
engine := New(Config{
Root: "_examples/test",
Extension: ".tpl",
Master: "layouts/master",
Partials: []string{},
Funcs: template.FuncMap{
"echo": func(v string) string {
return "$" + v
},
},
DisableCache: true,
})
Use(engine)
recorder := httptest.NewRecorder()
expect := "<v>Index</v>"
err := Render(recorder, http.StatusOK, "index", M{})
if err != nil {
t.Errorf("render error: %v", err)
return
}
assertRecorder(t, recorder, http.StatusOK, expect)
}
func assertRecorder(t *testing.T, recorder *httptest.ResponseRecorder, expectStatusCode int, expectOut string) {
result := recorder.Result()
if result.StatusCode != expectStatusCode {
t.Errorf("actual: %v, expect: %v", result.StatusCode, expectStatusCode)
}
resultBytes, err := ioutil.ReadAll(result.Body)
if err != nil {
t.Errorf("read result body error: %v", err)
return
}
val := string(resultBytes)
if val != expectOut {
t.Errorf("actual: %v, expect: %v", val, expectOut)
}
}