diff options
| author | Vincent Prouillet | 2019-10-20 18:18:41 +0200 |
|---|---|---|
| committer | GitHub | 2019-10-20 18:18:41 +0200 |
| commit | 97a11c88f26b00e897d21709d9576ad3cea6f220 (patch) | |
| tree | 7469738d72627423c5b99df7dfae099951f4d322 /validator_derive/tests | |
| parent | 87cf7cdac24dd0d4ea83a8a88640da95ae7ac93a (diff) | |
| parent | 52cd62998b8f45be56512ce1d89aef8bf1a9f670 (diff) | |
| download | validator-97a11c88f26b00e897d21709d9576ad3cea6f220.tar.bz2 | |
Merge pull request #87 from Keats/next
Use Github actions for CI
Diffstat (limited to 'validator_derive/tests')
| -rw-r--r-- | validator_derive/tests/non_control.rs | 75 | ||||
| -rw-r--r-- | validator_derive/tests/regex.rs | 8 | ||||
| -rw-r--r-- | validator_derive/tests/run-pass/custom.rs | 4 | ||||
| -rw-r--r-- | validator_derive/tests/run-pass/regex.rs | 4 |
4 files changed, 83 insertions, 8 deletions
diff --git a/validator_derive/tests/non_control.rs b/validator_derive/tests/non_control.rs new file mode 100644 index 0000000..37fb153 --- /dev/null +++ b/validator_derive/tests/non_control.rs @@ -0,0 +1,75 @@ +#[macro_use] +extern crate validator_derive; +extern crate validator; + +use validator::Validate; + +#[cfg(feature = "unic")] +#[test] +fn can_validate_utf8_ok() { + #[derive(Debug, Validate)] + struct TestStruct { + #[validate(non_control_character)] + val: String, + } + + let s = TestStruct { val: "하늘".to_string() }; + + assert!(s.validate().is_ok()); +} + +#[cfg(feature = "unic")] +#[test] +fn utf8_with_control_fails_validation() { + #[derive(Debug, Validate)] + struct TestStruct { + #[validate(non_control_character)] + val: String, + } + + let s = TestStruct { val: "\u{009F}하늘".to_string() }; + let res = s.validate(); + assert!(res.is_err()); + let err = res.unwrap_err(); + let errs = err.field_errors(); + assert!(errs.contains_key("val")); + assert_eq!(errs["val"].len(), 1); + assert_eq!(errs["val"][0].code, "non_control_character"); +} + +#[cfg(feature = "unic")] +#[test] +fn can_specify_code_for_non_control_character() { + #[derive(Debug, Validate)] + struct TestStruct { + #[validate(non_control_character(code = "oops"))] + val: String, + } + let s = TestStruct { val: "\u{009F}하늘".to_string() }; + let res = s.validate(); + assert!(res.is_err()); + let err = res.unwrap_err(); + let errs = err.field_errors(); + assert!(errs.contains_key("val")); + assert_eq!(errs["val"].len(), 1); + assert_eq!(errs["val"][0].code, "oops"); + assert_eq!(errs["val"][0].params["value"], "\u{9F}하늘"); +} + +#[cfg(feature = "unic")] +#[test] +fn can_specify_message_for_non_control_character() { + #[derive(Debug, Validate)] + struct TestStruct { + #[validate(non_control_character(message = "oops"))] + val: String, + } + let s = TestStruct { val: "\u{009F}하늘".to_string() }; + let res = s.validate(); + assert!(res.is_err()); + let err = res.unwrap_err(); + let errs = err.field_errors(); + assert!(errs.contains_key("val")); + assert_eq!(errs["val"].len(), 1); + assert_eq!(errs["val"][0].clone().message.unwrap(), "oops"); +} diff --git a/validator_derive/tests/regex.rs b/validator_derive/tests/regex.rs index 7e3dfc5..d3e3c1e 100644 --- a/validator_derive/tests/regex.rs +++ b/validator_derive/tests/regex.rs @@ -16,7 +16,7 @@ lazy_static! { fn can_validate_valid_regex() { #[derive(Debug, Validate)] struct TestStruct { - #[validate(regex = "RE2")] + #[validate(regex = "crate::RE2")] val: String, } @@ -29,7 +29,7 @@ fn can_validate_valid_regex() { fn bad_value_for_regex_fails_validation() { #[derive(Debug, Validate)] struct TestStruct { - #[validate(regex = "RE2")] + #[validate(regex = "crate::RE2")] val: String, } @@ -48,7 +48,7 @@ fn bad_value_for_regex_fails_validation() { fn can_specify_code_for_regex() { #[derive(Debug, Validate)] struct TestStruct { - #[validate(regex(path = "RE2", code = "oops"))] + #[validate(regex(path = "crate::RE2", code = "oops"))] val: String, } let s = TestStruct { val: "2".to_string() }; @@ -65,7 +65,7 @@ fn can_specify_code_for_regex() { fn can_specify_message_for_regex() { #[derive(Debug, Validate)] struct TestStruct { - #[validate(regex(path = "RE2", message = "oops"))] + #[validate(regex(path = "crate::RE2", message = "oops"))] val: String, } let s = TestStruct { val: "2".to_string() }; diff --git a/validator_derive/tests/run-pass/custom.rs b/validator_derive/tests/run-pass/custom.rs index dff6375..f745de6 100644 --- a/validator_derive/tests/run-pass/custom.rs +++ b/validator_derive/tests/run-pass/custom.rs @@ -6,13 +6,13 @@ use validator::{Validate, ValidationError}; #[derive(Validate)] struct Test { - #[validate(custom = "validate_something")] + #[validate(custom = "crate::validate_something")] s: String, } #[derive(Validate)] struct TestPath { - #[validate(custom = "::validate_something")] + #[validate(custom = "crate::validate_something")] s: String, } diff --git a/validator_derive/tests/run-pass/regex.rs b/validator_derive/tests/run-pass/regex.rs index dcac3c0..e15b3a6 100644 --- a/validator_derive/tests/run-pass/regex.rs +++ b/validator_derive/tests/run-pass/regex.rs @@ -14,13 +14,13 @@ lazy_static! { #[derive(Validate)] struct Test { - #[validate(regex = "RE2")] + #[validate(regex = "crate::RE2")] s: String, } #[derive(Validate)] struct TestPath { - #[validate(regex = "::RE2")] + #[validate(regex = "crate::RE2")] s: String, } |
