-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_test.go
64 lines (58 loc) · 1.44 KB
/
template_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 main
import (
"bytes"
"encoding/json"
"testing"
)
func TestAssemble(t *testing.T) {
m := map[string]interface{}{}
b := bytes.NewBufferString("{\"name\":\"Cuthbert\"}").Bytes()
json.Unmarshal(b, &m)
text, err := Assemble("testing", "Hello {{ .name }}", &m)
if err != nil {
t.Errorf("Error: %s", err.Error())
t.FailNow()
}
if text != "Hello Cuthbert" {
t.Errorf("Text did not match: %s", text)
}
}
func TestAssembleFromMessage(t *testing.T) {
b := bytes.NewBufferString("{\"params\":{\"name\":\"Cuthbert\", \"food\":\"Cake\"}}")
decoder := json.NewDecoder(b)
msg := &Message{}
err := decoder.Decode(msg)
if err != nil {
t.Errorf("Unable to decode: %s", err.Error())
t.FailNow()
}
tpl := &Template{"testing", "Hello {{ .name }} and my favourite food is {{ .food }}"}
text, err := AssembleTemplate(tpl, msg)
if err != nil {
t.Errorf("Error: %s", err.Error())
t.FailNow()
}
if text != "Hello Cuthbert and my favourite food is Cake" {
t.Errorf("Text did not match: %s", text)
}
}
func TestImportEmailTemplate(t *testing.T) {
tm := NewTemplateManager()
tpl, err := tm.ImportTemplate("test")
if err != nil {
t.Errorf("Error: %s", err.Error())
t.FailNow()
}
if tpl.Subject == "" {
t.Errorf("Subject not set")
t.Fail()
}
if tpl.Text == nil || tpl.Text.Name == "" {
t.Errorf("Text template not set")
t.Fail()
}
if tpl.Html == nil || tpl.Html.Name == "" {
t.Errorf("Html template not set")
t.Fail()
}
}