From a1ed777e42e3dcc57449ab95d6384cfcc29628cc Mon Sep 17 00:00:00 2001 From: est31 Date: Mon, 15 Jul 2019 01:27:47 +0200 Subject: [PATCH] Make the tests use serde_json instead of rustc_serialize --- Cargo.toml | 1 - tests/data.rs | 40 +++++++++++++++++++--------------------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 533f82c18..b28fab832 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,6 @@ test = false [dev-dependencies] rustc-test = "0.3" -rustc-serialize = "0.3" serde_json = "1.0" bencher = "0.1" diff --git a/tests/data.rs b/tests/data.rs index e9203b1b0..29d2e0deb 100644 --- a/tests/data.rs +++ b/tests/data.rs @@ -8,17 +8,17 @@ //! Data-driven tests -extern crate rustc_serialize; +extern crate serde_json; extern crate rustc_test as test; extern crate url; -use rustc_serialize::json::{self, Json}; +use serde_json::Value; use url::{Url, quirks}; +use std::str::FromStr; fn check_invariants(url: &Url) { url.check_invariants().unwrap(); #[cfg(feature="serde")] { - extern crate serde_json; let bytes = serde_json::to_vec(url).unwrap(); let new_url: Url = serde_json::from_slice(&bytes).unwrap(); assert_eq!(url, &new_url); @@ -84,33 +84,28 @@ struct ExpectedAttributes { } trait JsonExt { - fn take(&mut self, key: &str) -> Option; - fn object(self) -> json::Object; + fn take_key(&mut self, key: &str) -> Option; fn string(self) -> String; fn take_string(&mut self, key: &str) -> String; } -impl JsonExt for Json { - fn take(&mut self, key: &str) -> Option { +impl JsonExt for Value { + fn take_key(&mut self, key: &str) -> Option { self.as_object_mut().unwrap().remove(key) } - fn object(self) -> json::Object { - if let Json::Object(o) = self { o } else { panic!("Not a Json::Object") } - } - fn string(self) -> String { - if let Json::String(s) = self { s } else { panic!("Not a Json::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).unwrap().string() + self.take_key(key).unwrap().string() } } fn collect_parsing(add_test: &mut F) { // Copied form https://github.com/w3c/web-platform-tests/blob/master/url/ - let mut json = Json::from_str(include_str!("urltestdata.json")) + 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() { @@ -118,12 +113,13 @@ fn collect_parsing(add_test: &mut F) { } let base = entry.take_string("base"); let input = entry.take_string("input"); - let expected = if entry.find("failure").is_some() { + let expected = if entry.take_key("failure").is_some() { Err(()) } else { Ok(ExpectedAttributes { href: entry.take_string("href"), - origin: entry.take("origin").map(Json::string), + origin: entry.take_key("origin") + .map(|s| s.string()), protocol: entry.take_string("protocol"), username: entry.take_string("username"), password: entry.take_string("password"), @@ -141,18 +137,20 @@ fn collect_parsing(add_test: &mut F) { } fn collect_setters(add_test: &mut F) where F: FnMut(String, test::TestFn) { - let mut json = Json::from_str(include_str!("setters_tests.json")) + 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($attr).unwrap(); + let mut tests = json.take_key($attr).unwrap(); for mut test in tests.as_array_mut().unwrap().drain(..) { - let comment = test.take("comment").map(Json::string).unwrap_or(String::new()); + 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("expected").unwrap(); + 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); @@ -167,7 +165,7 @@ fn collect_setters(add_test: &mut F) where F: FnMut(String, test::TestFn) { macro_rules! assert_attributes { ($url: expr, $expected: expr, $($attr: ident)+) => { $( - if let Some(value) = $expected.take(stringify!($attr)) { + if let Some(value) = $expected.take_key(stringify!($attr)) { assert_eq!(quirks::$attr(&$url), value.string()) } )+