Skip to content

Commit 4c31c81

Browse files
committed
test: Move tests to a separate directory
1 parent 672a96f commit 4c31c81

File tree

3 files changed

+67
-90
lines changed

3 files changed

+67
-90
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ readme = "README.md"
88
description = "A crate for inlining CSS into HTML documents"
99
repository = "https://github.com/Stranger6667/css-inline"
1010
keywords = ["html", "css"]
11-
exclude = [".github", ".pre-commit-config.yaml", ".yamllint", ".gitignore"]
11+
exclude = [".github", ".pre-commit-config.yaml", ".yamllint", ".gitignore", "tests"]
1212
categories = ["web-programming"]
1313

1414
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

src/lib.rs

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -197,92 +197,3 @@ fn merge_styles(existing_style: &str, new_styles: &[Declaration]) -> Result<Stri
197197
.map(|(key, value)| format!("{}:{};", key, value))
198198
.collect::<String>())
199199
}
200-
201-
#[cfg(test)]
202-
mod tests {
203-
use crate::*;
204-
205-
const HTML: &str = r#"<html>
206-
<head>
207-
<title>Test</title>
208-
<style>
209-
h1, h2 { color:red; }
210-
strong {
211-
text-decoration:none
212-
}
213-
p { font-size:2px }
214-
p.footer { font-size: 1px}
215-
</style>
216-
</head>
217-
<body>
218-
<h1>Big Text</h1>
219-
<p><strong>Yes!</strong></p>
220-
<p class="footer">Foot notes</p>
221-
</body>
222-
</html>"#;
223-
224-
#[test]
225-
fn test_inline() {
226-
let inlined = inline(HTML).expect("Should be valid");
227-
assert_eq!(
228-
inlined,
229-
r#"<html><head>
230-
<title>Test</title>
231-
<style>
232-
h1, h2 { color:red; }
233-
strong {
234-
text-decoration:none
235-
}
236-
p { font-size:2px }
237-
p.footer { font-size: 1px}
238-
</style>
239-
</head>
240-
<body>
241-
<h1 style="color:red;">Big Text</h1>
242-
<p style="font-size:2px ;"><strong style="text-decoration:none
243-
;">Yes!</strong></p>
244-
<p class="footer" style="font-size: 1px;">Foot notes</p>
245-
246-
</body></html>"#
247-
)
248-
}
249-
250-
#[test]
251-
fn test_merge_styles() {
252-
let html = r#"<html>
253-
<head>
254-
<title>Test</title>
255-
<style>
256-
h1 { color:red; }
257-
</style>
258-
</head>
259-
<body>
260-
<h1 style="font-size: 1px">Big Text</h1>
261-
</body>
262-
</html>"#;
263-
let inlined = inline(html).expect("Should be valid");
264-
let valid = (inlined
265-
== r#"<html><head>
266-
<title>Test</title>
267-
<style>
268-
h1 { color:red; }
269-
</style>
270-
</head>
271-
<body>
272-
<h1 style="color:red;font-size: 1px;">Big Text</h1>
273-
274-
</body></html>"#)
275-
|| (inlined
276-
== r#"<html><head>
277-
<title>Test</title>
278-
<style>
279-
h1 { color:red; }
280-
</style>
281-
</head>
282-
<body>
283-
<h1 style="font-size: 1px;color:red;">Big Text</h1>
284-
285-
</body></html>"#);
286-
assert!(valid, inlined)
287-
}
288-
}

tests/test_inlining.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use css_inline::inline;
2+
3+
macro_rules! html {
4+
($style: expr, $body: expr) => {
5+
format!(
6+
r#"<html><head><title>Test</title><style>{}</style></head><body>{}</body></html>"#,
7+
$style, $body
8+
)
9+
};
10+
}
11+
12+
macro_rules! assert_inlined {
13+
(style = $style: expr, body = $body: expr, expected = $expected: expr) => {{
14+
let html = html!($style, $body);
15+
let inlined = inline(&html).unwrap();
16+
assert_eq!(inlined, html!($style, $expected))
17+
}};
18+
}
19+
20+
#[test]
21+
fn no_existing_style() {
22+
// When no "style" attributes exist
23+
assert_inlined!(
24+
style = r#"h1, h2 { color:red; }
25+
strong { text-decoration:none }
26+
p { font-size:2px }
27+
p.footer { font-size: 1px}"#,
28+
body = r#"<h1>Big Text</h1>
29+
<p><strong>Yes!</strong></p>
30+
<p class="footer">Foot notes</p>"#,
31+
// Then all styles should be added to new "style" attributes
32+
expected = r#"<h1 style="color:red;">Big Text</h1>
33+
<p style="font-size:2px ;"><strong style="text-decoration:none ;">Yes!</strong></p>
34+
<p class="footer" style="font-size: 1px;">Foot notes</p>"#
35+
)
36+
}
37+
38+
#[test]
39+
fn simple_merge() {
40+
// When "style" attributes exist and collides with values defined in "style" tag
41+
let style = "h1 { color:red; }";
42+
let html = html!(style, r#"<h1 style="font-size: 1px">Big Text</h1>"#);
43+
let inlined = inline(&html).unwrap();
44+
// Then new styles should be merged with the existing ones
45+
let option_1 = html!(
46+
style,
47+
r#"<h1 style="font-size: 1px;color:red;">Big Text</h1>"#
48+
);
49+
let option_2 = html!(
50+
style,
51+
r#"<h1 style="color:red;font-size: 1px;">Big Text</h1>"#
52+
);
53+
let valid = (inlined == option_1) || (inlined == option_2);
54+
assert!(valid, inlined);
55+
}
56+
57+
#[test]
58+
fn overloaded_styles() {
59+
// When there is a style, applied to an ID
60+
assert_inlined!(
61+
style = "h1 { color: red; } #test { color: blue; }",
62+
body = r#"<h1 id="test">Hello world!</h1>"#,
63+
// Then it should be preferred over a more generic style
64+
expected = r#"<h1 id="test" style="color: blue;">Hello world!</h1>"#
65+
)
66+
}

0 commit comments

Comments
 (0)