data.rs 6.9 KB

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