-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathclass-wp-style-engine-css-rule-test.php
188 lines (161 loc) · 6.43 KB
/
class-wp-style-engine-css-rule-test.php
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
<?php
/**
* Tests the Style Engine CSS Rule class.
*
* @package Gutenberg
* @subpackage style-engine
*/
/**
* Tests for registering, storing and generating CSS rules.
*
* @group style-engine
* @coversDefaultClass WP_Style_Engine_CSS_Rule_Gutenberg
*/
class WP_Style_Engine_CSS_Rule_Test extends WP_UnitTestCase {
/**
* Tests that declarations are set on instantiation.
*
* @covers ::__construct
*/
public function test_should_instantiate_with_selector_and_rules() {
$selector = '.law-and-order';
$input_declarations = array(
'margin-top' => '10px',
'font-size' => '2rem',
);
$css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $input_declarations );
$css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( $selector, $css_declarations );
$this->assertSame( $selector, $css_rule->get_selector(), 'Return value of get_selector() does not match value passed to constructor.' );
$expected = "$selector{{$css_declarations->get_declarations_string()}}";
$this->assertSame( $expected, $css_rule->get_css(), 'Value returned by get_css() does not match expected declarations string.' );
}
/**
* Tests setting and getting a rules group.
*
* @covers ::set_rules_group
* @covers ::get_rules_group
*/
public function test_should_set_rules_group() {
$rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.heres-johnny', array(), '@layer state' );
$this->assertSame( '@layer state', $rule->get_rules_group(), 'Return value of get_rules_group() does not match value passed to constructor.' );
$rule->set_rules_group( '@layer pony' );
$this->assertSame( '@layer pony', $rule->get_rules_group(), 'Return value of get_rules_group() does not match value passed to set_rules_group().' );
}
/**
* Tests that declaration properties are deduplicated.
*
* @covers ::add_declarations
* @covers ::get_css
*/
public function test_should_dedupe_properties_in_rules() {
$selector = '.taggart';
$first_declaration = array(
'font-size' => '2rem',
);
$overwrite_first_declaration = array(
'font-size' => '4px',
);
$css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( $selector, $first_declaration );
$css_rule->add_declarations( new WP_Style_Engine_CSS_Declarations_Gutenberg( $overwrite_first_declaration ) );
$expected = '.taggart{font-size:4px;}';
$this->assertSame( $expected, $css_rule->get_css() );
}
/**
* Tests that declarations can be added to existing rules.
*
* @covers ::add_declarations
* @covers ::get_css
*/
public function test_should_add_declarations_to_existing_rules() {
// Declarations using a WP_Style_Engine_CSS_Declarations object.
$some_css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( array( 'margin-top' => '10px' ) );
// Declarations using a property => value array.
$some_more_css_declarations = array( 'font-size' => '1rem' );
$css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.hill-street-blues', $some_css_declarations );
$css_rule->add_declarations( $some_more_css_declarations );
$expected = '.hill-street-blues{margin-top:10px;font-size:1rem;}';
$this->assertSame( $expected, $css_rule->get_css() );
}
/**
* Tests setting a selector to a rule.
*
* @covers ::set_selector
*/
public function test_should_set_selector() {
$selector = '.taggart';
$css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( $selector );
$this->assertSame( $selector, $css_rule->get_selector(), 'Return value of get_selector() does not match value passed to constructor.' );
$css_rule->set_selector( '.law-and-order' );
$this->assertSame( '.law-and-order', $css_rule->get_selector(), 'Return value of get_selector() does not match value passed to set_selector().' );
}
/**
* Tests generating a CSS rule string.
*
* @covers ::get_css
*/
public function test_should_generate_css_rule_string() {
$selector = '.chips';
$input_declarations = array(
'margin-top' => '10px',
'font-size' => '2rem',
);
$css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $input_declarations );
$css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( $selector, $css_declarations );
$expected = "$selector{{$css_declarations->get_declarations_string()}}";
$this->assertSame( $expected, $css_rule->get_css() );
}
/**
* Tests that an empty string will be returned where there are no declarations in a CSS rule.
*
* @covers ::get_css
*/
public function test_should_return_empty_string_with_no_declarations() {
$selector = '.holmes';
$input_declarations = array();
$css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $input_declarations );
$css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( $selector, $css_declarations );
$this->assertSame( '', $css_rule->get_css() );
}
/**
* Tests that CSS rules are prettified.
*
* @covers ::get_css
*/
public function test_should_prettify_css_rule_output() {
$selector = '.baptiste';
$input_declarations = array(
'margin-left' => '0',
'font-family' => 'Detective Sans',
);
$css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $input_declarations );
$css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( $selector, $css_declarations );
$expected = '.baptiste {
margin-left: 0;
font-family: Detective Sans;
}';
$this->assertSame( $expected, $css_rule->get_css( true ) );
}
/**
* Tests that a string of multiple selectors is trimmed.
*
* @covers ::get_css
*/
public function test_should_trim_multiple_selectors() {
$selector = '.poirot, .poirot:active, #miss-marple > .st-mary-mead ';
$input_declarations = array(
'margin-left' => '0',
'font-family' => 'Detective Sans',
);
$css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $input_declarations );
$css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( $selector, $css_declarations );
$expected = '.poirot, .poirot:active, #miss-marple > .st-mary-mead {margin-left:0;font-family:Detective Sans;}';
$this->assertSame( $expected, $css_rule->get_css(), 'Return value should be not prettified.' );
$expected_prettified = '.poirot,
.poirot:active,
#miss-marple > .st-mary-mead {
margin-left: 0;
font-family: Detective Sans;
}';
$this->assertSame( $expected_prettified, $css_rule->get_css( true ), 'Return value should be prettified with new lines and indents.' );
}
}