-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
fixture.rs
210 lines (160 loc) · 5.57 KB
/
fixture.rs
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
//! Tests ported from https://github.com/thysultan/stylis.js
//!
//! License is MIT, which is original license at the time of copying.
//! Original test authors have copyright for their work.
#![deny(warnings)]
#![allow(clippy::needless_update)]
use std::path::PathBuf;
use swc_common::{errors::HANDLER, sync::Lrc, SourceFile};
use swc_css_ast::Stylesheet;
use swc_css_codegen::{
writer::basic::{BasicCssWriter, BasicCssWriterConfig},
CodegenConfig, Emit,
};
use swc_css_compat::{
compiler::{Compiler, Config},
feature::Features,
nesting::nesting,
};
use swc_css_parser::{parse_file, parser::ParserConfig};
use swc_css_visit::VisitMutWith;
use testing::NormalizedOutput;
fn parse_stylesheet(fm: &Lrc<SourceFile>) -> Stylesheet {
let mut errors = vec![];
let ss: Stylesheet = parse_file(
fm,
ParserConfig {
allow_wrong_line_comments: true,
..Default::default()
},
&mut errors,
)
.unwrap();
for err in errors {
HANDLER.with(|handler| {
err.to_diagnostics(handler).emit();
});
}
ss
}
fn print_stylesheet(ss: &Stylesheet) -> String {
let mut s = String::new();
{
let mut wr = BasicCssWriter::new(&mut s, None, BasicCssWriterConfig::default());
let mut gen = swc_css_codegen::CodeGenerator::new(&mut wr, CodegenConfig { minify: false });
gen.emit(&ss).unwrap();
}
s
}
fn test_nesting(input: PathBuf, suffix: Option<&str>) {
let parent = input.parent().unwrap();
let output = match suffix {
Some(suffix) => parent.join("output.".to_owned() + suffix + ".css"),
_ => parent.join("output.css"),
};
testing::run_test(false, |cm, _| {
let fm = cm.load_file(&input).unwrap();
let mut ss = parse_stylesheet(&fm);
ss.visit_mut_with(&mut nesting());
let s = print_stylesheet(&ss);
NormalizedOutput::from(s).compare_to_file(&output).unwrap();
Ok(())
})
.unwrap();
}
#[testing::fixture("tests/nesting/**/input.css")]
fn test_nesting_without_env(input: PathBuf) {
test_nesting(input, None)
}
#[testing::fixture("tests/custom-media-query/**/*.css", exclude("expect.css"))]
fn test_custom_media_query(input: PathBuf) {
let output = input.with_extension("expect.css");
testing::run_test(false, |cm, _| {
let fm = cm.load_file(&input).unwrap();
let mut ss = parse_stylesheet(&fm);
ss.visit_mut_with(&mut Compiler::new(Config {
process: Features::CUSTOM_MEDIA,
}));
let s = print_stylesheet(&ss);
NormalizedOutput::from(s).compare_to_file(&output).unwrap();
Ok(())
})
.unwrap();
}
#[testing::fixture("tests/media-query-ranges/**/*.css", exclude("expect.css"))]
fn test_media_query_ranges(input: PathBuf) {
let output = input.with_extension("expect.css");
testing::run_test(false, |cm, _| {
let fm = cm.load_file(&input).unwrap();
let mut ss = parse_stylesheet(&fm);
ss.visit_mut_with(&mut Compiler::new(Config {
process: Features::MEDIA_QUERY_RANGES,
}));
let s = print_stylesheet(&ss);
NormalizedOutput::from(s).compare_to_file(&output).unwrap();
Ok(())
})
.unwrap();
}
#[testing::fixture("tests/color-hex-alpha/**/*.css", exclude("expect.css"))]
fn test_color_hex_alpha(input: PathBuf) {
let output = input.with_extension("expect.css");
testing::run_test(false, |cm, _| {
let fm = cm.load_file(&input).unwrap();
let mut ss = parse_stylesheet(&fm);
ss.visit_mut_with(&mut Compiler::new(Config {
process: Features::COLOR_HEX_ALPHA,
}));
let s = print_stylesheet(&ss);
NormalizedOutput::from(s).compare_to_file(&output).unwrap();
Ok(())
})
.unwrap();
}
#[testing::fixture("tests/color-legacy/**/*.css", exclude("expect.css"))]
fn test_color_space_separated_function_notation(input: PathBuf) {
let output = input.with_extension("expect.css");
testing::run_test(false, |cm, _| {
let fm = cm.load_file(&input).unwrap();
let mut ss = parse_stylesheet(&fm);
ss.visit_mut_with(&mut Compiler::new(Config {
process: Features::COLOR_SPACE_SEPARATED_PARAMETERS
| Features::COLOR_ALPHA_PARAMETER
| Features::COLOR_LEGACY_RGB_AND_HSL,
}));
let s = print_stylesheet(&ss);
NormalizedOutput::from(s).compare_to_file(&output).unwrap();
Ok(())
})
.unwrap();
}
#[testing::fixture("tests/selector-not/**/*.css", exclude("expect.css"))]
fn test_selector_not(input: PathBuf) {
let output = input.with_extension("expect.css");
testing::run_test(false, |cm, _| {
let fm = cm.load_file(&input).unwrap();
let mut ss = parse_stylesheet(&fm);
ss.visit_mut_with(&mut Compiler::new(Config {
process: Features::SELECTOR_NOT,
}));
let s = print_stylesheet(&ss);
NormalizedOutput::from(s).compare_to_file(&output).unwrap();
Ok(())
})
.unwrap();
}
#[testing::fixture("tests/color-hwb/**/*.css", exclude("expect.css"))]
fn test_color_hwb(input: PathBuf) {
let output = input.with_extension("expect.css");
testing::run_test(false, |cm, _| {
let fm = cm.load_file(&input).unwrap();
let mut ss = parse_stylesheet(&fm);
ss.visit_mut_with(&mut Compiler::new(Config {
process: Features::COLOR_HWB,
}));
let s = print_stylesheet(&ss);
NormalizedOutput::from(s).compare_to_file(&output).unwrap();
Ok(())
})
.unwrap();
}