-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm_test.go
117 lines (113 loc) · 2.22 KB
/
m_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
package m
import (
"testing"
)
func Test_map(t *testing.T) {
people := []struct {
Name string
State string
}{
{"Alice", "AZ"},
{"Bob", "TX"},
{"Eve", "TX"},
}
tests := []struct {
Element Element
Expected string
}{
{
M(""),
`<div></div>`,
},
{
Document(M("html[lang=en]")),
"<!DOCTYPE html>\n" + `<html lang="en"></html>`,
},
{
T(`Me & You`),
`Me & You`,
},
{
S(T("A"), T("B"), T("C")),
`ABC`,
},
{
M("div", Attrf("data-x", "%d", 123)),
`<div data-x="123"></div>`,
},
{
F("Hello, %s", `Tim<`),
`Hello, Tim<`,
},
{
If(true, T("true")),
`true`,
},
{
If(false, T("true")),
``,
},
{
Range(0, func(i int) Element {
return F("%d ", i)
}),
``,
},
{
Range(3, func(i int) Element {
return F("%d ", i)
}),
`0 1 2 `,
},
{
For(2, 0, -1, func(i int) Element {
return F("%d ", i)
}),
`2 1 0 `,
},
{
Group(len(people), func(i, j int) bool {
return people[i].State == people[j].State
}, func(i, j int) Element {
return S(
M("h2", T(people[i].State)),
M("ul",
For(i, j, 1, func(i int) Element {
return M("li", T(people[i].Name))
}),
),
)
}),
`<h2>AZ</h2><ul><li>Alice</li></ul><h2>TX</h2><ul><li>Bob</li><li>Eve</li></ul>`,
},
{
Range(3, func(i int) Element {
return M("option", If(i == 2, Attr("selected", "")), Attrf("value", "%d", i),
F("Element %d", i),
)
}),
`<option value="0">Element 0</option><option value="1">Element 1</option><option selected="" value="2">Element 2</option>`,
},
{
M("p.text-right", If(false, Attr("class", "m-0")), Attr("class", "alpha-25"), T("Text")),
`<p class="text-right alpha-25">Text</p>`,
},
{
M("p#id-0", If(false, Attr("id", "id-1")), T("Text")),
`<p id="id-0">Text</p>`,
},
{
M("p#id-0", If(true, Attr("id", "id-1")), T("Text")),
`<p id="id-1">Text</p>`,
},
{
M("p#id-0", If(true, Attr("id", "id-1")), Attr("id", "id-2"), T("Text")),
`<p id="id-2">Text</p>`,
},
}
for _, tt := range tests {
if output := RenderString(tt.Element); output != tt.Expected {
t.Errorf("RenderString(%#v)\ngot:\n%#v\nexpected:\n%#v", tt.Element, output, tt.Expected)
}
}
}