csspool 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/helper.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "test/unit"
2
+
3
+ require "rubygems"
4
+ require "flexmock"
5
+ require "flexmock/test_unit"
6
+
7
+ require "css/sac"
data/test/test_all.rb ADDED
@@ -0,0 +1,4 @@
1
+ Dir[File.dirname(__FILE__) + "/test_*.rb"].
2
+ reject { |f| f == __FILE__ }.
3
+ collect { |f| File.basename(f, ".rb") }.
4
+ each { |f| require f }
@@ -0,0 +1,39 @@
1
+ require File.dirname(__FILE__) + "/helper"
2
+
3
+ class LexemeTest < Test::Unit::TestCase
4
+ include CSS
5
+ #include CSS::SAC # grrr annoying: SAC should be a module
6
+
7
+ def test_lexemes_have_a_required_name
8
+ assert_equal(:foo, SAC::Lexeme.new(:foo, /foo/).name)
9
+ assert_raise(ArgumentError) { SAC::Lexeme.new(nil, /foo/) }
10
+ end
11
+
12
+ def test_lexemes_require_at_least_one_pattern
13
+ assert_raise(ArgumentError) { SAC::Lexeme.new(:foo) }
14
+ end
15
+
16
+ def test_simple_lexemes_can_specify_a_pattern_inline
17
+ lexeme = SAC::Lexeme.new(:foo, /bar/)
18
+ assert(lexeme.pattern.match("bar"))
19
+ end
20
+
21
+ def test_more_patterns_can_be_added_in_a_block
22
+ lexeme = SAC::Lexeme.new(:foo) do |patterns|
23
+ patterns << /foo/
24
+ patterns << /bar/
25
+ patterns << /baz/
26
+ end
27
+
28
+ # ...but the single exposed lexeme pattern is a union
29
+ %w(foo bar baz).each do |candidate|
30
+ assert_match(lexeme.pattern, candidate)
31
+ end
32
+ end
33
+
34
+ def test_patterns_are_start_anchored_and_case_insensitive
35
+ lexeme = SAC::Lexeme.new(:foo, /foo/)
36
+ assert_match(lexeme.pattern, "FOO")
37
+ assert_no_match(lexeme.pattern, " foo")
38
+ end
39
+ end
@@ -0,0 +1,96 @@
1
+ require File.dirname(__FILE__) + "/helper"
2
+
3
+ class LexicalUnitTest < Test::Unit::TestCase
4
+ def setup
5
+ @sac = CSS::SAC::Parser.new()
6
+ end
7
+
8
+ def test_invalid_property
9
+ flexmock(@sac.document_handler).
10
+ should_receive(:property).never
11
+
12
+ @sac.parse('h1 { background-color: lakdjsfhlakdhjsf; }')
13
+ flexmock_verify
14
+ end
15
+
16
+ def test_color
17
+ flexmock(@sac.document_handler).
18
+ should_receive(:property).with('background-color', on { |list|
19
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
20
+ list.first.lexical_unit_type == :SAC_RGBCOLOR &&
21
+ list.first.string_value == "#345" &&
22
+ list.first.parameters.length == 3 &&
23
+ list.first.parameters.map { |x| x.integer_value } == [3,4,5] &&
24
+ list.first.integer_value.nil?
25
+ }, false).once
26
+
27
+ @sac.parse('h1 { background-color: #345; }')
28
+ flexmock_verify
29
+ end
30
+
31
+ def test_color_6
32
+ flexmock(@sac.document_handler).
33
+ should_receive(:property).with('background-color', on { |list|
34
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
35
+ list.first.lexical_unit_type == :SAC_RGBCOLOR &&
36
+ list.first.string_value == "#330405" &&
37
+ list.first.parameters.length == 3 &&
38
+ list.first.parameters.map { |x| x.integer_value } == [51,4,5] &&
39
+ list.first.integer_value.nil?
40
+ }, false).once
41
+
42
+ @sac.parse('h1 { background-color: #330405; }')
43
+ flexmock_verify
44
+ end
45
+
46
+ def test_uri
47
+ flexmock(@sac.document_handler).
48
+ should_receive(:property).with('content', on { |list|
49
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
50
+ list.first.lexical_unit_type == :SAC_URI &&
51
+ list.first.string_value == "\"aaron\"" &&
52
+ list.first.integer_value.nil?
53
+ }, false).once
54
+
55
+ @sac.parse('h1 { content: url("aaron"); }')
56
+ flexmock_verify
57
+ end
58
+
59
+ def test_string
60
+ flexmock(@sac.document_handler).
61
+ should_receive(:property).with('content', on { |list|
62
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
63
+ list.first.lexical_unit_type == :SAC_STRING_VALUE &&
64
+ list.first.string_value == "\"aaron\"" &&
65
+ list.first.integer_value.nil?
66
+ }, false).once
67
+
68
+ @sac.parse('h1 { content: "aaron"; }')
69
+ flexmock_verify
70
+ end
71
+
72
+ def test_ident
73
+ flexmock(@sac.document_handler).
74
+ should_receive(:property).with('color', on { |list|
75
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
76
+ list.first.lexical_unit_type == :SAC_IDENT &&
77
+ list.first.string_value == "black" &&
78
+ list.first.integer_value.nil?
79
+ }, false).once
80
+
81
+ @sac.parse('h1 { color: black; }')
82
+ flexmock_verify
83
+ end
84
+
85
+ def test_mm
86
+ flexmock(@sac.document_handler).
87
+ should_receive(:property).with('height', on { |list|
88
+ list.length == 1 && list.first.dimension_unit_text == 'mm' &&
89
+ list.first.lexical_unit_type == :SAC_MILLIMETER &&
90
+ list.first.integer_value == 1
91
+ }, false).once
92
+
93
+ @sac.parse('h1 { height: 1mm; }')
94
+ flexmock_verify
95
+ end
96
+ end
@@ -0,0 +1,199 @@
1
+ require File.dirname(__FILE__) + "/helper"
2
+
3
+ class ParseErrorTest < Test::Unit::TestCase
4
+ def setup
5
+ @sac = CSS::SAC::Parser.new()
6
+ end
7
+
8
+ def test_unknown_properties
9
+ flexmock(@sac.document_handler).
10
+ should_receive(:start_document).ordered.once
11
+ flexmock(@sac.document_handler).
12
+ should_receive(:property).ordered.with('color', on { |list|
13
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
14
+ list.first.lexical_unit_type == :SAC_IDENT &&
15
+ list.first.string_value == "red" &&
16
+ list.first.integer_value.nil?
17
+ }, false).once
18
+ flexmock(@sac.error_handler).
19
+ should_receive(:error).ordered.once
20
+ flexmock(@sac.document_handler).
21
+ should_receive(:property).ordered.with('color', on { |list|
22
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
23
+ list.first.lexical_unit_type == :SAC_IDENT &&
24
+ list.first.string_value == "black" &&
25
+ list.first.integer_value.nil?
26
+ }, false).once
27
+ flexmock(@sac.document_handler).
28
+ should_receive(:end_document).ordered.once
29
+ @sac.parse('h1 { color: red; rotation: 70minutes; } h2 { color: black }')
30
+ flexmock_verify
31
+ end
32
+
33
+ def test_error_media
34
+ flexmock(@sac.document_handler).
35
+ should_receive(:start_media).ordered.
36
+ with(['all']).once
37
+ flexmock(@sac.error_handler).
38
+ should_receive(:error).ordered.once
39
+ flexmock(@sac.document_handler).
40
+ should_receive(:end_media).ordered
41
+ @sac.parse('@media all and (min-width:0px) { }')
42
+ flexmock_verify
43
+ end
44
+
45
+ def test_bad_selector
46
+ flexmock(@sac.document_handler).
47
+ should_receive(:start_selector).ordered.once
48
+ flexmock(@sac.document_handler).
49
+ should_receive(:property).ordered.with('height', any, false).once
50
+ flexmock(@sac.document_handler).
51
+ should_receive(:end_selector).ordered.once
52
+
53
+ @sac.parse('head~body div#user-section { height: 0; }')
54
+ flexmock_verify
55
+ end
56
+
57
+ def test_malformed_decl_missing_value
58
+ flexmock(@sac.document_handler).
59
+ should_receive(:property).ordered.with('color', on { |list|
60
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
61
+ list.first.lexical_unit_type == :SAC_IDENT &&
62
+ list.first.string_value == "green" &&
63
+ list.first.integer_value.nil?
64
+ }, false).once
65
+ @sac.parse('p { color:green; color }')
66
+ flexmock_verify
67
+ end
68
+
69
+ def test_function_recover
70
+ flexmock(@sac.document_handler).
71
+ should_receive(:property).ordered.with('color', on { |list|
72
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
73
+ list.first.lexical_unit_type == :SAC_IDENT &&
74
+ list.first.string_value == "red" &&
75
+ list.first.integer_value.nil?
76
+ }, false).once
77
+ @sac.parse('#modal_cover { filter: alpha(opacity=75); color: red; }')
78
+ flexmock_verify
79
+ end
80
+
81
+ def test_malformed_decl_missing_value_recovery
82
+ flexmock(@sac.document_handler).
83
+ should_receive(:property).ordered.with('color', on { |list|
84
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
85
+ list.first.lexical_unit_type == :SAC_IDENT &&
86
+ list.first.string_value == "red" &&
87
+ list.first.integer_value.nil?
88
+ }, false).once
89
+ flexmock(@sac.document_handler).
90
+ should_receive(:property).ordered.with('color', on { |list|
91
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
92
+ list.first.lexical_unit_type == :SAC_IDENT &&
93
+ list.first.string_value == "green" &&
94
+ list.first.integer_value.nil?
95
+ }, false).once
96
+ @sac.parse('p { color:red; color; color:green }')
97
+ flexmock_verify
98
+ end
99
+
100
+ def test_malformed_decl_missing_value_with_colon
101
+ flexmock(@sac.document_handler).
102
+ should_receive(:property).ordered.with('color', on { |list|
103
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
104
+ list.first.lexical_unit_type == :SAC_IDENT &&
105
+ list.first.string_value == "green" &&
106
+ list.first.integer_value.nil?
107
+ }, false).once
108
+ @sac.parse('p { color:green; color: }')
109
+ flexmock_verify
110
+ end
111
+
112
+ def test_malformed_decl_missing_value_recovery_with_colon
113
+ flexmock(@sac.document_handler).
114
+ should_receive(:property).ordered.with('color', on { |list|
115
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
116
+ list.first.lexical_unit_type == :SAC_IDENT &&
117
+ list.first.string_value == "red" &&
118
+ list.first.integer_value.nil?
119
+ }, false).once
120
+ flexmock(@sac.document_handler).
121
+ should_receive(:property).ordered.with('color', on { |list|
122
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
123
+ list.first.lexical_unit_type == :SAC_IDENT &&
124
+ list.first.string_value == "green" &&
125
+ list.first.integer_value.nil?
126
+ }, false).once
127
+ @sac.parse('p { color:red; color:; color:green }')
128
+ flexmock_verify
129
+ end
130
+
131
+ def test_malformed_decl_missing_value_unexpected_tokens
132
+ flexmock(@sac.document_handler).
133
+ should_receive(:property).ordered.with('color', on { |list|
134
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
135
+ list.first.lexical_unit_type == :SAC_IDENT &&
136
+ list.first.string_value == "green" &&
137
+ list.first.integer_value.nil?
138
+ }, false).once
139
+ @sac.parse('p { color:green; color{;color:maroon} }')
140
+ flexmock_verify
141
+ end
142
+
143
+ def test_malformed_decl_missing_value_unexpected_tokens_recover
144
+ flexmock(@sac.document_handler).
145
+ should_receive(:property).ordered.with('color', on { |list|
146
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
147
+ list.first.lexical_unit_type == :SAC_IDENT &&
148
+ list.first.string_value == "red" &&
149
+ list.first.integer_value.nil?
150
+ }, false).once
151
+ flexmock(@sac.document_handler).
152
+ should_receive(:property).ordered.with('color', on { |list|
153
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
154
+ list.first.lexical_unit_type == :SAC_IDENT &&
155
+ list.first.string_value == "green" &&
156
+ list.first.integer_value.nil?
157
+ }, false).once
158
+ @sac.parse('p { color:red; color{;color:maroon}; color:green }')
159
+ flexmock_verify
160
+ end
161
+
162
+ def test_invalid_at_keyword
163
+ flexmock(@sac.document_handler).
164
+ should_receive(:property).ordered.with('color', on { |list|
165
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
166
+ list.first.lexical_unit_type == :SAC_IDENT &&
167
+ list.first.string_value == "blue" &&
168
+ list.first.integer_value.nil?
169
+ }, false).once
170
+ @sac.parse(
171
+ '@three-dee {
172
+ @background-lighting {
173
+ azimuth: 30deg;
174
+ elevation: 190deg;
175
+ }
176
+ h1 { color: red }
177
+ }
178
+ h1 { color: blue }')
179
+ end
180
+
181
+ def test_extra_semi
182
+ flexmock(@sac.document_handler).
183
+ should_receive(:property).ordered.with('color', on { |list|
184
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
185
+ list.first.lexical_unit_type == :SAC_IDENT &&
186
+ list.first.string_value == "red" &&
187
+ list.first.integer_value.nil?
188
+ }, false).once
189
+ flexmock(@sac.document_handler).
190
+ should_receive(:property).ordered.with('color', on { |list|
191
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
192
+ list.first.lexical_unit_type == :SAC_IDENT &&
193
+ list.first.string_value == "green" &&
194
+ list.first.integer_value.nil?
195
+ }, false).once
196
+ @sac.parse('p { color:red; ; color:green }')
197
+ flexmock_verify
198
+ end
199
+ end
@@ -0,0 +1,183 @@
1
+ require File.dirname(__FILE__) + "/helper"
2
+
3
+ class ParserTest < Test::Unit::TestCase
4
+ def setup
5
+ @sac = CSS::SAC::Parser.new()
6
+ end
7
+
8
+ def teardown
9
+ flexmock_verify
10
+ flexmock_close
11
+ end
12
+
13
+ def test_page
14
+ flexmock(@sac.document_handler).
15
+ should_receive(:start_page).ordered.
16
+ with('foo', 'print').once
17
+ flexmock(@sac.document_handler).
18
+ should_receive(:property).ordered.with('color', on { |list|
19
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
20
+ list.first.lexical_unit_type == :SAC_IDENT &&
21
+ list.first.string_value == "black" &&
22
+ list.first.integer_value.nil?
23
+ }, false).once
24
+ flexmock(@sac.document_handler).
25
+ should_receive(:end_page).ordered.
26
+ with('foo', 'print').once
27
+ @sac.parse('@page foo:print { color: black }')
28
+ end
29
+
30
+ def test_empty
31
+ assert_nothing_raised {
32
+ @sac.parse('')
33
+ }
34
+ end
35
+
36
+ def test_at_import
37
+ flexmock(@sac.document_handler).
38
+ should_receive(:import_style).ordered.
39
+ with('"subs.css"', []).once
40
+ @sac.parse('@import "subs.css";')
41
+ flexmock_verify
42
+ end
43
+
44
+ def test_multi_import
45
+ flexmock(@sac.document_handler).
46
+ should_receive(:import_style).ordered.
47
+ with('"subs.css"', []).once
48
+ flexmock(@sac.document_handler).
49
+ should_receive(:import_style).ordered.
50
+ with('"subs1.css"', []).once
51
+ @sac.parse('@import "subs.css"; @import "subs1.css";')
52
+ flexmock_verify
53
+ end
54
+
55
+ def test_media
56
+ flexmock(@sac.document_handler).
57
+ should_receive(:start_media).ordered.
58
+ with(['print', 'tv']).once
59
+ flexmock(@sac.document_handler).
60
+ should_receive(:start_selector).ordered.once
61
+ flexmock(@sac.document_handler).
62
+ should_receive(:property).ordered.with('color', on { |list|
63
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
64
+ list.first.lexical_unit_type == :SAC_IDENT &&
65
+ list.first.string_value == "black" &&
66
+ list.first.integer_value.nil?
67
+ }, false).once
68
+ flexmock(@sac.document_handler).
69
+ should_receive(:end_selector).ordered.once
70
+ flexmock(@sac.document_handler).
71
+ should_receive(:end_media).ordered.
72
+ with(['print', 'tv']).once
73
+ @sac.parse('@media print, tv { h1 { color: black } }')
74
+ end
75
+
76
+ def test_media_single
77
+ flexmock(@sac.document_handler).
78
+ should_receive(:start_media).ordered.
79
+ with(['print']).once
80
+ flexmock(@sac.document_handler).
81
+ should_receive(:end_media).ordered.
82
+ with(['print']).once
83
+ @sac.parse('@media print { h1 { color: black } }')
84
+ end
85
+
86
+ def test_comment
87
+ flexmock(@sac.document_handler).
88
+ should_receive(:comment).ordered.
89
+ with("/* sup bro */").once
90
+ @sac.parse('/* sup bro */')
91
+ end
92
+
93
+ def test_weird
94
+ @sac.parse('#topnav legend { display: none; }')
95
+ end
96
+
97
+ def test_ignorable_at_rule
98
+ flexmock(@sac.document_handler).
99
+ should_receive(:start_document).ordered.once
100
+ flexmock(@sac.document_handler).
101
+ should_receive(:ignorable_at_rule).ordered.
102
+ with("blah").once
103
+ flexmock(@sac.document_handler).
104
+ should_receive(:end_document).ordered.once
105
+ @sac.parse('@blah "style.css";')
106
+ end
107
+
108
+ def test_selector
109
+ flexmock(@sac.document_handler).
110
+ should_receive(:start_document).ordered.once
111
+ flexmock(@sac.document_handler).
112
+ should_receive(:start_selector).ordered.once
113
+ flexmock(@sac.document_handler).
114
+ should_receive(:property).ordered.with('color', on { |list|
115
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
116
+ list.first.lexical_unit_type == :SAC_IDENT &&
117
+ list.first.string_value == "black" &&
118
+ list.first.integer_value.nil?
119
+ }, false).once
120
+ flexmock(@sac.document_handler).
121
+ should_receive(:end_selector).ordered.once
122
+ flexmock(@sac.document_handler).
123
+ should_receive(:end_document).ordered.once
124
+
125
+ @sac.parse('div h1 { color: black; }')
126
+ flexmock_verify
127
+ end
128
+
129
+ def test_properties
130
+ flexmock(@sac.document_handler).
131
+ should_receive(:property).with('color', on { |list|
132
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
133
+ list.first.lexical_unit_type == :SAC_IDENT &&
134
+ list.first.string_value == "black" &&
135
+ list.first.integer_value.nil?
136
+ }, false).once
137
+ @sac.parse('div h1 { color: black; }')
138
+ flexmock_verify
139
+
140
+ flexmock(@sac.document_handler).
141
+ should_receive(:property).never
142
+ @sac.parse('div h1 { }')
143
+ flexmock_verify
144
+ end
145
+
146
+ def test_properties_unary_op
147
+ flexmock(@sac.document_handler).
148
+ should_receive(:property).with('border-spacing', on { |list|
149
+ list.length == 1 && list.first.dimension_unit_text == 'em' &&
150
+ list.first.lexical_unit_type == :SAC_EM &&
151
+ list.first.integer_value == -10
152
+ }, false).once
153
+ @sac.parse('div h1 { border-spacing: -10em; }')
154
+ @sac.parse('div h1 { border: dashed black dotted; }')
155
+ flexmock_verify
156
+ end
157
+
158
+ def test_properties_function
159
+ flexmock(@sac.document_handler).
160
+ should_receive(:property).with('clip', on { |list|
161
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
162
+ list.first.lexical_unit_type == :SAC_RECT_FUNCTION &&
163
+ list.first.string_value == "rect(1in, 1in, 1in, 2in)" &&
164
+ list.first.parameters.length == 4 &&
165
+ list.first.parameters.map { |x| x.to_s } == %w{ 1in 1in 1in 2in } &&
166
+ list.first.integer_value.nil?
167
+ }, false).once
168
+ @sac.parse('div h1 { clip: rect(1in, 1in, 1in, 2in); }')
169
+ flexmock_verify
170
+ end
171
+
172
+ def test_important_properties
173
+ flexmock(@sac.document_handler).
174
+ should_receive(:property).with('color', on { |list|
175
+ list.length == 1 && list.first.dimension_unit_text.nil? &&
176
+ list.first.lexical_unit_type == :SAC_IDENT &&
177
+ list.first.string_value == "black" &&
178
+ list.first.integer_value.nil?
179
+ }, true).once
180
+ @sac.parse('h1 { color: black !important; }')
181
+ flexmock_verify
182
+ end
183
+ end