forked from swc-project/swc
/
fixture.rs
127 lines (101 loc) · 3.26 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
//! 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();
}