-
Notifications
You must be signed in to change notification settings - Fork 1
/
html_test.go
66 lines (48 loc) · 1.63 KB
/
html_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
package temple
import (
"bytes"
. "gopkg.in/check.v1"
"testing"
"time"
)
func TestHtml(t *testing.T) { TestingT(t) }
type HtmlSuite struct{}
var _ = Suite(&HtmlSuite{})
func (s *HtmlSuite) TestExterns(c *C) {
t := NewHtml("test")
t, err := t.Parse(`{{ define "a"}}Extends: {{extends .Name .Data}}{{ end }}{{define "b"}}this is B, {{ . }}{{end}}{{define "c"}}this is C, {{ . }}{{end}}`)
c.Assert(err, IsNil)
var buf *bytes.Buffer
buf = bytes.NewBuffer([]byte{})
err = t.ExecuteTemplate(buf, "a", struct{ Name, Data string }{"b", "100"})
c.Assert(err, IsNil)
c.Assert(buf.String(), Equals, "Extends: this is B, 100")
buf = bytes.NewBuffer([]byte{})
err = t.ExecuteTemplate(buf, "a", struct{ Name, Data string }{"c", "xyz"})
c.Assert(err, IsNil)
c.Assert(buf.String(), Equals, "Extends: this is C, xyz")
buf = bytes.NewBuffer([]byte{})
err = t.ExecuteTemplate(buf, "a", struct{ Name, Data string }{"d", "aaa"})
c.Assert(err, Not(IsNil))
}
func (s *HtmlSuite) TestBytes(c *C) {
t := NewHtml("test")
t, err := t.Parse(`{{ define "a"}}{{.|inBytes}},{{.|inKBytes}},{{.|inMBytes}}{{end}}`)
c.Assert(err, IsNil)
var buf *bytes.Buffer
buf = bytes.NewBuffer([]byte{})
err = t.ExecuteTemplate(buf, "a", 23*1024*1024)
c.Assert(err, IsNil)
c.Assert(buf.String(), Equals, "23MB,23GB,23TB")
}
func (s *HtmlSuite) TestElapsed(c *C) {
t := NewHtml("test")
now := time.Now()
t, err := t.Parse(`{{ define "a"}}{{.|elapsed}}{{end}}`)
c.Assert(err, IsNil)
var buf *bytes.Buffer
buf = bytes.NewBuffer([]byte{})
err = t.ExecuteTemplate(buf, "a", now.Add(-31*time.Minute))
c.Assert(err, IsNil)
c.Assert(buf.String(), Equals, "31 mins ago")
}