]>
Commit | Line | Data |
---|---|---|
1 | // Take a look at the license at the top of the repository in the LICENSE file. | |
2 | ||
3 | extern crate minifier; | |
4 | ||
5 | use std::ops::Range; | |
6 | ||
7 | fn 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 | ||
23 | fn 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 { | |
31 | println!( | |
32 | "{}\n==== differs from: ====\n{}", | |
33 | &minified[get_ranges(pos, minified)], | |
34 | &str2[get_ranges(pos, str2)] | |
35 | ); | |
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] | |
55 | fn 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"); | |
60 | compare_strs(&minify(source).to_string(), expected_result); | |
61 | } |