Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion crates/engine/tests/never_silent_roundtrip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ fn value(ty: FieldType) -> BoxedStrategy<Value> {
])
.prop_map(|s| Value::Text(s.to_string())),
1 => (1usize..400).prop_map(|n| Value::Text("é".repeat(n))),
// Digits a Double doesn't hold: identifiers past i64, and decimals past 17 digits.
1 => prop::sample::select(vec![
"12345678901234567891", "12345678901234567000", "9007199254740993", "0.12345678901234567891", "-99999999999999999999",
])
.prop_map(|s| Value::Text(s.to_string())),
]
.boxed(),
_ => any::<bool>().prop_map(Value::Bool).boxed(),
Expand Down Expand Up @@ -208,6 +213,24 @@ fn tag(v: &Value) -> &'static str {
}
}

/// Whether the number `v` keeps every digit of the text `t`: a whole number exactly, a decimal at
/// the precision of its digits, as the readers type text (`text_type`).
fn keeps_digits(t: &str, v: &Value) -> bool {
let t = t.trim();
let Value::Float(f) = v else { return true };
let digits = |s: &str| {
let m = s.split(['e', 'E']).next().unwrap_or_default();
m.chars().filter(char::is_ascii_digit).collect::<String>().trim_start_matches('0').trim_end_matches('0').to_string()
};
let whole = t.strip_prefix(['-', '+']).unwrap_or(t);
if whole.bytes().all(|b| b.is_ascii_digit()) {
let d = whole.trim_start_matches('0');
return format!("{:.0}", f.abs()) == if d.is_empty() { "0" } else { d };
}
let want = digits(t);
want.is_empty() || digits(&format!("{f:.*e}", want.len() - 1)) == want
}

/// How a value changed, if it did; the same number in another type isn't a change. The kinds
/// name what a format that holds text does to a value, so [`LIMITS`] can allow just that.
fn value_change(a: &Value, b: &Value) -> Option<String> {
Expand All @@ -232,7 +255,7 @@ fn value_change(a: &Value, b: &Value) -> Option<String> {
Some(match (a, b) {
(Value::Text(x), Value::Null) if x.trim().is_empty() => "blank text became null".into(),
(Value::Text(x), Value::Text(y)) if x.trim() == y.trim() => "text trimmed".into(),
(Value::Text(x), _) if reads_as(x, b) => "text became a number".into(),
(Value::Text(x), _) if reads_as(x, b) && keeps_digits(x, b) => "text became a number".into(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small diagnostic gap, not a correctness problem. When text reads as the same f64 but keeps_digits is false (e.g. 12345678901234567000 read back as Float), the arm falls through and the failure reads as text changed to float, which doesn't say why it failed. Naming that case in its own arm makes the failure actionable for whoever hits it:

Suggested change
(Value::Text(x), _) if reads_as(x, b) && keeps_digits(x, b) => "text became a number".into(),
(Value::Text(x), _) if reads_as(x, b) && keeps_digits(x, b) => "text became a number".into(),
(Value::Text(x), _) if reads_as(x, b) => "text became a number that drops digits".into(),

This is a quality point; it can wait for the release quality pass if you prefer.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left for the release quality pass: the failure already shows the text and the number (Text("…") → Float(…)), which says what was lost; a more specific label is wording.

(Value::Float(x), Value::Text(y))
if !x.is_finite() && y.trim().parse::<f64>().is_ok_and(|m| m == *x || m.is_nan() && x.is_nan()) =>
{
Expand Down
Loading