/
go2html_test.go
186 lines (184 loc) · 5.7 KB
/
go2html_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package go2html_test
import (
. "github.com/Contra-Culture/go2html"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("go2html", func() {
Describe("Template", func() {
Describe("NewTemplate()", func() {
It("returns template", func() {
t := NewTemplate("test", func(t *TemplateCfgr) {
t.Comment("comment text")
})
Expect(t).NotTo(BeNil())
})
})
Describe(".Populate()", func() {
Context("when all replacements provided", func() {
It("returns string", func() {
t := NewTemplate("test", func(t *TemplateCfgr) {
t.Comment("comment text")
})
Expect(t.Populate(map[string]interface{}{})).To(Equal("<!-- comment text -->"))
t = NewTemplate("test", func(t *TemplateCfgr) {
t.Doctype()
t.Comment("comment text")
t.Text("Some text here.")
t.TextInjection("text1")
t.Elem(
"p",
func(e *ElemCfgr) {
e.Class("p-normal")
e.Attr("class", "paragraph")
},
func(n *NestedNodesCfgr) {
n.UnsafeTextInjection("paragraph1")
})
t.Elem(
"div",
func(e *ElemCfgr) {
e.ID("wrapper")
e.AttrsInjection("div1-attr")
e.AttrValueInjection("data-ok", "div1-data-ok")
e.AttrValueInjection("data-confirm", "div1-data-confirm")
},
func(n *NestedNodesCfgr) {
n.Variants(
map[string]*Template{
"div_nested_1": NewTemplate(
"",
func(cfg *TemplateCfgr) {
cfg.TextInjection("text")
},
),
"div_nested_2": NewTemplate(
"",
func(cfg *TemplateCfgr) {
cfg.TextInjection("anotherText")
},
),
},
NewTemplate(
"",
func(cfg *TemplateCfgr) {
cfg.Text("no variant provided")
},
))
n.TemplateInjection("templateINJ")
n.Elem(
"h1",
func(e *ElemCfgr) {
e.Attr("class", "div-header")
},
func(n *NestedNodesCfgr) {
n.Text("Header1")
n.Elem(
"span",
func(e *ElemCfgr) {},
func(n *NestedNodesCfgr) {
n.TextInjection("span1-text")
})
})
n.Template(
"",
NewTemplate(
"nestedTemplate1",
func(t *TemplateCfgr) {
t.Variants(
map[string]*Template{
"nested_nested_1": NewTemplate(
"",
func(cfg *TemplateCfgr) {
cfg.TextInjection("text")
}),
"nested_nested_2": NewTemplate(
"",
func(cfg *TemplateCfgr) {
cfg.TextInjection("anotherText")
}),
},
NewTemplate(
"",
func(cfg *TemplateCfgr) {
cfg.Text("no variant provided")
},
))
t.Elem(
"h2",
func(e *ElemCfgr) {
e.AttrValueInjection("class", "header2-class")
},
func(n *NestedNodesCfgr) {},
)
t.Elem(
"p",
func(*ElemCfgr) {},
func(n *NestedNodesCfgr) {
n.TextInjection("paragraph2-text")
},
)
}))
n.Repeat(
"paragraphs",
NewTemplate(
"nestedTemplate2",
func(t *TemplateCfgr) {
t.Elem(
"p",
func(e *ElemCfgr) {
e.Attr("class", "repeatable-paragraph")
},
func(n *NestedNodesCfgr) {
n.TextInjection("paragraph-text")
},
)
},
))
})
})
Expect(
t.Populate(
map[string]interface{}{
"text1": "Inserted text here.",
"paragraph1": "Inserted <b>paragraph1</b> text.",
"div1-attr": map[string]string{"title": "Some title"},
"div1-data-ok": "1",
"div1-data-confirm": "1",
"div_nested_1": map[string]interface{}{
"text": "variant div_nested_1 text",
},
"templateINJ": map[string]interface{}{
"template": NewTemplate("templateINJ", func(c *TemplateCfgr) {
c.Elem(
"p",
func(c *ElemCfgr) {
c.AttrValueInjection("class", "value1")
},
func(c *NestedNodesCfgr) {
c.TextInjection("value2")
})
}),
"values": map[string]interface{}{
"value1": "Value-1-class",
"value2": "Value-2-text",
},
},
"span1-text": "Some text here.",
"nestedTemplate1": map[string]interface{}{
"header2-class": "subheader",
"paragraph2-text": "Second <i>paragraph</i>.",
},
"paragraphs": []map[string]interface{}{
{"paragraph-text": "Injected paragraph text 1."},
{"paragraph-text": "Injected paragraph text 2."},
{"paragraph-text": "Injected paragraph text 3."},
},
},
),
).To(Equal("<!DOCTYPE html><!-- comment text -->Some text here.Inserted text here.<p class=\"p-normal\" class=\"paragraph\">Inserted <b>paragraph1</b> text.</p><div id=\"wrapper\" title=\"Some title\" data-ok=\"1\" data-confirm=\"1\">variant div_nested_1 text<p class=\"Value-1-class\">Value-2-text</p><h1 class=\"div-header\">Header1<span>Some text here.</span></h1>no variant provided<h2 class=\"subheader\"></h2><p>Second <i>paragraph</i>.</p><p class=\"repeatable-paragraph\">Injected paragraph text 1.</p><p class=\"repeatable-paragraph\">Injected paragraph text 2.</p><p class=\"repeatable-paragraph\">Injected paragraph text 3.</p></div>"))
})
})
})
})
})