forked from starshipyard/go-css
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
217 lines (197 loc) · 4.12 KB
/
parser_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
package css
import (
"fmt"
"testing"
)
func TestParseSimple(t *testing.T) {
ex1 := `rule {
style1: value1;
style2: value2;
}`
ex2 := `{
style1: value1;
}`
/*
ex3 := `rule {
style1: value1
style2: value2;
}`
*/
ex4 := `rule {
style1: value1;
style2:;
}`
ex5 := `}
rule {
style1: value1;
style2:;
}`
t.Run("GoodCSS", func(t *testing.T) {
css, err := Unmarshal([]byte(ex1))
if err != nil {
t.Fatal(err)
}
rule, ok := css["rule"]
if !ok {
t.Fatal("rule 'rule' doesn't exist")
}
if value, ok := rule["style1"]; !ok {
t.Fatal("syle 'style1' doesn't exist")
} else if value != "value1" {
t.Fatalf("incorrect value for 'style1', got '%v', expected 'value1'", value)
}
if value, ok := rule["style2"]; !ok {
t.Fatal("style 'style2' doesn't exist")
} else if value != "value2" {
t.Fatalf("incorrect value for 'style2', got '%v', expected 'value2'", value)
}
})
t.Run("MissingRule", func(t *testing.T) {
if _, err := Unmarshal([]byte(ex2)); err == nil {
t.Fatal("should error out")
}
})
/*
t.Run("StatementMissingSemicolon", func(t *testing.T) {
if css, err := Unmarshal([]byte(ex3)); err == nil {
for k, v := range css {
t.Logf("k: %v, v: %v\n", k, v)
}
t.Fatal("should error out")
}
})
*/
t.Run("StyleWithoutValue", func(t *testing.T) {
if _, err := Unmarshal([]byte(ex4)); err == nil {
t.Fatal("should error out")
}
})
t.Run("BlockEndsWithoutBeginning", func(t *testing.T) {
if _, err := Unmarshal([]byte(ex5)); err == nil {
t.Fatal("should error out")
}
})
}
func TestParseHarder(t *testing.T) {
t.Run("MultiRules", func(t *testing.T) {
ex1 := `rule1 {
style1: value1;
style2: value2;
}
rule2 {
style3: value3;
}`
css, err := Unmarshal([]byte(ex1))
if err != nil {
t.Fatal(err)
}
if len(css) != 2 {
t.Fatalf("expected 2 rules, got %d", len(css))
}
if _, ok := css["rule1"]; !ok {
t.Fatal("missing rule 'rule1'")
}
if _, ok := css["rule2"]; !ok {
t.Fatal("missing rule 'rule2'")
}
})
t.Run("PropertyWithSpace", func(t *testing.T) {
ex1 := `body {
font-family: 'Zil', serif;
}`
css, err := Unmarshal([]byte(ex1))
if err != nil {
t.Fatal(err)
}
if css["body"]["font-family"] != "'Zil', serif" {
t.Fatalf("invalid rule 'font-family', got %q", css["body"]["font-family"])
}
})
t.Run("MergedRules", func(t *testing.T) {
ex1 := `rule1 {
style1: value1;
style2: value2;
}
rule1 {
style1: value3;
}`
css, err := Unmarshal([]byte(ex1))
if err != nil {
t.Fatal(err)
}
if len(css) != 1 {
t.Fatalf("there should be only one rule, got %d", len(css))
}
if len(css["rule1"]) != 2 {
t.Fatalf("there should be two styles for rule 'rule1', got %d", len(css["rule1"]))
}
if css["rule1"]["style1"] != "value3" {
t.Fatalf("value of 'style1' should be 'value3' but got '%v'", css["rule1"]["style1"])
}
})
t.Run("RealWorldCSS", func(t *testing.T) {
ex1 := `body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
}`
_, err := Unmarshal([]byte(ex1))
if err != nil {
t.Fatal(err)
}
})
}
func TestParseSelectors(t *testing.T) {
ex1 := `.rule {
style1: value1;
style2: value2;
}
#rule1 sad asd {
style3: value3;
style4: value4;
}`
css, err := Unmarshal([]byte(ex1))
if err != nil {
t.Fatal(err)
}
if _, ok := css[".rule"]; !ok {
t.Fatal("Missing '.rule' rule")
}
if _, ok := css["#rule1"]; !ok {
t.Fatal("Missing '.rule' rule")
}
}
func TestParseSelectorGroup(t *testing.T) {
ex1 := `.rule1 #rule2 rule3 {
style1: value1;
style2: value2;
}`
css, err := Unmarshal([]byte(ex1))
if err != nil {
t.Fatal(err)
}
if _, ok := css[".rule1"]; !ok {
t.Fatal("Missing '.rule1' rule")
}
if _, ok := css["#rule2"]; !ok {
t.Fatal("Missing '#rule2' rule")
}
if _, ok := css["rule3"]; !ok {
t.Fatal("Missing '.rule3' rule")
}
}
func BenchmarkParser(b *testing.B) {
ex1 := ""
for i := 0; i < 100; i++ {
ex1 += fmt.Sprintf(`block%d {
style%d: value%d;
}`, i, i, i)
}
styleSheet := []byte(ex1)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := Unmarshal(styleSheet)
if err != nil {
b.Fatal(err)
}
}
}