wpt.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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, Position};
  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_eq {
  26. ($expected: expr, $got: expr) => {
  27. {
  28. let expected = $expected;
  29. let got = $got;
  30. assert!(expected == got, "{:?} != {} {:?} for URL {:?}",
  31. got, stringify!($expected), expected, url);
  32. }
  33. }
  34. }
  35. assert_eq!(expected.href, url.as_str());
  36. if let Some(expected_origin) = expected.origin {
  37. assert_eq!(expected_origin, url.origin().unicode_serialization());
  38. }
  39. assert_eq!(expected.protocol, &url.as_str()[..url.scheme().len() + ":".len()]);
  40. assert_eq!(expected.username, url.username());
  41. assert_eq!(expected.password, url.password().unwrap_or(""));
  42. assert_eq!(expected.host, &url[Position::BeforeHost..Position::AfterPort]);
  43. assert_eq!(expected.hostname, url.host_str().unwrap_or(""));
  44. assert_eq!(expected.port, &url[Position::BeforePort..Position::AfterPort]);
  45. assert_eq!(expected.pathname, url.path());
  46. assert_eq!(expected.search, trim(&url[Position::AfterPath..Position::AfterQuery]));
  47. assert_eq!(expected.hash, trim(&url[Position::AfterQuery..]));
  48. }
  49. fn trim(s: &str) -> &str {
  50. if s.len() == 1 {
  51. ""
  52. } else {
  53. s
  54. }
  55. }
  56. struct TestCase {
  57. href: String,
  58. origin: Option<String>,
  59. protocol: String,
  60. username: String,
  61. password: String,
  62. host: String,
  63. hostname: String,
  64. port: String,
  65. pathname: String,
  66. search: String,
  67. hash: String,
  68. }
  69. fn main() {
  70. let json = Json::from_str(include_str!("urltestdata.json"))
  71. .expect("JSON parse error in urltestdata.json");
  72. let tests = json.as_array().unwrap().iter().filter_map(|entry| {
  73. if entry.is_string() {
  74. return None // ignore comments
  75. }
  76. let string = |key| entry.find(key).unwrap().as_string().unwrap().to_owned();
  77. let base = string("base");
  78. let input = string("input");
  79. let expected = if entry.find("failure").is_some() {
  80. Err(())
  81. } else {
  82. Ok(TestCase {
  83. href: string("href"),
  84. origin: entry.find("origin").map(|j| j.as_string().unwrap().to_owned()),
  85. protocol: string("protocol"),
  86. username: string("username"),
  87. password: string("password"),
  88. host: string("host"),
  89. hostname: string("hostname"),
  90. port: string("port"),
  91. pathname: string("pathname"),
  92. search: string("search"),
  93. hash: string("hash"),
  94. })
  95. };
  96. Some(test::TestDescAndFn {
  97. desc: test::TestDesc {
  98. name: test::DynTestName(format!("{:?} @ base {:?}", input, base)),
  99. ignore: false,
  100. should_panic: test::ShouldPanic::No,
  101. },
  102. testfn: test::TestFn::dyn_test_fn(move || run_one(input, base, expected)),
  103. })
  104. }).collect();
  105. test::test_main(&std::env::args().collect::<Vec<_>>(), tests)
  106. }