Skip to content

Commit 56578ff

Browse files
committed
uefi: time: add unit test helpers for external crate integrations
1 parent 7be3135 commit 56578ff

1 file changed

Lines changed: 104 additions & 0 deletions

File tree

uefi/src/runtime/time_defs/integration_common.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,107 @@ impl Error for ConversionErrorInner {
7474
}
7575
}
7676
}
77+
78+
#[cfg(test)]
79+
#[allow(unused)]
80+
pub(super) mod test_helpers {
81+
use super::*;
82+
use crate::runtime::TimeParams;
83+
use uefi::runtime::Time;
84+
use uefi_raw::time::Daylight;
85+
86+
pub fn sample_time(tz: i16) -> Time {
87+
let params = TimeParams {
88+
year: 2024,
89+
month: 3,
90+
day: 14,
91+
hour: 12,
92+
minute: 34,
93+
second: 56,
94+
nanosecond: 123_456_789,
95+
time_zone: Some(tz),
96+
daylight: Daylight::default(),
97+
};
98+
Time::new(params).unwrap()
99+
}
100+
101+
fn invalid_date() -> Time {
102+
let mut t = sample_time(0);
103+
t.0.month = 2;
104+
t.0.day = 31;
105+
t
106+
}
107+
108+
pub fn primitive_roundtrip<T>()
109+
where
110+
T: TryFrom<Time, Error = ConversionError> + TryInto<Time, Error = ConversionError>,
111+
{
112+
let t = sample_time(Time::UNSPECIFIED_TIMEZONE);
113+
114+
let dt: T = t.try_into().unwrap();
115+
let back: Time = dt.try_into().unwrap();
116+
117+
assert_eq!(back.0.year, t.0.year);
118+
assert_eq!(back.0.month, t.0.month);
119+
assert_eq!(back.0.day, t.0.day);
120+
assert_eq!(back.0.hour, t.0.hour);
121+
assert_eq!(back.0.minute, t.0.minute);
122+
assert_eq!(back.0.second, t.0.second);
123+
assert_eq!(back.0.nanosecond, t.0.nanosecond);
124+
}
125+
126+
pub fn zoned_roundtrip<T>()
127+
where
128+
T: TryFrom<Time, Error = ConversionError> + TryInto<Time, Error = ConversionError>,
129+
{
130+
let t = sample_time(120);
131+
132+
let dt: T = t.try_into().unwrap();
133+
let back: Time = dt.try_into().unwrap();
134+
135+
assert_eq!(back.0.time_zone, 120);
136+
assert_eq!(back.0.hour, t.0.hour);
137+
assert_eq!(back.0.minute, t.0.minute);
138+
}
139+
140+
pub fn negative_offset_roundtrip<T>()
141+
where
142+
T: TryFrom<Time, Error = ConversionError> + TryInto<Time, Error = ConversionError>,
143+
{
144+
let t = sample_time(-330);
145+
146+
let dt: T = t.try_into().unwrap();
147+
let back: Time = dt.try_into().unwrap();
148+
149+
assert_eq!(back.0.time_zone, -330);
150+
}
151+
152+
pub fn preserves_nanoseconds<T>()
153+
where
154+
T: TryFrom<Time, Error = ConversionError> + TryInto<Time, Error = ConversionError>,
155+
{
156+
let t = sample_time(60);
157+
158+
let dt: T = t.try_into().unwrap();
159+
let back: Time = dt.try_into().unwrap();
160+
161+
assert_eq!(back.0.nanosecond, 123_456_789);
162+
}
163+
164+
pub fn unspecified_timezone_fails<T>()
165+
where
166+
T: TryFrom<Time, Error = ConversionError>,
167+
{
168+
let t = sample_time(Time::UNSPECIFIED_TIMEZONE);
169+
let result: Result<T, _> = t.try_into();
170+
assert!(result.is_err());
171+
}
172+
173+
pub fn invalid_calendar_date_fails<T>()
174+
where
175+
T: TryFrom<Time, Error = ConversionError>,
176+
{
177+
let result: Result<T, _> = invalid_date().try_into();
178+
assert!(result.is_err());
179+
}
180+
}

0 commit comments

Comments
 (0)