validator/validation/
must_match.rs

1/// Validates that the 2 given fields match.
2/// Both fields are optionals
3#[must_use]
4pub fn validate_must_match<T: Eq>(a: T, b: T) -> bool {
5    a == b
6}
7
8#[cfg(test)]
9mod tests {
10    use std::borrow::Cow;
11
12    use super::validate_must_match;
13
14    #[test]
15    fn test_validate_must_match_strings_valid() {
16        assert!(validate_must_match("hey".to_string(), "hey".to_string()))
17    }
18
19    #[test]
20    fn test_validate_must_match_cows_valid() {
21        let left: Cow<'static, str> = "hey".into();
22        let right: Cow<'static, str> = String::from("hey").into();
23        assert!(validate_must_match(left, right))
24    }
25
26    #[test]
27    fn test_validate_must_match_numbers() {
28        assert!(validate_must_match(2, 2))
29    }
30
31    #[test]
32    fn test_validate_must_match_numbers_false() {
33        assert!(!validate_must_match(2, 3));
34    }
35
36    #[test]
37    fn test_validate_must_match_numbers_option_false() {
38        assert!(!validate_must_match(Some(2), Some(3)));
39    }
40
41    #[test]
42    fn test_validate_must_match_numbers_option_true() {
43        assert!(validate_must_match(Some(6), Some(6)));
44    }
45
46    #[test]
47    fn test_validate_must_match_none_some_false() {
48        assert!(!validate_must_match(None, Some(3)));
49    }
50
51    #[test]
52    fn test_validate_must_match_some_none_false() {
53        assert!(!validate_must_match(Some(3), None));
54    }
55
56    #[test]
57    fn test_validate_must_match_none_none_true() {
58        // We need to define one of the values here as rust
59        // can not infer the generic type from None and None
60        let a: Option<u64> = None;
61        assert!(validate_must_match(a, None));
62    }
63}