| 1 |
use std::fmt::Debug; |
| 2 |
use std::num::NonZero; |
| 3 |
|
| 4 |
use rstest::rstest; |
| 5 |
use time::format_description::modifier::{Hour12, WeekNumberIso}; |
| 6 |
use time::format_description::{BorrowedFormatItem, Component}; |
| 7 |
use time::parsing::Parsed; |
| 8 |
use time::{Month, Time, Weekday, error}; |
| 9 |
|
| 10 |
#[rstest] |
| 11 |
#[case(Parsed::set_year, Parsed::year, 5)] |
| 12 |
#[case(Parsed::set_year_last_two, Parsed::year_last_two, 5)] |
| 13 |
#[case(Parsed::set_iso_year, Parsed::iso_year, 5)] |
| 14 |
#[case(Parsed::set_iso_year_last_two, Parsed::iso_year_last_two, 5)] |
| 15 |
#[case(Parsed::set_month, Parsed::month, Month::May)] |
| 16 |
#[case(Parsed::set_sunday_week_number, Parsed::sunday_week_number, 5)] |
| 17 |
#[case(Parsed::set_monday_week_number, Parsed::monday_week_number, 5)] |
| 18 |
#[case(Parsed::set_iso_week_number, Parsed::iso_week_number, const { NonZero::new(5).unwrap() })] |
| 19 |
#[case(Parsed::set_weekday, Parsed::weekday, Weekday::Monday)] |
| 20 |
#[case(Parsed::set_ordinal, Parsed::ordinal, const { NonZero::new(5).unwrap() })] |
| 21 |
#[case(Parsed::set_day, Parsed::day, const { NonZero::new(5).unwrap() })] |
| 22 |
#[case(Parsed::set_hour_24, Parsed::hour_24, 5)] |
| 23 |
#[case(Parsed::set_hour_12, Parsed::hour_12, const { NonZero::new(5).unwrap() })] |
| 24 |
#[case(Parsed::set_hour_12_is_pm, Parsed::hour_12_is_pm, true)] |
| 25 |
#[case(Parsed::set_minute, Parsed::minute, 5)] |
| 26 |
#[case(Parsed::set_second, Parsed::second, 5)] |
| 27 |
#[case(Parsed::set_subsecond, Parsed::subsecond, 5)] |
| 28 |
#[case(Parsed::set_offset_hour, Parsed::offset_hour, 5)] |
| 29 |
#[expect(deprecated)] |
| 30 |
#[case(Parsed::set_offset_minute, Parsed::offset_minute, 5)] |
| 31 |
#[case(Parsed::set_offset_minute_signed, Parsed::offset_minute_signed, -5)] |
| 32 |
#[expect(deprecated)] |
| 33 |
#[case(Parsed::set_offset_second, Parsed::offset_second, 5)] |
| 34 |
#[case(Parsed::set_offset_second_signed, Parsed::offset_second_signed, -5)] |
| 35 |
fn getters_setters<T>( |
| 36 |
#[case] setter: fn(&mut Parsed, T) -> Option<()>, |
| 37 |
#[case] getter: fn(&Parsed) -> Option<T>, |
| 38 |
#[case] value: T, |
| 39 |
) where |
| 40 |
T: PartialEq + Copy + Debug, |
| 41 |
{ |
| 42 |
let mut parsed = Parsed::new(); |
| 43 |
assert!(setter(&mut parsed, value).is_some()); |
| 44 |
assert_eq!(getter(&parsed), Some(value)); |
| 45 |
} |
| 46 |
|
| 47 |
#[rstest] |
| 48 |
#[expect(deprecated)] |
| 49 |
#[case(Parsed::set_offset_minute, Parsed::offset_minute, 200)] |
| 50 |
#[expect(deprecated)] |
| 51 |
#[case(Parsed::set_offset_second, Parsed::offset_second, 200)] |
| 52 |
fn getters_setters_fail<T>( |
| 53 |
#[case] setter: fn(&mut Parsed, T) -> Option<()>, |
| 54 |
#[case] getter: fn(&Parsed) -> Option<T>, |
| 55 |
#[case] value: T, |
| 56 |
) { |
| 57 |
let mut parsed = Parsed::new(); |
| 58 |
assert!(setter(&mut parsed, value).is_none()); |
| 59 |
assert!(getter(&parsed).is_none()); |
| 60 |
} |
| 61 |
|
| 62 |
#[rstest] |
| 63 |
#[case(Parsed::with_year, Parsed::year, 5)] |
| 64 |
#[case(Parsed::with_year_last_two, Parsed::year_last_two, 5)] |
| 65 |
#[case(Parsed::with_iso_year, Parsed::iso_year, 5)] |
| 66 |
#[case(Parsed::with_iso_year_last_two, Parsed::iso_year_last_two, 5)] |
| 67 |
#[case(Parsed::with_month, Parsed::month, Month::May)] |
| 68 |
#[case(Parsed::with_sunday_week_number, Parsed::sunday_week_number, 5)] |
| 69 |
#[case(Parsed::with_monday_week_number, Parsed::monday_week_number, 5)] |
| 70 |
#[case(Parsed::with_iso_week_number, Parsed::iso_week_number, const { NonZero::new(5).unwrap() })] |
| 71 |
#[case(Parsed::with_weekday, Parsed::weekday, Weekday::Monday)] |
| 72 |
#[case(Parsed::with_ordinal, Parsed::ordinal, const { NonZero::new(5).unwrap() })] |
| 73 |
#[case(Parsed::with_day, Parsed::day, const { NonZero::new(5).unwrap() })] |
| 74 |
#[case(Parsed::with_hour_24, Parsed::hour_24, 5)] |
| 75 |
#[case(Parsed::with_hour_12, Parsed::hour_12, const { NonZero::new(5).unwrap() })] |
| 76 |
#[case(Parsed::with_hour_12_is_pm, Parsed::hour_12_is_pm, true)] |
| 77 |
#[case(Parsed::with_minute, Parsed::minute, 5)] |
| 78 |
#[case(Parsed::with_second, Parsed::second, 5)] |
| 79 |
#[case(Parsed::with_subsecond, Parsed::subsecond, 5)] |
| 80 |
#[case(Parsed::with_offset_hour, Parsed::offset_hour, 5)] |
| 81 |
#[expect(deprecated)] |
| 82 |
#[case(Parsed::with_offset_minute, Parsed::offset_minute, 5)] |
| 83 |
#[case(Parsed::with_offset_minute_signed, Parsed::offset_minute_signed, -5)] |
| 84 |
#[expect(deprecated)] |
| 85 |
#[case(Parsed::with_offset_second, Parsed::offset_second, 5)] |
| 86 |
#[case(Parsed::with_offset_second_signed, Parsed::offset_second_signed, -5)] |
| 87 |
fn builder_methods<T>( |
| 88 |
#[case] builder: fn(Parsed, T) -> Option<Parsed>, |
| 89 |
#[case] getter: fn(&Parsed) -> Option<T>, |
| 90 |
#[case] value: T, |
| 91 |
) where |
| 92 |
T: PartialEq + Copy + Debug, |
| 93 |
{ |
| 94 |
let parsed = builder(Parsed::new(), value).expect("valid value"); |
| 95 |
assert_eq!(getter(&parsed), Some(value)); |
| 96 |
} |
| 97 |
|
| 98 |
#[rstest] |
| 99 |
#[expect(deprecated)] |
| 100 |
#[case(Parsed::with_offset_minute, Parsed::offset_minute, 200)] |
| 101 |
#[expect(deprecated)] |
| 102 |
#[case(Parsed::with_offset_second, Parsed::offset_second, 200)] |
| 103 |
fn builder_methods_fail<T>( |
| 104 |
#[case] builder: fn(Parsed, T) -> Option<Parsed>, |
| 105 |
#[case] getter: fn(&Parsed) -> Option<T>, |
| 106 |
#[case] value: T, |
| 107 |
) { |
| 108 |
assert!(builder(Parsed::new(), value).is_none()); |
| 109 |
assert!(getter(&Parsed::new()).is_none()); |
| 110 |
} |
| 111 |
|
| 112 |
#[rstest] |
| 113 |
#[case("a", BorrowedFormatItem::StringLiteral("a"))] |
| 114 |
#[case("b", BorrowedFormatItem::StringLiteral("a"))] |
| 115 |
fn single_item_parse(#[case] input: &str, #[case] format_item: BorrowedFormatItem) { |
| 116 |
assert!(Time::parse(input, &format_item).is_err()); |
| 117 |
} |
| 118 |
|
| 119 |
#[rstest] |
| 120 |
#[case("day", Component::Day(<_>::default()), b"")] |
| 121 |
#[case("month", Component::MonthNumerical(<_>::default()), b"")] |
| 122 |
#[case("ordinal", Component::Ordinal(<_>::default()), b"")] |
| 123 |
#[case("weekday", Component::WeekdayLong(<_>::default()), b"")] |
| 124 |
#[case("week number", Component::WeekNumberIso(<_>::default()), b"")] |
| 125 |
#[case("year", Component::CalendarYearFullExtendedRange(<_>::default()), b"")] |
| 126 |
#[case("minute", Component::Minute(<_>::default()), b"")] |
| 127 |
#[case("period", Component::Period(<_>::default()), b"")] |
| 128 |
#[case("second", Component::Second(<_>::default()), b"")] |
| 129 |
#[case("subsecond", Component::Subsecond(<_>::default()), b"")] |
| 130 |
#[case("offset hour", Component::OffsetHour(<_>::default()), b"")] |
| 131 |
#[case("offset minute", Component::OffsetMinute(<_>::default()), b"")] |
| 132 |
#[case("offset second", Component::OffsetSecond(<_>::default()), b"")] |
| 133 |
#[case("unix_timestamp", Component::UnixTimestampSecond(<_>::default()), b"")] |
| 134 |
#[case( |
| 135 |
"week number", |
| 136 |
Component::WeekNumberIso(WeekNumberIso::default()), |
| 137 |
b"00" |
| 138 |
)] |
| 139 |
#[case("hour", Component::Hour12(Hour12::default()), b"00")] |
| 140 |
fn component_err( |
| 141 |
#[case] component_name: &'static str, |
| 142 |
#[case] component: Component, |
| 143 |
#[case] input: &[u8], |
| 144 |
) { |
| 145 |
let mut parsed = Parsed::new(); |
| 146 |
assert_eq!( |
| 147 |
parsed.parse_component(input, component), |
| 148 |
Err(error::ParseFromDescription::InvalidComponent( |
| 149 |
component_name |
| 150 |
)) |
| 151 |
); |
| 152 |
} |