Skip to content

Commit

Permalink
Add tests for OwnedFormatItem
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Nov 3, 2022
1 parent 4d18c08 commit 2cba2ef
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 7 deletions.
6 changes: 5 additions & 1 deletion tests/derives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::hash::Hash;

use time::error::{self, ConversionRange, IndeterminateOffset, TryFromParsed};
use time::ext::NumericalDuration;
use time::format_description::{self, modifier, well_known, Component, FormatItem};
use time::format_description::{self, modifier, well_known, Component, FormatItem, OwnedFormatItem};
use time::macros::{date, offset, time};
use time::parsing::Parsed;
use time::{Duration, Error, Instant, Month, Time, Weekday};
Expand Down Expand Up @@ -176,5 +176,9 @@ fn debug() {
FormatItem::Compound(&[FormatItem::Component(Component::Day(modifier::Day::default()))]);
FormatItem::Optional(&FormatItem::Compound(&[]));
FormatItem::First(&[]);
OwnedFormatItem::from(FormatItem::Literal(b"abcdef"));
OwnedFormatItem::from(FormatItem::Compound(&[FormatItem::Component(Component::Day(modifier::Day::default()))]));
OwnedFormatItem::from(FormatItem::Optional(&FormatItem::Compound(&[])));
OwnedFormatItem::from(FormatItem::First(&[]));
}
}
44 changes: 40 additions & 4 deletions tests/format_description.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use time::format_description::{modifier, Component, FormatItem};
use time::format_description::{modifier, Component, FormatItem, OwnedFormatItem};

#[test]
fn format_item_component_conversions() {
fn borrowed_format_item_component_conversions() {
let component = Component::Year(modifier::Year::default());
let item = FormatItem::from(component);
assert!(matches!(item, FormatItem::Component(inner) if inner == component));
Expand All @@ -11,15 +11,15 @@ fn format_item_component_conversions() {
}

#[test]
fn format_item_compound_conversions() {
fn borrowed_format_item_compound_conversions() {
let compound = [FormatItem::Literal(b"")].as_slice();
let item = FormatItem::from(compound);
assert!(matches!(item, FormatItem::Compound(inner) if inner == compound));
assert_eq!(<&[FormatItem<'_>]>::try_from(item), Ok(compound));
}

#[test]
fn format_item_equality() {
fn borrowed_format_item_equality() {
let component = Component::Year(modifier::Year::default());
let compound = [FormatItem::Literal(b"")].as_slice();
let component_item = FormatItem::from(component);
Expand All @@ -30,3 +30,39 @@ fn format_item_equality() {
assert_eq!(compound, compound_item);
assert_eq!(compound_item, compound);
}

#[test]
fn owned_format_item_component_conversions() {
let component = Component::Year(modifier::Year::default());
let item = OwnedFormatItem::from(component);
assert!(matches!(item, OwnedFormatItem::Component(inner) if inner == component));
assert_eq!(Component::try_from(item), Ok(component));
assert!(Component::try_from(OwnedFormatItem::Literal(Box::new([]))).is_err());
assert!(Vec::<OwnedFormatItem>::try_from(OwnedFormatItem::Literal(Box::new([]))).is_err());
}

#[test]
fn owned_format_item_compound_conversions() {
let compound = vec![OwnedFormatItem::Literal(Box::new([]))];
let item = OwnedFormatItem::from(compound.clone());
assert!(matches!(item.clone(), OwnedFormatItem::Compound(inner) if inner.to_vec() == compound));
assert_eq!(Vec::<OwnedFormatItem>::try_from(item), Ok(compound));
}

#[test]
fn owned_format_item_equality() {
let component = Component::Year(modifier::Year::default());
let compound = OwnedFormatItem::from([FormatItem::Literal(b"")].as_slice());
let component_item = OwnedFormatItem::from(component);

assert_eq!(component, component_item);
assert_eq!(component_item, component);
assert_eq!(
compound,
[OwnedFormatItem::Literal(Box::new([]))].as_slice()
);
assert_eq!(
[OwnedFormatItem::Literal(Box::new([]))].as_slice(),
compound
);
}
62 changes: 61 additions & 1 deletion tests/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::io;

use time::format_description::well_known::iso8601::{DateKind, OffsetPrecision, TimePrecision};
use time::format_description::well_known::{iso8601, Iso8601, Rfc2822, Rfc3339};
use time::format_description::{self, FormatItem};
use time::format_description::{self, FormatItem, OwnedFormatItem};
use time::macros::{date, datetime, format_description as fd, offset, time};
use time::{OffsetDateTime, Time};

Expand Down Expand Up @@ -271,6 +271,15 @@ fn format_time() -> time::Result<()> {
.format_into(&mut io::sink(), format_description)
.is_ok()
);
assert_eq!(
time!(13:02:03.456_789_012).format(&OwnedFormatItem::from(format_description))?,
output
);
assert!(
time!(13:02:03.456_789_012)
.format_into(&mut io::sink(), &OwnedFormatItem::from(format_description))
.is_ok()
);
}

assert_eq!(
Expand Down Expand Up @@ -369,6 +378,15 @@ fn format_date() -> time::Result<()> {
.format_into(&mut io::sink(), format_description)
.is_ok()
);
assert_eq!(
date!(2019 - 12 - 31).format(&OwnedFormatItem::from(format_description))?,
output
);
assert!(
date!(2019 - 12 - 31)
.format_into(&mut io::sink(), &OwnedFormatItem::from(format_description))
.is_ok()
);
}

Ok(())
Expand Down Expand Up @@ -421,6 +439,15 @@ fn format_offset() -> time::Result<()> {
.format_into(&mut io::sink(), format_description)
.is_ok()
);
assert_eq!(
value.format(&OwnedFormatItem::from(format_description))?,
output
);
assert!(
value
.format_into(&mut io::sink(), &OwnedFormatItem::from(format_description))
.is_ok()
);
}

Ok(())
Expand Down Expand Up @@ -452,6 +479,15 @@ fn format_pdt() -> time::Result<()> {
.format_into(&mut io::sink(), format_description)
.is_ok()
);
assert_eq!(
datetime!(1970-01-01 0:00).format(&OwnedFormatItem::from(format_description))?,
"1970-01-01 00:00:00.0"
);
assert!(
datetime!(1970-01-01 0:00)
.format_into(&mut io::sink(), &OwnedFormatItem::from(format_description))
.is_ok()
);

Ok(())
}
Expand Down Expand Up @@ -484,6 +520,15 @@ fn format_odt() -> time::Result<()> {
.format_into(&mut io::sink(), &format_description)
.is_ok()
);
assert_eq!(
datetime!(1970-01-01 0:00 UTC).format(&OwnedFormatItem::from(&format_description))?,
"1970-01-01 00:00:00.0 +00:00:00"
);
assert!(
datetime!(1970-01-01 0:00 UTC)
.format_into(&mut io::sink(), &OwnedFormatItem::from(format_description))
.is_ok()
);

Ok(())
}
Expand Down Expand Up @@ -538,11 +583,16 @@ fn failed_write() -> time::Result<()> {
}

assert_err!(Time::MIDNIGHT, fd!("foo"));
assert_err!(Time::MIDNIGHT, OwnedFormatItem::from(fd!("foo")));
assert_err!(Time::MIDNIGHT, FormatItem::Compound(fd!("foo")));
assert_err!(
Time::MIDNIGHT,
FormatItem::Optional(&FormatItem::Compound(fd!("foo")))
);
assert_err!(
Time::MIDNIGHT,
OwnedFormatItem::from(FormatItem::Optional(&FormatItem::Compound(fd!("foo"))))
);
assert_err!(OffsetDateTime::UNIX_EPOCH, Rfc3339);
assert_err!(datetime!(2021-001 0:00:00.1 UTC), Rfc3339);
assert_err!(datetime!(2021-001 0:00 +0:01), Rfc3339);
Expand Down Expand Up @@ -668,6 +718,16 @@ fn first() -> time::Result<()> {
Time::MIDNIGHT.format(&FormatItem::First(&[FormatItem::Compound(fd!("[hour]"))]))?,
"00"
);
assert_eq!(
Time::MIDNIGHT.format(&OwnedFormatItem::First(Box::new([])))?,
""
);
assert_eq!(
Time::MIDNIGHT.format(&OwnedFormatItem::from(FormatItem::First(&[
FormatItem::Compound(fd!("[hour]"))
])))?,
"00"
);

Ok(())
}
105 changes: 104 additions & 1 deletion tests/parsing.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::num::NonZeroU8;

use time::format_description::well_known::{Iso8601, Rfc2822, Rfc3339};
use time::format_description::{modifier, Component, FormatItem};
use time::format_description::{modifier, Component, FormatItem, OwnedFormatItem};
use time::macros::{date, datetime, offset, time};
use time::parsing::Parsed;
use time::{
Expand Down Expand Up @@ -642,6 +642,17 @@ fn parse_time() -> time::Result<()> {

for (format_description, input, output) in &format_input_output {
assert_eq!(&Time::parse(input, format_description)?, output);
assert_eq!(
&Time::parse(input, &OwnedFormatItem::from(format_description))?,
output
);
assert_eq!(
&Time::parse(
input,
[OwnedFormatItem::from(format_description)].as_slice()
)?,
output
);
}

Ok(())
Expand Down Expand Up @@ -687,6 +698,24 @@ fn parse_time_err() -> time::Result<()> {
error::ParseFromDescription::InvalidComponent("subsecond")
))
));
assert!(matches!(
Time::parse(
"1a",
&OwnedFormatItem::from(fd::parse("[subsecond digits:2]")?)
),
Err(error::Parse::ParseFromDescription(
error::ParseFromDescription::InvalidComponent("subsecond")
))
));
assert!(matches!(
Time::parse(
"1a",
[OwnedFormatItem::from(fd::parse("[subsecond digits:2]")?)].as_slice()
),
Err(error::Parse::ParseFromDescription(
error::ParseFromDescription::InvalidComponent("subsecond")
))
));
assert!(matches!(
Time::parse("12a", &fd::parse("[subsecond digits:3]")?),
Err(error::Parse::ParseFromDescription(
Expand Down Expand Up @@ -803,6 +832,10 @@ fn parse_date() -> time::Result<()> {

for (format_description, input, output) in &format_input_output {
assert_eq!(&Date::parse(input, format_description)?, output);
assert_eq!(
&Date::parse(input, &OwnedFormatItem::from(format_description))?,
output
);
}

Ok(())
Expand Down Expand Up @@ -1252,6 +1285,18 @@ fn parse_optional() -> time::Result<()> {
assert_eq!(parsed.month(), Some(Month::January));
assert_eq!(parsed.day().map(NonZeroU8::get), Some(2));

let mut parsed = Parsed::new();
let remaining_input = parsed.parse_item(
b"2021-01-02",
&OwnedFormatItem::from(FormatItem::Optional(&FormatItem::Compound(&fd::parse(
"[year]-[month]-[day]",
)?))),
)?;
assert!(remaining_input.is_empty());
assert_eq!(parsed.year(), Some(2021));
assert_eq!(parsed.month(), Some(Month::January));
assert_eq!(parsed.day().map(NonZeroU8::get), Some(2));

// Ensure a successful partial parse *does not* mutate `parsed`.
let mut parsed = Parsed::new();
let remaining_input = parsed.parse_item(
Expand All @@ -1263,6 +1308,18 @@ fn parse_optional() -> time::Result<()> {
assert!(parsed.month().is_none());
assert!(parsed.day().is_none());

let mut parsed = Parsed::new();
let remaining_input = parsed.parse_item(
b"2021-01",
&OwnedFormatItem::from(FormatItem::Optional(&FormatItem::Compound(&fd::parse(
"[year]-[month]-[day]",
)?))),
)?;
assert_eq!(remaining_input, b"2021-01");
assert!(parsed.year().is_none());
assert!(parsed.month().is_none());
assert!(parsed.day().is_none());

Ok(())
}

Expand All @@ -1279,6 +1336,18 @@ fn parse_first() -> time::Result<()> {
assert_eq!(parsed.month(), Some(Month::January));
assert_eq!(parsed.day().map(NonZeroU8::get), Some(2));

let mut parsed = Parsed::new();
let remaining_input = parsed.parse_item(
b"2021-01-02",
&OwnedFormatItem::from(FormatItem::First(&[FormatItem::Compound(&fd::parse(
"[year]-[month]-[day]",
)?)])),
)?;
assert!(remaining_input.is_empty());
assert_eq!(parsed.year(), Some(2021));
assert_eq!(parsed.month(), Some(Month::January));
assert_eq!(parsed.day().map(NonZeroU8::get), Some(2));

// Ensure an empty slice is a no-op success.
let mut parsed = Parsed::new();
let remaining_input = parsed.parse_item(b"2021-01-02", &FormatItem::First(&[]))?;
Expand All @@ -1287,6 +1356,14 @@ fn parse_first() -> time::Result<()> {
assert!(parsed.month().is_none());
assert!(parsed.day().is_none());

let mut parsed = Parsed::new();
let remaining_input =
parsed.parse_item(b"2021-01-02", &OwnedFormatItem::First(Box::new([])))?;
assert_eq!(remaining_input, b"2021-01-02");
assert!(parsed.year().is_none());
assert!(parsed.month().is_none());
assert!(parsed.day().is_none());

// Ensure success when the first item fails.
let mut parsed = Parsed::new();
let remaining_input = parsed.parse_item(
Expand All @@ -1302,6 +1379,20 @@ fn parse_first() -> time::Result<()> {
assert_eq!(parsed.month(), Some(Month::January));
assert_eq!(parsed.day().map(NonZeroU8::get), Some(2));

let mut parsed = Parsed::new();
let remaining_input = parsed.parse_item(
b"2021-01-02",
&OwnedFormatItem::from(FormatItem::First(&[
FormatItem::Compound(&fd::parse("[period]")?),
FormatItem::Compound(&fd::parse("x")?),
FormatItem::Compound(&fd::parse("[year]-[month]-[day]")?),
])),
)?;
assert!(remaining_input.is_empty());
assert_eq!(parsed.year(), Some(2021));
assert_eq!(parsed.month(), Some(Month::January));
assert_eq!(parsed.day().map(NonZeroU8::get), Some(2));

// Ensure the first error is returned.
let mut parsed = Parsed::new();
let err = parsed
Expand All @@ -1315,5 +1406,17 @@ fn parse_first() -> time::Result<()> {
.unwrap_err();
assert_eq!(err, error::ParseFromDescription::InvalidComponent("period"));

let mut parsed = Parsed::new();
let err = parsed
.parse_item(
b"2021-01-02",
&OwnedFormatItem::from(FormatItem::First(&[
FormatItem::Compound(&fd::parse("[period]")?),
FormatItem::Compound(&fd::parse("x")?),
])),
)
.unwrap_err();
assert_eq!(err, error::ParseFromDescription::InvalidComponent("period"));

Ok(())
}

0 comments on commit 2cba2ef

Please sign in to comment.