diff options
Diffstat (limited to 'validator_derive/tests')
19 files changed, 438 insertions, 0 deletions
diff --git a/validator_derive/tests/compile-fail/custom_not_string.rs b/validator_derive/tests/compile-fail/custom_not_string.rs new file mode 100644 index 0000000..e576c5c --- /dev/null +++ b/validator_derive/tests/compile-fail/custom_not_string.rs @@ -0,0 +1,15 @@ +#![feature(proc_macro, attr_literals)] + +#[macro_use] extern crate validator_derive; +extern crate validator; +use validator::Validate; + +#[derive(Validate)] +//~^ ERROR: custom derive attribute panicked +//~^^ HELP: Invalid attribute #[validate] on field `s`: invalid argument for `custom` validator: only strings are allowed +struct Test { + #[validate(custom = 2)] + s: String, +} + +fn main() {} diff --git a/validator_derive/tests/compile-fail/length/equal_and_min_max_set.rs b/validator_derive/tests/compile-fail/length/equal_and_min_max_set.rs new file mode 100644 index 0000000..cc6654c --- /dev/null +++ b/validator_derive/tests/compile-fail/length/equal_and_min_max_set.rs @@ -0,0 +1,15 @@ +#![feature(proc_macro, attr_literals)] + +#[macro_use] extern crate validator_derive; +extern crate validator; +use validator::Validate; + +#[derive(Validate)] +//~^ ERROR: custom derive attribute panicked +//~^^ HELP: Invalid attribute #[validate] on field `s`: both `equal` and `min` or `max` have been set in `length` validator: probably a mistake +struct Test { + #[validate(length(min = 1, equal = 2))] + s: String, +} + +fn main() {} diff --git a/validator_derive/tests/compile-fail/length/no_args.rs b/validator_derive/tests/compile-fail/length/no_args.rs new file mode 100644 index 0000000..35a44df --- /dev/null +++ b/validator_derive/tests/compile-fail/length/no_args.rs @@ -0,0 +1,15 @@ +#![feature(proc_macro, attr_literals)] + +#[macro_use] extern crate validator_derive; +extern crate validator; +use validator::Validate; + +#[derive(Validate)] +//~^ ERROR: custom derive attribute panicked +//~^^ HELP: Invalid attribute #[validate] on field `s`: Validator `length` requires at least 1 argument out of `min`, `max` and `equal` +struct Test { + #[validate(length())] + s: String, +} + +fn main() {} diff --git a/validator_derive/tests/compile-fail/length/unknown_arg.rs b/validator_derive/tests/compile-fail/length/unknown_arg.rs new file mode 100644 index 0000000..d8e6185 --- /dev/null +++ b/validator_derive/tests/compile-fail/length/unknown_arg.rs @@ -0,0 +1,15 @@ +#![feature(proc_macro, attr_literals)] + +#[macro_use] extern crate validator_derive; +extern crate validator; +use validator::Validate; + +#[derive(Validate)] +//~^ ERROR: custom derive attribute panicked +//~^^ HELP: Invalid attribute #[validate] on field `s`: unknown argument `eq` for validator `length` (it only has `min`, `max`, `equal`) +struct Test { + #[validate(length(eq = 2))] + s: String, +} + +fn main() {} diff --git a/validator_derive/tests/compile-fail/length/wrong_arg_type.rs b/validator_derive/tests/compile-fail/length/wrong_arg_type.rs new file mode 100644 index 0000000..4d3f602 --- /dev/null +++ b/validator_derive/tests/compile-fail/length/wrong_arg_type.rs @@ -0,0 +1,15 @@ +#![feature(proc_macro, attr_literals)] + +#[macro_use] extern crate validator_derive; +extern crate validator; +use validator::Validate; + +#[derive(Validate)] +//~^ ERROR: custom derive attribute panicked +//~^^ HELP: Invalid attribute #[validate] on field `s`: invalid argument type for `min` of `length` validator: only integers are allowed +struct Test { + #[validate(length(min = "2"))] + s: String, +} + +fn main() {} diff --git a/validator_derive/tests/compile-fail/length/wrong_type.rs b/validator_derive/tests/compile-fail/length/wrong_type.rs new file mode 100644 index 0000000..e8a28ca --- /dev/null +++ b/validator_derive/tests/compile-fail/length/wrong_type.rs @@ -0,0 +1,15 @@ +#![feature(proc_macro, attr_literals)] + +#[macro_use] extern crate validator_derive; +extern crate validator; +use validator::Validate; + +#[derive(Validate)] +//~^ ERROR: custom derive attribute panicked +//~^^ HELP: Invalid attribute #[validate] on field `s`: Validator `length` can only be used on types `String` or `Vec` but found `usize` +struct Test { + #[validate(length())] + s: usize, +} + +fn main() {} diff --git a/validator_derive/tests/compile-fail/no_validations.rs b/validator_derive/tests/compile-fail/no_validations.rs new file mode 100644 index 0000000..0a65bdd --- /dev/null +++ b/validator_derive/tests/compile-fail/no_validations.rs @@ -0,0 +1,15 @@ +#![feature(proc_macro, attr_literals)] + +#[macro_use] extern crate validator_derive; +extern crate validator; +use validator::Validate; + +#[derive(Validate)] +//~^ ERROR: custom derive attribute panicked +//~^^ HELP: Invalid attribute #[validate] on field `s`: it needs at least one validator +struct Test { + #[validate()] + s: String, +} + +fn main() {} diff --git a/validator_derive/tests/compile-fail/range/missing_arg.rs b/validator_derive/tests/compile-fail/range/missing_arg.rs new file mode 100644 index 0000000..94913cf --- /dev/null +++ b/validator_derive/tests/compile-fail/range/missing_arg.rs @@ -0,0 +1,15 @@ +#![feature(proc_macro, attr_literals)] + +#[macro_use] extern crate validator_derive; +extern crate validator; +use validator::Validate; + +#[derive(Validate)] +//~^ ERROR: custom derive attribute panicked +//~^^ HELP: Invalid attribute #[validate] on field `s`: Validator `range` requires 2 arguments: `min` and `max` +struct Test { + #[validate(range(min = 2.0))] + s: i32, +} + +fn main() {} diff --git a/validator_derive/tests/compile-fail/range/no_args.rs b/validator_derive/tests/compile-fail/range/no_args.rs new file mode 100644 index 0000000..0532cc0 --- /dev/null +++ b/validator_derive/tests/compile-fail/range/no_args.rs @@ -0,0 +1,15 @@ +#![feature(proc_macro, attr_literals)] + +#[macro_use] extern crate validator_derive; +extern crate validator; +use validator::Validate; + +#[derive(Validate)] +//~^ ERROR: custom derive attribute panicked +//~^^ HELP: Invalid attribute #[validate] on field `s`: Validator `range` requires 2 arguments: `min` and `max` +struct Test { + #[validate(range())] + s: i32, +} + +fn main() {} diff --git a/validator_derive/tests/compile-fail/range/unknown_arg.rs b/validator_derive/tests/compile-fail/range/unknown_arg.rs new file mode 100644 index 0000000..7d2ac57 --- /dev/null +++ b/validator_derive/tests/compile-fail/range/unknown_arg.rs @@ -0,0 +1,15 @@ +#![feature(proc_macro, attr_literals)] + +#[macro_use] extern crate validator_derive; +extern crate validator; +use validator::Validate; + +#[derive(Validate)] +//~^ ERROR: custom derive attribute panicked +//~^^ HELP: Invalid attribute #[validate] on field `s`: unknown argument `mi` for validator `range` (it only has `min`, `max`) +struct Test { + #[validate(range(mi = 2, max = 3))] + s: i32, +} + +fn main() {} diff --git a/validator_derive/tests/compile-fail/range/wrong_arg_type.rs b/validator_derive/tests/compile-fail/range/wrong_arg_type.rs new file mode 100644 index 0000000..a62a5ff --- /dev/null +++ b/validator_derive/tests/compile-fail/range/wrong_arg_type.rs @@ -0,0 +1,15 @@ +#![feature(proc_macro, attr_literals)] + +#[macro_use] extern crate validator_derive; +extern crate validator; +use validator::Validate; + +#[derive(Validate)] +//~^ ERROR: custom derive attribute panicked +//~^^ HELP: Invalid attribute #[validate] on field `s`: invalid argument type for `min` of `range` validator: only integers are allowed +struct Test { + #[validate(range(min = "2", max = 3))] + s: i32, +} + +fn main() {} diff --git a/validator_derive/tests/compile-fail/range/wrong_type.rs b/validator_derive/tests/compile-fail/range/wrong_type.rs new file mode 100644 index 0000000..e341364 --- /dev/null +++ b/validator_derive/tests/compile-fail/range/wrong_type.rs @@ -0,0 +1,15 @@ +#![feature(proc_macro, attr_literals)] + +#[macro_use] extern crate validator_derive; +extern crate validator; +use validator::Validate; + +#[derive(Validate)] +//~^ ERROR: custom derive attribute panicked +//~^^ HELP: Invalid attribute #[validate] on field `s`: Validator `range` can only be used on number types but found `String` +struct Test { + #[validate(range(min = 10.0, max = 12.0))] + s: String, +} + +fn main() {} diff --git a/validator_derive/tests/compile_test.rs b/validator_derive/tests/compile_test.rs new file mode 100644 index 0000000..b6fe8e5 --- /dev/null +++ b/validator_derive/tests/compile_test.rs @@ -0,0 +1,24 @@ +extern crate compiletest_rs as compiletest; + +use std::path::PathBuf; + +fn run_mode(mode: &'static str) { + let mut config = compiletest::default_config(); + let cfg_mode = mode.parse().expect("Invalid mode"); + + config.target_rustcflags = Some("-L target/debug/ -L target/debug/deps/".to_string()); + config.mode = cfg_mode; + config.src_base = PathBuf::from(format!("tests/{}", mode)); + + compiletest::run_tests(&config); +} + +#[test] +fn test_compile_fail() { + run_mode("compile-fail"); +} + +#[test] +fn test_run_pass() { + run_mode("run-pass"); +} diff --git a/validator_derive/tests/run-pass/custom.rs b/validator_derive/tests/run-pass/custom.rs new file mode 100644 index 0000000..205198e --- /dev/null +++ b/validator_derive/tests/run-pass/custom.rs @@ -0,0 +1,17 @@ +#![feature(proc_macro, attr_literals)] + +#[macro_use] extern crate validator_derive; +extern crate validator; +use validator::Validate; + +#[derive(Validate)] +struct Test { + #[validate(custom = "validate_something")] + s: String, +} + +fn validate_something(s: &str) -> Option<String> { + Some(s.to_string()) +} + +fn main() {} diff --git a/validator_derive/tests/run-pass/email.rs b/validator_derive/tests/run-pass/email.rs new file mode 100644 index 0000000..edfc357 --- /dev/null +++ b/validator_derive/tests/run-pass/email.rs @@ -0,0 +1,13 @@ +#![feature(proc_macro, attr_literals)] + +#[macro_use] extern crate validator_derive; +extern crate validator; +use validator::Validate; + +#[derive(Validate)] +struct Test { + #[validate(email)] + s: String, +} + +fn main() {} diff --git a/validator_derive/tests/run-pass/length.rs b/validator_derive/tests/run-pass/length.rs new file mode 100644 index 0000000..01b85ea --- /dev/null +++ b/validator_derive/tests/run-pass/length.rs @@ -0,0 +1,28 @@ +#![feature(proc_macro, attr_literals)] + +#[macro_use] extern crate validator_derive; +extern crate validator; +use validator::Validate; + +#[derive(Validate)] +struct Test { + #[validate(length(min = 1))] + s: String, + #[validate(length(min = 1, max = 2))] + s2: String, + #[validate(length(equal = 1))] + s3: String, + #[validate(length(max = 1))] + s4: String, + + #[validate(length(min = 1))] + s5: Vec<String>, + #[validate(length(min = 1, max = 2))] + s6: Vec<String>, + #[validate(length(equal = 1))] + s7: Vec<String>, + #[validate(length(max = 1))] + s8: Vec<String>, +} + +fn main() {} diff --git a/validator_derive/tests/run-pass/range.rs b/validator_derive/tests/run-pass/range.rs new file mode 100644 index 0000000..8f3a047 --- /dev/null +++ b/validator_derive/tests/run-pass/range.rs @@ -0,0 +1,27 @@ +#![feature(proc_macro, attr_literals)] + +#[macro_use] extern crate validator_derive; +extern crate validator; +use validator::Validate; + +#[derive(Validate)] +struct Test { + #[validate(range(min = 1, max = 2.2))] + s: isize, + #[validate(range(min = 1, max = 2))] + s2: usize, + #[validate(range(min = 18, max = 22))] + s3: i32, + #[validate(range(min = 18, max = 22))] + s4: i64, + #[validate(range(min = 18, max = 22))] + s5: u32, + #[validate(range(min = 18, max = 22))] + s6: u64, + #[validate(range(min = 18.1, max = 22))] + s7: i8, + #[validate(range(min = 18.0, max = 22))] + s8: u8, +} + +fn main() {} diff --git a/validator_derive/tests/run-pass/url.rs b/validator_derive/tests/run-pass/url.rs new file mode 100644 index 0000000..910676a --- /dev/null +++ b/validator_derive/tests/run-pass/url.rs @@ -0,0 +1,13 @@ +#![feature(proc_macro, attr_literals)] + +#[macro_use] extern crate validator_derive; +extern crate validator; +use validator::Validate; + +#[derive(Validate)] +struct Test { + #[validate(url)] + s: String, +} + +fn main() {} diff --git a/validator_derive/tests/test_derive.rs b/validator_derive/tests/test_derive.rs new file mode 100644 index 0000000..1e310b1 --- /dev/null +++ b/validator_derive/tests/test_derive.rs @@ -0,0 +1,136 @@ +#![feature(proc_macro, attr_literals)] + +#[macro_use] extern crate validator_derive; +extern crate validator; +#[macro_use] extern crate serde_derive; +extern crate serde_json; + +use validator::Validate; + + +#[derive(Debug, Validate, Deserialize)] +struct SignupData { + #[validate(email)] + mail: String, + #[validate(url)] + site: String, + #[validate(length(min = 1), custom = "validate_unique_username")] + #[serde(rename = "firstName")] + first_name: String, + #[validate(range(min = 18, max = 20))] + age: u32, +} + +fn validate_unique_username(username: &str) -> Option<String> { + if username == "xXxShad0wxXx" { + return Some("terrible_username".to_string()); + } + + None +} + +#[test] +fn test_can_validate_ok() { + let signup = SignupData { + mail: "bob@bob.com".to_string(), + site: "http://hello.com".to_string(), + first_name: "Bob".to_string(), + age: 18, + }; + + assert!(signup.validate().is_ok()); +} + +#[test] +fn test_bad_email_fails_validation() { + let signup = SignupData { + mail: "bob".to_string(), + site: "http://hello.com".to_string(), + first_name: "Bob".to_string(), + age: 18, + }; + let res = signup.validate(); + assert!(res.is_err()); + let errs = res.unwrap_err(); + assert!(errs.contains_key("mail")); + assert_eq!(errs["mail"], vec!["email".to_string()]); +} + +#[test] +fn test_bad_url_fails_validation() { + let signup = SignupData { + mail: "bob@bob.com".to_string(), + site: "//hello.com".to_string(), + first_name: "Bob".to_string(), + age: 18, + }; + let res = signup.validate(); + assert!(res.is_err()); + let errs = res.unwrap_err(); + assert!(errs.contains_key("site")); + assert_eq!(errs["site"], vec!["url".to_string()]); +} + +#[test] +fn test_bad_length_fails_validation_and_points_to_original_name() { + let signup = SignupData { + mail: "bob@bob.com".to_string(), + site: "http://hello.com".to_string(), + first_name: "".to_string(), + age: 18, + }; + let res = signup.validate(); + assert!(res.is_err()); + let errs = res.unwrap_err(); + println!("{:?}", errs); + assert!(errs.contains_key("firstName")); + assert_eq!(errs["firstName"], vec!["length".to_string()]); +} + + +#[test] +fn test_bad_range_fails_validation() { + let signup = SignupData { + mail: "bob@bob.com".to_string(), + site: "https://hello.com".to_string(), + first_name: "Bob".to_string(), + age: 1, + }; + let res = signup.validate(); + assert!(res.is_err()); + let errs = res.unwrap_err(); + assert!(errs.contains_key("age")); + assert_eq!(errs["age"], vec!["range".to_string()]); +} + +#[test] +fn test_can_have_multiple_errors() { + let signup = SignupData { + mail: "bob@bob.com".to_string(), + site: "https://hello.com".to_string(), + first_name: "".to_string(), + age: 1, + }; + let res = signup.validate(); + assert!(res.is_err()); + let errs = res.unwrap_err(); + assert!(errs.contains_key("age")); + assert!(errs.contains_key("firstName")); + assert_eq!(errs["age"], vec!["range".to_string()]); + assert_eq!(errs["firstName"], vec!["length".to_string()]); +} + +#[test] +fn test_custom_validation_error() { + let signup = SignupData { + mail: "bob@bob.com".to_string(), + site: "https://hello.com".to_string(), + first_name: "xXxShad0wxXx".to_string(), + age: 18, + }; + let res = signup.validate(); + assert!(res.is_err()); + let errs = res.unwrap_err(); + assert!(errs.contains_key("firstName")); + assert_eq!(errs["firstName"], vec!["terrible_username".to_string()]); +} |
