aboutsummaryrefslogtreecommitdiffstats
path: root/validator_derive/tests
diff options
context:
space:
mode:
authorVincent Prouillet2016-12-29 13:13:42 +0900
committerVincent Prouillet2016-12-29 13:16:41 +0900
commita0f2b4c0d4820bfdcc13c7abdfdc7802d9f6d493 (patch)
tree75b5bc7b008ac374ac0a437de580d5eda1c67a52 /validator_derive/tests
parentce9ee1ce4ac73ba7989c88c67b16243cbcec2b98 (diff)
downloadvalidator-a0f2b4c0d4820bfdcc13c7abdfdc7802d9f6d493.tar.bz2
Added must_match
Diffstat (limited to 'validator_derive/tests')
-rw-r--r--validator_derive/tests/compile-fail/must_match/field_doesnt_exist.rs15
-rw-r--r--validator_derive/tests/compile-fail/must_match/field_type_doesnt_match.rs16
-rw-r--r--validator_derive/tests/run-pass/must_match.rs18
-rw-r--r--validator_derive/tests/test_derive.rs27
4 files changed, 76 insertions, 0 deletions
diff --git a/validator_derive/tests/compile-fail/must_match/field_doesnt_exist.rs b/validator_derive/tests/compile-fail/must_match/field_doesnt_exist.rs
new file mode 100644
index 0000000..03828e2
--- /dev/null
+++ b/validator_derive/tests/compile-fail/must_match/field_doesnt_exist.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 `password`: invalid argument for `must_match` validator: field doesn't exist in struct
+struct Test {
+ #[validate(must_match = "password2")]
+ password: String,
+}
+
+fn main() {}
diff --git a/validator_derive/tests/compile-fail/must_match/field_type_doesnt_match.rs b/validator_derive/tests/compile-fail/must_match/field_type_doesnt_match.rs
new file mode 100644
index 0000000..61c0847
--- /dev/null
+++ b/validator_derive/tests/compile-fail/must_match/field_type_doesnt_match.rs
@@ -0,0 +1,16 @@
+#![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 `password`: invalid argument for `must_match` validator: types of field can't match
+struct Test {
+ #[validate(must_match = "password2")]
+ password: String,
+ password2: i32,
+}
+
+fn main() {}
diff --git a/validator_derive/tests/run-pass/must_match.rs b/validator_derive/tests/run-pass/must_match.rs
new file mode 100644
index 0000000..0d2d917
--- /dev/null
+++ b/validator_derive/tests/run-pass/must_match.rs
@@ -0,0 +1,18 @@
+#![feature(proc_macro, attr_literals)]
+
+#[macro_use] extern crate validator_derive;
+extern crate validator;
+use validator::Validate;
+
+#[derive(Validate)]
+struct Test {
+ #[validate(must_match = "s2")]
+ s: String,
+ s2: String,
+
+ #[validate(must_match = "s4")]
+ s3: usize,
+ s4: usize,
+}
+
+fn main() {}
diff --git a/validator_derive/tests/test_derive.rs b/validator_derive/tests/test_derive.rs
index 1e310b1..7cc63eb 100644
--- a/validator_derive/tests/test_derive.rs
+++ b/validator_derive/tests/test_derive.rs
@@ -21,6 +21,14 @@ struct SignupData {
age: u32,
}
+#[derive(Debug, Validate)]
+struct PasswordData {
+ #[validate(must_match = "password2")]
+ password: String,
+ password2: String,
+}
+
+
fn validate_unique_username(username: &str) -> Option<String> {
if username == "xXxShad0wxXx" {
return Some("terrible_username".to_string());
@@ -134,3 +142,22 @@ fn test_custom_validation_error() {
assert!(errs.contains_key("firstName"));
assert_eq!(errs["firstName"], vec!["terrible_username".to_string()]);
}
+
+#[test]
+fn test_must_match_can_work() {
+ let data = PasswordData {
+ password: "passw0rd".to_string(),
+ password2: "passw0rd".to_string(),
+ };
+ assert!(data.validate().is_ok())
+}
+
+
+#[test]
+fn test_must_match_can_fail() {
+ let data = PasswordData {
+ password: "passw0rd".to_string(),
+ password2: "password".to_string(),
+ };
+ assert!(data.validate().is_err())
+}