data.rs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 serde_json;
  10. extern crate rustc_test as test;
  11. extern crate url;
  12. use serde_json::Value;
  13. use url::{Url, quirks};
  14. use std::str::FromStr;
  15. fn check_invariants(url: &Url) {
  16. url.check_invariants().unwrap();
  17. #[cfg(feature="serde")] {
  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_key(&mut self, key: &str) -> Option<Value>;
  75. fn string(self) -> String;
  76. fn take_string(&mut self, key: &str) -> String;
  77. }
  78. impl JsonExt for Value {
  79. fn take_key(&mut self, key: &str) -> Option<Value> {
  80. self.as_object_mut().unwrap().remove(key)
  81. }
  82. fn string(self) -> String {
  83. if let Value::String(s) = self { s } else { panic!("Not a Value::String") }
  84. }
  85. fn take_string(&mut self, key: &str) -> String {
  86. self.take_key(key).unwrap().string()
  87. }
  88. }
  89. fn collect_parsing<F: FnMut(String, test::TestFn)>(add_test: &mut F) {
  90. // Copied form https://github.com/w3c/web-platform-tests/blob/master/url/
  91. let mut json = Value::from_str(include_str!("urltestdata.json"))
  92. .expect("JSON parse error in urltestdata.json");
  93. for entry in json.as_array_mut().unwrap() {
  94. if entry.is_string() {
  95. continue // ignore comments
  96. }
  97. let base = entry.take_string("base");
  98. let input = entry.take_string("input");
  99. let expected = if entry.take_key("failure").is_some() {
  100. Err(())
  101. } else {
  102. Ok(ExpectedAttributes {
  103. href: entry.take_string("href"),
  104. origin: entry.take_key("origin")
  105. .map(|s| s.string()),
  106. protocol: entry.take_string("protocol"),
  107. username: entry.take_string("username"),
  108. password: entry.take_string("password"),
  109. host: entry.take_string("host"),
  110. hostname: entry.take_string("hostname"),
  111. port: entry.take_string("port"),
  112. pathname: entry.take_string("pathname"),
  113. search: entry.take_string("search"),
  114. hash: entry.take_string("hash"),
  115. })
  116. };
  117. add_test(format!("{:?} @ base {:?}", input, base),
  118. test::TestFn::dyn_test_fn(move || run_parsing(&input, &base, expected)));
  119. }
  120. }
  121. fn collect_setters<F>(add_test: &mut F) where F: FnMut(String, test::TestFn) {
  122. let mut json = Value::from_str(include_str!("setters_tests.json"))
  123. .expect("JSON parse error in setters_tests.json");
  124. macro_rules! setter {
  125. ($attr: expr, $setter: ident) => {{
  126. let mut tests = json.take_key($attr).unwrap();
  127. for mut test in tests.as_array_mut().unwrap().drain(..) {
  128. let comment = test.take_key("comment")
  129. .map(|s| s.string())
  130. .unwrap_or(String::new());
  131. let href = test.take_string("href");
  132. let new_value = test.take_string("new_value");
  133. let name = format!("{:?}.{} = {:?} {}", href, $attr, new_value, comment);
  134. let mut expected = test.take_key("expected").unwrap();
  135. add_test(name, test::TestFn::dyn_test_fn(move || {
  136. let mut url = Url::parse(&href).unwrap();
  137. check_invariants(&url);
  138. let _ = quirks::$setter(&mut url, &new_value);
  139. assert_attributes!(url, expected,
  140. href protocol username password host hostname port pathname search hash);
  141. check_invariants(&url);
  142. }))
  143. }
  144. }}
  145. }
  146. macro_rules! assert_attributes {
  147. ($url: expr, $expected: expr, $($attr: ident)+) => {
  148. $(
  149. if let Some(value) = $expected.take_key(stringify!($attr)) {
  150. assert_eq!(quirks::$attr(&$url), value.string())
  151. }
  152. )+
  153. }
  154. }
  155. setter!("protocol", set_protocol);
  156. setter!("username", set_username);
  157. setter!("password", set_password);
  158. setter!("hostname", set_hostname);
  159. setter!("host", set_host);
  160. setter!("port", set_port);
  161. setter!("pathname", set_pathname);
  162. setter!("search", set_search);
  163. setter!("hash", set_hash);
  164. }
  165. fn main() {
  166. let mut tests = Vec::new();
  167. {
  168. let mut add_one = |name: String, run: test::TestFn| {
  169. tests.push(test::TestDescAndFn {
  170. desc: test::TestDesc::new(test::DynTestName(name)),
  171. testfn: run,
  172. })
  173. };
  174. collect_parsing(&mut add_one);
  175. collect_setters(&mut add_one);
  176. }
  177. test::test_main(&std::env::args().collect::<Vec<_>>(), tests)
  178. }