execution_time/traits/
singular_plural.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/// Unit types with their singular/plural pairs
#[derive(Debug, Clone, Copy)]
pub enum Unit {
    Second,
    Minute,
    Hour,
    Day,
    Foot,  // for testing purposes only
    Meter, // for testing purposes only
    Ox,    // for testing purposes only
}

/// Trait for defining singular and plural forms
pub trait SingularPlural {
    /// Get singular form
    fn singular(&self) -> &str;

    /// Get plural form
    fn plural(&self) -> &str;
}

impl SingularPlural for Unit {
    fn singular(&self) -> &str {
        match self {
            Unit::Second => "second",
            Unit::Minute => "minute",
            Unit::Hour => "hour",
            Unit::Day => "day",
            Unit::Foot => "foot",
            Unit::Meter => "meter",
            Unit::Ox => "ox",
        }
    }

    fn plural(&self) -> &str {
        match self {
            Unit::Second => "seconds",
            Unit::Minute => "minutes",
            Unit::Hour => "hours",
            Unit::Day => "days",
            Unit::Foot => "feet",
            Unit::Meter => "meters",
            Unit::Ox => "oxen",
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_singular_forms() {
        // Test that singular method returns the correct singular form for all units
        assert_eq!(Unit::Second.singular(), "second");
        assert_eq!(Unit::Minute.singular(), "minute");
        assert_eq!(Unit::Hour.singular(), "hour");
        assert_eq!(Unit::Day.singular(), "day");
        assert_eq!(Unit::Foot.singular(), "foot");
        assert_eq!(Unit::Meter.singular(), "meter");
        assert_eq!(Unit::Ox.singular(), "ox");
    }

    #[test]
    fn test_plural_forms() {
        // Test that plural method returns the correct plural form for all units
        assert_eq!(Unit::Second.plural(), "seconds");
        assert_eq!(Unit::Minute.plural(), "minutes");
        assert_eq!(Unit::Hour.plural(), "hours");
        assert_eq!(Unit::Day.plural(), "days");
        assert_eq!(Unit::Foot.plural(), "feet");
        assert_eq!(Unit::Meter.plural(), "meters");
        assert_eq!(Unit::Ox.plural(), "oxen");
    }

    #[test]
    fn test_singular_plural_trait() {
        // Test that a type implementing SingularPlural returns singular and plural
        let unit = Unit::Hour;
        assert_eq!(unit.singular(), "hour");
        assert_eq!(unit.plural(), "hours");

        let unit = Unit::Foot;
        assert_eq!(unit.singular(), "foot");
        assert_eq!(unit.plural(), "feet");
    }

    #[test]
    fn test_unit_copy_trait() {
        // Test that a Unit can be copied.
        let unit1 = Unit::Minute;
        let unit2 = unit1; // Copy unit1 to unit2

        assert_eq!(unit1.singular(), unit2.singular());
        assert_eq!(unit1.plural(), unit2.plural());
    }
}