data.rs 4.1 KB

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