| 1 |
//! Test Cases |
| 2 |
|
| 3 |
mod utils; |
| 4 |
|
| 5 |
use crate::utils::{check_deserialization, check_error_deserialization, is_equal}; |
| 6 |
use expect_test::expect; |
| 7 |
use serde::{Deserialize, Serialize}; |
| 8 |
use serde_with::{ |
| 9 |
serde_as, DurationMicroSeconds, DurationMicroSecondsWithFrac, DurationMilliSeconds, |
| 10 |
DurationMilliSecondsWithFrac, DurationNanoSeconds, DurationNanoSecondsWithFrac, |
| 11 |
DurationSeconds, DurationSecondsWithFrac, TimestampMicroSeconds, TimestampMicroSecondsWithFrac, |
| 12 |
TimestampMilliSeconds, TimestampMilliSecondsWithFrac, TimestampNanoSeconds, |
| 13 |
TimestampNanoSecondsWithFrac, TimestampSeconds, TimestampSecondsWithFrac, |
| 14 |
}; |
| 15 |
use time_0_3::{Duration, OffsetDateTime, PrimitiveDateTime, UtcOffset}; |
| 16 |
|
| 17 |
/// Create a [`PrimitiveDateTime`] for the Unix Epoch |
| 18 |
fn unix_epoch_primitive() -> PrimitiveDateTime { |
| 19 |
PrimitiveDateTime::new( |
| 20 |
time_0_3::Date::from_ordinal_date(1970, 1).unwrap(), |
| 21 |
time_0_3::Time::from_hms_nano(0, 0, 0, 0).unwrap(), |
| 22 |
) |
| 23 |
} |
| 24 |
|
| 25 |
macro_rules! smoketest { |
| 26 |
($($valuety:ty, $adapter:literal, $value:expr, $expect:tt;)*) => { |
| 27 |
$({ |
| 28 |
#[serde_as] |
| 29 |
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 30 |
struct S(#[serde_as(as = $adapter)] $valuety); |
| 31 |
#[allow(unused_braces)] |
| 32 |
is_equal(S($value), $expect); |
| 33 |
})* |
| 34 |
}; |
| 35 |
} |
| 36 |
|
| 37 |
#[test] |
| 38 |
fn test_duration_smoketest() { |
| 39 |
let zero = Duration::seconds(0); |
| 40 |
let one_second = Duration::seconds(1); |
| 41 |
|
| 42 |
smoketest! { |
| 43 |
Duration, "DurationSeconds<i64>", one_second, {expect![[r#"1"#]]}; |
| 44 |
Duration, "DurationSeconds<f64>", one_second, {expect![[r#"1.0"#]]}; |
| 45 |
Duration, "DurationMilliSeconds<i64>", one_second, {expect![[r#"1000"#]]}; |
| 46 |
Duration, "DurationMilliSeconds<f64>", one_second, {expect![[r#"1000.0"#]]}; |
| 47 |
Duration, "DurationMicroSeconds<i64>", one_second, {expect![[r#"1000000"#]]}; |
| 48 |
Duration, "DurationMicroSeconds<f64>", one_second, {expect![[r#"1000000.0"#]]}; |
| 49 |
Duration, "DurationNanoSeconds<i64>", one_second, {expect![[r#"1000000000"#]]}; |
| 50 |
Duration, "DurationNanoSeconds<f64>", one_second, {expect![[r#"1000000000.0"#]]}; |
| 51 |
}; |
| 52 |
|
| 53 |
smoketest! { |
| 54 |
Duration, "DurationSecondsWithFrac", one_second, {expect![[r#"1.0"#]]}; |
| 55 |
Duration, "DurationSecondsWithFrac<String>", one_second, {expect![[r#""1""#]]}; |
| 56 |
Duration, "DurationMilliSecondsWithFrac", one_second, {expect![[r#"1000.0"#]]}; |
| 57 |
Duration, "DurationMilliSecondsWithFrac<String>", one_second, {expect![[r#""1000""#]]}; |
| 58 |
Duration, "DurationMicroSecondsWithFrac", one_second, {expect![[r#"1000000.0"#]]}; |
| 59 |
Duration, "DurationMicroSecondsWithFrac<String>", one_second, {expect![[r#""1000000""#]]}; |
| 60 |
Duration, "DurationNanoSecondsWithFrac", one_second, {expect![[r#"1000000000.0"#]]}; |
| 61 |
Duration, "DurationNanoSecondsWithFrac<String>", one_second, {expect![[r#""1000000000""#]]}; |
| 62 |
}; |
| 63 |
|
| 64 |
smoketest! { |
| 65 |
Duration, "DurationSecondsWithFrac", zero, {expect![[r#"0.0"#]]}; |
| 66 |
Duration, "DurationSecondsWithFrac", zero + Duration::nanoseconds(500_000_000), {expect![[r#"0.5"#]]}; |
| 67 |
Duration, "DurationSecondsWithFrac", zero + Duration::seconds(1), {expect![[r#"1.0"#]]}; |
| 68 |
Duration, "DurationSecondsWithFrac", zero - Duration::nanoseconds(500_000_000), {expect![[r#"-0.5"#]]}; |
| 69 |
Duration, "DurationSecondsWithFrac", zero - Duration::seconds(1), {expect![[r#"-1.0"#]]}; |
| 70 |
}; |
| 71 |
} |
| 72 |
|
| 73 |
#[test] |
| 74 |
fn test_datetime_utc_smoketest() { |
| 75 |
let zero = OffsetDateTime::UNIX_EPOCH; |
| 76 |
let one_second = zero + Duration::seconds(1); |
| 77 |
|
| 78 |
smoketest! { |
| 79 |
OffsetDateTime, "TimestampSeconds<i64>", one_second, {expect![[r#"1"#]]}; |
| 80 |
OffsetDateTime, "TimestampSeconds<f64>", one_second, {expect![[r#"1.0"#]]}; |
| 81 |
OffsetDateTime, "TimestampMilliSeconds<i64>", one_second, {expect![[r#"1000"#]]}; |
| 82 |
OffsetDateTime, "TimestampMilliSeconds<f64>", one_second, {expect![[r#"1000.0"#]]}; |
| 83 |
OffsetDateTime, "TimestampMicroSeconds<i64>", one_second, {expect![[r#"1000000"#]]}; |
| 84 |
OffsetDateTime, "TimestampMicroSeconds<f64>", one_second, {expect![[r#"1000000.0"#]]}; |
| 85 |
OffsetDateTime, "TimestampNanoSeconds<i64>", one_second, {expect![[r#"1000000000"#]]}; |
| 86 |
OffsetDateTime, "TimestampNanoSeconds<f64>", one_second, {expect![[r#"1000000000.0"#]]}; |
| 87 |
}; |
| 88 |
|
| 89 |
smoketest! { |
| 90 |
OffsetDateTime, "TimestampSecondsWithFrac", one_second, {expect![[r#"1.0"#]]}; |
| 91 |
OffsetDateTime, "TimestampSecondsWithFrac<String>", one_second, {expect![[r#""1""#]]}; |
| 92 |
OffsetDateTime, "TimestampMilliSecondsWithFrac", one_second, {expect![[r#"1000.0"#]]}; |
| 93 |
OffsetDateTime, "TimestampMilliSecondsWithFrac<String>", one_second, {expect![[r#""1000""#]]}; |
| 94 |
OffsetDateTime, "TimestampMicroSecondsWithFrac", one_second, {expect![[r#"1000000.0"#]]}; |
| 95 |
OffsetDateTime, "TimestampMicroSecondsWithFrac<String>", one_second, {expect![[r#""1000000""#]]}; |
| 96 |
OffsetDateTime, "TimestampNanoSecondsWithFrac", one_second, {expect![[r#"1000000000.0"#]]}; |
| 97 |
OffsetDateTime, "TimestampNanoSecondsWithFrac<String>", one_second, {expect![[r#""1000000000""#]]}; |
| 98 |
}; |
| 99 |
|
| 100 |
smoketest! { |
| 101 |
OffsetDateTime, "TimestampSecondsWithFrac", zero, {expect![[r#"0.0"#]]}; |
| 102 |
OffsetDateTime, "TimestampSecondsWithFrac", zero + Duration::nanoseconds(500_000_000), {expect![[r#"0.5"#]]}; |
| 103 |
OffsetDateTime, "TimestampSecondsWithFrac", zero + Duration::seconds(1), {expect![[r#"1.0"#]]}; |
| 104 |
OffsetDateTime, "TimestampSecondsWithFrac", zero - Duration::nanoseconds(500_000_000), {expect![[r#"-0.5"#]]}; |
| 105 |
OffsetDateTime, "TimestampSecondsWithFrac", zero - Duration::seconds(1), {expect![[r#"-1.0"#]]}; |
| 106 |
}; |
| 107 |
} |
| 108 |
|
| 109 |
#[test] |
| 110 |
fn test_naive_datetime_smoketest() { |
| 111 |
let zero = unix_epoch_primitive(); |
| 112 |
let one_second = zero + Duration::seconds(1); |
| 113 |
|
| 114 |
smoketest! { |
| 115 |
PrimitiveDateTime, "TimestampSeconds<i64>", one_second, {expect![[r#"1"#]]}; |
| 116 |
PrimitiveDateTime, "TimestampSeconds<f64>", one_second, {expect![[r#"1.0"#]]}; |
| 117 |
PrimitiveDateTime, "TimestampMilliSeconds<i64>", one_second, {expect![[r#"1000"#]]}; |
| 118 |
PrimitiveDateTime, "TimestampMilliSeconds<f64>", one_second, {expect![[r#"1000.0"#]]}; |
| 119 |
PrimitiveDateTime, "TimestampMicroSeconds<i64>", one_second, {expect![[r#"1000000"#]]}; |
| 120 |
PrimitiveDateTime, "TimestampMicroSeconds<f64>", one_second, {expect![[r#"1000000.0"#]]}; |
| 121 |
PrimitiveDateTime, "TimestampNanoSeconds<i64>", one_second, {expect![[r#"1000000000"#]]}; |
| 122 |
PrimitiveDateTime, "TimestampNanoSeconds<f64>", one_second, {expect![[r#"1000000000.0"#]]}; |
| 123 |
}; |
| 124 |
|
| 125 |
smoketest! { |
| 126 |
PrimitiveDateTime, "TimestampSecondsWithFrac", one_second, {expect![[r#"1.0"#]]}; |
| 127 |
PrimitiveDateTime, "TimestampSecondsWithFrac<String>", one_second, {expect![[r#""1""#]]}; |
| 128 |
PrimitiveDateTime, "TimestampMilliSecondsWithFrac", one_second, {expect![[r#"1000.0"#]]}; |
| 129 |
PrimitiveDateTime, "TimestampMilliSecondsWithFrac<String>", one_second, {expect![[r#""1000""#]]}; |
| 130 |
PrimitiveDateTime, "TimestampMicroSecondsWithFrac", one_second, {expect![[r#"1000000.0"#]]}; |
| 131 |
PrimitiveDateTime, "TimestampMicroSecondsWithFrac<String>", one_second, {expect![[r#""1000000""#]]}; |
| 132 |
PrimitiveDateTime, "TimestampNanoSecondsWithFrac", one_second, {expect![[r#"1000000000.0"#]]}; |
| 133 |
PrimitiveDateTime, "TimestampNanoSecondsWithFrac<String>", one_second, {expect![[r#""1000000000""#]]}; |
| 134 |
}; |
| 135 |
|
| 136 |
smoketest! { |
| 137 |
PrimitiveDateTime, "TimestampSecondsWithFrac", zero, {expect![[r#"0.0"#]]}; |
| 138 |
PrimitiveDateTime, "TimestampSecondsWithFrac", zero + Duration::nanoseconds(500_000_000), {expect![[r#"0.5"#]]}; |
| 139 |
PrimitiveDateTime, "TimestampSecondsWithFrac", zero + Duration::seconds(1), {expect![[r#"1.0"#]]}; |
| 140 |
PrimitiveDateTime, "TimestampSecondsWithFrac", zero - Duration::nanoseconds(500_000_000), {expect![[r#"-0.5"#]]}; |
| 141 |
PrimitiveDateTime, "TimestampSecondsWithFrac", zero - Duration::seconds(1), {expect![[r#"-1.0"#]]}; |
| 142 |
}; |
| 143 |
} |
| 144 |
|
| 145 |
#[test] |
| 146 |
fn test_offset_datetime_rfc2822() { |
| 147 |
#[serde_as] |
| 148 |
#[derive(Debug, PartialEq, Deserialize, Serialize)] |
| 149 |
struct S(#[serde_as(as = "time_0_3::format_description::well_known::Rfc2822")] OffsetDateTime); |
| 150 |
|
| 151 |
is_equal( |
| 152 |
S(OffsetDateTime::UNIX_EPOCH), |
| 153 |
expect![[r#""Thu, 01 Jan 1970 00:00:00 +0000""#]], |
| 154 |
); |
| 155 |
|
| 156 |
check_error_deserialization::<S>( |
| 157 |
r#""Foobar""#, |
| 158 |
expect!["the 'day' component could not be parsed at line 1 column 8"], |
| 159 |
); |
| 160 |
check_error_deserialization::<S>( |
| 161 |
r#""Fri, 2000""#, |
| 162 |
expect![[r#"a character literal was not valid at line 1 column 11"#]], |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
#[test] |
| 167 |
fn test_offset_datetime_rfc3339() { |
| 168 |
#[serde_as] |
| 169 |
#[derive(Debug, PartialEq, Deserialize, Serialize)] |
| 170 |
struct S(#[serde_as(as = "time_0_3::format_description::well_known::Rfc3339")] OffsetDateTime); |
| 171 |
|
| 172 |
is_equal( |
| 173 |
S(OffsetDateTime::UNIX_EPOCH), |
| 174 |
expect![[r#""1970-01-01T00:00:00Z""#]], |
| 175 |
); |
| 176 |
check_deserialization::<S>( |
| 177 |
S( |
| 178 |
OffsetDateTime::from_unix_timestamp_nanos(482_196_050_520_000_000) |
| 179 |
.unwrap() |
| 180 |
.to_offset(UtcOffset::from_hms(0, 0, 0).unwrap()), |
| 181 |
), |
| 182 |
r#""1985-04-12T23:20:50.52Z""#, |
| 183 |
); |
| 184 |
check_deserialization::<S>( |
| 185 |
S(OffsetDateTime::from_unix_timestamp(851_042_397) |
| 186 |
.unwrap() |
| 187 |
.to_offset(UtcOffset::from_hms(-8, 0, 0).unwrap())), |
| 188 |
r#""1996-12-19T16:39:57-08:00""#, |
| 189 |
); |
| 190 |
check_deserialization::<S>( |
| 191 |
S( |
| 192 |
OffsetDateTime::from_unix_timestamp_nanos(662_687_999_999_999_999) |
| 193 |
.unwrap() |
| 194 |
.to_offset(UtcOffset::from_hms(0, 0, 0).unwrap()), |
| 195 |
), |
| 196 |
r#""1990-12-31T23:59:60Z""#, |
| 197 |
); |
| 198 |
check_deserialization::<S>( |
| 199 |
S( |
| 200 |
OffsetDateTime::from_unix_timestamp_nanos(662_687_999_999_999_999) |
| 201 |
.unwrap() |
| 202 |
.to_offset(UtcOffset::from_hms(-8, 0, 0).unwrap()), |
| 203 |
), |
| 204 |
r#""1990-12-31T15:59:60-08:00""#, |
| 205 |
); |
| 206 |
check_deserialization::<S>( |
| 207 |
S( |
| 208 |
OffsetDateTime::from_unix_timestamp_nanos(-1_041_337_172_130_000_000) |
| 209 |
.unwrap() |
| 210 |
.to_offset(UtcOffset::from_hms(0, 20, 0).unwrap()), |
| 211 |
), |
| 212 |
r#""1937-01-01T12:00:27.87+00:20""#, |
| 213 |
); |
| 214 |
|
| 215 |
check_error_deserialization::<S>( |
| 216 |
r#""Foobar""#, |
| 217 |
expect![[r#"the 'year' component could not be parsed at line 1 column 8"#]], |
| 218 |
); |
| 219 |
check_error_deserialization::<S>( |
| 220 |
r#""2000-AA""#, |
| 221 |
expect![[r#"the 'month' component could not be parsed at line 1 column 9"#]], |
| 222 |
); |
| 223 |
} |
| 224 |
|
| 225 |
#[test] |
| 226 |
fn test_offset_datetime_iso8601() { |
| 227 |
/// The default configuration for [`Iso8601`]. |
| 228 |
const DEFAULT_CONFIG: time_0_3::format_description::well_known::iso8601::EncodedConfig = |
| 229 |
time_0_3::format_description::well_known::iso8601::Config::DEFAULT.encode(); |
| 230 |
|
| 231 |
#[serde_as] |
| 232 |
#[derive(Debug, PartialEq, Deserialize, Serialize)] |
| 233 |
struct S( |
| 234 |
#[serde_as(as = "time_0_3::format_description::well_known::Iso8601<DEFAULT_CONFIG>")] |
| 235 |
OffsetDateTime, |
| 236 |
); |
| 237 |
|
| 238 |
is_equal( |
| 239 |
S(OffsetDateTime::UNIX_EPOCH), |
| 240 |
expect![[r#""1970-01-01T00:00:00.000000000Z""#]], |
| 241 |
); |
| 242 |
check_deserialization::<S>( |
| 243 |
S( |
| 244 |
OffsetDateTime::from_unix_timestamp_nanos(482_196_050_520_000_000) |
| 245 |
.unwrap() |
| 246 |
.to_offset(UtcOffset::from_hms(0, 0, 0).unwrap()), |
| 247 |
), |
| 248 |
r#""1985-04-12T23:20:50.52Z""#, |
| 249 |
); |
| 250 |
check_deserialization::<S>( |
| 251 |
S(OffsetDateTime::from_unix_timestamp(851_042_397) |
| 252 |
.unwrap() |
| 253 |
.to_offset(UtcOffset::from_hms(-8, 0, 0).unwrap())), |
| 254 |
r#""1996-12-19T16:39:57-08:00""#, |
| 255 |
); |
| 256 |
check_deserialization::<S>( |
| 257 |
S( |
| 258 |
OffsetDateTime::from_unix_timestamp_nanos(-1_041_337_172_130_000_000) |
| 259 |
.unwrap() |
| 260 |
.to_offset(UtcOffset::from_hms(0, 20, 0).unwrap()), |
| 261 |
), |
| 262 |
r#""1937-01-01T12:00:27.87+00:20""#, |
| 263 |
); |
| 264 |
|
| 265 |
check_error_deserialization::<S>( |
| 266 |
r#""Foobar""#, |
| 267 |
expect![[r#"the 'year' component could not be parsed at line 1 column 8"#]], |
| 268 |
); |
| 269 |
check_error_deserialization::<S>( |
| 270 |
r#""2000-AA""#, |
| 271 |
expect!["unexpected trailing characters; the end of input was expected at line 1 column 9"], |
| 272 |
); |
| 273 |
} |