| 1 |
use std::num::NonZero; |
| 2 |
|
| 3 |
use time::format_description::modifier::{Hour, WeekNumber, WeekNumberRepr}; |
| 4 |
use time::format_description::{BorrowedFormatItem, Component}; |
| 5 |
use time::parsing::Parsed; |
| 6 |
use time::{Month, Time, Weekday, error}; |
| 7 |
|
| 8 |
#[test] |
| 9 |
fn getters_setters() { |
| 10 |
macro_rules! getters_setters { |
| 11 |
($( $(#[$attr:meta])* $setter:ident $getter:ident $value:expr;)*) => {$( |
| 12 |
$(#[$attr])* |
| 13 |
{ |
| 14 |
let mut parsed = Parsed::new(); |
| 15 |
parsed.$setter($value); |
| 16 |
assert_eq!(parsed.$getter(), Some($value)); |
| 17 |
} |
| 18 |
)*}; |
| 19 |
} |
| 20 |
|
| 21 |
getters_setters! { |
| 22 |
set_year year 5; |
| 23 |
set_year_last_two year_last_two 5; |
| 24 |
set_iso_year iso_year 5; |
| 25 |
set_iso_year_last_two iso_year_last_two 5; |
| 26 |
set_month month Month::May; |
| 27 |
set_sunday_week_number sunday_week_number 5; |
| 28 |
set_monday_week_number monday_week_number 5; |
| 29 |
set_iso_week_number iso_week_number NonZero::new(5).expect("valid value"); |
| 30 |
set_weekday weekday Weekday::Monday; |
| 31 |
set_ordinal ordinal NonZero::new(5).expect("valid value"); |
| 32 |
set_day day NonZero::new(5).expect("valid value"); |
| 33 |
set_hour_24 hour_24 5; |
| 34 |
set_hour_12 hour_12 NonZero::new(5).expect("valid value"); |
| 35 |
set_hour_12_is_pm hour_12_is_pm true; |
| 36 |
set_minute minute 5; |
| 37 |
set_second second 5; |
| 38 |
set_subsecond subsecond 5; |
| 39 |
set_offset_hour offset_hour 5; |
| 40 |
#[expect(deprecated)] set_offset_minute offset_minute 5; |
| 41 |
set_offset_minute_signed offset_minute_signed -5; |
| 42 |
#[expect(deprecated)] set_offset_second offset_second 5; |
| 43 |
set_offset_second_signed offset_second_signed -5; |
| 44 |
} |
| 45 |
|
| 46 |
#[expect(deprecated)] |
| 47 |
{ |
| 48 |
let mut parsed = Parsed::new(); |
| 49 |
parsed.set_offset_minute(200); |
| 50 |
assert_eq!(parsed.offset_minute(), None); |
| 51 |
parsed.set_offset_second(200); |
| 52 |
assert_eq!(parsed.offset_second(), None); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
#[test] |
| 57 |
fn builder_methods() { |
| 58 |
#[expect(deprecated)] |
| 59 |
let parsed = Parsed::new() |
| 60 |
.with_year(5) |
| 61 |
.and_then(|parsed| parsed.with_year_last_two(5)) |
| 62 |
.and_then(|parsed| parsed.with_iso_year(5)) |
| 63 |
.and_then(|parsed| parsed.with_iso_year_last_two(5)) |
| 64 |
.and_then(|parsed| parsed.with_month(Month::May)) |
| 65 |
.and_then(|parsed| parsed.with_sunday_week_number(5)) |
| 66 |
.and_then(|parsed| parsed.with_monday_week_number(5)) |
| 67 |
.and_then(|parsed| parsed.with_iso_week_number(NonZero::new(5).expect("valid value"))) |
| 68 |
.and_then(|parsed| parsed.with_weekday(Weekday::Monday)) |
| 69 |
.and_then(|parsed| parsed.with_ordinal(NonZero::new(5).expect("valid value"))) |
| 70 |
.and_then(|parsed| parsed.with_day(NonZero::new(5).expect("valid value"))) |
| 71 |
.and_then(|parsed| parsed.with_hour_24(5)) |
| 72 |
.and_then(|parsed| parsed.with_hour_12(NonZero::new(5).expect("valid value"))) |
| 73 |
.and_then(|parsed| parsed.with_hour_12_is_pm(true)) |
| 74 |
.and_then(|parsed| parsed.with_minute(5)) |
| 75 |
.and_then(|parsed| parsed.with_second(5)) |
| 76 |
.and_then(|parsed| parsed.with_subsecond(5)) |
| 77 |
.and_then(|parsed| parsed.with_offset_hour(5)) |
| 78 |
.and_then(|parsed| parsed.with_offset_minute(5)) |
| 79 |
.and_then(|parsed| parsed.with_offset_second(5)) |
| 80 |
.expect("all values are valid"); |
| 81 |
|
| 82 |
assert_eq!(parsed.year(), Some(5)); |
| 83 |
assert_eq!(parsed.year_last_two(), Some(5)); |
| 84 |
assert_eq!(parsed.iso_year(), Some(5)); |
| 85 |
assert_eq!(parsed.iso_year_last_two(), Some(5)); |
| 86 |
assert_eq!(parsed.month(), Some(Month::May)); |
| 87 |
assert_eq!(parsed.sunday_week_number(), Some(5)); |
| 88 |
assert_eq!(parsed.monday_week_number(), Some(5)); |
| 89 |
assert_eq!( |
| 90 |
parsed.iso_week_number(), |
| 91 |
Some(NonZero::new(5).expect("valid value")) |
| 92 |
); |
| 93 |
assert_eq!(parsed.weekday(), Some(Weekday::Monday)); |
| 94 |
assert_eq!( |
| 95 |
parsed.ordinal(), |
| 96 |
Some(NonZero::new(5).expect("valid value")) |
| 97 |
); |
| 98 |
assert_eq!(parsed.day(), Some(NonZero::new(5).expect("valid value"))); |
| 99 |
assert_eq!(parsed.hour_24(), Some(5)); |
| 100 |
assert_eq!( |
| 101 |
parsed.hour_12(), |
| 102 |
Some(NonZero::new(5).expect("valid value")) |
| 103 |
); |
| 104 |
assert_eq!(parsed.hour_12_is_pm(), Some(true)); |
| 105 |
assert_eq!(parsed.minute(), Some(5)); |
| 106 |
assert_eq!(parsed.second(), Some(5)); |
| 107 |
assert_eq!(parsed.subsecond(), Some(5)); |
| 108 |
assert_eq!(parsed.offset_hour(), Some(5)); |
| 109 |
#[expect(deprecated)] |
| 110 |
{ |
| 111 |
assert_eq!(parsed.offset_minute(), Some(5)); |
| 112 |
assert_eq!(parsed.offset_second(), Some(5)); |
| 113 |
} |
| 114 |
|
| 115 |
let parsed = Parsed::new() |
| 116 |
.with_offset_minute_signed(-5) |
| 117 |
.and_then(|parsed| parsed.with_offset_second_signed(-5)) |
| 118 |
.expect("all values are valid"); |
| 119 |
|
| 120 |
assert_eq!(parsed.offset_minute_signed(), Some(-5)); |
| 121 |
assert_eq!(parsed.offset_second_signed(), Some(-5)); |
| 122 |
|
| 123 |
#[expect(deprecated)] |
| 124 |
{ |
| 125 |
assert!(Parsed::new().with_offset_minute(200).is_none()); |
| 126 |
assert!(Parsed::new().with_offset_second(200).is_none()); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
#[test] |
| 131 |
fn single_item_parse() { |
| 132 |
assert!(Time::parse("a", &BorrowedFormatItem::Literal(b"a")).is_err()); |
| 133 |
assert!(Time::parse("b", &BorrowedFormatItem::Literal(b"a")).is_err()); |
| 134 |
} |
| 135 |
|
| 136 |
#[test] |
| 137 |
fn component_err() { |
| 138 |
macro_rules! input_or_empty { |
| 139 |
() => { |
| 140 |
b"" |
| 141 |
}; |
| 142 |
($input:expr) => { |
| 143 |
$input |
| 144 |
}; |
| 145 |
} |
| 146 |
macro_rules! assert_invalid_component { |
| 147 |
($component_name:expr, $component:expr $(, $input:expr)?) => {{ |
| 148 |
let mut parsed = Parsed::new(); |
| 149 |
assert_eq!( |
| 150 |
parsed.parse_component(input_or_empty!($($input)?), $component), |
| 151 |
Err(error::ParseFromDescription::InvalidComponent( |
| 152 |
$component_name |
| 153 |
)) |
| 154 |
); |
| 155 |
}}; |
| 156 |
} |
| 157 |
|
| 158 |
assert_invalid_component!("day", Component::Day(<_>::default())); |
| 159 |
assert_invalid_component!("month", Component::Month(<_>::default())); |
| 160 |
assert_invalid_component!("ordinal", Component::Ordinal(<_>::default())); |
| 161 |
assert_invalid_component!("weekday", Component::Weekday(<_>::default())); |
| 162 |
assert_invalid_component!("week number", Component::WeekNumber(<_>::default())); |
| 163 |
assert_invalid_component!("year", Component::Year(<_>::default())); |
| 164 |
assert_invalid_component!("minute", Component::Minute(<_>::default())); |
| 165 |
assert_invalid_component!("period", Component::Period(<_>::default())); |
| 166 |
assert_invalid_component!("second", Component::Second(<_>::default())); |
| 167 |
assert_invalid_component!("subsecond", Component::Subsecond(<_>::default())); |
| 168 |
assert_invalid_component!("offset hour", Component::OffsetHour(<_>::default())); |
| 169 |
assert_invalid_component!("offset minute", Component::OffsetMinute(<_>::default())); |
| 170 |
assert_invalid_component!("offset second", Component::OffsetSecond(<_>::default())); |
| 171 |
|
| 172 |
assert_invalid_component!( |
| 173 |
"week number", |
| 174 |
Component::WeekNumber(WeekNumber::default().with_repr(WeekNumberRepr::Iso)), |
| 175 |
b"00" |
| 176 |
); |
| 177 |
assert_invalid_component!( |
| 178 |
"hour", |
| 179 |
Component::Hour(Hour::default().with_is_12_hour_clock(true)), |
| 180 |
b"00" |
| 181 |
); |
| 182 |
} |