forked from gobuffalo/tags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag_test.go
162 lines (127 loc) · 3.4 KB
/
tag_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
package tags_test
import (
"fmt"
"testing"
"time"
"github.com/gobuffalo/tags"
"github.com/stretchr/testify/require"
)
func Test_Tag(t *testing.T) {
r := require.New(t)
tag := tags.New("input", tags.Options{})
r.Equal("input", tag.Name)
}
func Test_Tag_WithName(t *testing.T) {
r := require.New(t)
tag := tags.New("br", tags.Options{})
r.Equal("br", tag.Name)
r.Equal(`<br />`, tag.String())
}
func Test_Tag_NonVoid(t *testing.T) {
r := require.New(t)
tag := tags.New("div", tags.Options{})
r.Equal("div", tag.Name)
r.Equal(`<div></div>`, tag.String())
}
func Test_Tag_WithValue(t *testing.T) {
r := require.New(t)
tag := tags.New("input", tags.Options{
"value": "Mark",
})
r.Equal(`<input value="Mark" />`, tag.String())
}
func Test_Tag_WithTimeValue(t *testing.T) {
r := require.New(t)
cases := map[string]string{
"": "0001-01-01",
"01-02-2006": "01-01-0001",
"01-02": "01-01",
}
for format, expected := range cases {
tag := tags.New("input", tags.Options{
"value": time.Time{},
"format": format,
})
r.Equal(fmt.Sprintf(`<input value="%v" />`, expected), tag.String())
}
}
func Test_Tag_WithBody(t *testing.T) {
r := require.New(t)
tag := tags.New("div", tags.Options{
"body": "hi there!",
})
r.Equal(`<div>hi there!</div>`, tag.String())
r.Nil(tag.Options["body"])
}
func Test_Tag_WithBody_And_BeforeTag(t *testing.T) {
r := require.New(t)
s := `<span>Test</span>`
tag := tags.New("div", tags.Options{
"body": "hi there!",
"before_tag": s,
})
r.Equal(`<span>Test</span><div>hi there!</div>`, tag.String())
r.Nil(tag.Options["body"])
}
func Test_Tag_WithBody_And_AfterTag(t *testing.T) {
r := require.New(t)
s := `<span>Test</span>`
tag := tags.New("div", tags.Options{
"body": "hi there!",
"after_tag": s,
})
r.Equal(`<div>hi there!</div><span>Test</span>`, tag.String())
r.Nil(tag.Options["body"])
}
func Test_Tag_String(t *testing.T) {
r := require.New(t)
tag := tags.New("div", tags.Options{
"body": "hi there!",
})
r.Equal(`<div>hi there!</div>`, tag.String())
}
func Test_Tag_String_WithOpts(t *testing.T) {
r := require.New(t)
tag := tags.New("div", tags.Options{
"body": "hi there!",
"class": "foo bar baz",
})
r.Equal(`<div class="foo bar baz">hi there!</div>`, tag.String())
}
func Test_Tag_String_SubTag(t *testing.T) {
r := require.New(t)
tag := tags.New("div", tags.Options{
"body": tags.New("p", tags.Options{
"body": "hi!",
}),
})
r.Equal(`<div><p>hi!</p></div>`, tag.String())
}
func Test_Tag_String_With_BeforeTag_Opt(t *testing.T) {
r := require.New(t)
s := `<span>Test</span>`
tag := tags.New("div", tags.Options{
"before_tag": s,
})
r.Equal(`<span>Test</span><div></div>`, tag.String())
}
func Test_Tag_String_With_AfterTag_Opt(t *testing.T) {
r := require.New(t)
s := `<span>Test</span>`
tag := tags.New("div", tags.Options{
"after_tag": s,
})
r.Equal(`<div></div><span>Test</span>`, tag.String())
}
func Test_Tag_With_Another_Tag_As_BeforeTag(t *testing.T) {
r := require.New(t)
s := tags.New("span", tags.Options{"body": "Test"})
tag := tags.New("div", tags.Options{"before_tag": s})
r.Equal(`<span>Test</span><div></div>`, tag.String())
}
func Test_Tag_With_Another_Tag_As_AfterTag(t *testing.T) {
r := require.New(t)
s := tags.New("span", tags.Options{"body": "Test"})
tag := tags.New("div", tags.Options{"after_tag": s})
r.Equal(`<div></div><span>Test</span>`, tag.String())
}