generated from clevergo/pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender_test.go
68 lines (58 loc) · 1.87 KB
/
render_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
65
66
67
68
// Copyright 2020 CleverGo. All rights reserved.
// Use of this source code is governed by a MIT style license that can be found
// in the LICENSE file.
package jetrenderer
import (
"bytes"
"errors"
"io"
"reflect"
"strings"
"testing"
"clevergo.tech/clevergo"
"github.com/CloudyKit/jet/v6"
"github.com/stretchr/testify/assert"
)
func TestNew(t *testing.T) {
set := jet.NewSet(jet.NewInMemLoader())
r := New(set)
assert.Equal(t, set, r.Set)
}
func fakeBeforeRender(callback func(), err error) BeforeRender {
return func(w io.Writer, name string, vars jet.VarMap, data interface{}, ctx *clevergo.Context) error {
callback()
return err
}
}
func TestRenderer_SetBeforeRender(t *testing.T) {
r := New(jet.NewSet(jet.NewInMemLoader()))
fn := fakeBeforeRender(func() {}, errors.New("before render test"))
r.SetBeforeRender(fn)
expected := fn(nil, "", nil, nil, nil)
actual := r.beforeRender(nil, "", nil, nil, nil)
assert.Equal(t, expected, actual)
}
func TestRenderer_Render(t *testing.T) {
r := New(jet.NewSet(jet.NewOSFileSystemLoader("./testdata/views")))
// invalid template.
err := r.Render(nil, "404.tmpl", nil, nil)
assert.NotNil(t, err)
terminatedErr := errors.New("terminated")
r.SetBeforeRender(fakeBeforeRender(func() {}, terminatedErr))
err = r.Render(nil, "index.tmpl", nil, nil)
assert.Equal(t, err, terminatedErr)
r.SetBeforeRender(func(w io.Writer, name string, vars jet.VarMap, data interface{}, ctx *clevergo.Context) error {
vars.SetFunc("title", jet.Func(func(args jet.Arguments) reflect.Value {
args.RequireNumOfArguments("title", 1, 1)
return reflect.ValueOf(strings.Title(args.Get(0).String()))
}))
return nil
})
for _, msg := range []string{"foo bar", "hello world"} {
buf := new(bytes.Buffer)
err = r.Render(buf, "index.tmpl", map[string]interface{}{
"message": msg,
}, nil)
assert.Contains(t, buf.String(), strings.Title(msg))
}
}