// 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 imported from web-platform-tests
use serde_json::Value; use std::collections::HashMap; use std::fmt::Write; #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] use std::sync::Mutex; use url::Url;
// wpt has its own test driver, but we shoe-horn this into wasm_bindgen_test // which will discard stdout and stderr. So, we make println! go to // console.log(), so we see failures that do not result in panics.
// Copied from https://github.com/web-platform-tests/wpt/blob/master/url/ let url_json: Vec<Value> = serde_json::from_str(include_str!("urltestdata.json"))
.expect("JSON parse error in urltestdata.json"); let url_tests = url_json
.into_iter()
.filter(|val| val.is_object())
.map(|val| serde_json::from_value::<UrlTest>(val).expect("parsing failed"))
.collect::<Vec<_>>();
let setter_json: HashMap<String, Value> =
serde_json::from_str(include_str!("setters_tests.json"))
.expect("JSON parse error in setters_tests.json"); let setter_tests = setter_json
.into_iter()
.filter(|(k, _)| k != "comment")
.map(|(k, v)| { let test = serde_json::from_value::<Vec<SetterTest>>(v).expect("parsing failed");
(k, test)
})
.collect::<HashMap<_, _>>();
for url_test in url_tests { letmut name = format!("<{}>", url_test.input.escape_default()); iflet Some(base) = &url_test.base {
write!(&mut name, " against <{}>", base.escape_default()).unwrap();
} if should_skip(&name, filter.as_deref()) { continue;
}
print!("{} ... ", name);
let res = run_url_test(url_test);
report(name, res, &mut errors, &mut expected_failures);
}
for (kind, tests) in setter_tests { for test in tests { let name = format!( "<{}> set {} to <{}>",
test.href.escape_default(),
kind,
test.new_value.escape_default()
); if should_skip(&name, filter.as_deref()) { continue;
}
print!("{} ... ", name);
let res = run_setter_test(&kind, test);
report(name, res, &mut errors, &mut expected_failures);
}
}
if !expected_failures.is_empty() && filter.is_none() {
println!();
println!("====================");
println!();
println!("tests were expected to fail but did not run:");
println!();
for name in expected_failures {
println!(" {}", name);
}
println!();
println!("if these tests were removed, update expected_failures.txt");
println!();
iflet Some(expected_href) = expected.href { let href = url::quirks::href(&url); if href != expected_href { return Err(format!( "expected href {:?}, but got {:?}",
expected_href, href
));
}
}
iflet Some(expected_protocol) = expected.protocol { let protocol = url::quirks::protocol(&url); if protocol != expected_protocol { return Err(format!( "expected protocol {:?}, but got {:?}",
expected_protocol, protocol
));
}
}
iflet Some(expected_username) = expected.username { let username = url::quirks::username(&url); if username != expected_username { return Err(format!( "expected username {:?}, but got {:?}",
expected_username, username
));
}
}
iflet Some(expected_password) = expected.password { let password = url::quirks::password(&url); if password != expected_password { return Err(format!( "expected password {:?}, but got {:?}",
expected_password, password
));
}
}
iflet Some(expected_host) = expected.host { let host = url::quirks::host(&url); if host != expected_host { return Err(format!( "expected host {:?}, but got {:?}",
expected_host, host
));
}
}
iflet Some(expected_hostname) = expected.hostname { let hostname = url::quirks::hostname(&url); if hostname != expected_hostname { return Err(format!( "expected hostname {:?}, but got {:?}",
expected_hostname, hostname
));
}
}
iflet Some(expected_port) = expected.port { let port = url::quirks::port(&url); if port != expected_port { return Err(format!( "expected port {:?}, but got {:?}",
expected_port, port
));
}
}
iflet Some(expected_pathname) = expected.pathname { let pathname = url::quirks::pathname(&url); if pathname != expected_pathname { return Err(format!( "expected pathname {:?}, but got {:?}",
expected_pathname, pathname
));
}
}
iflet Some(expected_search) = expected.search { let search = url::quirks::search(&url); if search != expected_search { return Err(format!( "expected search {:?}, but got {:?}",
expected_search, search
));
}
}
iflet Some(expected_hash) = expected.hash { let hash = url::quirks::hash(&url); if hash != expected_hash { return Err(format!( "expected hash {:?}, but got {:?}",
expected_hash, hash
));
}
}
Ok(())
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.14 Sekunden
(vorverarbeitet am 2026-06-17)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.