|
2 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this
|
3 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
4 | 4 |
|
5 |
| -#![feature(macro_rules, globs)] |
| 5 | +#![feature(macro_rules, globs, phase)] |
| 6 | + |
| 7 | +#[phase(plugin, link)] extern crate log; |
6 | 8 |
|
7 | 9 | extern crate serialize;
|
8 | 10 | extern crate encoding;
|
9 | 11 | extern crate cssparser;
|
10 | 12 |
|
11 |
| -use std::{io, task}; |
12 |
| -use std::io::{File, Command, Writer, TempDir}; |
13 |
| -use serialize::{json}; |
14 |
| -use serialize::json::ToJson; |
15 |
| - |
16 |
| -use encoding::label::encoding_from_whatwg_label; |
| 13 | +use std::io; |
| 14 | +use std::io::{File, Writer}; |
17 | 15 |
|
18 | 16 | use cssparser::*;
|
19 | 17 | use cssparser::ast::*;
|
20 | 18 |
|
21 |
| -fn write_whole_file(path: &Path, data: &str) { |
22 |
| - match File::open_mode(path, io::Open, io::Write) { |
23 |
| - Ok(mut writer) => { writer.write(data.as_bytes()).unwrap(); }, |
24 |
| - _ => fail!("could not open file"), |
25 |
| - } |
26 |
| -} |
| 19 | +macro_rules! try_write( |
| 20 | + ($buf:expr, $($args:tt)*) => (match write!($buf, $($args)*) { Ok(e) => e, Err(_e) => return Err(()) }) |
| 21 | +) |
27 | 22 |
|
| 23 | +fn print_css(mut rules: Vec<Result<Rule, SyntaxError>>) -> Result<String, ()> { |
| 24 | + let mut buffer = io::MemWriter::new(); |
28 | 25 |
|
29 |
| -fn almost_equals(a: &json::Json, b: &json::Json) -> bool { |
30 |
| - match (a, b) { |
31 |
| - (&json::Number(a), &json::Number(b)) => (a - b).abs() < 1e-6, |
32 |
| - (&json::String(ref a), &json::String(ref b)) => a == b, |
33 |
| - (&json::Boolean(a), &json::Boolean(b)) => a == b, |
34 |
| - (&json::List(ref a), &json::List(ref b)) |
35 |
| - => a.iter().zip(b.iter()).all(|(ref a, ref b)| almost_equals(*a, *b)), |
36 |
| - (&json::Object(_), &json::Object(_)) => fail!("Not implemented"), |
37 |
| - (&json::Null, &json::Null) => true, |
38 |
| - _ => false, |
| 26 | + fn print_prelude(buffer: &mut io::MemWriter, |
| 27 | + prelude: &mut Vec<ComponentValue>) -> Result<(), ()> { |
| 28 | + for p in prelude.mut_iter() { |
| 29 | + try_write!(buffer, "{}", p.to_css()); |
| 30 | + } |
| 31 | + Ok(()) |
39 | 32 | }
|
40 |
| -} |
41 | 33 |
|
| 34 | + fn print_block(buffer: &mut io::MemWriter, |
| 35 | + block: &mut Vec<Node>) -> Result<(), ()> { |
| 36 | + try_write!(buffer, "{{"); |
| 37 | + for &(ref mut b, _) in block.mut_iter() { |
| 38 | + try_write!(buffer, "{}", b.to_css()); |
| 39 | + } |
| 40 | + try_write!(buffer, "}}"); |
42 | 41 |
|
43 |
| -fn assert_json_eq(results: json::Json, expected: json::Json, message: String) { |
44 |
| - if !almost_equals(&results, &expected) { |
45 |
| - let temp = TempDir::new("rust-cssparser-tests").unwrap(); |
46 |
| - let results = results.to_pretty_str().append("\n"); |
47 |
| - let expected = expected.to_pretty_str().append("\n"); |
48 |
| - task::try(proc() { |
49 |
| - let mut result_path = temp.path().clone(); |
50 |
| - result_path.push("results.json"); |
51 |
| - let mut expected_path = temp.path().clone(); |
52 |
| - expected_path.push("expected.json"); |
53 |
| - write_whole_file(&result_path, results.as_slice()); |
54 |
| - write_whole_file(&expected_path, expected.as_slice()); |
55 |
| - Command::new("colordiff") |
56 |
| - .arg("-u1000") |
57 |
| - .arg(result_path) |
58 |
| - .arg(expected_path) |
59 |
| - .status().unwrap_or_else(|e| fail!("Failed to get status of colordiff: {}", e)); |
60 |
| - }).unwrap_or_else(|_e| fail!("Failed to execute task")); |
61 |
| - |
62 |
| - fail!(message) |
| 42 | + Ok(()) |
63 | 43 | }
|
64 |
| -} |
65 | 44 |
|
66 |
| - |
67 |
| -fn run_raw_json_tests(json_data: &str, run: |json::Json, json::Json|) { |
68 |
| - let items = match json::from_str(json_data) { |
69 |
| - Ok(json::List(items)) => items, |
70 |
| - _ => fail!("Invalid JSON") |
71 |
| - }; |
72 |
| - assert!(items.len() % 2 == 0); |
73 |
| - let mut input = None; |
74 |
| - for item in items.move_iter() { |
75 |
| - match (&input, item) { |
76 |
| - (&None, json_obj) => input = Some(json_obj), |
77 |
| - (&Some(_), expected) => { |
78 |
| - let input = input.take_unwrap(); |
79 |
| - run(input, expected) |
| 45 | + for r in rules.mut_iter() { |
| 46 | + match *r { |
| 47 | + Ok(ref mut rule) => { |
| 48 | + match *rule { |
| 49 | + QualifiedRule(ref mut q) => { |
| 50 | + try!(print_prelude(&mut buffer, &mut q.prelude)); |
| 51 | + try!(print_block(&mut buffer, &mut q.block)); |
| 52 | + }, |
| 53 | + AtRule(ref mut a) => { |
| 54 | + //try_write!(buffer, "{}\n", a); |
| 55 | + try_write!(buffer, "@{}", a.name); |
| 56 | + try!(print_prelude(&mut buffer, &mut a.prelude)); |
| 57 | + match a.block { |
| 58 | + None => (), |
| 59 | + Some(ref mut b) => try!(print_block(&mut buffer, b)) |
| 60 | + } |
| 61 | + }, |
| 62 | + NonRule(ref mut component_value) => { |
| 63 | + try_write!(buffer, "{}", component_value.to_css()); |
| 64 | + } |
| 65 | + } |
80 | 66 | },
|
| 67 | + Err(e) => { |
| 68 | + try_write!(buffer, "Err: {}", e); |
| 69 | + } |
81 | 70 | };
|
82 | 71 | }
|
83 |
| -} |
84 |
| - |
85 |
| - |
86 |
| -fn run_json_tests<T: ToJson>(json_data: &str, parse: |input: &str| -> T) { |
87 |
| - run_raw_json_tests(json_data, |input, expected| { |
88 |
| - match input { |
89 |
| - json::String(input) => { |
90 |
| - let result = parse(input.as_slice()).to_json(); |
91 |
| - assert_json_eq(result, expected, input); |
92 |
| - }, |
93 |
| - _ => fail!("Unexpected JSON") |
94 |
| - } |
95 |
| - }); |
96 |
| -} |
97 |
| - |
98 | 72 |
|
99 |
| - |
100 |
| -#[test] |
101 |
| -fn stylesheet() { |
102 |
| - /*run_json_tests(include_str!("../css-parsing-tests/stylesheet.json"), |input| { |
103 |
| - parse_stylesheet_rules(tokenize(input)).collect::<Vec<Result<Rule, SyntaxError>>>() |
104 |
| - });*/ |
| 73 | + String::from_utf8(buffer.unwrap()) |
| 74 | + .map_err(|_| ()) |
105 | 75 | }
|
106 | 76 |
|
107 |
| -#[test] |
108 |
| -fn stylesheet_from_bytes() { |
109 |
| - /*run_raw_json_tests(include_str!("../css-parsing-tests/stylesheet_bytes.json"), |
110 |
| - |input, expected| { |
111 |
| - let map = match input { |
112 |
| - json::Object(map) => map, |
113 |
| - _ => fail!("Unexpected JSON") |
114 |
| - }; |
115 |
| -
|
116 |
| - let result = { |
117 |
| - let css = get_string(&map, &"css_bytes".to_string()).unwrap().chars().map(|c| { |
118 |
| - assert!(c as u32 <= 0xFF); |
119 |
| - c as u8 |
120 |
| - }).collect::<Vec<u8>>(); |
121 |
| - let protocol_encoding_label = get_string(&map, &"protocol_encoding".to_string()); |
122 |
| - let environment_encoding = get_string(&map, &"environment_encoding".to_string()) |
123 |
| - .and_then(encoding_from_whatwg_label); |
124 |
| -
|
125 |
| - let (mut rules, used_encoding) = parse_stylesheet_rules_from_bytes( |
126 |
| - css.as_slice(), protocol_encoding_label, environment_encoding); |
127 |
| -
|
128 |
| - (rules.collect::<Vec<Result<Rule, SyntaxError>>>(), used_encoding.name().to_string()).to_json() |
129 |
| - }; |
130 |
| - assert_json_eq(result, expected, json::Object(map).to_pretty_str()); |
131 |
| - }); |
132 |
| -
|
133 |
| - fn get_string<'a>(map: &'a json::Object, key: &String) -> Option<&'a str> { |
134 |
| - match map.find(key) { |
135 |
| - Some(&json::String(ref s)) => Some(s.as_slice()), |
136 |
| - Some(&json::Null) => None, |
137 |
| - None => None, |
138 |
| - _ => fail!("Unexpected JSON"), |
139 |
| - } |
140 |
| - }*/ |
| 77 | +fn print_json(rules: &Vec<Result<Rule, SyntaxError>>) { |
| 78 | + use serialize::json::ToJson; |
| 79 | + debug!("{}", rules.to_json()); |
141 | 80 | }
|
142 | 81 |
|
143 |
| - |
144 |
| -fn run_color_tests(json_data: &str, to_json: |result: Option<Color>| -> json::Json) { |
145 |
| - run_json_tests(json_data, |input| { |
146 |
| - match parse_one_component_value(tokenize(input)) { |
147 |
| - Ok(component_value) => to_json(Color::parse(&component_value)), |
148 |
| - Err(_reason) => json::Null, |
149 |
| - } |
150 |
| - }); |
151 |
| -} |
152 |
| - |
153 |
| - |
154 | 82 | fn main() {
|
155 |
| - let contents = File::open(&Path::new("test/less/css.less")).read_to_end(); |
| 83 | + let contents = File::open(&Path::new("test/less/variables.less")).read_to_end(); |
156 | 84 |
|
157 | 85 | let contents = match contents {
|
158 | 86 | Err(e) => fail!(e),
|
159 | 87 | Ok(c) => c
|
160 | 88 | };
|
161 | 89 |
|
162 |
| - println!("{}", contents); |
| 90 | + let (css_unicode, _encoding) = decode_stylesheet_bytes(contents.as_slice(), None, None); |
| 91 | + |
| 92 | + let tokens = tokenize(css_unicode.as_slice()); |
| 93 | + let mut rules = parse_stylesheet_rules(tokens); |
163 | 94 |
|
164 |
| - let (mut rules, used_encoding) = cssparser::parse_stylesheet_rules_from_bytes( |
165 |
| - contents.as_slice(), None, None); |
| 95 | + let rules = rules.collect::<Vec<Result<Rule, SyntaxError>>>(); |
| 96 | + print_json(&rules); |
166 | 97 |
|
167 |
| - println!("{}", rules.collect::<Vec<Result<Rule, SyntaxError>>>().to_json()); |
| 98 | + match print_css(rules) { |
| 99 | + Ok(res) => println!("{}", res), |
| 100 | + Err(e) => println!("Err: {}", e) |
| 101 | + } |
168 | 102 | }
|
0 commit comments