File: data.rs

package info (click to toggle)
rust-url 2.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 588 kB
  • sloc: makefile: 2
file content (210 lines) | stat: -rw-r--r-- 6,924 bytes parent folder | download | duplicates (25)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright 2013-2014 The rust-url developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Data-driven tests

extern crate rustc_test as test;
extern crate serde_json;
extern crate url;

use serde_json::Value;
use std::str::FromStr;
use url::{quirks, Url};

fn check_invariants(url: &Url) {
    url.check_invariants().unwrap();
    #[cfg(feature = "serde")]
    {
        let bytes = serde_json::to_vec(url).unwrap();
        let new_url: Url = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(url, &new_url);
    }
}

fn run_parsing(input: &str, base: &str, expected: Result<ExpectedAttributes, ()>) {
    let base = match Url::parse(&base) {
        Ok(base) => base,
        Err(_) if expected.is_err() => return,
        Err(message) => panic!("Error parsing base {:?}: {}", base, message),
    };
    let (url, expected) = match (base.join(&input), expected) {
        (Ok(url), Ok(expected)) => (url, expected),
        (Err(_), Err(())) => return,
        (Err(message), Ok(_)) => panic!("Error parsing URL {:?}: {}", input, message),
        (Ok(_), Err(())) => panic!("Expected a parse error for URL {:?}", input),
    };

    check_invariants(&url);

    macro_rules! assert_eq {
        ($expected: expr, $got: expr) => {{
            let expected = $expected;
            let got = $got;
            assert!(
                expected == got,
                "\n{:?}\n!= {}\n{:?}\nfor URL {:?}\n",
                got,
                stringify!($expected),
                expected,
                url
            );
        }};
    }

    macro_rules! assert_attributes {
        ($($attr: ident)+) => {
            {
                $(
                    assert_eq!(expected.$attr, quirks::$attr(&url));
                )+;
            }
        }
    }

    assert_attributes!(href protocol username password host hostname port pathname search hash);

    if let Some(expected_origin) = expected.origin {
        assert_eq!(expected_origin, quirks::origin(&url));
    }
}

struct ExpectedAttributes {
    href: String,
    origin: Option<String>,
    protocol: String,
    username: String,
    password: String,
    host: String,
    hostname: String,
    port: String,
    pathname: String,
    search: String,
    hash: String,
}

trait JsonExt {
    fn take_key(&mut self, key: &str) -> Option<Value>;
    fn string(self) -> String;
    fn take_string(&mut self, key: &str) -> String;
}

impl JsonExt for Value {
    fn take_key(&mut self, key: &str) -> Option<Value> {
        self.as_object_mut().unwrap().remove(key)
    }

    fn string(self) -> String {
        if let Value::String(s) = self {
            s
        } else {
            panic!("Not a Value::String")
        }
    }

    fn take_string(&mut self, key: &str) -> String {
        self.take_key(key).unwrap().string()
    }
}

fn collect_parsing<F: FnMut(String, test::TestFn)>(add_test: &mut F) {
    // Copied form https://github.com/w3c/web-platform-tests/blob/master/url/
    let mut json = Value::from_str(include_str!("urltestdata.json"))
        .expect("JSON parse error in urltestdata.json");
    for entry in json.as_array_mut().unwrap() {
        if entry.is_string() {
            continue; // ignore comments
        }
        let base = entry.take_string("base");
        let input = entry.take_string("input");
        let expected = if entry.take_key("failure").is_some() {
            Err(())
        } else {
            Ok(ExpectedAttributes {
                href: entry.take_string("href"),
                origin: entry.take_key("origin").map(|s| s.string()),
                protocol: entry.take_string("protocol"),
                username: entry.take_string("username"),
                password: entry.take_string("password"),
                host: entry.take_string("host"),
                hostname: entry.take_string("hostname"),
                port: entry.take_string("port"),
                pathname: entry.take_string("pathname"),
                search: entry.take_string("search"),
                hash: entry.take_string("hash"),
            })
        };
        add_test(
            format!("{:?} @ base {:?}", input, base),
            test::TestFn::dyn_test_fn(move || run_parsing(&input, &base, expected)),
        );
    }
}

fn collect_setters<F>(add_test: &mut F)
where
    F: FnMut(String, test::TestFn),
{
    let mut json = Value::from_str(include_str!("setters_tests.json"))
        .expect("JSON parse error in setters_tests.json");

    macro_rules! setter {
        ($attr: expr, $setter: ident) => {{
            let mut tests = json.take_key($attr).unwrap();
            for mut test in tests.as_array_mut().unwrap().drain(..) {
                let comment = test.take_key("comment")
                    .map(|s| s.string())
                    .unwrap_or(String::new());
                let href = test.take_string("href");
                let new_value = test.take_string("new_value");
                let name = format!("{:?}.{} = {:?} {}", href, $attr, new_value, comment);
                let mut expected = test.take_key("expected").unwrap();
                add_test(name, test::TestFn::dyn_test_fn(move || {
                    let mut url = Url::parse(&href).unwrap();
                    check_invariants(&url);
                    let _ = quirks::$setter(&mut url, &new_value);
                    assert_attributes!(url, expected,
                        href protocol username password host hostname port pathname search hash);
                    check_invariants(&url);
                }))
            }
        }}
    }
    macro_rules! assert_attributes {
        ($url: expr, $expected: expr, $($attr: ident)+) => {
            $(
                if let Some(value) = $expected.take_key(stringify!($attr)) {
                    assert_eq!(quirks::$attr(&$url), value.string())
                }
            )+
        }
    }
    setter!("protocol", set_protocol);
    setter!("username", set_username);
    setter!("password", set_password);
    setter!("hostname", set_hostname);
    setter!("host", set_host);
    setter!("port", set_port);
    setter!("pathname", set_pathname);
    setter!("search", set_search);
    setter!("hash", set_hash);
}

fn main() {
    let mut tests = Vec::new();
    {
        let mut add_one = |name: String, run: test::TestFn| {
            tests.push(test::TestDescAndFn {
                desc: test::TestDesc::new(test::DynTestName(name)),
                testfn: run,
            })
        };
        collect_parsing(&mut add_one);
        collect_setters(&mut add_one);
    }
    test::test_main(&std::env::args().collect::<Vec<_>>(), tests)
}