data.rs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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::{self, Json};
  13. use url::{Url, quirks};
  14. fn run_parsing(input: String, base: String, expected: Result<ExpectedAttributes, ()>) {
  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. macro_rules! assert_attributes {
  37. ($($attr: ident)+) => {
  38. {
  39. $(
  40. assert_eq!(expected.$attr, quirks::$attr(&url));
  41. )+;
  42. }
  43. }
  44. }
  45. assert_attributes!(href protocol username password host hostname port pathname search hash);
  46. if let Some(expected_origin) = expected.origin {
  47. assert_eq!(expected_origin, quirks::origin(&url));
  48. }
  49. }
  50. struct ExpectedAttributes {
  51. href: String,
  52. origin: Option<String>,
  53. protocol: String,
  54. username: String,
  55. password: String,
  56. host: String,
  57. hostname: String,
  58. port: String,
  59. pathname: String,
  60. search: String,
  61. hash: String,
  62. }
  63. trait JsonExt {
  64. fn take(&mut self, key: &str) -> Option<Json>;
  65. fn object(self) -> json::Object;
  66. fn string(self) -> String;
  67. fn take_string(&mut self, key: &str) -> String;
  68. }
  69. impl JsonExt for Json {
  70. fn take(&mut self, key: &str) -> Option<Json> {
  71. self.as_object_mut().unwrap().remove(key)
  72. }
  73. fn object(self) -> json::Object {
  74. if let Json::Object(o) = self { o } else { panic!("Not a Json::Object") }
  75. }
  76. fn string(self) -> String {
  77. if let Json::String(s) = self { s } else { panic!("Not a Json::String") }
  78. }
  79. fn take_string(&mut self, key: &str) -> String {
  80. self.take(key).unwrap().string()
  81. }
  82. }
  83. fn collect_parsing<F: FnMut(String, test::TestFn)>(add_test: &mut F) {
  84. // Copied form https://github.com/w3c/web-platform-tests/blob/master/url/
  85. let mut json = Json::from_str(include_str!("urltestdata.json"))
  86. .expect("JSON parse error in urltestdata.json");
  87. for entry in json.as_array_mut().unwrap() {
  88. if entry.is_string() {
  89. continue // ignore comments
  90. }
  91. let base = entry.take_string("base");
  92. let input = entry.take_string("input");
  93. let expected = if entry.find("failure").is_some() {
  94. Err(())
  95. } else {
  96. Ok(ExpectedAttributes {
  97. href: entry.take_string("href"),
  98. origin: entry.take("origin").map(Json::string),
  99. protocol: entry.take_string("protocol"),
  100. username: entry.take_string("username"),
  101. password: entry.take_string("password"),
  102. host: entry.take_string("host"),
  103. hostname: entry.take_string("hostname"),
  104. port: entry.take_string("port"),
  105. pathname: entry.take_string("pathname"),
  106. search: entry.take_string("search"),
  107. hash: entry.take_string("hash"),
  108. })
  109. };
  110. add_test(format!("{:?} @ base {:?}", input, base),
  111. test::TestFn::dyn_test_fn(move || run_parsing(input, base, expected)));
  112. }
  113. }
  114. fn collect_setters<F>(add_test: &mut F) where F: FnMut(String, test::TestFn) {
  115. let mut json = Json::from_str(include_str!("setters_tests.json"))
  116. .expect("JSON parse error in setters_tests.json");
  117. macro_rules! setter {
  118. ($attr: expr, $setter: ident) => {{
  119. let mut tests = json.take($attr).unwrap();
  120. for mut test in tests.as_array_mut().unwrap().drain(..) {
  121. let comment = test.take("comment").map(Json::string).unwrap_or(String::new());
  122. let href = test.take_string("href");
  123. let new_value = test.take_string("new_value");
  124. let name = format!("{:?}.{} = {:?} {}", href, $attr, new_value, comment);
  125. let mut expected = test.take("expected").unwrap();
  126. add_test(name, test::TestFn::dyn_test_fn(move || {
  127. let mut url = Url::parse(&href).unwrap();
  128. url.assert_invariants();
  129. let _ = quirks::$setter(&mut url, &new_value);
  130. assert_attributes!(url, expected,
  131. href protocol username password host hostname port pathname search hash);
  132. url.assert_invariants();
  133. }))
  134. }
  135. }}
  136. }
  137. macro_rules! assert_attributes {
  138. ($url: expr, $expected: expr, $($attr: ident)+) => {
  139. $(
  140. if let Some(value) = $expected.take(stringify!($attr)) {
  141. assert_eq!(quirks::$attr(&$url), value.string())
  142. }
  143. )+
  144. }
  145. }
  146. setter!("protocol", set_protocol);
  147. setter!("username", set_username);
  148. setter!("password", set_password);
  149. setter!("hostname", set_hostname);
  150. setter!("host", set_host);
  151. setter!("port", set_port);
  152. setter!("pathname", set_pathname);
  153. setter!("search", set_search);
  154. setter!("hash", set_hash);
  155. }
  156. fn main() {
  157. let mut tests = Vec::new();
  158. {
  159. let mut add_one = |name: String, run: test::TestFn| {
  160. tests.push(test::TestDescAndFn {
  161. desc: test::TestDesc {
  162. name: test::DynTestName(name),
  163. ignore: false,
  164. should_panic: test::ShouldPanic::No,
  165. },
  166. testfn: run,
  167. })
  168. };
  169. collect_parsing(&mut add_one);
  170. collect_setters(&mut add_one);
  171. }
  172. test::test_main(&std::env::args().collect::<Vec<_>>(), tests)
  173. }