Files
workbook/tests/grid_test.zi

98 lines
4.6 KiB
Ziran

// The embeddable grid: real workbook cells and formulas, column formats,
// column positions and scrolling, without a window.
#load "test_support.zi";
using Grid :: #import "grid";
Engine :: #import "engine";
#program_export
main :: () -> s32 {
micros, ok := Grid.GridMicros("-12.3456785")
Check(ok && micros == -12345679, "decimal text in millionths, rounded")
unused_value, bad := Grid.GridMicros("1e5")
Check(!bad && unused_value == 0, "exponents are not numbers")
Check(Grid.GridCountText(4795296906000000) == "4,795,296,906" && Grid.GridCountText(-1000000000) == "-1,000", "counts with separators")
Check(Grid.GridDollars(1475751234) == "$1,475.75" && Grid.GridDollars(-3204999) == "-$3.20" && Grid.GridDollars(5000) == "$0.01", "money rounds to cents")
Check(Grid.GridColumnName(0) == "A" && Grid.GridColumnName(25) == "Z" && Grid.GridColumnName(27) == "AB", "column letters")
book := Engine.WorkbookNew()
defer { Engine.WorkbookFree(book) }
first := Grid.GridAddSheet(book, "Subscription")
second := Grid.GridAddSheet(book, "API comparison")
Check(first == 0 && second == 1 && Grid.GridSheetName(book, 1) == "API comparison", "named sheets")
sources: [4][3]string = .{.{"Folder", "Tokens", "Cost"}, .{"aurea", "1250000", "12.345"}, .{"max", "734", "0.5"}, .{"Total", "=SUM(B2:B3)", "=SUM(C2:C3)"}}
for row: 0..3 { for column: 0..2 { Check(Grid.GridSet(book, first, cast(s32)row, cast(s32)column, sources[row][column]), "cell set") } }
Engine.WorkbookEvaluate(book)
view: Grid.GridView
view.formats[1] = Grid.GridCount
view.formats[2] = Grid.GridMoney
view.widths[0] = 180.0
out0: [256]u8
out1: [256]u8
out2: [256]u8
out3: [256]u8
out4: [256]u8
text, number := Grid.GridCellText(book, *view, 3, 1, out0[:])
Check(number && text == "1,250,734", "a SUM total in the count format")
money, priced := Grid.GridCellText(book, *view, 3, 2, out1[:])
Check(priced && money == "$12.85", "a SUM total in the money format")
label, numeric := Grid.GridCellText(book, *view, 1, 0, out2[:])
Check(!numeric && label == "aurea", "text stays text")
header, counted := Grid.GridCellText(book, *view, 0, 1, out3[:])
Check(!counted && header == "Tokens", "headers are text in a number column")
empty, blank := Grid.GridCellText(book, *view, 9, 9, out4[:])
Check(!blank && empty == "", "empty cells show nothing")
Check(Grid.GridWidth(*view, 0) == 180.0 && Grid.GridWidth(*view, 1) == Grid.GridColumnWidth, "column widths")
Check(Grid.GridColumnX(*view, 10.0, 2) == 10.0 + Grid.GridRowHeaderWidth + 180.0 + Grid.GridColumnWidth, "columns follow their widths")
view.visible_rows = 2
view.scroll_row = 9
view.sheet = 5
Grid.GridClamp(book, *view)
Check(view.sheet == 1, "sheet index stays in the workbook")
view.sheet = 0
view.scroll_row = 9
Grid.GridClamp(book, *view)
Check(view.scroll_row == 2, "scrolling stops at a full last screen")
// The wheel: three rows a notch, one for a small touchpad step, a column
// with shift, and nothing outside the grid.
tall := Grid.GridAddSheet(book, "Sessions")
for row: 0..39 { Check(Grid.GridSet(book, tall, cast(s32)row, 0, "x"), "tall sheet") }
Check(Grid.GridSet(book, tall, 0, 4, "wide"), "wide sheet")
view.sheet = tall
view.scroll_row = 0
view.visible_rows = 10
area := Rectangle.{0.0, 0.0, 400.0, 300.0}
input: Grid.GridInput
input.x = 20.0
input.y = 20.0
input.wheel = -1.0
Grid.GridScroll(book, *view, input, area)
Check(view.scroll_row == Grid.GridWheelRows, "a notch down scrolls three rows")
input.wheel = 0.2
Grid.GridScroll(book, *view, input, area)
Check(view.scroll_row == Grid.GridWheelRows - 1, "a small step up scrolls one row")
input.wheel = -100.0
Grid.GridScroll(book, *view, input, area)
Check(view.scroll_row == 30, "the wheel stops at a full last screen")
input.shift = true
input.wheel = -1.0
Grid.GridScroll(book, *view, input, area)
Check(view.scroll_column == 1 && view.scroll_row == 30, "shift scrolls one column")
input.shift = false
input.x = 500.0
input.wheel = 1.0
Grid.GridScroll(book, *view, input, area)
Check(view.scroll_row == 30, "the wheel outside the grid does nothing")
// An ISO date stays the text it was given.
Check(Grid.GridSet(book, tall, 0, 1, "2026-11-03"), "date cell")
Engine.WorkbookEvaluate(book)
out5: [256]u8
day, dated := Grid.GridCellText(book, *view, 0, 1, out5[:])
Check(day == "2026-11-03", "dates show as YYYY-MM-DD")
unused dated
WriteText(1, "grid: ok\n")
return 0
}