✨ feat(table): add set_align and test for table alignment#1290
Conversation
- Add set_align function to table.mq for setting table alignment - Add test_table_set_align and test case to module_tests.mq - Support to_string for Symbol in builtin.rs Closes #1188
There was a problem hiding this comment.
Pull request overview
This PR adds support for setting table column alignments in the mq language. It introduces a new set_align function to the table module, adds Symbol-to-string conversion support for the builtin to_string function, and includes tests for the new functionality.
Changes:
- Added
to_stringsupport for Symbol type in builtin.rs to enable converting symbols (like:left,:center,:right) to their string representations - Implemented
set_alignfunction in table.mq module to set column alignments using an array of alignment symbols - Added test case for the new
set_alignfunction in module_tests.mq
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| crates/mq-lang/src/eval/builtin.rs | Added Symbol case to TO_STRING builtin to convert symbols to strings without the colon prefix |
| crates/mq-lang/src/eval.rs | Added test case verifying Symbol to_string conversion returns a plain string |
| crates/mq-lang/modules/table.mq | Added set_align function to set table column alignments from an array of alignment symbols |
| crates/mq-lang/modules/module_tests.mq | Added test for set_align function and registered it in the test suite |
|
|
||
| # Set the alignment for a table. | ||
| def set_align(table, align): | ||
| set(table, :align, set_attr(table[:align], "align", join(map(align, to_string), ","))) |
There was a problem hiding this comment.
The set_align function should validate that the length of the align array matches the length of table[:header], similar to how add_row validates row length and add_column validates column length. This prevents setting invalid alignment configurations that don't match the table structure.
For example, you could add validation like:
if (len(align) != len(table[:header])):
error("Alignment length does not match table header length.")
else:
# existing implementation
| set(table, :align, set_attr(table[:align], "align", join(map(align, to_string), ","))) | |
| if (len(align) != len(table[:header])): | |
| error("Alignment length does not match table header length.") | |
| else: | |
| set(table, :align, set_attr(table[:align], "align", join(map(align, to_string), ","))) | |
| end |
| | let t1 = table::set_align(t1, [:left, :center, :right]) | ||
| | assert_eq(to_string(t1[:align]), "|:---|:---:|---:|") | ||
| end | ||
|
|
There was a problem hiding this comment.
The test should verify that the alignment length matches the table header length (3 columns). Add a test case that attempts to set an alignment array with incorrect length to ensure the validation works correctly (once validation is added to the set_align function).
| def test_table_set_align_invalid_length(): | |
| let t1 = do table::tables(table_nodes) | first(); | |
| | let t1 = table::set_align(t1, [:left, :center]) | |
| | assert_eq(len(t1[:align]), 3) | |
| end |
Closes #1188