|
| 1 | +use refined_type::{length_equal, length_greater_than, length_less_than, Refined}; |
| 2 | +use refined_type::rule::composer::{And, Or}; |
| 3 | + |
| 4 | +length_greater_than!(5); |
| 5 | +length_equal!(5, 10); |
| 6 | +length_less_than!(10); |
| 7 | + |
| 8 | +#[test] |
| 9 | +fn test_length() -> Result<(), refined_type::result::Error> { |
| 10 | + type Password = Refined< |
| 11 | + And< |
| 12 | + Or<LengthEqualRule5<String>, LengthGreaterThanRule5<String>>, |
| 13 | + Or<LengthLessThanRule10<String>, LengthEqualRule10<String>>, |
| 14 | + >, |
| 15 | + >; |
| 16 | + let raw_password = "password"; |
| 17 | + let password = Password::new(raw_password.to_string())?; |
| 18 | + assert_eq!(password.into_value(), "password"); |
| 19 | + Ok(()) |
| 20 | +} |
| 21 | + |
| 22 | +#[test] |
| 23 | +fn test_length_fail() { |
| 24 | + type Password = Refined< |
| 25 | + And< |
| 26 | + And<LengthEqualRule5<String>, LengthGreaterThanRule5<String>>, |
| 27 | + And<LengthLessThanRule10<String>, LengthEqualRule10<String>>, |
| 28 | + >, |
| 29 | + >; |
| 30 | + let raw_password = "password password"; |
| 31 | + let password = Password::new(raw_password.to_string()); |
| 32 | + assert!(password.is_err()); |
| 33 | +} |
| 34 | + |
| 35 | +#[test] |
| 36 | +fn test_length_greater_than_5() -> Result<(), refined_type::result::Error> { |
| 37 | + let target = "123456"; |
| 38 | + let refined = LengthGreaterThan5::new(target)?; |
| 39 | + assert_eq!(refined.into_value(), "123456"); |
| 40 | + Ok(()) |
| 41 | +} |
| 42 | + |
| 43 | +#[test] |
| 44 | +fn test_length_greater_than_5_fail() { |
| 45 | + let target = "1234"; |
| 46 | + let refined = LengthGreaterThan5::new(target); |
| 47 | + assert!(refined.is_err()); |
| 48 | +} |
| 49 | + |
| 50 | +#[test] |
| 51 | +fn test_length_equal_5() -> Result<(), refined_type::result::Error> { |
| 52 | + let target = "12345"; |
| 53 | + let refined = LengthEqual5::new(target)?; |
| 54 | + assert_eq!(refined.into_value(), "12345"); |
| 55 | + Ok(()) |
| 56 | +} |
| 57 | + |
| 58 | +#[test] |
| 59 | +fn test_length_equal_5_fail() { |
| 60 | + let target = "1234"; |
| 61 | + let refined = LengthEqual5::new(target); |
| 62 | + assert!(refined.is_err()); |
| 63 | +} |
| 64 | + |
| 65 | +#[test] |
| 66 | +fn test_length_equal_10() -> Result<(), refined_type::result::Error> { |
| 67 | + let target = "1234567890"; |
| 68 | + let refined = LengthEqual10::new(target)?; |
| 69 | + assert_eq!(refined.into_value(), "1234567890"); |
| 70 | + Ok(()) |
| 71 | +} |
| 72 | + |
| 73 | +#[test] |
| 74 | +fn test_length_equal_10_fail() { |
| 75 | + let target = "123456789"; |
| 76 | + let refined = LengthEqual10::new(target); |
| 77 | + assert!(refined.is_err()); |
| 78 | +} |
| 79 | + |
| 80 | +#[test] |
| 81 | +fn test_length_less_than_10() -> Result<(), refined_type::result::Error> { |
| 82 | + let target = "123456789"; |
| 83 | + let refined = LengthLessThan10::new(target)?; |
| 84 | + assert_eq!(refined.into_value(), "123456789"); |
| 85 | + Ok(()) |
| 86 | +} |
| 87 | + |
| 88 | +#[test] |
| 89 | +fn test_length_less_than_10_fail() { |
| 90 | + let target = "1234567890"; |
| 91 | + let refined = LengthLessThan10::new(target); |
| 92 | + assert!(refined.is_err()); |
| 93 | +} |
0 commit comments