Skip to content

Commit

Permalink
Make the tests use serde_json instead of rustc_serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
est31 committed Jul 15, 2019
1 parent ca5f6c8 commit b97ef45
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ test = false

[dev-dependencies]
rustc-test = "0.3"
rustc-serialize = "0.3"
serde_json = "1.0"

bencher = "0.1"
Expand Down
42 changes: 20 additions & 22 deletions tests/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -84,46 +84,42 @@ struct ExpectedAttributes {
}

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

impl JsonExt for Json {
fn take(&mut self, key: &str) -> Option<Json> {
impl JsonExt for Value {
fn take_key(&mut self, key: &str) -> Option<Value> {
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<F: FnMut(String, test::TestFn)>(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() {
continue // ignore comments
}
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"),
Expand All @@ -141,18 +137,20 @@ fn collect_parsing<F: FnMut(String, test::TestFn)>(add_test: &mut F) {
}

fn collect_setters<F>(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);
Expand All @@ -167,7 +165,7 @@ fn collect_setters<F>(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())
}
)+
Expand All @@ -184,7 +182,7 @@ fn collect_setters<F>(add_test: &mut F) where F: FnMut(String, test::TestFn) {
setter!("hash", set_hash);
}

fn main() {
pub fn main() {
let mut tests = Vec::new();
{
let mut add_one = |name: String, run: test::TestFn| {
Expand Down

0 comments on commit b97ef45

Please sign in to comment.