import pytest
from jq_interpreter.errors import CompileError
from jq_interpreter.parser import Node, Program, parse
def test_parser_accepts_composed_expression_without_evaluation():
tree = parse("(1, 2) | .")
assert isinstance(tree, Program)
assert tree.tree is not None
assert tree.tree.kind == "program"
assert tree.tree.children[0].kind == "pipe"
@pytest.mark.parametrize("source", ["{", ". as $x | $missing", "break $missing"])
def test_parser_rejects_syntax_and_static_scope_errors(source):
with pytest.raises(CompileError):
parse(source)
def test_parser_preserves_precedence_and_generator_nodes():
tree = parse(".foo + 2 * 3").tree
assert tree is not None
expression = tree.children[0]
assert expression.kind == "binary" and expression.value == "+"
assert expression.children[1].value == "*"
def test_parser_validates_labels_and_accepts_scoped_break():
parse("label $done | foreach .[] as $item (.; break $done)")
with pytest.raises(CompileError):
parse("break $done")
def test_parser_validates_function_arity_and_patterns():
parse("def twice($x): $x + $x; twice(2)")
with pytest.raises(CompileError):
parse("def twice($x): $x + $x; twice(1; 2)")
parse(". as [$first, $second] | {$first, $second}")
def test_parser_handles_constructors_interpolation_and_directives():
parse('module {"name": "demo"}; "hello \\(.name)"')
parse('{"value": .foo, "nested": [.bar]}')
def test_parser_accepts_variable_object_keys():
parse('. as $key | {($key): 1}')
parse('. as $key | {$key: 1}')
Run artifact