data.rs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. trait JsonExt {
  31. fn take_key(&mut self, key: &str) -> Option<Value>;
  32. fn string(self) -> String;
  33. fn take_string(&mut self, key: &str) -> String;
  34. }
  35. impl JsonExt for Value {
  36. fn take_key(&mut self, key: &str) -> Option<Value> {
  37. self.as_object_mut().unwrap().remove(key)
  38. }
  39. fn string(self) -> String {
  40. if let Value::String(s) = self {
  41. s
  42. } else {
  43. panic!("Not a Value::String")
  44. }
  45. }
  46. fn take_string(&mut self, key: &str) -> String {
  47. self.take_key(key).unwrap().string()
  48. }
  49. }
  50. #[test]
  51. fn urltestdata() {
  52. // Copied form https://github.com/w3c/web-platform-tests/blob/master/url/
  53. let mut json = Value::from_str(include_str!("urltestdata.json"))
  54. .expect("JSON parse error in urltestdata.json");
  55. let mut passed = true;
  56. for entry in json.as_array_mut().unwrap() {
  57. if entry.is_string() {
  58. continue; // ignore comments
  59. }
  60. let base = entry.take_string("base");
  61. let input = entry.take_string("input");
  62. let failure = entry.take_key("failure").is_some();
  63. let base = match Url::parse(&base) {
  64. Ok(base) => base,
  65. Err(_) if failure => continue,
  66. Err(message) => {
  67. eprint_failure(
  68. format!(" failed: error parsing base {:?}: {}", base, message),
  69. &format!("parse base for {:?}", input),
  70. None,
  71. );
  72. passed = false;
  73. continue;
  74. }
  75. };
  76. let url = match (base.join(&input), failure) {
  77. (Ok(url), false) => url,
  78. (Err(_), true) => continue,
  79. (Err(message), false) => {
  80. eprint_failure(
  81. format!(" failed: {}", message),
  82. &format!("parse URL for {:?}", input),
  83. None,
  84. );
  85. passed = false;
  86. continue;
  87. }
  88. (Ok(_), true) => {
  89. eprint_failure(
  90. format!(" failed: expected parse error for URL {:?}", input),
  91. &format!("parse URL for {:?}", input),
  92. None,
  93. );
  94. passed = false;
  95. continue;
  96. }
  97. };
  98. passed &= check_invariants(&url, &format!("invariants for {:?}", input), None);
  99. for &attr in ATTRIBS {
  100. passed &= test_eq_eprint(
  101. entry.take_string(attr),
  102. get(&url, attr),
  103. &format!("{:?} - {}", input, attr),
  104. None,
  105. );
  106. }
  107. if let Some(expected_origin) = entry.take_key("origin").map(|s| s.string()) {
  108. passed &= test_eq_eprint(
  109. expected_origin,
  110. &quirks::origin(&url),
  111. &format!("origin for {:?}", input),
  112. None,
  113. );
  114. }
  115. }
  116. assert!(passed)
  117. }
  118. #[test]
  119. fn setters_tests() {
  120. let mut json = Value::from_str(include_str!("setters_tests.json"))
  121. .expect("JSON parse error in setters_tests.json");
  122. let mut passed = true;
  123. for &attr in ATTRIBS {
  124. if attr == "href" {
  125. continue;
  126. }
  127. let mut tests = json.take_key(attr).unwrap();
  128. for mut test in tests.as_array_mut().unwrap().drain(..) {
  129. let comment = test.take_key("comment").map(|s| s.string());
  130. let href = test.take_string("href");
  131. let new_value = test.take_string("new_value");
  132. let name = format!("{:?}.{} = {:?}", href, attr, new_value);
  133. let mut expected = test.take_key("expected").unwrap();
  134. let mut url = Url::parse(&href).unwrap();
  135. let comment_ref = comment.as_deref();
  136. passed &= check_invariants(&url, &name, comment_ref);
  137. let _ = set(&mut url, attr, &new_value);
  138. for attr in ATTRIBS {
  139. if let Some(value) = expected.take_key(attr) {
  140. passed &= test_eq_eprint(value.string(), get(&url, attr), &name, comment_ref);
  141. };
  142. }
  143. passed &= check_invariants(&url, &name, comment_ref);
  144. }
  145. }
  146. assert!(passed);
  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. ];