-
Notifications
You must be signed in to change notification settings - Fork 15
/
test_parser.rb
221 lines (167 loc) · 5.04 KB
/
test_parser.rb
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
require 'helper'
require 'benchmark'
module CSSPool
class TestParser < CSSPool::TestCase
def test_empty_doc_on_blank
assert CSSPool.CSS(nil)
assert CSSPool.CSS('')
end
def test_doc_charset
doc = CSSPool.CSS <<-eocss
@charset "UTF-8";
@import url("foo.css") screen;
div#a, a.foo, a:hover, a[href][int="10"]{ background: red; }
eocss
assert_equal 'UTF-8', doc.charsets.first.name
end
def test_doc_parser
doc = CSSPool.CSS <<-eocss
@charset "UTF-8";
div#a, a.foo, a:hover, a[href][int="10"]{ background: red; }
eocss
assert_equal 1, doc.rule_sets.length
rule_set = doc.rule_sets.first
assert_equal 4, rule_set.selectors.length
assert_equal 1, rule_set.declarations.length
assert_equal 'background', rule_set.declarations.first.property
end
def test_universal_to_css
doc = CSSPool.CSS <<-eocss
* { background: red, blue; }
eocss
assert_match '*', doc.to_css
end
def test_doc_to_css
doc = CSSPool.CSS <<-eocss
div#a, a.foo, a:hover, a[href][int="10"]{ background: red, blue; }
eocss
assert_match 'div#a, a.foo, a:hover, a[href][int="10"]', doc.to_css
assert_match 'background: red, blue;', doc.to_css
end
def test_doc_desc_to_css
doc = CSSPool.CSS <<-eocss
div > a { background: #123; }
eocss
assert_match 'div > a', doc.to_css
end
def test_doc_pseudo_to_css
doc = CSSPool.CSS <<-eocss
:hover { background: #123; }
eocss
assert_match ':hover', doc.to_css
end
def test_doc_id_to_css
doc = CSSPool.CSS <<-eocss
#hover { background: #123; }
eocss
assert_match '#hover', doc.to_css
end
def test_important
doc = CSSPool.CSS <<-eocss
div > a { background: #123 !important; }
eocss
assert_match '!important', doc.to_css
end
def test_doc_func_to_css
doc = CSSPool.CSS <<-eocss
div { border: foo(1, 2); }
eocss
assert_match('foo(1, 2)', doc.to_css)
end
def test_url
doc = CSSPool.CSS <<-eocss
div { background: url(http://example.com); }
eocss
assert_match 'http://example.com', doc.to_css
end
def test_url_capitalized
doc = CSSPool.CSS <<-eocss
div { background: URL(http://example.com); }
eocss
assert_match 'http://example.com', doc.to_css
end
def test_uri_linefeed_n
doc = CSSPool.CSS "div { background: url('http://\\\nexample.com/image.png') }"
# FIXME: (mt) Sort this out; these tests don't currently run, but should both run and pass
#assert_equal "http://\\\nexample.com/image.png", doc.rule_sets.first.declarations.first.expressions.first.value
end
def test_uri_linefeed_r
doc = CSSPool.CSS "div { background: url('http://\\\rexample.com/image.png') }"
# FIXME: (mt) Sort this out; these tests don't currently run, but should both run and pass
#assert_equal "http://\\\rexample.com/image.png", doc.rule_sets.first.declarations.first.expressions.first.value
end
def test_uri_linefeed_rn
doc = CSSPool.CSS "div { background: url('http://\\\r\nexample.com/image.png') }"
# FIXME: (mt) Sort this out; these tests don't currently run, but should both run and pass
#assert_equal "http://\\\r\nexample.com/image.png", doc.rule_sets.first.declarations.first.expressions.first.value
end
def test_error_message_context
begin
doc = CSSPool.CSS "syntax } error"
# should not reach this
assert false
rescue ParseError => ex
# ensure the context around the failing token (the bracket) are included
assert_match 'syntax } error', ex.message
end
end
def test_error_message_line
begin
doc = CSSPool.CSS "\n\n\nsyntax } error"
# should not reach this
assert false
rescue ParseError => ex
# ensure the context around the failing token (the bracket) are included
assert_match 'line 4', ex.message
end
end
def test_only_comments
CSSPool.CSS <<-eocss
/* Comment */
eocss
end
def test_comments
CSSPool.CSS <<-eocss
/* Comment */
a { color: blue;}
eocss
end
def test_comments_linebreak
CSSPool.CSS <<-eocss
/* Multi
line
comment */
a { color: blue;}
eocss
end
def test_comments_with_asterisk
CSSPool.CSS <<-eocss
/* Don't get confused by the * in this comment */
a { color: blue;}
eocss
end
def test_broken_comment
time = Benchmark.measure {
begin
CSSPool.CSS <<-eocss
/*
eocss
rescue
end
}
assert time.real < 1
end
def test_broken_comment_exclamation
time = Benchmark.measure {
begin
CSSPool.CSS <<-eocss
!
/**/
eocss
rescue
end
}
assert time.real < 1
end
end
end