-
Notifications
You must be signed in to change notification settings - Fork 33
test: property-check table formatter alignment under unicode content #595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+125
−0
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
82ff3b0
test: property-check table formatter alignment under unicode content
claude 4ea2124
test: cover right-aligned overflow truncation too
claude c93fbbc
test: validate overflow prefix on the left channel; generate default …
claude 148b38a
Merge branch 'main' into claude/audit-76-table-formatter-property
ndycode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import * as fc from "fast-check"; | ||
| import { | ||
| buildTable, | ||
| buildTableRow, | ||
| type TableColumn, | ||
| type TableOptions, | ||
| } from "../../lib/table-formatter.js"; | ||
| import { displayWidth } from "../../lib/ui/display-width.js"; | ||
|
|
||
| // Mix single-column ASCII with double-column CJK and emoji so padding and | ||
| // truncation are exercised in display columns, not UTF-16 code units (ui-02). | ||
| const arbCellText = fc | ||
| .array(fc.constantFrom("a", "B", "7", " ", "-", "é", "漢", "字", "🚀", "⚡"), { | ||
| minLength: 0, | ||
| maxLength: 12, | ||
| }) | ||
| .map((chars) => chars.join("")); | ||
|
|
||
| const arbColumn: fc.Arbitrary<TableColumn> = fc.record({ | ||
| header: arbCellText, | ||
| width: fc.integer({ min: 0, max: 10 }), | ||
| align: fc.constantFrom<"left" | "right">("left", "right"), | ||
| }); | ||
|
|
||
| const arbOptions: fc.Arbitrary<TableOptions> = fc | ||
| .array(arbColumn, { minLength: 1, maxLength: 5 }) | ||
| .map((columns) => ({ columns })); | ||
|
|
||
| function expectedLineWidth(options: TableOptions): number { | ||
| const widths = options.columns.map((column) => Math.max(0, column.width)); | ||
| return widths.reduce((sum, width) => sum + width, 0) + (options.columns.length - 1); | ||
| } | ||
|
|
||
| describe("table formatter property invariants", () => { | ||
| it("every line of any table has the exact same display width as the layout", () => { | ||
| fc.assert( | ||
| fc.property( | ||
| arbOptions, | ||
| fc.array(fc.array(arbCellText, { minLength: 0, maxLength: 6 }), { | ||
| minLength: 0, | ||
| maxLength: 8, | ||
| }), | ||
| (options, rows) => { | ||
| const lines = buildTable(rows, options); | ||
| expect(lines).toHaveLength(rows.length + 2); | ||
| const layoutWidth = expectedLineWidth(options); | ||
| for (const line of lines) { | ||
| // Header, separator, and every data row stay in lockstep no | ||
| // matter what content (incl. CJK/emoji, missing cells, or | ||
| // extra cells beyond the column count) lands in the rows. | ||
| expect(displayWidth(line)).toBe(layoutWidth); | ||
| } | ||
| }, | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| it("content that fits is preserved verbatim with padding on the declared side", () => { | ||
| fc.assert( | ||
| fc.property( | ||
| arbCellText, | ||
| fc.integer({ min: 1, max: 14 }), | ||
| fc.constantFrom<"left" | "right">("left", "right"), | ||
| (value, width, align) => { | ||
| fc.pre(displayWidth(value) <= width); | ||
| const row = buildTableRow([value], { | ||
| columns: [{ header: "h", width, align }], | ||
| }); | ||
| const pad = " ".repeat(width - displayWidth(value)); | ||
| expect(row).toBe(align === "right" ? pad + value : value + pad); | ||
| }, | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| it("content that overflows is truncated to a prefix plus ellipsis, never overflowing", () => { | ||
| fc.assert( | ||
| fc.property( | ||
| arbCellText, | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| fc.integer({ min: 1, max: 6 }), | ||
| fc.constantFrom<"left" | "right">("left", "right"), | ||
| (value, width, align) => { | ||
| fc.pre(displayWidth(value) > width); | ||
| const row = buildTableRow([value], { | ||
| columns: [{ header: "h", width, align }], | ||
| }); | ||
| expect(displayWidth(row)).toBe(width); | ||
| // Truncation happens before alignment padding, so stripping the | ||
| // padding side must reveal a prefix of the original value | ||
| // followed by the ellipsis — for either alignment. | ||
| const visible = | ||
| align === "left" ? row.replace(/ +$/, "") : row.replace(/^ +/, ""); | ||
| expect(visible.endsWith("…")).toBe(true); | ||
| expect(value.startsWith(visible.slice(0, -1))).toBe(true); | ||
| expect(displayWidth(visible)).toBeLessThanOrEqual(width); | ||
| }, | ||
| ), | ||
| ); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| it("zero-width columns render empty and never leak an ellipsis into the layout", () => { | ||
| fc.assert( | ||
| fc.property(arbCellText, (value) => { | ||
| const row = buildTableRow([value], { | ||
| columns: [{ header: "h", width: 0 }], | ||
| }); | ||
| expect(row).toBe(""); | ||
| }), | ||
| ); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.