]> git.proxmox.com Git - rustc.git/blame - vendor/minifier/tests/js_minify.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / vendor / minifier / tests / js_minify.rs
CommitLineData
5099ac24
FG
1// Take a look at the license at the top of the repository in the LICENSE file.
2
416331ca
XL
3extern crate minifier;
4
5use std::ops::Range;
6
7fn get_ranges(pos: usize, s: &str) -> Range<usize> {
8 let mut start = pos;
9 let mut end = pos;
10 if start >= 20 {
11 start -= 20;
12 } else {
13 start = 0;
14 }
15 if end < s.len() - 20 {
16 end += 20;
17 } else {
18 end = s.len();
19 }
20 start..end
21}
22
23fn compare_strs(minified: &str, str2: &str) {
24 let mut it1 = minified.char_indices();
25 let mut it2 = str2.char_indices();
26
27 loop {
28 match (it1.next(), it2.next()) {
29 (Some((pos, c1)), Some((_, c2))) => {
30 if c1 != c2 {
6a06907d
XL
31 println!(
32 "{}\n==== differs from: ====\n{}",
33 &minified[get_ranges(pos, minified)],
34 &str2[get_ranges(pos, str2)]
35 );
416331ca
XL
36 panic!("Chars differ");
37 }
38 }
39 (None, Some((pos, _))) => {
40 println!("missing: {}...", &str2[get_ranges(pos + 20, str2)]);
41 panic!("Missing parts of minified content");
42 }
43 (Some((pos, _)), None) => {
44 println!("extra: {}...", &minified[get_ranges(pos + 20, minified)]);
45 panic!("Extra part in minified content");
46 }
47 (None, None) => {
48 break;
49 }
50 }
51 }
52}
53
54#[test]
55fn test_minification() {
56 use minifier::js::minify;
57
58 let source = include_str!("./files/main.js");
59 let expected_result = include_str!("./files/minified_main.js");
923072b8 60 compare_strs(&minify(source).to_string(), expected_result);
416331ca 61}