wpt.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2013-2014 Simon Sapin.
  2. //
  3. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  4. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  5. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  6. // option. This file may not be copied, modified, or distributed
  7. // except according to those terms.
  8. //! Tests copied form https://github.com/w3c/web-platform-tests/blob/master/url/
  9. extern crate rustc_serialize;
  10. extern crate test;
  11. extern crate url;
  12. use rustc_serialize::json::Json;
  13. use url::{Url, WebIdl};
  14. fn run_one(input: String, base: String, expected: Result<TestCase, ()>) {
  15. let base = match Url::parse(&base) {
  16. Ok(base) => base,
  17. Err(message) => panic!("Error parsing base {:?}: {}", base, message)
  18. };
  19. let (url, expected) = match (base.join(&input), expected) {
  20. (Ok(url), Ok(expected)) => (url, expected),
  21. (Err(_), Err(())) => return,
  22. (Err(message), Ok(_)) => panic!("Error parsing URL {:?}: {}", input, message),
  23. (Ok(_), Err(())) => panic!("Expected a parse error for URL {:?}", input),
  24. };
  25. macro_rules! assert_getter {
  26. ($attribute: ident) => { assert_getter!($attribute, expected.$attribute) };
  27. ($attribute: ident, $expected: expr) => {
  28. {
  29. let a = WebIdl::$attribute(&url);
  30. let b = $expected;
  31. assert!(a == b, "{:?} != {:?} for URL {:?}", a, b, url);
  32. }
  33. }
  34. }
  35. assert_getter!(href);
  36. if let Some(expected_origin) = expected.origin {
  37. assert_getter!(origin, expected_origin);
  38. }
  39. assert_getter!(protocol);
  40. assert_getter!(username);
  41. assert_getter!(password);
  42. assert_getter!(host);
  43. assert_getter!(hostname);
  44. assert_getter!(port);
  45. assert_getter!(pathname);
  46. assert_getter!(search);
  47. assert_getter!(hash);
  48. }
  49. struct TestCase {
  50. href: String,
  51. origin: Option<String>,
  52. protocol: String,
  53. username: String,
  54. password: String,
  55. host: String,
  56. hostname: String,
  57. port: String,
  58. pathname: String,
  59. search: String,
  60. hash: String,
  61. }
  62. fn main() {
  63. let json = Json::from_str(include_str!("urltestdata.json"))
  64. .expect("JSON parse error in urltestdata.json");
  65. let tests = json.as_array().unwrap().iter().filter_map(|entry| {
  66. if entry.is_string() {
  67. return None // ignore comments
  68. }
  69. let string = |key| entry.find(key).unwrap().as_string().unwrap().to_owned();
  70. let base = string("base");
  71. let input = string("input");
  72. let expected = if entry.find("failure").is_some() {
  73. Err(())
  74. } else {
  75. Ok(TestCase {
  76. href: string("href"),
  77. origin: entry.find("origin").map(|j| j.as_string().unwrap().to_owned()),
  78. protocol: string("protocol"),
  79. username: string("username"),
  80. password: string("password"),
  81. host: string("host"),
  82. hostname: string("hostname"),
  83. port: string("port"),
  84. pathname: string("pathname"),
  85. search: string("search"),
  86. hash: string("hash"),
  87. })
  88. };
  89. Some(test::TestDescAndFn {
  90. desc: test::TestDesc {
  91. name: test::DynTestName(format!("{:?} @ base {:?}", input, base)),
  92. ignore: false,
  93. should_panic: test::ShouldPanic::No,
  94. },
  95. testfn: test::TestFn::dyn_test_fn(move || run_one(input, base, expected)),
  96. })
  97. }).collect();
  98. test::test_main(&std::env::args().collect::<Vec<_>>(), tests)
  99. }