use super::test_support::{component_map, EPOCH_REF_MILLIS, EPOCH_REF_SECONDS};
use super::*;
#[test]
fn construct_named_rejects_more_than_two_arguments() {
let value = Value::String("2024-06-27".into());
let pattern = Value::String("yyyy-MM-dd".into());
let extra = Value::String("extra".into());
for name in ["date", "LOCAL_TIME", "zoned_datetime", "duration"] {
let err = construct_named(name, &[value.clone(), pattern.clone(), extra.clone()])
.expect_err("three arguments must be rejected");
assert!(
err.to_string().contains("InvalidNumberOfArguments"),
"{name}: {err}"
);
}
assert!(construct_named("date", &[]).is_ok(), "the clock form");
assert!(construct_named("date", std::slice::from_ref(&value)).is_ok());
assert!(construct_named("date", &[value, pattern]).is_ok());
}
#[test]
fn pattern_form_operand_faults_split_by_side() {
let err = construct_named(
"duration",
&[Value::Int(123), Value::String("s 'seconds'".into())],
)
.expect_err("a non-string value must fail the call");
assert!(matches!(err, Error::ProcedureCall(_)), "{err}");
let err = construct_named("date", &[Value::String("20150721".into()), Value::Int(42)])
.expect_err("a non-string pattern must fail the call");
assert!(matches!(err, Error::InvalidArgumentType(_)), "{err}");
}
#[test]
fn call_signature_faults_report_the_procedure_class() {
let duration = construct_named("duration", &[Value::String("P1D".into())])
.expect("duration('P1D') must construct");
let cases: [(&str, Vec<Value>); 4] = [
("duration", vec![Value::Int(123)]),
("date", vec![Value::Int(123)]),
("localdatetime", vec![Value::Int(123)]),
("date", vec![duration]),
];
for (name, vals) in cases {
let err = construct_named(name, &vals).expect_err("the call must fail");
assert!(matches!(err, Error::ProcedureCall(_)), "{name}: {err}");
}
}
#[test]
fn every_clock_constructor_rejects_null() {
for kind in ClockKind::all() {
assert_eq!(
super::construct::construct(kind, Some(&Value::Null)),
Err(Error::procedure_call(format!(
"Invalid call signature: {}()",
kind.name()
)))
);
}
}
#[test]
fn temporal_map_non_integer_components_are_typed_errors() {
let string = || Value::String("x".into());
let list = || Value::List(vec![Value::Int(1), Value::Int(2)].into());
let cases: Vec<(&str, Result<Temporal, Error>, &str)> = vec![
(
"date month",
construct_date(Some(&component_map(&[
("year", Value::Int(2000)),
("month", string()),
]))),
"cannot assign 'month' of a STRING",
),
(
"date week",
construct_date(Some(&component_map(&[
("year", Value::Int(2000)),
("week", string()),
]))),
"cannot assign 'week' of a STRING",
),
(
"date float year",
construct_date(Some(&component_map(&[("year", Value::Float(2000.0))]))),
"cannot assign 'year' of a FLOAT",
),
(
"localtime minute",
construct_localtime(Some(&component_map(&[
("hour", Value::Int(12)),
("minute", list()),
]))),
"cannot assign 'minute' of a LIST",
),
(
"datetime hour",
construct_datetime(Some(&component_map(&[
("year", Value::Int(2000)),
("month", Value::Int(1)),
("day", Value::Int(1)),
("hour", Value::Bool(true)),
]))),
"cannot assign 'hour' of a BOOLEAN",
),
(
"localdatetime nanosecond",
construct_localdatetime(Some(&component_map(&[
("year", Value::Int(2000)),
("month", Value::Int(1)),
("day", Value::Int(1)),
("hour", Value::Int(12)),
("minute", Value::Int(0)),
("second", Value::Int(0)),
("nanosecond", Value::Bool(true)),
]))),
"cannot assign 'nanosecond' of a BOOLEAN",
),
];
for (case, outcome, expected) in cases {
let err = outcome.expect_err("a non-integer component must be rejected");
assert!(
matches!(err, Error::ArgumentError(_)),
"{case}: the class must be ArgumentError (22G06), got {err:?}"
);
assert!(err.to_string().contains(expected), "{case}: {err}");
}
}
#[test]
fn temporal_map_subsecond_fields_are_range_checked() {
let time = |entries: &[(&str, Value)]| {
let mut all = vec![
("hour", Value::Int(1)),
("minute", Value::Int(1)),
("second", Value::Int(1)),
];
all.extend_from_slice(entries);
construct_time(Some(&component_map(&all)))
};
for entries in [
vec![("millisecond", Value::Int(-1))],
vec![("millisecond", Value::Int(1000))],
vec![
("millisecond", Value::Int(0)),
("microsecond", Value::Int(1000)),
],
vec![
("millisecond", Value::Int(0)),
("nanosecond", Value::Int(1_000_000)),
],
vec![
("microsecond", Value::Int(0)),
("nanosecond", Value::Int(1000)),
],
vec![("microsecond", Value::Int(1_000_000))],
vec![("nanosecond", Value::Int(1_000_000_000))],
] {
assert!(time(&entries).is_err(), "{entries:?}");
}
assert!(time(&[
("millisecond", Value::Int(999)),
("microsecond", Value::Int(999)),
("nanosecond", Value::Int(999)),
])
.is_ok());
}
#[test]
fn temporal_map_quarter_components_are_validated() {
assert!(construct_date(Some(&component_map(&[
("year", Value::Int(2020)),
("quarter", Value::Int(0)),
])))
.is_err());
assert!(construct_date(Some(&component_map(&[
("year", Value::Int(2020)),
("quarter", Value::Int(1)),
("dayOfQuarter", Value::Int(-1)),
])))
.is_err());
}
#[test]
fn temporal_map_null_day_of_quarter_does_not_shift_the_quarter() {
let date = Value::Temporal(parse_iso("date", "2020-09-15").unwrap());
let with_null = construct_date(Some(&component_map(&[
("date", date.clone()),
("quarter", Value::Int(3)),
("dayOfQuarter", Value::Null),
])))
.unwrap();
let absent = construct_date(Some(&component_map(&[
("date", date),
("quarter", Value::Int(3)),
])))
.unwrap();
assert_eq!(with_null, absent);
assert_eq!(with_null.iso_string(), "2020-09-15");
}
#[test]
fn datetime_kinds_need_a_time_bearing_source() {
let date = Value::Temporal(parse_iso("date", "1984-02-11").unwrap());
let time = Value::Temporal(parse_iso("localtime", "12:00").unwrap());
for rejected in [
construct_datetime(Some(&date)),
construct_localdatetime(Some(&date)),
construct_datetime(Some(&time)),
construct_localdatetime(Some(&time)),
] {
let err = rejected.expect_err("a time-incompatible source must be rejected");
assert!(matches!(err, Error::Type(_)), "{err}");
}
let local = Value::Temporal(parse_iso("localdatetime", "1984-02-11T12:00").unwrap());
assert!(construct_datetime(Some(&local)).is_ok());
}
#[test]
fn datetime_from_a_datetime_source_keeps_its_offset() {
let x = parse_iso("datetime", "2025-10-26T02:30:00+01:00[Europe/Stockholm]").unwrap();
let source = || Value::Temporal(x.clone());
assert_eq!(
construct_datetime(Some(&component_map(&[("datetime", source())])))
.unwrap()
.iso_string(),
"2025-10-26T02:30+01:00[Europe/Stockholm]"
);
assert_eq!(
construct_datetime(Some(&component_map(&[
("datetime", source()),
("timezone", Value::String("UTC".into())),
])))
.unwrap()
.iso_string(),
"2025-10-26T01:30Z"
);
assert_eq!(
construct_datetime(Some(&component_map(&[
("datetime", source()),
("year", Value::Int(2024)),
])))
.unwrap()
.iso_string(),
"2024-10-26T02:30+02:00[Europe/Stockholm]"
);
}
#[test]
fn datetime_map_timezone_must_be_a_string() {
assert_eq!(
construct_datetime(Some(&component_map(&[
("year", Value::Int(1984)),
("month", Value::Int(11)),
("day", Value::Int(11)),
("hour", Value::Int(12)),
("timezone", Value::Int(42)),
]))),
Err(Error::argument_error("timezone must be STRING"))
);
let d = construct_datetime(Some(&component_map(&[
("year", Value::Int(1984)),
("month", Value::Int(11)),
("day", Value::Int(11)),
("hour", Value::Int(12)),
])))
.unwrap();
assert_eq!(d.component("timezone"), Ok(Value::String("Z".into())));
assert_eq!(d.component("offset"), Ok(Value::String("Z".into())));
}
#[test]
fn datetime_from_epoch_millis_and_epoch_seconds_map_fields() {
let cases: [(&str, i64, &str); 2] = [
("epochMillis", EPOCH_REF_MILLIS, "1984-11-11T11:31:14.645Z"),
("epochSeconds", EPOCH_REF_SECONDS, "1984-11-11T11:31:14Z"),
];
for (field, value, iso) in cases {
let built =
construct_datetime(Some(&component_map(&[(field, Value::Int(value))]))).unwrap();
assert_eq!(built.iso_string(), iso, "{field}");
assert_eq!(built.component(field), Ok(Value::Int(value)), "{field}");
}
}
#[test]
fn datetime_from_epoch_millis_with_timezone() {
let dt = construct_datetime(Some(&component_map(&[
("epochMillis", Value::Int(EPOCH_REF_MILLIS)),
("timezone", Value::String("Europe/Stockholm".into())),
])))
.unwrap();
assert_eq!(dt.component("offset"), Ok(Value::String("+01:00".into())));
assert_eq!(
dt.component("epochMillis"),
Ok(Value::Int(EPOCH_REF_MILLIS))
);
}
#[test]
fn non_datetime_kinds_reject_epoch_millis() {
let ms = component_map(&[("epochMillis", Value::Int(EPOCH_REF_MILLIS))]);
for rejected in [
construct_date(Some(&ms)),
construct_localdatetime(Some(&ms)),
construct_time(Some(&ms)),
construct_localtime(Some(&ms)),
] {
let err = rejected.expect_err("an epoch map is datetime-only");
assert!(
matches!(err, Error::Type(_)),
"the 22G05 Not-supported rejection, got {err:?}"
);
}
}
#[test]
fn epoch_fields_out_of_range_error() {
let ms = component_map(&[("epochMillis", Value::Int(i64::MAX))]);
let err = construct_datetime(Some(&ms)).expect_err("an out-of-range epoch must fail");
assert!(matches!(err, Error::ArgumentError(_)), "{err}");
}
#[test]
fn epoch_fields_must_be_integers() {
let ms = component_map(&[("epochMillis", Value::Float(469020674.645))]);
let err = construct_datetime(Some(&ms)).expect_err("a non-integer epoch must be rejected");
assert!(
matches!(err, Error::ArgumentError(_)),
"the 22G06 integer rejection, got {err:?}"
);
}