| 1 |
use std::error::Error as _; |
| 2 |
use std::io; |
| 3 |
|
| 4 |
use time::error::{ |
| 5 |
ComponentRange, ConversionRange, DifferentVariant, Error, Format, IndeterminateOffset, |
| 6 |
InvalidFormatDescription, InvalidVariant, Parse, ParseFromDescription, TryFromParsed, |
| 7 |
}; |
| 8 |
use time::macros::format_description; |
| 9 |
use time::parsing::Parsed; |
| 10 |
use time::{Date, Time, format_description}; |
| 11 |
|
| 12 |
macro_rules! assert_display_eq { |
| 13 |
($a:expr, $b:expr $(,)?) => { |
| 14 |
assert_eq!($a.to_string(), $b.to_string()) |
| 15 |
}; |
| 16 |
} |
| 17 |
|
| 18 |
macro_rules! assert_dbg_reflexive { |
| 19 |
($a:expr) => { |
| 20 |
assert_eq!(format!("{:?}", $a), format!("{:?}", $a)) |
| 21 |
}; |
| 22 |
} |
| 23 |
|
| 24 |
macro_rules! assert_source { |
| 25 |
($err:expr,None $(,)?) => { |
| 26 |
assert!($err.source().is_none()) |
| 27 |
}; |
| 28 |
($err:expr, $source:ty $(,)?) => { |
| 29 |
assert!($err.source().unwrap().is::<$source>()) |
| 30 |
}; |
| 31 |
} |
| 32 |
|
| 33 |
fn component_range() -> ComponentRange { |
| 34 |
Date::from_ordinal_date(0, 367).expect_err("367 is not a valid day") |
| 35 |
} |
| 36 |
|
| 37 |
fn insufficient_type_information() -> Format { |
| 38 |
Time::MIDNIGHT |
| 39 |
.format(format_description!("[year]")) |
| 40 |
.expect_err("missing date and UTC offset") |
| 41 |
} |
| 42 |
|
| 43 |
fn unexpected_trailing_characters() -> Parse { |
| 44 |
Time::parse("a", format_description!("")).expect_err("should fail to parse") |
| 45 |
} |
| 46 |
|
| 47 |
fn invalid_format_description() -> InvalidFormatDescription { |
| 48 |
format_description::parse("[").expect_err("format description is invalid") |
| 49 |
} |
| 50 |
|
| 51 |
fn io_error() -> io::Error { |
| 52 |
io::Error::last_os_error() |
| 53 |
} |
| 54 |
|
| 55 |
fn invalid_literal() -> ParseFromDescription { |
| 56 |
Parsed::parse_literal(b"a", b"b").expect_err("should fail to parse") |
| 57 |
} |
| 58 |
|
| 59 |
#[test] |
| 60 |
fn debug() { |
| 61 |
assert_dbg_reflexive!(Parse::from(ParseFromDescription::InvalidComponent("a"))); |
| 62 |
assert_dbg_reflexive!(invalid_format_description()); |
| 63 |
assert_dbg_reflexive!(DifferentVariant); |
| 64 |
assert_dbg_reflexive!(InvalidVariant); |
| 65 |
} |
| 66 |
|
| 67 |
#[test] |
| 68 |
fn display() { |
| 69 |
assert_display_eq!(ConversionRange, Error::from(ConversionRange)); |
| 70 |
assert_display_eq!(component_range(), Error::from(component_range())); |
| 71 |
assert_display_eq!(component_range(), TryFromParsed::from(component_range())); |
| 72 |
assert_display_eq!(IndeterminateOffset, Error::from(IndeterminateOffset)); |
| 73 |
assert_display_eq!( |
| 74 |
TryFromParsed::InsufficientInformation, |
| 75 |
Error::from(TryFromParsed::InsufficientInformation) |
| 76 |
); |
| 77 |
assert_display_eq!( |
| 78 |
insufficient_type_information(), |
| 79 |
Error::from(insufficient_type_information()) |
| 80 |
); |
| 81 |
assert_display_eq!( |
| 82 |
Format::InvalidComponent("a"), |
| 83 |
Error::from(Format::InvalidComponent("a")) |
| 84 |
); |
| 85 |
assert_display_eq!( |
| 86 |
ParseFromDescription::InvalidComponent("a"), |
| 87 |
Error::from(Parse::from(ParseFromDescription::InvalidComponent("a"))) |
| 88 |
); |
| 89 |
assert_display_eq!(invalid_literal(), Parse::from(invalid_literal())); |
| 90 |
assert_display_eq!( |
| 91 |
component_range(), |
| 92 |
Error::from(Parse::from(TryFromParsed::from(component_range()))) |
| 93 |
); |
| 94 |
assert_display_eq!( |
| 95 |
ParseFromDescription::InvalidComponent("a"), |
| 96 |
Parse::from(ParseFromDescription::InvalidComponent("a")) |
| 97 |
); |
| 98 |
assert_display_eq!( |
| 99 |
component_range(), |
| 100 |
Parse::from(TryFromParsed::from(component_range())) |
| 101 |
); |
| 102 |
assert_display_eq!( |
| 103 |
unexpected_trailing_characters(), |
| 104 |
Error::from(unexpected_trailing_characters()), |
| 105 |
); |
| 106 |
assert_display_eq!( |
| 107 |
invalid_format_description(), |
| 108 |
Error::from(invalid_format_description()) |
| 109 |
); |
| 110 |
assert_display_eq!(io_error(), Format::from(io_error())); |
| 111 |
assert_display_eq!(DifferentVariant, Error::from(DifferentVariant)); |
| 112 |
assert_display_eq!(InvalidVariant, Error::from(InvalidVariant)); |
| 113 |
} |
| 114 |
|
| 115 |
#[test] |
| 116 |
fn source() { |
| 117 |
assert_source!(Error::from(ConversionRange), ConversionRange); |
| 118 |
assert_source!(Error::from(component_range()), ComponentRange); |
| 119 |
assert_source!(TryFromParsed::from(component_range()), ComponentRange); |
| 120 |
assert_source!(TryFromParsed::InsufficientInformation, None); |
| 121 |
assert_source!(insufficient_type_information(), None); |
| 122 |
assert_source!(Format::InvalidComponent("a"), None); |
| 123 |
assert_source!(Error::from(insufficient_type_information()), Format); |
| 124 |
assert_source!(Error::from(IndeterminateOffset), IndeterminateOffset); |
| 125 |
assert_source!( |
| 126 |
Parse::from(TryFromParsed::InsufficientInformation), |
| 127 |
TryFromParsed |
| 128 |
); |
| 129 |
assert_source!( |
| 130 |
Error::from(TryFromParsed::InsufficientInformation), |
| 131 |
TryFromParsed |
| 132 |
); |
| 133 |
assert_source!( |
| 134 |
Parse::from(ParseFromDescription::InvalidComponent("a")), |
| 135 |
ParseFromDescription |
| 136 |
); |
| 137 |
assert_source!( |
| 138 |
Error::from(ParseFromDescription::InvalidComponent("a")), |
| 139 |
ParseFromDescription |
| 140 |
); |
| 141 |
assert_source!(unexpected_trailing_characters(), ParseFromDescription); |
| 142 |
assert_source!( |
| 143 |
Error::from(unexpected_trailing_characters()), |
| 144 |
ParseFromDescription |
| 145 |
); |
| 146 |
assert_source!( |
| 147 |
Error::from(invalid_format_description()), |
| 148 |
InvalidFormatDescription |
| 149 |
); |
| 150 |
assert_source!(Format::from(io_error()), io::Error); |
| 151 |
assert_source!(Error::from(DifferentVariant), DifferentVariant); |
| 152 |
assert_source!(Error::from(InvalidVariant), InvalidVariant); |
| 153 |
} |
| 154 |
|
| 155 |
#[test] |
| 156 |
fn component_name() { |
| 157 |
assert_eq!(component_range().name(), "ordinal"); |
| 158 |
} |
| 159 |
|
| 160 |
#[expect(clippy::cognitive_complexity, reason = "all test the same thing")] |
| 161 |
#[test] |
| 162 |
fn conversion() { |
| 163 |
assert!(ComponentRange::try_from(Error::from(component_range())).is_ok()); |
| 164 |
assert!(ConversionRange::try_from(Error::from(ConversionRange)).is_ok()); |
| 165 |
assert!(Format::try_from(Error::from(insufficient_type_information())).is_ok()); |
| 166 |
assert!(IndeterminateOffset::try_from(Error::from(IndeterminateOffset)).is_ok()); |
| 167 |
assert!(InvalidFormatDescription::try_from(Error::from(invalid_format_description())).is_ok()); |
| 168 |
assert!(ParseFromDescription::try_from(Error::from(invalid_literal())).is_ok()); |
| 169 |
assert!(ParseFromDescription::try_from(Parse::from(invalid_literal())).is_ok()); |
| 170 |
assert!(ParseFromDescription::try_from(unexpected_trailing_characters()).is_ok()); |
| 171 |
assert!(Parse::try_from(Error::from(unexpected_trailing_characters())).is_ok()); |
| 172 |
assert!(Parse::try_from(Error::from(invalid_literal())).is_ok()); |
| 173 |
assert!(Parse::try_from(Error::from(TryFromParsed::InsufficientInformation)).is_ok()); |
| 174 |
assert!(DifferentVariant::try_from(Error::from(DifferentVariant)).is_ok()); |
| 175 |
assert!(InvalidVariant::try_from(Error::from(InvalidVariant)).is_ok()); |
| 176 |
assert!(ComponentRange::try_from(TryFromParsed::ComponentRange(component_range())).is_ok()); |
| 177 |
assert!(TryFromParsed::try_from(Error::from(TryFromParsed::InsufficientInformation)).is_ok()); |
| 178 |
assert!(TryFromParsed::try_from(Parse::from(TryFromParsed::InsufficientInformation)).is_ok()); |
| 179 |
assert!(io::Error::try_from(Format::from(io_error())).is_ok()); |
| 180 |
|
| 181 |
assert!(ComponentRange::try_from(Error::from(IndeterminateOffset)).is_err()); |
| 182 |
assert!(ConversionRange::try_from(Error::from(IndeterminateOffset)).is_err()); |
| 183 |
assert!(Format::try_from(Error::from(IndeterminateOffset)).is_err()); |
| 184 |
assert!(IndeterminateOffset::try_from(Error::from(ConversionRange)).is_err()); |
| 185 |
assert!(InvalidFormatDescription::try_from(Error::from(IndeterminateOffset)).is_err()); |
| 186 |
assert!(ParseFromDescription::try_from(Error::from(IndeterminateOffset)).is_err()); |
| 187 |
assert!(Parse::try_from(Error::from(IndeterminateOffset)).is_err()); |
| 188 |
assert!(DifferentVariant::try_from(Error::from(IndeterminateOffset)).is_err()); |
| 189 |
assert!(InvalidVariant::try_from(Error::from(IndeterminateOffset)).is_err()); |
| 190 |
assert!(ComponentRange::try_from(TryFromParsed::InsufficientInformation).is_err()); |
| 191 |
assert!(TryFromParsed::try_from(Error::from(IndeterminateOffset)).is_err()); |
| 192 |
assert!(TryFromParsed::try_from(unexpected_trailing_characters()).is_err()); |
| 193 |
assert!(io::Error::try_from(insufficient_type_information()).is_err()); |
| 194 |
} |