import io
import pytest
from jq_interpreter.errors import CompileError, RuntimeError as JqRuntimeError
from jq_interpreter.evaluator import Environment, evaluate
from jq_interpreter.lexer import Token, tokenize
from jq_interpreter.parser import Program, parse
def test_foundation_errors_are_distinct_exception_types():
assert issubclass(CompileError, Exception)
assert issubclass(JqRuntimeError, Exception)
assert CompileError is not JqRuntimeError
assert not issubclass(CompileError, JqRuntimeError)
assert not issubclass(JqRuntimeError, CompileError)
def test_tokenize_preserves_order_and_locations():
assert list(tokenize("a\n b")) == [
Token("IDENT", "a", 1, 1),
Token("IDENT", "b", 2, 2),
]
def test_parse_returns_program_ast():
ast = parse(".")
assert isinstance(ast, Program)
assert ast.source == "."
assert [token.value for token in ast.tokens] == ["."]
def test_parse_rejects_non_text_source():
with pytest.raises(CompileError):
parse(None) # type: ignore[arg-type]
def test_environment_is_lexically_scoped_without_mutating_parent():
parent = Environment({"outer": 1, "shared": "parent"})
child = parent.child(shared="child", inner=2)
assert parent.get("shared") == "parent"
assert child.get("outer") == 1
assert child.get("shared") == "child"
assert child.get("inner") == 2
def test_evaluate_returns_lazy_identity_stream():
results = evaluate(parse("."), {"ok": True})
assert iter(results) is results
assert list(results) == [{"ok": True}]
def test_command_boundary_is_importable():
from jq_interpreter.command import main
output = io.StringIO()
assert main(["-c", "."], io.StringIO("1\n"), output, io.StringIO()) == 0
assert output.getvalue() == "1\n"
Run artifact