-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_component_gtext_test.go
More file actions
203 lines (180 loc) · 7.23 KB
/
node_component_gtext_test.go
File metadata and controls
203 lines (180 loc) · 7.23 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package gotmx
import (
stringutils "github.com/authentic-devel/gotmx/utils"
"testing"
)
//********************************* g-inner-text ***********************************************************************
//**********************************************************************************************************************
func TestGInnerTextWorks(t *testing.T) {
data := nodeComponentTestModel{
StringValue: `Hello World with special chars > < & " '`,
}
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
| <!-- long version -->
| <div data-g-inner-text="Hello World">This should get replaced by the text</div>
| <!-- short version -->
| <div g-inner-text="Hello World">This should get replaced by the text</div>
| <!-- With model path -->
| <div g-inner-text="[[ .StringValue ]]">This should get replaced by the text</div>
| <!-- With Golang template -->
| <div g-inner-text="{{ .StringValue }}">This should get replaced by the text</div>
|</div>`)
expected := stringutils.TrimMargin(
`<div>
|
| <div>Hello World</div>
|
| <div>Hello World</div>
|
| <div>Hello World with special chars > < & " '</div>
|
| <div>Hello World with special chars > < & " '</div>
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", data, expected, t)
}
func TestGInnerTextLongVersionHasHigherPriority(t *testing.T) {
template := stringutils.TrimMargin(
`<div data-g-define="my-template" g-inner-text="ignore-me" data-g-inner-text="Hello World">
|This should get replaced by the text
|</div>`)
expectation := stringutils.TrimMargin(
`<div>Hello World</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", nil, expectation, t)
}
// Tests that the g-inner-text attribute is evaluated after the g-if attribute
func TestGInnerTextIsEvaluatedAfterIf(t *testing.T) {
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
| <div g-if="[[ .BooleanValue ]]" g-inner-text="Hello World">This should get replaced by the text</div>
|</div>`)
expectationForTrue := stringutils.TrimMargin(
`<div>
| <div>Hello World</div>
|</div>`)
expectationForFalse := stringutils.TrimMargin(
`<div>
|
|</div>`)
data := nodeComponentTestModel{
BooleanValue: true,
}
parseRenderAndCompareTemplate(nil, template, "my-template", data, expectationForTrue, t)
data.BooleanValue = false
parseRenderAndCompareTemplate(nil, template, "my-template", data, expectationForFalse, t)
}
func TestGInnerTextWorksWithIgnoreOuter(t *testing.T) {
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
| <div g-ignore="outer" g-inner-text="Hello World">Lorem ipsum</div>
|</div>`)
expectation := stringutils.TrimMargin(
`<div>
|
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", nil, expectation, t)
}
func TestGInnerTextWorksWithIgnoreInner(t *testing.T) {
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
| <div g-ignore="inner" g-inner-text="Hello World">Lorem ipsum</div>
|</div>`)
expectation := stringutils.TrimMargin(
`<div>
| <div></div>
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", nil, expectation, t)
}
func TestGInnerTextWorksWithIgnoreOuterOnly(t *testing.T) {
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
| <div g-ignore="outer-only" g-inner-text="Hello World">Lorem ipsum</div>
|</div>`)
expectation := stringutils.TrimMargin(
`<div>
| Hello World
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", nil, expectation, t)
}
//********************************* g-outer-text ***********************************************************************
//**********************************************************************************************************************
func TestGOuterTextWorks(t *testing.T) {
data := nodeComponentTestModel{
StringValue: `Hello World with special chars > < & " '`,
}
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
| <!-- long version -->
| <div data-g-outer-text="Hello World">This should get replaced by the text</div>
| <!-- short version -->
| <div g-outer-text="Hello World">This should get replaced by the text</div>
| <!-- With model path -->
| <div g-outer-text="[[ .StringValue ]]">This should get replaced by the text</div>
| <!-- With Golang template -->
| <div g-outer-text="{{ .StringValue }}">This should get replaced by the text</div>
|</div>`)
expected := stringutils.TrimMargin(
`<div>
|
| Hello World
|
| Hello World
|
| Hello World with special chars > < & " '
|
| Hello World with special chars > < & " '
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", data, expected, t)
}
func TestGOuterTextIsIgnoredForAnyIgnore(t *testing.T) {
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
|<div g-ignore="outer" g-outer-text="Hello World">Lorem ipsum</div>
|<div g-ignore="inner" g-outer-text="Hello World">Lorem ipsum</div>
|<div g-ignore="outer-only" g-outer-text="Hello World">Lorem ipsum</div>
|<div g-ignore="invalid" g-outer-text="Hello World">Lorem ipsum</div>
|<div g-ignore g-outer-text="Hello World">Lorem ipsum</div>
|<div data-g-ignore g-outer-text="Hello World">Lorem ipsum</div>
|</div>`)
expectation := stringutils.TrimMargin(
`<div>
|
|
|
|
|
|
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", nil, expectation, t)
}
func TestGOuterTextWorksWithGIf(t *testing.T) {
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
|<div data-g-if="false" g-outer-text="Hello World" >Lorem ipsum</div>
|---
|<div data-g-if="true" g-outer-text="Hello World" >Lorem ipsum</div>
|</div>`)
expectation := stringutils.TrimMargin(
`<div>
|
|---
|Hello World
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", nil, expectation, t)
}
func TestGOuterTextWorksWithGRepeat(t *testing.T) {
data := nodeComponentTestModel{
StringSlice: []string{"String 1\n", "Spacial chars > < & '\n", "String 3"},
}
template := stringutils.TrimMargin(
`<div data-g-define="my-template">
|<div data-g-outer-repeat="[[ .StringSlice ]]" g-outer-text="[[ . ]]" >Lorem ipsum</div>
|</div>`)
expectation := stringutils.TrimMargin(
`<div>
|String 1
|Spacial chars > < & '
|String 3
|</div>`)
parseRenderAndCompareTemplate(nil, template, "my-template", data, expectation, t)
}