Added "table" and "tables" in the DSL

This commit is contained in:
2026-02-07 22:48:51 +01:00
parent 08c8c00e28
commit 6160e91665
18 changed files with 717 additions and 54 deletions

View File

@@ -142,6 +142,49 @@ def test_i_can_detect_scope_with_multiple_declarations():
assert scope.column_name == "amount"
def test_i_can_detect_table_scope():
"""Test detection of table scope."""
text = 'table "products":\n style()'
scope = detect_scope(text, current_line=1)
assert scope.scope_type == "table"
assert scope.table_name == "products"
def test_i_can_detect_table_scope_with_spaces():
"""Test detection of table scope with spaces in name."""
text = 'table "financial report":\n format()'
scope = detect_scope(text, current_line=1)
assert scope.scope_type == "table"
assert scope.table_name == "financial report"
def test_i_can_detect_tables_scope():
"""Test detection of global tables scope."""
text = "tables:\n style()"
scope = detect_scope(text, current_line=1)
assert scope.scope_type == "tables"
assert scope.table_name is None
@pytest.mark.parametrize("scope_def,expected_type,expected_attrs", [
('column amount:\n style()', "column", {"column_name": "amount"}),
('row 5:\n style()', "row", {"row_index": 5}),
('cell (amount, 3):\n style()', "cell", {"column_name": "amount", "row_index": 3}),
('table "products":\n style()', "table", {"table_name": "products"}),
('tables:\n style()', "tables", {}),
])
def test_i_can_detect_all_scope_types(scope_def, expected_type, expected_attrs):
"""Test detection of all 5 scope types."""
scope = detect_scope(scope_def, current_line=1)
assert scope.scope_type == expected_type
for attr, value in expected_attrs.items():
assert getattr(scope, attr) == value
# =============================================================================
# Context Detection - Scope Contexts
# =============================================================================
@@ -227,6 +270,42 @@ def test_context_cell_row_after_comma_quoted():
assert context == Context.CELL_ROW
def test_context_table_name_after_table():
"""Test TABLE_NAME context after 'table '."""
text = "table "
cursor = Position(line=0, ch=6)
scope = DetectedScope()
context = detect_context(text, cursor, scope)
assert context == Context.TABLE_NAME
def test_context_tables_scope_after_tables():
"""Test TABLES_SCOPE context after 'tables'."""
text = "tables"
cursor = Position(line=0, ch=6)
scope = DetectedScope()
context = detect_context(text, cursor, scope)
assert context == Context.TABLES_SCOPE
@pytest.mark.parametrize("text,cursor_ch,expected_context", [
("column ", 7, Context.COLUMN_NAME),
("row ", 4, Context.ROW_INDEX),
("cell ", 5, Context.CELL_START),
("table ", 6, Context.TABLE_NAME),
("tables", 6, Context.TABLES_SCOPE),
])
def test_i_can_detect_all_scope_contexts(text, cursor_ch, expected_context):
"""Test detection of all scope-related contexts."""
cursor = Position(line=0, ch=cursor_ch)
scope = DetectedScope()
context = detect_context(text, cursor, scope)
assert context == expected_context
# =============================================================================
# Context Detection - Rule Contexts
# =============================================================================
@@ -553,6 +632,32 @@ def test_suggestions_scope_keyword(provider):
assert "column" in labels
assert "row" in labels
assert "cell" in labels
assert "table" in labels
assert "tables" in labels
def test_suggestions_table_name(provider):
"""Test suggestions for TABLE_NAME context."""
engine = FormattingCompletionEngine(provider, "app.orders")
scope = DetectedScope()
suggestions = engine.get_suggestions(Context.TABLE_NAME, scope, "")
labels = [s.label for s in suggestions]
# Should suggest the current table name in quotes
assert '"app.orders"' in labels
def test_suggestions_tables_scope(provider):
"""Test suggestions for TABLES_SCOPE context."""
engine = FormattingCompletionEngine(provider, "app.orders")
scope = DetectedScope()
suggestions = engine.get_suggestions(Context.TABLES_SCOPE, scope, "")
labels = [s.label for s in suggestions]
# Should suggest colon to complete the scope
assert ":" in labels
def test_suggestions_style_preset(provider):

View File

@@ -17,6 +17,8 @@ from myfasthtml.core.formatting.dsl import (
ColumnScope,
RowScope,
CellScope,
TableScope,
TablesScope,
DSLSyntaxError,
)
@@ -125,6 +127,62 @@ cell tcell_grid1-3-2:
assert rules[0].scope.row is None
class TestTableScope:
"""Tests for table scope parsing."""
def test_i_can_parse_table_scope(self):
"""Test parsing a table scope."""
dsl = """
table "products":
style("neutral")
"""
rules = parse_dsl(dsl)
assert len(rules) == 1
assert isinstance(rules[0].scope, TableScope)
assert rules[0].scope.table == "products"
def test_i_can_parse_table_scope_with_spaces(self):
"""Test parsing a table scope with spaces in name."""
dsl = """
table "financial report":
style("info")
"""
rules = parse_dsl(dsl)
assert len(rules) == 1
assert isinstance(rules[0].scope, TableScope)
assert rules[0].scope.table == "financial report"
class TestTablesScope:
"""Tests for tables scope (global) parsing."""
def test_i_can_parse_tables_scope(self):
"""Test parsing the global tables scope."""
dsl = """
tables:
style("neutral")
"""
rules = parse_dsl(dsl)
assert len(rules) == 1
assert isinstance(rules[0].scope, TablesScope)
def test_i_can_parse_tables_scope_with_multiple_rules(self):
"""Test parsing tables scope with multiple rules."""
dsl = """
tables:
style("neutral")
format.number(precision=2)
"""
rules = parse_dsl(dsl)
assert len(rules) == 2
assert isinstance(rules[0].scope, TablesScope)
assert isinstance(rules[1].scope, TablesScope)
# =============================================================================
# Style Tests
# =============================================================================
@@ -496,7 +554,49 @@ row 0:
# Third rule: row 0
assert isinstance(rules[2].scope, RowScope)
assert rules[2].scope.row == 0
def test_i_can_parse_all_scope_types(self):
"""Test parsing all 5 scope types together."""
dsl = """
tables:
style(font_size="14px")
table "products":
format.number(precision=2)
column amount:
format("EUR")
row 0:
style("neutral", bold=True)
cell (amount, 5):
style("highlight")
"""
rules = parse_dsl(dsl)
assert len(rules) == 5
# First rule: tables (global)
assert isinstance(rules[0].scope, TablesScope)
# Second rule: table "products"
assert isinstance(rules[1].scope, TableScope)
assert rules[1].scope.table == "products"
# Third rule: column amount
assert isinstance(rules[2].scope, ColumnScope)
assert rules[2].scope.column == "amount"
# Fourth rule: row 0
assert isinstance(rules[3].scope, RowScope)
assert rules[3].scope.row == 0
# Fifth rule: cell (amount, 5)
assert isinstance(rules[4].scope, CellScope)
assert rules[4].scope.column == "amount"
assert rules[4].scope.row == 5
def test_i_can_parse_style_and_format_combined(self):
"""Test parsing style and format on same line."""
dsl = """