Skip to content

Commit

Permalink
url: Run wpt url parsing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwuelker committed Jul 18, 2024
1 parent 71ac5ca commit 1b8ea11
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "tests/bmp-testsuite"]
path = tests/bmp-testsuite
url = https://github.com/Wuelle/bmp-testsuite
[submodule "tests/wpt"]
path = tests/wpt
url = https://github.com/web-platform-tests/wpt
12 changes: 10 additions & 2 deletions crates/url/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ license.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dev-dependencies]
criterion = { workspace = true }

[dependencies]
log = { workspace = true }
sl-std = { workspace = true }
serialize = { workspace = true, optional = true }

[dev-dependencies]
criterion = { workspace = true }
serialize = { workspace = true, features = ["derive"] }
serialize-json = { workspace = true }

[features]
serialize = ["dep:serialize", "sl-std/serialize"]

Expand All @@ -25,3 +28,8 @@ workspace = true
[[bench]]
name = "parse_url"
harness = false

[[test]]
name = "parse_wpt"
path = "tests/parse_wpt.rs"
harness = false
99 changes: 99 additions & 0 deletions crates/url/tests/parse_wpt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use std::fs;

use serialize::deserialization::Deserializer;
use serialize_json::{JsonDeserializer, Value};
use url::URL;

pub const WPT_TESTCASES: &str = concat!(env!("TEST_DIR"), "/wpt/url/resources/urltestdata.json");

#[derive(Debug)]
struct Error;

fn main() -> Result<(), Error> {
let json_data = fs::read_to_string(WPT_TESTCASES).expect("wpt testcases not found");
let wpt_data: Vec<Value> = JsonDeserializer::new(&json_data)
.deserialize()
.expect("wpt testcases contain invalid json");

let test_cases = wpt_data.iter().flat_map(|value| value.as_map());

let mut test_cases_succeeded = 0;
let mut test_cases_run = 0;
for test_case in test_cases {
// Refer to https://github.com/web-platform-tests/wpt/tree/master/url for a description of the format
let input = test_case.get("input").unwrap().as_str().unwrap();
let base_str = match test_case.get("base").unwrap() {
Value::Null => None,
Value::String(s) => Some(s.as_str()),
other => panic!("invalid base: {other:?}"),
};
let base = base_str.map(|s| s.parse().unwrap());
let url = URL::parse_with_base(input, base.clone(), None, None);
let succeeded;

// Start test output
print!("{:?}", input.escape_debug().collect::<String>());
if let Some(base_str) = base_str {
print!(
" with base {:?}",
base_str.escape_debug().collect::<String>()
);
}
if test_case.contains_key("failure") {
print!(" (should fail)");
}
print!(": ");

if test_case.contains_key("failure") {
succeeded = url.is_err();
} else {
let href = test_case.get("href").unwrap().as_str().unwrap();
let origin = test_case.get("origin").map(|v| v.as_str().unwrap());
let protocol = test_case.get("protocol").unwrap().as_str().unwrap();
let username = test_case.get("username").unwrap().as_str().unwrap();
let password = test_case.get("password").unwrap().as_str().unwrap();
let host = test_case.get("host").unwrap().as_str().unwrap();
let hostname = test_case.get("hostname").unwrap().as_str().unwrap();
let port = test_case.get("port").unwrap().as_str().unwrap();
let pathname = test_case.get("pathname").unwrap().as_str().unwrap();

match url {
Ok(url) => {
// FIXME compare all the values here
let _ = url;
let _ = href;
let _ = origin;
let _ = protocol;
let _ = username;
let _ = password;
let _ = host;
let _ = hostname;
let _ = port;
let _ = pathname;

succeeded = true;
},
Err(_) => {
succeeded = false;
},
}
}

if succeeded {
println!("✅");
test_cases_succeeded += 1;
} else {
println!("❌");
}

test_cases_run += 1;
}

println!("{test_cases_succeeded} out of {test_cases_run} test cases succeeded");

if test_cases_run != test_cases_succeeded {
Err(Error)
} else {
Ok(())
}
}
1 change: 1 addition & 0 deletions tests/wpt
Submodule wpt added at b469a6

0 comments on commit 1b8ea11

Please sign in to comment.