use super::*;
fn provenance(value: &str) -> Provenance {
Provenance::default().push(NodeId::from_raw([9; 16]), "Button", "paint", value)
}
fn check_failure(check: RuntimeCheckSpec, value: &RuntimeValue) -> RuntimeCheckFailure {
check.evaluate(value).expect_err("the check must fail")
}
#[test]
fn coordinates_in_viewport_accepts_inside_and_rejects_overflow_and_space_mismatch() {
let check = RuntimeCheckSpec::CoordinatesInViewport;
let inside = RuntimeValue::Coordinates(Coordinates::new(
10.0,
10.0,
90.0,
40.0,
100.0,
60.0,
"logical",
"logical",
provenance("10,10,90,40"),
));
assert!(
check.evaluate(&inside).is_ok(),
"a rect that fits the viewport passes"
);
let overflow = RuntimeValue::Coordinates(Coordinates::new(
50.0,
10.0,
60.0,
40.0,
100.0,
60.0,
"logical",
"logical",
provenance("50,10,60,40"),
));
let failure = check_failure(check, &overflow);
assert_eq!(failure.check, "coordinates_in_viewport");
assert_eq!(
failure.message,
"rect (50.0, 10.0, 60.0, 40.0) exceeds viewport (100.0, 60.0)"
);
assert_eq!(failure.provenance, provenance("50,10,60,40"));
let mismatch = RuntimeValue::Coordinates(Coordinates::new(
10.0,
10.0,
90.0,
40.0,
100.0,
60.0,
"logical",
"screen",
provenance("10,10,90,40"),
));
let failure = check_failure(check, &mismatch);
assert_eq!(failure.check, "coordinates_in_viewport");
assert_eq!(
failure.message,
"coordinate space `logical` does not match expected `screen`"
);
assert_eq!(failure.provenance, provenance("10,10,90,40"));
}
#[test]
fn finite_number_accepts_finite_and_rejects_nan_and_infinity() {
let check = RuntimeCheckSpec::FiniteNumber;
assert!(
check
.evaluate(&RuntimeValue::number(1.0, provenance("1.0")))
.is_ok(),
"1.0 is finite"
);
let failure = check_failure(check, &RuntimeValue::number(f64::NAN, provenance("NaN")));
assert_eq!(failure.check, "finite_number");
assert_eq!(failure.message, "number `NaN` is NaN or infinite");
assert_eq!(failure.provenance, provenance("NaN"));
let failure = check_failure(
check,
&RuntimeValue::number(f64::INFINITY, provenance("inf")),
);
assert_eq!(failure.check, "finite_number");
assert_eq!(failure.message, "number `inf` is NaN or infinite");
assert_eq!(failure.provenance, provenance("inf"));
}
#[test]
fn number_in_range_is_inclusive_and_reports_inverted_bounds() {
let check = RuntimeCheckSpec::NumberInRange { min: 0, max: 10 };
assert!(
check
.evaluate(&RuntimeValue::number(0.0, provenance("0.0")))
.is_ok(),
"the lower bound is inclusive"
);
assert!(
check
.evaluate(&RuntimeValue::number(10.0, provenance("10.0")))
.is_ok(),
"the upper bound is inclusive"
);
let failure = check_failure(check, &RuntimeValue::number(10.5, provenance("10.5")));
assert_eq!(failure.check, "number_in_range");
assert_eq!(
failure.message,
"number `10.5` is outside inclusive range 0..=10"
);
assert_eq!(failure.provenance, provenance("10.5"));
let inverted = RuntimeCheckSpec::NumberInRange { min: 10, max: 0 };
let failure = check_failure(inverted, &RuntimeValue::number(5.0, provenance("5.0")));
assert_eq!(failure.check, "number_in_range");
assert_eq!(
failure.message,
"invalid check bounds: minimum 10 exceeds maximum 0"
);
assert_eq!(failure.provenance, provenance("5.0"));
}
#[test]
fn non_empty_text_rejects_empty_and_whitespace_only() {
let check = RuntimeCheckSpec::NonEmptyText;
assert!(
check
.evaluate(&RuntimeValue::text("x", provenance("x")))
.is_ok(),
"one visible character is not empty"
);
for blank in ["", " "] {
let failure = check_failure(check, &RuntimeValue::text(blank, provenance(blank)));
assert_eq!(failure.check, "non_empty_text");
assert_eq!(failure.message, "text is empty or whitespace only");
assert_eq!(failure.provenance, provenance(blank));
}
}
#[test]
fn text_length_counts_characters_not_bytes() {
let check = RuntimeCheckSpec::TextLength { min: 1, max: 3 };
assert!(
check
.evaluate(&RuntimeValue::text("abc", provenance("abc")))
.is_ok(),
"three ASCII characters"
);
assert!(
check
.evaluate(&RuntimeValue::text("日本語", provenance("日本語")))
.is_ok(),
"three characters even though they are nine bytes"
);
let failure = check_failure(check, &RuntimeValue::text("abcd", provenance("abcd")));
assert_eq!(failure.check, "text_length");
assert_eq!(
failure.message,
"text length 4 is outside inclusive range 1..=3"
);
assert_eq!(failure.provenance, provenance("abcd"));
}
#[test]
fn number_in_range_refuses_a_bound_it_cannot_state_exactly() {
let beyond = RuntimeCheckSpec::NumberInRange {
min: 9_007_199_254_740_993,
max: 9_007_199_254_740_993,
};
let failure = check_failure(
beyond,
&RuntimeValue::number(9_007_199_254_740_992.0, provenance("9007199254740992")),
);
assert_eq!(failure.check, "number_in_range");
assert!(
failure.message.contains("cannot be represented exactly"),
"{}",
failure.message
);
let exact = RuntimeCheckSpec::NumberInRange {
min: 9_007_199_254_740_992,
max: 9_007_199_254_740_992,
};
assert!(
exact
.evaluate(&RuntimeValue::number(
9_007_199_254_740_992.0,
provenance("x")
))
.is_ok()
);
assert!(
exact
.evaluate(&RuntimeValue::number(
9_007_199_254_740_994.0,
provenance("y")
))
.is_err()
);
}