data.rs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. use serde_json::Value;
  10. use std::str::FromStr;
  11. use url::{quirks, Url};
  12. fn check_invariants(url: &Url, name: &str, comment: Option<&str>) -> bool {
  13. let mut passed = true;
  14. if let Err(e) = url.check_invariants() {
  15. passed = false;
  16. eprint_failure(
  17. format!(" failed: invariants checked -> {:?}", e),
  18. name,
  19. comment,
  20. );
  21. }
  22. #[cfg(feature = "serde")]
  23. {
  24. let bytes = serde_json::to_vec(url).unwrap();
  25. let new_url: Url = serde_json::from_slice(&bytes).unwrap();
  26. passed &= test_eq(url, &new_url, name, comment);
  27. }
  28. passed
  29. }
  30. struct ExpectedAttributes {
  31. href: String,
  32. origin: Option<String>,
  33. protocol: String,
  34. username: String,
  35. password: String,
  36. host: String,
  37. hostname: String,
  38. port: String,
  39. pathname: String,
  40. search: String,
  41. hash: String,
  42. }
  43. trait JsonExt {
  44. fn take_key(&mut self, key: &str) -> Option<Value>;
  45. fn string(self) -> String;
  46. fn take_string(&mut self, key: &str) -> String;
  47. }
  48. impl JsonExt for Value {
  49. fn take_key(&mut self, key: &str) -> Option<Value> {
  50. self.as_object_mut().unwrap().remove(key)
  51. }
  52. fn string(self) -> String {
  53. if let Value::String(s) = self {
  54. s
  55. } else {
  56. panic!("Not a Value::String")
  57. }
  58. }
  59. fn take_string(&mut self, key: &str) -> String {
  60. self.take_key(key).unwrap().string()
  61. }
  62. }
  63. #[test]
  64. fn urltestdata() {
  65. // Copied form https://github.com/w3c/web-platform-tests/blob/master/url/
  66. let mut json = Value::from_str(include_str!("urltestdata.json"))
  67. .expect("JSON parse error in urltestdata.json");
  68. let mut passed = true;
  69. for entry in json.as_array_mut().unwrap() {
  70. if entry.is_string() {
  71. continue; // ignore comments
  72. }
  73. let base = entry.take_string("base");
  74. let input = entry.take_string("input");
  75. let expected = if entry.take_key("failure").is_some() {
  76. Err(())
  77. } else {
  78. Ok(ExpectedAttributes {
  79. href: entry.take_string("href"),
  80. origin: entry.take_key("origin").map(|s| s.string()),
  81. protocol: entry.take_string("protocol"),
  82. username: entry.take_string("username"),
  83. password: entry.take_string("password"),
  84. host: entry.take_string("host"),
  85. hostname: entry.take_string("hostname"),
  86. port: entry.take_string("port"),
  87. pathname: entry.take_string("pathname"),
  88. search: entry.take_string("search"),
  89. hash: entry.take_string("hash"),
  90. })
  91. };
  92. let base = match Url::parse(&base) {
  93. Ok(base) => base,
  94. Err(_) if expected.is_err() => continue,
  95. Err(message) => {
  96. eprint_failure(
  97. format!(" failed: error parsing base {:?}: {}", base, message),
  98. &format!("parse base for {:?}", input),
  99. None,
  100. );
  101. passed = false;
  102. continue;
  103. }
  104. };
  105. let (url, expected) = match (base.join(&input), expected) {
  106. (Ok(url), Ok(expected)) => (url, expected),
  107. (Err(_), Err(())) => continue,
  108. (Err(message), Ok(_)) => {
  109. eprint_failure(
  110. format!(" failed: {}", message),
  111. &format!("parse URL for {:?}", input),
  112. None,
  113. );
  114. passed = false;
  115. continue;
  116. }
  117. (Ok(_), Err(())) => {
  118. eprint_failure(
  119. format!(" failed: expected parse error for URL {:?}", input),
  120. &format!("parse URL for {:?}", input),
  121. None,
  122. );
  123. passed = false;
  124. continue;
  125. }
  126. };
  127. passed &= check_invariants(&url, &format!("invariants for {:?}", input), None);
  128. macro_rules! assert_attributes {
  129. ($($attr: ident)+) => {$(test_eq_eprint(
  130. expected.$attr,
  131. quirks::$attr(&url),
  132. &format!("{:?} - {}", input, stringify!($attr)),
  133. None,
  134. ))&+}
  135. }
  136. passed &= assert_attributes!(
  137. href protocol username password host hostname port pathname search hash
  138. );
  139. if let Some(expected_origin) = expected.origin {
  140. passed &= test_eq_eprint(
  141. expected_origin,
  142. &quirks::origin(&url),
  143. &format!("origin for {:?}", input),
  144. None,
  145. );
  146. }
  147. }
  148. assert!(passed)
  149. }
  150. #[test]
  151. fn setters_tests() {
  152. let mut json = Value::from_str(include_str!("setters_tests.json"))
  153. .expect("JSON parse error in setters_tests.json");
  154. macro_rules! setter {
  155. ($attr: expr, $setter: ident) => {{
  156. let mut tests = json.take_key($attr).unwrap();
  157. let mut passed = true;
  158. for mut test in tests.as_array_mut().unwrap().drain(..) {
  159. let comment = test.take_key("comment").map(|s| s.string());
  160. let href = test.take_string("href");
  161. let new_value = test.take_string("new_value");
  162. let name = format!("{:?}.{} = {:?}", href, $attr, new_value);
  163. let mut expected = test.take_key("expected").unwrap();
  164. let mut url = Url::parse(&href).unwrap();
  165. let comment_ref = comment.as_deref();
  166. passed &= check_invariants(&url, &name, comment_ref);
  167. let _ = quirks::$setter(&mut url, &new_value);
  168. passed &= assert_attributes!(&name, comment_ref, url, expected,
  169. href protocol username password host hostname port pathname search hash);
  170. passed &= check_invariants(&url, &name, comment_ref);
  171. }
  172. passed
  173. }}
  174. }
  175. macro_rules! assert_attributes {
  176. ($name: expr, $comment: expr, $url: expr, $expected: expr, $($attr: ident)+) => {
  177. $(match $expected.take_key(stringify!($attr)) {
  178. Some(value) => test_eq_eprint(
  179. value.string(),
  180. quirks::$attr(&$url),
  181. $name,
  182. $comment,
  183. ),
  184. None => true,
  185. })&+
  186. }
  187. }
  188. let mut passed = true;
  189. passed &= setter!("protocol", set_protocol);
  190. passed &= setter!("username", set_username);
  191. passed &= setter!("password", set_password);
  192. passed &= setter!("hostname", set_hostname);
  193. passed &= setter!("host", set_host);
  194. passed &= setter!("port", set_port);
  195. passed &= setter!("pathname", set_pathname);
  196. passed &= setter!("search", set_search);
  197. passed &= setter!("hash", set_hash);
  198. assert!(passed);
  199. }
  200. fn test_eq_eprint(expected: String, actual: &str, name: &str, comment: Option<&str>) -> bool {
  201. if expected == actual {
  202. return true;
  203. }
  204. eprint_failure(
  205. format!("expected: {}\n actual: {}", expected, actual),
  206. name,
  207. comment,
  208. );
  209. false
  210. }
  211. fn eprint_failure(err: String, name: &str, comment: Option<&str>) {
  212. eprintln!(" test: {}\n{}", name, err);
  213. if let Some(comment) = comment {
  214. eprintln!("{}\n", comment);
  215. } else {
  216. eprintln!("");
  217. }
  218. }