From befcf7c3edda91f3ab793c420c4e4222440c40fd Mon Sep 17 00:00:00 2001 From: Pyfisch Date: Fri, 20 Nov 2015 22:17:48 +0100 Subject: [PATCH] Move all tests to tests/ folder. The folder is provided by Cargo and makes it easier to find tests. --- Cargo.toml | 2 +- src/form_urlencoded.rs | 33 ---------------- src/format.rs | 60 ------------------------------ src/lib.rs | 4 -- src/punycode.rs | 54 --------------------------- tests/form_urlencoded.rs | 29 +++++++++++++++ tests/format.rs | 56 ++++++++++++++++++++++++++++ tests/punycode.rs | 52 ++++++++++++++++++++++++++ {src => tests}/punycode_tests.json | 0 {src => tests}/tests.rs | 3 +- {src => tests}/urltestdata.txt | 0 11 files changed, 140 insertions(+), 153 deletions(-) create mode 100644 tests/form_urlencoded.rs create mode 100644 tests/format.rs create mode 100644 tests/punycode.rs rename {src => tests}/punycode_tests.json (100%) rename {src => tests}/tests.rs (99%) rename {src => tests}/urltestdata.txt (100%) diff --git a/Cargo.toml b/Cargo.toml index a95afc87..a7d64ac5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "url" -version = "0.5.0" +version = "0.5.1" authors = [ "Simon Sapin " ] description = "URL library for Rust, based on the WHATWG URL Standard" diff --git a/src/form_urlencoded.rs b/src/form_urlencoded.rs index 65209030..ad8e250b 100644 --- a/src/form_urlencoded.rs +++ b/src/form_urlencoded.rs @@ -142,36 +142,3 @@ where I: IntoIterator, I::Item: Borrow<(K, V)>, K: AsRef, V: AsRef { } output } - - - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_form_urlencoded() { - let pairs = &[ - ("foo".to_string(), "é&".to_string()), - ("bar".to_string(), "".to_string()), - ("foo".to_string(), "#".to_string()) - ]; - let encoded = serialize(pairs); - assert_eq!(encoded, "foo=%C3%A9%26&bar=&foo=%23"); - assert_eq!(parse(encoded.as_bytes()), pairs.to_vec()); - } - - #[test] - fn test_form_serialize() { - let pairs = [("foo", "é&"), - ("bar", ""), - ("foo", "#")]; - - let want = "foo=%C3%A9%26&bar=&foo=%23"; - // Works with referenced tuples - assert_eq!(serialize(pairs.iter()), want); - // Works with owned tuples - assert_eq!(serialize(pairs.iter().map(|p| (p.0, p.1))), want); - - } -} diff --git a/src/format.rs b/src/format.rs index c448bd53..ad656056 100644 --- a/src/format.rs +++ b/src/format.rs @@ -79,63 +79,3 @@ impl<'a> fmt::Display for UrlNoFragmentFormatter<'a> { Ok(()) } } - - -/// Formatting Tests -#[cfg(test)] -mod tests { - use super::super::Url; - use super::{PathFormatter, UserInfoFormatter}; - - #[test] - fn path_formatting() { - let data = [ - (vec![], "/"), - (vec![""], "/"), - (vec!["test", "path"], "/test/path"), - (vec!["test", "path", ""], "/test/path/") - ]; - for &(ref path, result) in &data { - assert_eq!(PathFormatter { - path: path - }.to_string(), result.to_string()); - } - } - - #[test] - fn userinfo_formatting() { - // Test data as (username, password, result) tuples. - let data = [ - ("", None, ""), - ("", Some(""), ":@"), - ("", Some("password"), ":password@"), - ("username", None, "username@"), - ("username", Some(""), "username:@"), - ("username", Some("password"), "username:password@") - ]; - for &(username, password, result) in &data { - assert_eq!(UserInfoFormatter { - username: username, - password: password - }.to_string(), result.to_string()); - } - } - - #[test] - fn relative_scheme_url_formatting() { - let data = [ - ("http://example.com/", "http://example.com/"), - ("http://addslash.com", "http://addslash.com/"), - ("http://@emptyuser.com/", "http://emptyuser.com/"), - ("http://:@emptypass.com/", "http://:@emptypass.com/"), - ("http://user@user.com/", "http://user@user.com/"), - ("http://user:pass@userpass.com/", "http://user:pass@userpass.com/"), - ("http://slashquery.com/path/?q=something", "http://slashquery.com/path/?q=something"), - ("http://noslashquery.com/path?q=something", "http://noslashquery.com/path?q=something") - ]; - for &(input, result) in &data { - let url = Url::parse(input).unwrap(); - assert_eq!(url.to_string(), result.to_string()); - } - } -} diff --git a/src/lib.rs b/src/lib.rs index d4681085..f03c8082 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -162,10 +162,6 @@ pub mod form_urlencoded; pub mod punycode; pub mod format; -#[cfg(test)] -mod tests; - - /// The parsed representation of an absolute URL. #[derive(PartialEq, Eq, Clone, Debug, Hash, PartialOrd, Ord)] #[cfg_attr(feature="heap_size", derive(HeapSizeOf))] diff --git a/src/punycode.rs b/src/punycode.rs index 3ca7f4b2..27525faf 100644 --- a/src/punycode.rs +++ b/src/punycode.rs @@ -211,57 +211,3 @@ fn value_to_digit(value: u32, output: &mut String) { }; unsafe { output.as_mut_vec().push(code_point as u8) } } - - -#[cfg(test)] -mod tests { - use super::{decode, encode_str}; - use rustc_serialize::json::{Json, Object}; - - fn one_test(description: &str, decoded: &str, encoded: &str) { - match decode(encoded) { - None => panic!("Decoding {} failed.", encoded), - Some(result) => { - let result = result.into_iter().collect::(); - assert!(result == decoded, - format!("Incorrect decoding of {}:\n {}\n!= {}\n{}", - encoded, result, decoded, description)) - } - } - - match encode_str(decoded) { - None => panic!("Encoding {} failed.", decoded), - Some(result) => { - assert!(result == encoded, - format!("Incorrect encoding of {}:\n {}\n!= {}\n{}", - decoded, result, encoded, description)) - } - } - } - - fn get_string<'a>(map: &'a Object, key: &str) -> &'a str { - match map.get(&key.to_string()) { - Some(&Json::String(ref s)) => s, - None => "", - _ => panic!(), - } - } - - #[test] - fn test_punycode() { - - match Json::from_str(include_str!("punycode_tests.json")) { - Ok(Json::Array(tests)) => for test in &tests { - match test { - &Json::Object(ref o) => one_test( - get_string(o, "description"), - get_string(o, "decoded"), - get_string(o, "encoded") - ), - _ => panic!(), - } - }, - other => panic!("{:?}", other) - } - } -} diff --git a/tests/form_urlencoded.rs b/tests/form_urlencoded.rs new file mode 100644 index 00000000..59080cf9 --- /dev/null +++ b/tests/form_urlencoded.rs @@ -0,0 +1,29 @@ +extern crate url; + +use url::form_urlencoded::*; + +#[test] +fn test_form_urlencoded() { + let pairs = &[ + ("foo".to_string(), "é&".to_string()), + ("bar".to_string(), "".to_string()), + ("foo".to_string(), "#".to_string()) + ]; + let encoded = serialize(pairs); + assert_eq!(encoded, "foo=%C3%A9%26&bar=&foo=%23"); + assert_eq!(parse(encoded.as_bytes()), pairs.to_vec()); +} + +#[test] +fn test_form_serialize() { + let pairs = [("foo", "é&"), + ("bar", ""), + ("foo", "#")]; + + let want = "foo=%C3%A9%26&bar=&foo=%23"; + // Works with referenced tuples + assert_eq!(serialize(pairs.iter()), want); + // Works with owned tuples + assert_eq!(serialize(pairs.iter().map(|p| (p.0, p.1))), want); + +} diff --git a/tests/format.rs b/tests/format.rs new file mode 100644 index 00000000..a32b9e40 --- /dev/null +++ b/tests/format.rs @@ -0,0 +1,56 @@ +extern crate url; + +use url::Url; +use url::format::{PathFormatter, UserInfoFormatter}; + +#[test] +fn path_formatting() { + let data = [ + (vec![], "/"), + (vec![""], "/"), + (vec!["test", "path"], "/test/path"), + (vec!["test", "path", ""], "/test/path/") + ]; + for &(ref path, result) in &data { + assert_eq!(PathFormatter { + path: path + }.to_string(), result.to_string()); + } +} + +#[test] +fn userinfo_formatting() { + // Test data as (username, password, result) tuples. + let data = [ + ("", None, ""), + ("", Some(""), ":@"), + ("", Some("password"), ":password@"), + ("username", None, "username@"), + ("username", Some(""), "username:@"), + ("username", Some("password"), "username:password@") + ]; + for &(username, password, result) in &data { + assert_eq!(UserInfoFormatter { + username: username, + password: password + }.to_string(), result.to_string()); + } +} + +#[test] +fn relative_scheme_url_formatting() { + let data = [ + ("http://example.com/", "http://example.com/"), + ("http://addslash.com", "http://addslash.com/"), + ("http://@emptyuser.com/", "http://emptyuser.com/"), + ("http://:@emptypass.com/", "http://:@emptypass.com/"), + ("http://user@user.com/", "http://user@user.com/"), + ("http://user:pass@userpass.com/", "http://user:pass@userpass.com/"), + ("http://slashquery.com/path/?q=something", "http://slashquery.com/path/?q=something"), + ("http://noslashquery.com/path?q=something", "http://noslashquery.com/path?q=something") + ]; + for &(input, result) in &data { + let url = Url::parse(input).unwrap(); + assert_eq!(url.to_string(), result.to_string()); + } +} diff --git a/tests/punycode.rs b/tests/punycode.rs new file mode 100644 index 00000000..ae42b34d --- /dev/null +++ b/tests/punycode.rs @@ -0,0 +1,52 @@ +extern crate url; +extern crate rustc_serialize; + +use url::punycode::{decode, encode_str}; +use rustc_serialize::json::{Json, Object}; + +fn one_test(description: &str, decoded: &str, encoded: &str) { + match decode(encoded) { + None => panic!("Decoding {} failed.", encoded), + Some(result) => { + let result = result.into_iter().collect::(); + assert!(result == decoded, + format!("Incorrect decoding of {}:\n {}\n!= {}\n{}", + encoded, result, decoded, description)) + } + } + + match encode_str(decoded) { + None => panic!("Encoding {} failed.", decoded), + Some(result) => { + assert!(result == encoded, + format!("Incorrect encoding of {}:\n {}\n!= {}\n{}", + decoded, result, encoded, description)) + } + } +} + +fn get_string<'a>(map: &'a Object, key: &str) -> &'a str { + match map.get(&key.to_string()) { + Some(&Json::String(ref s)) => s, + None => "", + _ => panic!(), + } +} + +#[test] +fn test_punycode() { + + match Json::from_str(include_str!("punycode_tests.json")) { + Ok(Json::Array(tests)) => for test in &tests { + match test { + &Json::Object(ref o) => one_test( + get_string(o, "description"), + get_string(o, "decoded"), + get_string(o, "encoded") + ), + _ => panic!(), + } + }, + other => panic!("{:?}", other) + } +} diff --git a/src/punycode_tests.json b/tests/punycode_tests.json similarity index 100% rename from src/punycode_tests.json rename to tests/punycode_tests.json diff --git a/src/tests.rs b/tests/tests.rs similarity index 99% rename from src/tests.rs rename to tests/tests.rs index c5d12c2c..c19a979a 100644 --- a/src/tests.rs +++ b/tests/tests.rs @@ -6,10 +6,11 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +extern crate url; use std::char; use std::net::{Ipv4Addr, Ipv6Addr}; -use super::{UrlParser, Url, SchemeData, RelativeSchemeData, Host}; +use url::{UrlParser, Url, SchemeData, RelativeSchemeData, Host}; #[test] diff --git a/src/urltestdata.txt b/tests/urltestdata.txt similarity index 100% rename from src/urltestdata.txt rename to tests/urltestdata.txt