From 419f05150f9fd94d1604781d9e40845fc81f44bd Mon Sep 17 00:00:00 2001 From: Jonas Bushart Date: Sun, 21 Jul 2024 17:05:11 +0200 Subject: [PATCH] Add new test cases Many of the tests fail currently --- serde_with/tests/chrono_0_4.rs | 55 +++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/serde_with/tests/chrono_0_4.rs b/serde_with/tests/chrono_0_4.rs index 5dd72b43..f995eec4 100644 --- a/serde_with/tests/chrono_0_4.rs +++ b/serde_with/tests/chrono_0_4.rs @@ -3,7 +3,8 @@ extern crate alloc; mod utils; use crate::utils::{ - check_deserialization, check_error_deserialization, check_serialization, is_equal, + check_deserialization, check_error_deserialization, check_error_serialization, + check_serialization, is_equal, }; use alloc::collections::BTreeMap; use chrono_0_4::{DateTime, Duration, Local, NaiveDateTime, Utc}; @@ -741,3 +742,55 @@ fn test_naive_datetime_smoketest() { NaiveDateTime, "TimestampSecondsWithFrac", zero - Duration::seconds(1), {expect![[r#"-1.0"#]]}; }; } + +#[test] +fn bug771_extreme_nanoseconds() { + #[serde_as] + #[derive(Debug, Serialize)] + struct S(#[serde_as(as = "serde_with::TimestampNanoSeconds")] DateTime); + + check_error_serialization( + S(DateTime::::MIN_UTC), + expect!["Failed to serialize value as the value cannot be represented."], + ); + check_error_serialization( + S("1385-06-12T00:25:26.290448384Z".parse().unwrap()), + expect!["Failed to serialize value as the value cannot be represented."], + ); + check_error_serialization( + S("1385-06-12T00:25:26.290448385Z".parse().unwrap()), + expect!["Failed to serialize value as the value cannot be represented."], + ); + check_error_serialization( + S("1677-09-21T00:12:43.145224191Z".parse().unwrap()), + expect!["Failed to serialize value as the value cannot be represented."], + ); + check_error_serialization( + S("1677-09-21T00:12:43.145224192Z".parse().unwrap()), + expect!["Failed to serialize value as the value cannot be represented."], + ); + check_serialization( + S("1677-09-21T00:12:43.145224193Z".parse().unwrap()), + expect!["-9223372036854775807"], + ); + check_serialization( + S("2262-04-11T23:47:16.854775807Z".parse().unwrap()), + expect!["9223372036854775807"], + ); + check_error_serialization( + S("2262-04-11T23:47:16.854775808Z".parse().unwrap()), + expect!["Failed to serialize value as the value cannot be represented."], + ); + check_error_serialization( + S("2554-07-21T23:34:33.709551615Z".parse().unwrap()), + expect!["Failed to serialize value as the value cannot be represented."], + ); + check_error_serialization( + S("2554-07-21T23:34:33.709551616Z".parse().unwrap()), + expect!["Failed to serialize value as the value cannot be represented."], + ); + check_error_serialization( + S(DateTime::::MAX_UTC), + expect!["Failed to serialize value as the value cannot be represented."], + ); +}