data.rs 6.7 KB

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