wpt.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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) => {
  27. {
  28. let a = WebIdl::$attribute(&url);
  29. let b = expected.$attribute;
  30. assert!(a == b, "{:?} != {:?} for URL {:?}", a, b, url);
  31. }
  32. }
  33. }
  34. assert_getter!(href);
  35. //assert_getter!(origin); FIXME
  36. assert_getter!(protocol);
  37. assert_getter!(username);
  38. assert_getter!(password);
  39. assert_getter!(host);
  40. assert_getter!(hostname);
  41. assert_getter!(port);
  42. assert_getter!(pathname);
  43. assert_getter!(search);
  44. assert_getter!(hash);
  45. }
  46. struct TestCase {
  47. href: String,
  48. origin: String,
  49. protocol: String,
  50. username: String,
  51. password: String,
  52. host: String,
  53. hostname: String,
  54. port: String,
  55. pathname: String,
  56. search: String,
  57. hash: String,
  58. }
  59. fn main() {
  60. let json = Json::from_str(include_str!("urltestdata.json"))
  61. .expect("JSON parse error in urltestdata.json");
  62. let tests = json.as_array().unwrap().iter().filter_map(|entry| {
  63. if entry.is_string() {
  64. return None // ignore comments
  65. }
  66. let string = |key| entry.find(key).unwrap().as_string().unwrap().to_owned();
  67. let base = string("base");
  68. let input = string("input");
  69. let expected = if entry.find("failure").is_some() {
  70. Err(())
  71. } else {
  72. Ok(TestCase {
  73. href: string("href"),
  74. origin: string("origin"),
  75. protocol: string("protocol"),
  76. username: string("username"),
  77. password: string("password"),
  78. host: string("host"),
  79. hostname: string("hostname"),
  80. port: string("port"),
  81. pathname: string("pathname"),
  82. search: string("search"),
  83. hash: string("hash"),
  84. })
  85. };
  86. Some(test::TestDescAndFn {
  87. desc: test::TestDesc {
  88. name: test::DynTestName(format!("{:?} @ base {:?}", input, base)),
  89. ignore: false,
  90. should_panic: test::ShouldPanic::No,
  91. },
  92. testfn: test::TestFn::dyn_test_fn(move || run_one(input, base, expected)),
  93. })
  94. }).collect();
  95. test::test_main(&std::env::args().collect::<Vec<_>>(), tests)
  96. }