-
Notifications
You must be signed in to change notification settings - Fork 35
/
renderer_test.go
106 lines (81 loc) · 2.09 KB
/
renderer_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package air
import (
"html/template"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNewRenderer(t *testing.T) {
a := New()
r := a.renderer
assert.NotNil(t, r)
assert.NotNil(t, r.a)
assert.NotNil(t, r.loadOnce)
assert.Nil(t, r.watcher)
assert.Nil(t, r.template)
}
func TestRendererLoad(t *testing.T) {
a := New()
dir, err := ioutil.TempDir("", "air.TestRendererLoad")
assert.NoError(t, err)
assert.NotEmpty(t, dir)
defer os.RemoveAll(dir)
a.RendererTemplateRoot = dir
r := a.renderer
r.load()
assert.Nil(t, r.loadError)
assert.NotNil(t, r.watcher)
assert.NotNil(t, r.template)
}
func TestRendererRender(t *testing.T) {
a := New()
dir, err := ioutil.TempDir("", "air.TestRendererRender")
assert.NoError(t, err)
assert.NotEmpty(t, dir)
defer os.RemoveAll(dir)
a.RendererTemplateRoot = dir
r := a.renderer
assert.NoError(t, ioutil.WriteFile(
filepath.Join(a.RendererTemplateRoot, "test.html"),
[]byte(`<a href="/">Go Home</a>`),
os.ModePerm,
))
assert.NoError(t, ioutil.WriteFile(
filepath.Join(a.RendererTemplateRoot, "test.ext"),
[]byte(`<a href="/">Go Home Again</a>`),
os.ModePerm,
))
assert.NoError(t, r.render(ioutil.Discard, "test.html", nil, locstr))
assert.Error(t, r.render(ioutil.Discard, "test.ext", nil, locstr))
a.I18nEnabled = true
assert.Error(t, r.render(ioutil.Discard, "test.html", nil, locstr))
a = New()
a.I18nEnabled = true
a.RendererTemplateRoot = dir
r = a.renderer
assert.NoError(t, r.render(ioutil.Discard, "test.html", nil, locstr))
}
func TestLocstr(t *testing.T) {
assert.Equal(t, "Foobar", locstr("Foobar"))
}
func TestStr2html(t *testing.T) {
assert.Equal(t, template.HTML("Foobar"), str2html("Foobar"))
}
func TestStrlen(t *testing.T) {
assert.Equal(t, 6, strlen("Foobar"))
assert.Equal(t, 2, strlen("测试"))
}
func TestSubstr(t *testing.T) {
assert.Equal(t, "o", substr("Foobar", 1, 2))
assert.Equal(t, "试", substr("测试", 1, 2))
}
func TestTimefmt(t *testing.T) {
assert.Equal(
t,
"1970-01-01T00:00:00Z",
timefmt(time.Unix(0, 0).UTC(), time.RFC3339),
)
}