forked from GitoxideLabs/gitoxide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.rs
More file actions
228 lines (204 loc) · 5.78 KB
/
parse.rs
File metadata and controls
228 lines (204 loc) · 5.78 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use std::time::SystemTime;
use gix_date::{time::Sign, Time};
#[test]
fn special_time_is_ok_for_now() {
assert_eq!(
gix_date::parse("1979-02-26 18:30:00", Some(SystemTime::now())).unwrap(),
Time {
seconds: 42,
offset: 1800,
sign: Sign::Plus,
}
);
}
#[test]
fn short() {
assert_eq!(
gix_date::parse("1979-02-26", Some(SystemTime::now())).unwrap(),
Time {
seconds: 288835200,
offset: 0,
sign: Sign::Plus,
},
"could not parse with SHORT format"
);
}
#[test]
fn rfc2822() {
assert_eq!(
gix_date::parse("Thu, 18 Aug 2022 12:45:06 +0800", None).unwrap(),
Time {
seconds: 1660797906,
offset: 28800,
sign: Sign::Plus,
},
);
}
#[test]
fn git_rfc2822() {
let expected = Time {
seconds: 1659329106,
offset: 28800,
sign: Sign::Plus,
};
assert_eq!(
gix_date::parse("Thu, 1 Aug 2022 12:45:06 +0800", None).unwrap(),
expected,
);
assert_eq!(
gix_date::parse("Thu, 1 Aug 2022 12:45:06 +0800", None).unwrap(),
expected,
);
}
#[test]
fn raw() {
assert_eq!(
gix_date::parse("1660874655 +0800", None).unwrap(),
Time {
seconds: 1660874655,
offset: 28800,
sign: Sign::Plus,
},
);
assert_eq!(
gix_date::parse("1112911993 +0100", None).unwrap(),
Time {
seconds: 1112911993,
offset: 3600,
sign: Sign::Plus,
},
);
let expected = Time {
seconds: 1660874655,
offset: -28800,
sign: Sign::Minus,
};
for date_str in [
"1660874655 -0800",
"1660874655 -0800 ",
" 1660874655 -0800",
" 1660874655 -0800 ",
" 1660874655 -0800 ",
"1660874655\t-0800",
] {
assert_eq!(gix_date::parse(date_str, None).unwrap(), expected);
}
}
#[test]
fn bad_raw() {
for bad_date_str in [
"123456 !0600",
"123456 +060",
"123456 -060",
"123456 +06000",
"123456 +10030",
"123456 06000",
"123456 0600",
"123456 +0600 extra",
"123456+0600",
"123456 + 600",
] {
assert!(gix_date::parse(bad_date_str, None).is_err());
}
}
#[test]
fn double_negation_in_offset() {
let actual = gix_date::parse("1288373970 --700", None).unwrap();
assert_eq!(
actual,
gix_date::Time {
seconds: 1288373970,
offset: 25200,
sign: Sign::Minus,
},
"double-negation stays negative, and is parseable."
);
assert_eq!(
actual.to_bstring(),
"1288373970 -0700",
"serialization corrects the issue"
);
}
#[test]
fn git_default() {
assert_eq!(
gix_date::parse("Thu Aug 8 12:45:06 2022 +0800", None).unwrap(),
Time {
seconds: 1659933906,
offset: 28800,
sign: Sign::Plus,
},
);
}
#[test]
fn invalid_dates_can_be_produced_without_current_time() {
assert!(matches!(
gix_date::parse("foobar", None).unwrap_err(),
gix_date::parse::Error::InvalidDateString { input } if input == "foobar"
));
}
mod relative {
use std::time::SystemTime;
use gix_date::time::Sign;
use jiff::{ToSpan, Zoned};
use pretty_assertions::assert_eq;
#[test]
fn large_offsets() {
gix_date::parse("999999999999999 weeks ago", Some(std::time::UNIX_EPOCH)).ok();
}
#[test]
fn large_offsets_do_not_panic() {
assert!(matches!(
gix_date::parse("9999999999 weeks ago", Some(std::time::UNIX_EPOCH)),
Err(gix_date::parse::Error::RelativeTimeConversion)
));
}
#[test]
fn offset_leading_to_before_unix_epoch_can_be_represented() {
let date = gix_date::parse("1 second ago", Some(std::time::UNIX_EPOCH)).unwrap();
assert_eq!(date.seconds, -1);
}
#[test]
fn various() {
let now = SystemTime::now();
let cases = [
("2 weeks ago", 2.weeks()),
("14 weeks ago", 14.weeks()),
("26 weeks ago", 26.weeks()),
("38 weeks ago", 38.weeks()),
("50 weeks ago", 50.weeks()),
("20160 minutes ago", 20_160.minutes()), // 2 weeks
("141120 minutes ago", 141_120.minutes()), // 14 weeks
("262080 minutes ago", 262_080.minutes()), // 26 weeks
("383040 minutes ago", 383_040.minutes()), // 38 weeks
("504000 minutes ago", 504_000.minutes()), // 50 weeks
];
let times = cases.map(|(input, _)| gix_date::parse(input, Some(now)).unwrap());
assert_eq!(times.map(|_| Sign::Plus), times.map(|time| time.sign));
assert_eq!(times.map(|_| 0), times.map(|time| time.offset));
let expected = cases.map(|(_, span)| {
Zoned::try_from(now)
.unwrap()
// account for the loss of precision when creating `Time` with seconds
.round(
jiff::ZonedRound::new()
.smallest(jiff::Unit::Second)
.mode(jiff::RoundMode::Trunc),
)
.unwrap()
.saturating_sub(span)
.timestamp()
});
let actual = times.map(|time| jiff::Timestamp::from_second(time.seconds).unwrap());
assert_eq!(actual, expected, "relative times differ");
}
}
/// Various cases the fuzzer found
mod fuzz {
#[test]
fn invalid_but_does_not_cause_panic() {
for input in ["-9999-1-1", "7 -𬞋", "5 ڜ-09", "-4 week ago Z", "8960609 day ago"] {
let _ = gix_date::parse(input, Some(std::time::UNIX_EPOCH)).unwrap_err();
}
}
}