use crate::prelude::*;
const WPT_SETTERS_TESTS: &str = include_str!("data/setters_tests.json");
const ADA_EXTRA_SETTERS_TESTS: &str = include_str!("data/ada_extra_setters_tests.json");
#[test]
fn wpt_setters_tests() {
serde_json::from_str::<Tests>(WPT_SETTERS_TESTS).expect("To parse the tests.").r#do()
}
#[test]
fn ada_extra_setters_tests() {
serde_json::from_str::<Tests>(ADA_EXTRA_SETTERS_TESTS).expect("To parse the tests.").r#do()
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct Tests {
#[expect(dead_code, reason = "Allows the deny_unknown_fields to be useful.")] comment : Vec<String>,
#[expect(dead_code, reason = "Allows the deny_unknown_fields to be useful.")] href : Vec<Test>,
#[serde(default)] protocol: Vec<Test>,
#[serde(default)] username: Vec<Test>,
#[serde(default)] password: Vec<Test>,
#[serde(default)] hostname: Vec<Test>,
#[serde(default)] host : Vec<Test>,
#[serde(default)] port : Vec<Test>,
#[serde(default)] pathname: Vec<Test>,
#[serde(default)] search : Vec<Test>,
#[serde(default)] hash : Vec<Test>,
}
#[derive(Debug, Deserialize)]
struct Test {
href: BetterUrl,
new_value: String,
expected: Expected,
}
#[derive(Debug, Deserialize)]
struct Expected {
href: BetterUrl,
}
impl Tests {
fn r#do(self) {
println!("Protocol:");
for mut test in self.protocol {
println!(" {} + {} -> {}", test.href, test.new_value, test.expected.href);
let _ = test.href.canon_set_protocol(test.new_value);
assert_identical(&test.href, &test.expected.href);
}
println!();
println!("Username:");
for mut test in self.username {
println!(" {} + {} -> {}", test.href, test.new_value, test.expected.href);
let _ = test.href.canon_set_username(test.new_value);
assert_identical(&test.href, &test.expected.href);
}
println!();
println!("Password:");
for mut test in self.password {
println!(" {} + {} -> {}", test.href, test.new_value, test.expected.href);
let _ = test.href.canon_set_password(test.new_value);
assert_identical(&test.href, &test.expected.href);
}
println!();
println!("Host:");
for mut test in self.host {
println!(" {} + {} -> {}", test.href, test.new_value, test.expected.href);
let _ = test.href.canon_set_host(test.new_value);
assert_identical(&test.href, &test.expected.href);
}
println!();
println!("Hostname:");
for mut test in self.hostname {
println!(" {} + {} -> {}", test.href, test.new_value, test.expected.href);
let _ = test.href.canon_set_hostname(test.new_value);
assert_identical(&test.href, &test.expected.href);
}
println!();
println!("Port:");
for mut test in self.port {
println!(" {} + {} -> {}", test.href, test.new_value, test.expected.href);
let _ = test.href.canon_set_port(test.new_value);
assert_identical(&test.href, &test.expected.href);
}
println!();
println!("Pathname:");
for mut test in self.pathname {
println!(" {} + {} -> {}", test.href, test.new_value, test.expected.href);
let _ = test.href.canon_set_pathname(test.new_value);
assert_identical(&test.href, &test.expected.href);
}
println!();
println!("Search:");
for mut test in self.search {
println!(" {} + {} -> {}", test.href, test.new_value, test.expected.href);
let _ = test.href.canon_set_search(test.new_value);
assert_identical(&test.href, &test.expected.href);
}
println!();
println!("Hash:");
for mut test in self.hash {
println!(" {} + {} -> {}", test.href, test.new_value, test.expected.href);
let _ = test.href.canon_set_hash(test.new_value);
assert_identical(&test.href, &test.expected.href);
}
println!();
}
}
fn assert_identical(x: &BetterUrl, y: &BetterUrl) {
assert_eq!(x.as_str (), y.as_str (), "as_str" );
assert_eq!(x.scheme (), y.scheme (), "scheme" );
assert_eq!(x.visible_username_str(), y.visible_username_str(), "visible_username_str");
assert_eq!(x.username (), y.username (), "username" );
assert_eq!(x.visible_password_str(), y.visible_password_str(), "visible_password_str");
assert_eq!(x.password (), y.password (), "password" );
assert_eq!(x.host (), y.host (), "host" );
assert_eq!(x.port_num (), y.port_num (), "port_num" );
assert_eq!(x.port_str (), y.port_str (), "port_str" );
assert_eq!(x.path (), y.path (), "path" );
assert_eq!(x.query (), y.query (), "query" );
assert_eq!(x.fragment (), y.fragment (), "fragment" );
assert_eq!(x.canon_get_href (), y.canon_get_href (), "canon_get_href" );
assert_eq!(x.canon_get_protocol (), y.canon_get_protocol (), "canon_get_protocol" );
assert_eq!(x.canon_get_username (), y.canon_get_username (), "canon_get_username" );
assert_eq!(x.canon_get_password (), y.canon_get_password (), "canon_get_password" );
assert_eq!(x.canon_get_hostname (), y.canon_get_hostname (), "canon_get_hostname" );
assert_eq!(x.canon_get_host (), y.canon_get_host (), "canon_get_host" );
assert_eq!(x.canon_get_port (), y.canon_get_port (), "canon_get_port" );
assert_eq!(x.canon_get_pathname (), y.canon_get_pathname (), "canon_get_pathname" );
assert_eq!(x.canon_get_search (), y.canon_get_search (), "canon_get_search" );
assert_eq!(x.canon_get_hash (), y.canon_get_hash (), "canon_get_hash" );
}