data.rs 7.0 KB

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