data.rs 7.8 KB

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