Files
improvise/tests/web_demo_model.rs
T
Edward Langley d00cea2477 fix: show decimals by default, and let a formula name its own format
improvise-e0ex. `Margin = Profit / Revenue` drew as `1`: `View::new` seeded
`,.0` — zero decimals — so a 0.66 ratio rounded half-away-from-zero. The cause
is the format, not the arithmetic; exact decimal math would not change the
rendering by one character, so nothing here touches the evaluator and gotcha 8
is unchanged.

Two halves, per the user's ruling:

* `View::new` seeds `,.2`. Revenue draws `18,400.00` and Margin `0.66`. The
  noise on whole numbers is the cheaper error — a currency column with two
  redundant zeroes is legible, a ratio with none is wrong — and a format cannot
  be guessed from a value.
* A formula may carry its own format, which wins for its target; a raw measure
  keeps the view's. `formula.pest` gains
  `format_clause = ${ ^"FORMAT" ~ sp+ ~ format_spec }`, with `sp+` for `WHERE`'s
  reason (a predicate is invisible to the grammar-walking generator) and
  `FORMAT` added to `quote::KEYWORDS`.

`improv.pest` is deliberately unchanged: `formula_raw` already carries the
`## Formulas` line verbatim, so the format rides in the formula text with
nothing to add — and the generator, which reads `improv.pest`, sees no new
spelling to violate.

Resolution has one home. `FormatPlan::of(model, view)` replaces the
`(comma, decimals)` pair every render path threaded — `GridLayout::display_text`,
`viewport::grid_body`/`compute_col_widths`, `project::grid_in` and
`persistence/export.rs::render_csv` — rather than being added beside it, which
would be two answers to "what format is this cell". It is asked of the cell's
`CellKey`, so a cell aggregating over `_Measure` keeps the view's format by
construction. `GridBody::col_formats` carries the answer to the totals strip,
which is formatted on its own path.

Persistence is untouched except for metadata: `format_f64` is display,
`persistence/format.rs::format_number` writes round-trip precision, and the two
have no call between them. Newly created views write `format: ,.2`; existing
documents are unaffected, because `format_md` has always written the line
explicitly. `web/demo.improv` moves to `,.2` on both views.

The trap this sets is gotcha 34's: `format_md` writes `Formula::raw`, so a
`FORMAT` suffix round-trips whether or not the walker parsed it. The round-trip
test therefore asserts the parsed field — injecting a walker that drops the
clause fails that one test and nothing else in `improvise-io`, 242 tests
including the whole characterization suite and the grammar properties.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FC2jVn8Au7E9JPkw6bwXXH
2026-09-21 23:33:52 -07:00

84 lines
2.8 KiB
Rust

//! The demo document must be one a visitor can *type into* (improvise-bck),
//! asked as the browser asks it. `context/test-evidence/tui.md`.
use improvise::protocol::CellId;
use improvise_command::ui::app::App;
use improvise_command::ui::project::{Viewport, grid_of};
fn demo_app() -> App {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("web/demo.improv");
let workbook = improvise::persistence::load(&path).expect("web/demo.improv loads");
App::new(workbook, Some(path))
}
/// Every cell the page paints is either one the visitor may change or one a
/// formula owns — never an aggregate. `context/test-evidence/tui.md`.
#[test]
fn every_web_demo_cell_is_settable_except_the_column_a_formula_owns() {
let app = demo_app();
let (rows, cols) = (app.layout.row_count(), app.layout.col_count());
assert!(rows > 0 && cols > 0, "the demo grid must not be empty");
let grid = grid_of(
&app,
Viewport {
row_offset: 0,
col_offset: 0,
rows,
cols,
},
);
for r in 0..rows {
for c in 0..cols {
let cell = grid.cell(r, c).expect("a projected cell at every position");
let expected_derived = grid
.col_frame
.item(c)
.is_some_and(|t| t.as_slice() == ["Profit"]);
let ok = match (&cell.id, expected_derived) {
(Some(CellId::Record(_)), false) => true,
(Some(CellId::Derived { target }), true) => target == "Profit",
_ => false,
};
assert!(
ok,
"cell ({r},{c}) — row {:?}, column {:?} — is {:?}; the two raw columns must be \
Records the page offers, and Profit must be Derived so it does not",
grid.row_frame.item(r),
grid.col_frame.item(c),
cell.id,
);
}
}
}
/// The payoff a visitor should see without pressing a key: a formula column
/// beside the two columns that feed it, so an edit visibly moves it.
#[test]
fn the_web_demo_shows_a_formula_beside_its_inputs() {
let app = demo_app();
let grid = grid_of(
&app,
Viewport {
row_offset: 0,
col_offset: 0,
rows: app.layout.row_count(),
cols: app.layout.col_count(),
},
);
assert_eq!(
grid.col_frame.leaf_labels(),
vec!["Revenue", "Cost", "Profit"]
);
assert_eq!(
grid.row_frame.leaf_labels(),
vec!["Espresso", "Latte", "Cold Brew", "Pastry"]
);
let profit = grid.cell(0, 2).expect("the Profit cell");
assert_eq!(
profit.text, "12,200.00",
"18,400 - 6,200, recomputed on load, under the decimal default"
);
}