|
| 1 | +use arbitrary::Arbitrary; |
| 2 | +use arbtest::arbtest; |
| 3 | +use nutype::nutype; |
| 4 | + |
| 5 | +fn main() { |
| 6 | + should_generate_arbitrary_string_without_validation_with_respect_to_sanitizers(); |
| 7 | + should_generate_arbitrary_string_with_trim_and_min_len_validation(); |
| 8 | + should_respect_not_empty_validation_with_trim(); |
| 9 | + should_respect_not_empty_validation_without_trim(); |
| 10 | + should_respect_len_char_max(); |
| 11 | + should_respec_both_len_boundaries(); |
| 12 | +} |
| 13 | + |
| 14 | +fn should_generate_arbitrary_string_without_validation_with_respect_to_sanitizers() { |
| 15 | + #[nutype(sanitize(lowercase), derive(Arbitrary, Debug))] |
| 16 | + struct LowercaseString(String); |
| 17 | + |
| 18 | + arbtest(|u| { |
| 19 | + let s = LowercaseString::arbitrary(u)?.into_inner(); |
| 20 | + assert_eq!(s.to_lowercase(), s); |
| 21 | + Ok(()) |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +fn should_generate_arbitrary_string_with_trim_and_min_len_validation() { |
| 26 | + #[nutype(sanitize(trim), validate(len_char_min = 3), derive(Arbitrary, Debug))] |
| 27 | + struct Name(String); |
| 28 | + |
| 29 | + arbtest(|u| { |
| 30 | + let s = Name::arbitrary(u)?.into_inner(); |
| 31 | + assert_eq!(s.trim(), s); |
| 32 | + assert!(s.chars().count() >= 3); |
| 33 | + Ok(()) |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +fn should_respect_not_empty_validation_with_trim() { |
| 38 | + #[nutype(sanitize(trim), validate(not_empty), derive(Arbitrary, Debug))] |
| 39 | + struct Title(String); |
| 40 | + |
| 41 | + arbtest(|u| { |
| 42 | + let s = Title::arbitrary(u)?.into_inner(); |
| 43 | + assert_eq!(s.trim(), s); |
| 44 | + assert!(!s.is_empty()); |
| 45 | + Ok(()) |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +fn should_respect_not_empty_validation_without_trim() { |
| 50 | + #[nutype(validate(not_empty), derive(Arbitrary, Debug))] |
| 51 | + struct Description(String); |
| 52 | + |
| 53 | + arbtest(|u| { |
| 54 | + let s = Description::arbitrary(u)?.into_inner(); |
| 55 | + assert!(!s.is_empty()); |
| 56 | + Ok(()) |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +fn should_respect_len_char_max() { |
| 61 | + #[nutype(validate(len_char_max = 7), derive(Arbitrary, Debug))] |
| 62 | + struct Text(String); |
| 63 | + |
| 64 | + arbtest(|u| { |
| 65 | + let s = Text::arbitrary(u)?.into_inner(); |
| 66 | + assert!(s.chars().count() <= 7); |
| 67 | + Ok(()) |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +fn should_respec_both_len_boundaries() { |
| 72 | + #[nutype(validate(len_char_min = 3, len_char_max = 5), derive(Arbitrary, Debug))] |
| 73 | + struct Text(String); |
| 74 | + |
| 75 | + arbtest(|u| { |
| 76 | + let s = Text::arbitrary(u)?.into_inner(); |
| 77 | + assert!(s.chars().count() >= 3); |
| 78 | + assert!(s.chars().count() <= 5); |
| 79 | + Ok(()) |
| 80 | + }); |
| 81 | +} |
0 commit comments