-
Notifications
You must be signed in to change notification settings - Fork 0
/
stylesheet_test.go
229 lines (201 loc) · 6.14 KB
/
stylesheet_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package smetana
import (
"errors"
"fmt"
"log"
"strings"
"testing"
)
func TestCanCreateStylesCss(t *testing.T) {
str := "body{padding:5px;}"
css := StylesCss(str)
assertEqual(t, str, string(css))
}
func TestCanCreateStylesFontFace(t *testing.T) {
font := StylesFontFace("OpenSans", "OpenSans.ttf")
expected := StyleSheetFontFace{"OpenSans", []string{"OpenSans.ttf"}}
assertEqual(t, expected, font)
}
func TestCanCreateStylesBlock(t *testing.T) {
block := StylesBlock("body", CssProps{{"background", "red"}})
expected := StyleSheetBlock{"body", CssProps{{"background", "red"}}}
assertEqual(t, expected, block)
}
func TestCanRenderEmptyStyleSheet(t *testing.T) {
styles := NewStyleSheet()
assertEqual(t, "", RenderCss(styles, Palette{}))
}
func TestCanCreateStyleSheetWithInitialElements(t *testing.T) {
css := `
body {
padding: 3em;
background: #ddd;
}
`
styles := NewStyleSheet(StylesCss(css))
assertEqual(t, css, RenderCss(styles, Palette{}))
}
func TestCanConvertFontNameToExtension(t *testing.T) {
fmt, err := fontUrlToFormat("a.ttf")
assertEqual(t, err, nil)
assertEqual(t, fmt, "truetype")
fmt, err = fontUrlToFormat("a.otf")
assertEqual(t, err, nil)
assertEqual(t, fmt, "opentype")
fmt, err = fontUrlToFormat("a.woff")
assertEqual(t, err, nil)
assertEqual(t, fmt, "woff")
fmt, err = fontUrlToFormat("a.woff2")
assertEqual(t, err, nil)
assertEqual(t, fmt, "woff2")
fmt, err = fontUrlToFormat("a.png")
assertEqual(t, fmt, "")
assertEqual(t, err, errors.New("Invalid font URL: a.png"))
}
func TestCanAddARawCssString(t *testing.T) {
styles := NewStyleSheet()
styles.AddCss(".hello{background:red;}")
assertEqual(t, ".hello{background:red;}", RenderCss(styles, Palette{}))
}
func TestCanAddFontFace(t *testing.T) {
styles := NewStyleSheet()
font := styles.AddFont("OpenSans", "OpenSans.ttf", "OpenSans.woff2")
assertEqual(t, "OpenSans", font)
css := RenderCss(styles, Palette{})
expected := "@font-face{font-family:OpenSans;src:url(OpenSans.ttf)format('truetype'),url(OpenSans.woff2)format('woff2');}"
assertEqual(t, expected, css)
}
func TestReportsErrorRenderingInvalidFontFormat(t *testing.T) {
styles := NewStyleSheet()
font := styles.AddFont("OpenSans", "OpenSans.png")
assertEqual(t, "OpenSans", font)
var buf strings.Builder
logger := log.New(&buf, "", 0)
css := RenderCssOpts(styles, Palette{}, logger)
expected := "@font-face{font-family:OpenSans;src:url(OpenSans.png)format('');}"
assertEqual(t, expected, css)
assertEqual(t, "Invalid font URL: OpenSans.png\n", buf.String())
}
func TestCanAddClassWithStringProp(t *testing.T) {
styles := NewStyleSheet()
class := styles.AddClass("container", CssProps{
{"cursor", "pointer"},
})
assertEqual(t, "container", class)
css := RenderCss(styles, Palette{})
assertEqual(t, fmt.Sprintf(".%s{cursor:pointer;}", class), css)
}
type CustomProp struct {
Value int
}
func (prop CustomProp) String() string {
return fmt.Sprintf("%d", prop.Value*2)
}
func TestCanAddClassWithCustomStringerProp(t *testing.T) {
styles := NewStyleSheet()
class := styles.AddClass("container", CssProps{
{"padding", CustomProp{4}},
})
assertEqual(t, "container", class)
css := RenderCss(styles, Palette{})
assertEqual(t, fmt.Sprintf(".%s{padding:8;}", class), css)
}
func TestCanAddClassWithIntProp(t *testing.T) {
styles := NewStyleSheet()
class := styles.AddClass("container", CssProps{
{"margin", 10},
})
assertEqual(t, "container", class)
css := RenderCss(styles, Palette{})
assertEqual(t, fmt.Sprintf(".%s{margin:10px;}", class), css)
}
func TestCanAddClassWithColorProp(t *testing.T) {
styles := NewStyleSheet()
class := styles.AddClass("container", CssProps{
{"color", Rgb(255, 0, 0)},
})
assertEqual(t, "container", class)
css := RenderCss(styles, Palette{})
assertEqual(t, fmt.Sprintf(".%s{color:#FF0000;}", class), css)
}
func TestCanAddAnonClass(t *testing.T) {
styles := NewStyleSheet()
class := styles.AddAnonClass(CssProps{
{"cursor", "pointer"},
})
assertEqual(t, 8, len(class))
css := RenderCss(styles, Palette{})
assertEqual(t, fmt.Sprintf(".%s{cursor:pointer;}", class), css)
}
func TestCanAddBlock(t *testing.T) {
styles := NewStyleSheet()
styles.AddBlock("body", CssProps{
{"background", "red"},
})
css := RenderCss(styles, Palette{})
assertEqual(t, "body{background:red;}", css)
}
func TestCanAddBlockWithPaletteValues(t *testing.T) {
styles := NewStyleSheet()
styles.AddBlock("body", CssProps{
{"background", PaletteValue("background-color")},
})
palette := Palette{
"background-color": Hex("#FF00FF"),
}
css := RenderCss(styles, palette)
assertEqual(t, "body{background:#FF00FF;}", css)
}
func TestCanAddBlockWithFormattedPaletteValues(t *testing.T) {
styles := NewStyleSheet()
styles.AddBlock("div", CssProps{
{"border", PalettePrintf(
"%s solid %s",
PX(2),
PaletteValue("border-color"),
)},
})
palette := Palette{
"border-color": Hex("#FF00FF"),
}
css := RenderCss(styles, palette)
assertEqual(t, "div{border:2px solid #FF00FF;}", css)
}
func TestCanAddBlockWithMissingPaletteValues(t *testing.T) {
styles := NewStyleSheet()
styles.AddBlock("body", CssProps{
{"background", PaletteValue("background-color")},
})
var buf strings.Builder
logger := log.New(&buf, "", 0)
css := RenderCssOpts(styles, Palette{}, logger)
assertEqual(t, "body{background:inherit;}", css)
assertEqual(t, "Missing palette value: background-color\n", buf.String())
}
func TestCanAddBlockWithInvalidCssValue(t *testing.T) {
styles := NewStyleSheet()
styles.AddBlock("body", CssProps{
{"background", NewStyleSheet()},
})
var buf strings.Builder
logger := log.New(&buf, "", 0)
css := RenderCssOpts(styles, Palette{}, logger)
assertEqual(t, "body{background:inherit;}", css)
assertEqual(t, "Invalid CSS value: {[]}\n", buf.String())
}
func TestCanAddPaletteCss(t *testing.T) {
styles := NewStyleSheet()
styles.AddPaletteCss(func(palette Palette) string {
color, err := CssValueToString(
palette,
PaletteValue("background-color"),
)
assertEqual(t, nil, err)
return fmt.Sprintf("body{background:%s;}", color)
})
palette := Palette{
"background-color": Hex("#FF00FF"),
}
css := RenderCss(styles, palette)
assertEqual(t, "body{background:#FF00FF;}", css)
}