-
-
Notifications
You must be signed in to change notification settings - Fork 283
/
month.rs
178 lines (169 loc) · 4.54 KB
/
month.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use rstest::rstest;
use time::Month::{self, *};
#[rstest]
#[case(January, December)]
#[case(February, January)]
#[case(March, February)]
#[case(April, March)]
#[case(May, April)]
#[case(June, May)]
#[case(July, June)]
#[case(August, July)]
#[case(September, August)]
#[case(October, September)]
#[case(November, October)]
#[case(December, November)]
fn previous(#[case] month: Month, #[case] expected: Month) {
assert_eq!(month.previous(), expected);
}
#[rstest]
#[case(January, February)]
#[case(February, March)]
#[case(March, April)]
#[case(April, May)]
#[case(May, June)]
#[case(June, July)]
#[case(July, August)]
#[case(August, September)]
#[case(September, October)]
#[case(October, November)]
#[case(November, December)]
#[case(December, January)]
fn next(#[case] month: Month, #[case] expected: Month) {
assert_eq!(month.next(), expected);
}
#[rstest]
#[case(January, 0, January)]
#[case(January, 1, February)]
#[case(January, 2, March)]
#[case(January, 3, April)]
#[case(January, 4, May)]
#[case(January, 5, June)]
#[case(January, 6, July)]
#[case(January, 7, August)]
#[case(January, 8, September)]
#[case(January, 9, October)]
#[case(January, 10, November)]
#[case(January, 11, December)]
#[case(December, 0, December)]
#[case(December, 1, January)]
#[case(December, 2, February)]
#[case(December, 3, March)]
#[case(December, 4, April)]
#[case(December, 5, May)]
#[case(December, 6, June)]
#[case(December, 7, July)]
#[case(December, 8, August)]
#[case(December, 9, September)]
#[case(December, 10, October)]
#[case(December, 11, November)]
#[case(January, 12, January)]
#[case(January, u8::MAX, April)]
#[case(December, 12, December)]
#[case(December, u8::MAX, March)]
fn nth_next(#[case] month: Month, #[case] n: u8, #[case] expected: Month) {
assert_eq!(month.nth_next(n), expected);
}
#[rstest]
#[case(January, 0, January)]
#[case(January, 1, December)]
#[case(January, 2, November)]
#[case(January, 3, October)]
#[case(January, 4, September)]
#[case(January, 5, August)]
#[case(January, 6, July)]
#[case(January, 7, June)]
#[case(January, 8, May)]
#[case(January, 9, April)]
#[case(January, 10, March)]
#[case(January, 11, February)]
#[case(December, 0, December)]
#[case(December, 1, November)]
#[case(December, 2, October)]
#[case(December, 3, September)]
#[case(December, 4, August)]
#[case(December, 5, July)]
#[case(December, 6, June)]
#[case(December, 7, May)]
#[case(December, 8, April)]
#[case(December, 9, March)]
#[case(December, 10, February)]
#[case(December, 11, January)]
#[case(January, 12, January)]
#[case(January, u8::MAX, October)]
#[case(December, 12, December)]
#[case(December, u8::MAX, September)]
fn nth_prev(#[case] month: Month, #[case] n: u8, #[case] expected: Month) {
assert_eq!(month.nth_prev(n), expected);
}
#[rstest]
#[case(January, "January")]
#[case(February, "February")]
#[case(March, "March")]
#[case(April, "April")]
#[case(May, "May")]
#[case(June, "June")]
#[case(July, "July")]
#[case(August, "August")]
#[case(September, "September")]
#[case(October, "October")]
#[case(November, "November")]
#[case(December, "December")]
fn display(#[case] month: Month, #[case] expected: &str) {
assert_eq!(month.to_string(), expected);
}
#[rstest]
#[case("January", Ok(January))]
#[case("February", Ok(February))]
#[case("March", Ok(March))]
#[case("April", Ok(April))]
#[case("May", Ok(May))]
#[case("June", Ok(June))]
#[case("July", Ok(July))]
#[case("August", Ok(August))]
#[case("September", Ok(September))]
#[case("October", Ok(October))]
#[case("November", Ok(November))]
#[case("December", Ok(December))]
#[case("foo", Err(time::error::InvalidVariant))]
fn from_str(#[case] s: &str, #[case] expected: Result<Month, time::error::InvalidVariant>) {
assert_eq!(s.parse::<Month>(), expected);
}
#[rstest]
#[case(January, 1)]
#[case(February, 2)]
#[case(March, 3)]
#[case(April, 4)]
#[case(May, 5)]
#[case(June, 6)]
#[case(July, 7)]
#[case(August, 8)]
#[case(September, 9)]
#[case(October, 10)]
#[case(November, 11)]
#[case(December, 12)]
fn to_u8(#[case] month: Month, #[case] expected: u8) {
assert_eq!(u8::from(month), expected);
}
#[rstest]
#[case(1, January)]
#[case(2, February)]
#[case(3, March)]
#[case(4, April)]
#[case(5, May)]
#[case(6, June)]
#[case(7, July)]
#[case(8, August)]
#[case(9, September)]
#[case(10, October)]
#[case(11, November)]
#[case(12, December)]
fn try_from_u8_success(#[case] input: u8, #[case] expected: Month) {
assert_eq!(Month::try_from(input), Ok(expected));
}
#[rstest]
#[case(0)]
#[case(13)]
fn try_from_u8_error(#[case] input: u8) {
assert!(matches!(Month::try_from(input), Err(err) if err.name() == "month"));
}