mirror of
https://github.com/chronotope/chrono.git
synced 2025-10-02 15:26:12 +00:00
Merge datetime::test_*_json
into their tests
This commit is contained in:
parent
e9849ed602
commit
ffe27452c6
@ -1933,89 +1933,3 @@ where
|
|||||||
/// --------
|
/// --------
|
||||||
/// 719163
|
/// 719163
|
||||||
const UNIX_EPOCH_DAY: i64 = 719_163;
|
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());
|
|
||||||
}
|
|
||||||
|
@ -1209,24 +1209,87 @@ pub mod ts_seconds_option {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
#[cfg(feature = "clock")]
|
#[cfg(feature = "clock")]
|
||||||
use crate::datetime::test_decodable_json;
|
use crate::Local;
|
||||||
use crate::datetime::test_encodable_json;
|
|
||||||
use crate::{DateTime, FixedOffset, TimeZone, Utc};
|
use crate::{DateTime, FixedOffset, TimeZone, Utc};
|
||||||
use core::fmt;
|
use core::fmt;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serde_serialize() {
|
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]
|
#[test]
|
||||||
fn test_serde_deserialize() {
|
fn test_serde_deserialize() {
|
||||||
test_decodable_json(
|
// should check against the offset as well (the normal DateTime comparison will ignore them)
|
||||||
|input| serde_json::from_str(input),
|
fn norm<Tz: TimeZone>(dt: &Option<DateTime<Tz>>) -> Option<(&DateTime<Tz>, &Tz::Offset)> {
|
||||||
|input| serde_json::from_str(input),
|
dt.as_ref().map(|dt| (dt, dt.offset()))
|
||||||
|input| serde_json::from_str(input),
|
}
|
||||||
|
|
||||||
|
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]
|
#[test]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user