From 9b175bc3ca7a2a65add0ec4a42a79b5c9f47314b Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Wed, 8 Nov 2017 17:16:24 +0100 Subject: Add missing credit_card derive impl --- validator_derive/tests/compile_test.rs | 2 +- validator_derive/tests/complex.rs | 1 + validator_derive/tests/credit_card.rs | 77 ++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 validator_derive/tests/credit_card.rs (limited to 'validator_derive/tests') diff --git a/validator_derive/tests/compile_test.rs b/validator_derive/tests/compile_test.rs index b6fe8e5..6c2b251 100644 --- a/validator_derive/tests/compile_test.rs +++ b/validator_derive/tests/compile_test.rs @@ -3,7 +3,7 @@ extern crate compiletest_rs as compiletest; use std::path::PathBuf; fn run_mode(mode: &'static str) { - let mut config = compiletest::default_config(); + let mut config = compiletest::Config::default(); let cfg_mode = mode.parse().expect("Invalid mode"); config.target_rustcflags = Some("-L target/debug/ -L target/debug/deps/".to_string()); diff --git a/validator_derive/tests/complex.rs b/validator_derive/tests/complex.rs index b9363e4..db62600 100644 --- a/validator_derive/tests/complex.rs +++ b/validator_derive/tests/complex.rs @@ -170,6 +170,7 @@ fn test_works_with_question_mark_operator() { let signup = SignupData { mail: "invalid_email".to_string(), site: "http://hello.com".to_string(), + phone: "+14152370800".to_string(), first_name: "Bob".to_string(), age: 18, }; diff --git a/validator_derive/tests/credit_card.rs b/validator_derive/tests/credit_card.rs new file mode 100644 index 0000000..2f1d720 --- /dev/null +++ b/validator_derive/tests/credit_card.rs @@ -0,0 +1,77 @@ +#[macro_use] +extern crate validator_derive; +extern crate validator; + +use validator::Validate; + + +#[test] +fn can_validate_valid_card_number() { + #[derive(Debug, Validate)] + struct TestStruct { + #[validate(credit_card)] + val: String, + } + + let s = TestStruct { + val: "5236313877109142".to_string(), + }; + + assert!(s.validate().is_ok()); +} + +#[test] +fn bad_credit_card_fails_validation() { + #[derive(Debug, Validate)] + struct TestStruct { + #[validate(email)] + val: String, + } + + let s = TestStruct { + val: "bob".to_string(), + }; + let res = s.validate(); + assert!(res.is_err()); + let errs = res.unwrap_err().inner(); + assert!(errs.contains_key("val")); + assert_eq!(errs["val"].len(), 1); + assert_eq!(errs["val"][0].code, "email"); + assert_eq!(errs["val"][0].params["value"], "bob"); +} + +#[test] +fn can_specify_code_for_credit_card() { + #[derive(Debug, Validate)] + struct TestStruct { + #[validate(credit_card(code = "oops"))] + val: String, + } + let s = TestStruct { + val: "bob".to_string(), + }; + let res = s.validate(); + assert!(res.is_err()); + let errs = res.unwrap_err().inner(); + assert!(errs.contains_key("val")); + assert_eq!(errs["val"].len(), 1); + assert_eq!(errs["val"][0].code, "oops"); +} + +#[test] +fn can_specify_message_for_credit_card() { + #[derive(Debug, Validate)] + struct TestStruct { + #[validate(credit_card(message = "oops"))] + val: String, + } + let s = TestStruct { + val: "bob".to_string(), + }; + let res = s.validate(); + assert!(res.is_err()); + let errs = res.unwrap_err().inner(); + assert!(errs.contains_key("val")); + assert_eq!(errs["val"].len(), 1); + assert_eq!(errs["val"][0].clone().message.unwrap(), "oops"); +} -- cgit v1.2.3