aboutsummaryrefslogtreecommitdiffstats
path: root/validator_derive/tests
diff options
context:
space:
mode:
authorVincent Prouillet2017-11-08 17:16:24 +0100
committerVincent Prouillet2017-11-08 17:16:24 +0100
commit9b175bc3ca7a2a65add0ec4a42a79b5c9f47314b (patch)
tree0d359b1d047841e1be14aeca8e0cf157cbdd6b42 /validator_derive/tests
parentf04f03f33c057e160856800bdd1a106b035d713b (diff)
downloadvalidator-9b175bc3ca7a2a65add0ec4a42a79b5c9f47314b.tar.bz2
Add missing credit_card derive impl
Diffstat (limited to 'validator_derive/tests')
-rw-r--r--validator_derive/tests/compile_test.rs2
-rw-r--r--validator_derive/tests/complex.rs1
-rw-r--r--validator_derive/tests/credit_card.rs77
3 files changed, 79 insertions, 1 deletions
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");
+}