Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline test_encodable_json and test_decodable_json functions #1550

Merged
merged 4 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 0 additions & 86 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1933,89 +1933,3 @@ where
/// --------
/// 719163
const UNIX_EPOCH_DAY: i64 = 719_163;

#[cfg(all(test, feature = "serde"))]
fn test_encodable_json<FUtc, FFixed, E>(to_string_utc: FUtc, to_string_fixed: FFixed)
where
FUtc: Fn(&DateTime<Utc>) -> Result<String, E>,
FFixed: Fn(&DateTime<FixedOffset>) -> Result<String, E>,
E: ::core::fmt::Debug,
{
assert_eq!(
to_string_utc(&Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()).ok(),
Some(r#""2014-07-24T12:34:06Z""#.into())
);

assert_eq!(
to_string_fixed(
&FixedOffset::east_opt(3660).unwrap().with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
)
.ok(),
Some(r#""2014-07-24T12:34:06+01:01""#.into())
);
assert_eq!(
to_string_fixed(
&FixedOffset::east_opt(3650).unwrap().with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
)
.ok(),
// An offset with seconds is not allowed by RFC 3339, so we round it to the nearest minute.
// In this case `+01:00:50` becomes `+01:01`
Some(r#""2014-07-24T12:34:06+01:01""#.into())
);
}

#[cfg(all(test, feature = "clock", feature = "serde"))]
fn test_decodable_json<FUtc, FFixed, FLocal, E>(
utc_from_str: FUtc,
fixed_from_str: FFixed,
local_from_str: FLocal,
) where
FUtc: Fn(&str) -> Result<DateTime<Utc>, E>,
FFixed: Fn(&str) -> Result<DateTime<FixedOffset>, E>,
FLocal: Fn(&str) -> Result<DateTime<Local>, E>,
E: ::core::fmt::Debug,
{
// should check against the offset as well (the normal DateTime comparison will ignore them)
fn norm<Tz: TimeZone>(dt: &Option<DateTime<Tz>>) -> Option<(&DateTime<Tz>, &Tz::Offset)> {
dt.as_ref().map(|dt| (dt, dt.offset()))
}

assert_eq!(
norm(&utc_from_str(r#""2014-07-24T12:34:06Z""#).ok()),
norm(&Some(Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()))
);
assert_eq!(
norm(&utc_from_str(r#""2014-07-24T13:57:06+01:23""#).ok()),
norm(&Some(Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()))
);

assert_eq!(
norm(&fixed_from_str(r#""2014-07-24T12:34:06Z""#).ok()),
norm(&Some(
FixedOffset::east_opt(0).unwrap().with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
))
);
assert_eq!(
norm(&fixed_from_str(r#""2014-07-24T13:57:06+01:23""#).ok()),
norm(&Some(
FixedOffset::east_opt(60 * 60 + 23 * 60)
.unwrap()
.with_ymd_and_hms(2014, 7, 24, 13, 57, 6)
.unwrap()
))
);

// we don't know the exact local offset but we can check that
// the conversion didn't change the instant itself
assert_eq!(
local_from_str(r#""2014-07-24T12:34:06Z""#).expect("local should parse"),
Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
);
assert_eq!(
local_from_str(r#""2014-07-24T13:57:06+01:23""#).expect("local should parse with offset"),
Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
);

assert!(utc_from_str(r#""2014-07-32T12:34:06Z""#).is_err());
assert!(fixed_from_str(r#""2014-07-32T12:34:06Z""#).is_err());
}
79 changes: 71 additions & 8 deletions src/datetime/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1209,24 +1209,87 @@ pub mod ts_seconds_option {
#[cfg(test)]
mod tests {
#[cfg(feature = "clock")]
use crate::datetime::test_decodable_json;
use crate::datetime::test_encodable_json;
use crate::Local;
use crate::{DateTime, FixedOffset, TimeZone, Utc};
use core::fmt;

#[test]
fn test_serde_serialize() {
test_encodable_json(serde_json::to_string, serde_json::to_string);
assert_eq!(
serde_json::to_string(&Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()).ok(),
Some(r#""2014-07-24T12:34:06Z""#.to_owned())
);
assert_eq!(
serde_json::to_string(
&FixedOffset::east_opt(3660)
.unwrap()
.with_ymd_and_hms(2014, 7, 24, 12, 34, 6)
.unwrap()
)
.ok(),
Some(r#""2014-07-24T12:34:06+01:01""#.to_owned())
);
assert_eq!(
serde_json::to_string(
&FixedOffset::east_opt(3650)
.unwrap()
.with_ymd_and_hms(2014, 7, 24, 12, 34, 6)
.unwrap()
)
.ok(),
// An offset with seconds is not allowed by RFC 3339, so we round it to the nearest minute.
// In this case `+01:00:50` becomes `+01:01`
Some(r#""2014-07-24T12:34:06+01:01""#.to_owned())
);
}

#[cfg(feature = "clock")]
#[test]
fn test_serde_deserialize() {
test_decodable_json(
|input| serde_json::from_str(input),
|input| serde_json::from_str(input),
|input| serde_json::from_str(input),
// should check against the offset as well (the normal DateTime comparison will ignore them)
fn norm<Tz: TimeZone>(dt: &Option<DateTime<Tz>>) -> Option<(&DateTime<Tz>, &Tz::Offset)> {
dt.as_ref().map(|dt| (dt, dt.offset()))
}

let dt: Option<DateTime<Utc>> = serde_json::from_str(r#""2014-07-24T12:34:06Z""#).ok();
assert_eq!(norm(&dt), norm(&Some(Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap())));
let dt: Option<DateTime<Utc>> = serde_json::from_str(r#""2014-07-24T13:57:06+01:23""#).ok();
assert_eq!(norm(&dt), norm(&Some(Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap())));

let dt: Option<DateTime<FixedOffset>> =
serde_json::from_str(r#""2014-07-24T12:34:06Z""#).ok();
assert_eq!(
norm(&dt),
norm(&Some(
FixedOffset::east_opt(0).unwrap().with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap()
))
);
let dt: Option<DateTime<FixedOffset>> =
serde_json::from_str(r#""2014-07-24T13:57:06+01:23""#).ok();
assert_eq!(
norm(&dt),
norm(&Some(
FixedOffset::east_opt(60 * 60 + 23 * 60)
.unwrap()
.with_ymd_and_hms(2014, 7, 24, 13, 57, 6)
.unwrap()
))
);

// we don't know the exact local offset but we can check that
// the conversion didn't change the instant itself
#[cfg(feature = "clock")]
{
let dt: DateTime<Local> =
serde_json::from_str(r#""2014-07-24T12:34:06Z""#).expect("local should parse");
assert_eq!(dt, Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap());

let dt: DateTime<Local> = serde_json::from_str(r#""2014-07-24T13:57:06+01:23""#)
.expect("local should parse with offset");
assert_eq!(dt, Utc.with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap());
}

assert!(serde_json::from_str::<DateTime<Utc>>(r#""2014-07-32T12:34:06Z""#).is_err());
assert!(serde_json::from_str::<DateTime<FixedOffset>>(r#""2014-07-32T12:34:06Z""#).is_err());
}

#[test]
Expand Down
133 changes: 65 additions & 68 deletions src/naive/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2387,71 +2387,6 @@ const YEAR_DELTAS: &[u8; 401] = &[
96, 97, 97, 97, 97, // 400+1
];

#[cfg(all(test, feature = "serde"))]
fn test_encodable_json<F, E>(to_string: F)
where
F: Fn(&NaiveDate) -> Result<String, E>,
E: ::std::fmt::Debug,
{
assert_eq!(
to_string(&NaiveDate::from_ymd_opt(2014, 7, 24).unwrap()).ok(),
Some(r#""2014-07-24""#.into())
);
assert_eq!(
to_string(&NaiveDate::from_ymd_opt(0, 1, 1).unwrap()).ok(),
Some(r#""0000-01-01""#.into())
);
assert_eq!(
to_string(&NaiveDate::from_ymd_opt(-1, 12, 31).unwrap()).ok(),
Some(r#""-0001-12-31""#.into())
);
assert_eq!(to_string(&NaiveDate::MIN).ok(), Some(r#""-262143-01-01""#.into()));
assert_eq!(to_string(&NaiveDate::MAX).ok(), Some(r#""+262142-12-31""#.into()));
}

#[cfg(all(test, feature = "serde"))]
fn test_decodable_json<F, E>(from_str: F)
where
F: Fn(&str) -> Result<NaiveDate, E>,
E: ::std::fmt::Debug,
{
use std::{i32, i64};

assert_eq!(
from_str(r#""2016-07-08""#).ok(),
Some(NaiveDate::from_ymd_opt(2016, 7, 8).unwrap())
);
assert_eq!(from_str(r#""2016-7-8""#).ok(), Some(NaiveDate::from_ymd_opt(2016, 7, 8).unwrap()));
assert_eq!(from_str(r#""+002016-07-08""#).ok(), NaiveDate::from_ymd_opt(2016, 7, 8));
assert_eq!(from_str(r#""0000-01-01""#).ok(), Some(NaiveDate::from_ymd_opt(0, 1, 1).unwrap()));
assert_eq!(from_str(r#""0-1-1""#).ok(), Some(NaiveDate::from_ymd_opt(0, 1, 1).unwrap()));
assert_eq!(
from_str(r#""-0001-12-31""#).ok(),
Some(NaiveDate::from_ymd_opt(-1, 12, 31).unwrap())
);
assert_eq!(from_str(r#""-262143-01-01""#).ok(), Some(NaiveDate::MIN));
assert_eq!(from_str(r#""+262142-12-31""#).ok(), Some(NaiveDate::MAX));

// bad formats
assert!(from_str(r#""""#).is_err());
assert!(from_str(r#""20001231""#).is_err());
assert!(from_str(r#""2000-00-00""#).is_err());
assert!(from_str(r#""2000-02-30""#).is_err());
assert!(from_str(r#""2001-02-29""#).is_err());
assert!(from_str(r#""2002-002-28""#).is_err());
assert!(from_str(r#""yyyy-mm-dd""#).is_err());
assert!(from_str(r#"0"#).is_err());
assert!(from_str(r#"20.01"#).is_err());
assert!(from_str(&i32::MIN.to_string()).is_err());
assert!(from_str(&i32::MAX.to_string()).is_err());
assert!(from_str(&i64::MIN.to_string()).is_err());
assert!(from_str(&i64::MAX.to_string()).is_err());
assert!(from_str(r#"{}"#).is_err());
// pre-0.3.0 rustc-serialize format is now invalid
assert!(from_str(r#"{"ymdf":20}"#).is_err());
assert!(from_str(r#"null"#).is_err());
}

#[cfg(feature = "serde")]
mod serde {
use super::NaiveDate;
Expand Down Expand Up @@ -2507,17 +2442,79 @@ mod serde {

#[cfg(test)]
mod tests {
use crate::naive::date::{test_decodable_json, test_encodable_json};
use crate::NaiveDate;

#[test]
fn test_serde_serialize() {
test_encodable_json(serde_json::to_string);
assert_eq!(
serde_json::to_string(&NaiveDate::from_ymd_opt(2014, 7, 24).unwrap()).ok(),
Some(r#""2014-07-24""#.into())
);
assert_eq!(
serde_json::to_string(&NaiveDate::from_ymd_opt(0, 1, 1).unwrap()).ok(),
Some(r#""0000-01-01""#.into())
);
assert_eq!(
serde_json::to_string(&NaiveDate::from_ymd_opt(-1, 12, 31).unwrap()).ok(),
Some(r#""-0001-12-31""#.into())
);
assert_eq!(
serde_json::to_string(&NaiveDate::MIN).ok(),
Some(r#""-262143-01-01""#.into())
);
assert_eq!(
serde_json::to_string(&NaiveDate::MAX).ok(),
Some(r#""+262142-12-31""#.into())
);
}

#[test]
fn test_serde_deserialize() {
test_decodable_json(|input| serde_json::from_str(input));
let from_str = serde_json::from_str::<NaiveDate>;

assert_eq!(
from_str(r#""2016-07-08""#).ok(),
Some(NaiveDate::from_ymd_opt(2016, 7, 8).unwrap())
);
assert_eq!(
from_str(r#""2016-7-8""#).ok(),
Some(NaiveDate::from_ymd_opt(2016, 7, 8).unwrap())
);
assert_eq!(from_str(r#""+002016-07-08""#).ok(), NaiveDate::from_ymd_opt(2016, 7, 8));
assert_eq!(
from_str(r#""0000-01-01""#).ok(),
Some(NaiveDate::from_ymd_opt(0, 1, 1).unwrap())
);
assert_eq!(
from_str(r#""0-1-1""#).ok(),
Some(NaiveDate::from_ymd_opt(0, 1, 1).unwrap())
);
assert_eq!(
from_str(r#""-0001-12-31""#).ok(),
Some(NaiveDate::from_ymd_opt(-1, 12, 31).unwrap())
);
assert_eq!(from_str(r#""-262143-01-01""#).ok(), Some(NaiveDate::MIN));
assert_eq!(from_str(r#""+262142-12-31""#).ok(), Some(NaiveDate::MAX));

// bad formats
assert!(from_str(r#""""#).is_err());
assert!(from_str(r#""20001231""#).is_err());
assert!(from_str(r#""2000-00-00""#).is_err());
assert!(from_str(r#""2000-02-30""#).is_err());
assert!(from_str(r#""2001-02-29""#).is_err());
assert!(from_str(r#""2002-002-28""#).is_err());
assert!(from_str(r#""yyyy-mm-dd""#).is_err());
assert!(from_str(r#"0"#).is_err());
assert!(from_str(r#"20.01"#).is_err());
let min = i32::MIN.to_string();
assert!(from_str(&min).is_err());
let max = i32::MAX.to_string();
assert!(from_str(&max).is_err());
let min = i64::MIN.to_string();
assert!(from_str(&min).is_err());
let max = i64::MAX.to_string();
assert!(from_str(&max).is_err());
assert!(from_str(r#"{}"#).is_err());
}

#[test]
Expand Down
Loading
Loading