import pytest
from jq_interpreter.errors import RuntimeError
from jq_interpreter.evaluator import evaluate
from jq_interpreter.parser import parse
def results(program, value):
return list(evaluate(parse(program), value))
def test_setpath_creates_missing_objects_and_arrays_without_mutating_input():
original = {"kept": True}
assert results('setpath(["foo", 1]; 7)', original) == [
{"kept": True, "foo": [None, 7]}
]
assert original == {"kept": True}
def test_delpaths_deletes_multiple_array_locations_from_right_to_left():
assert results("delpaths([[1], [3]])", [0, 1, 2, 3, 4]) == [[0, 2, 4]]
def test_del_derives_paths_and_missing_paths_are_idempotent():
value = {"a": 1, "b": 2}
assert results("del(.a)", value) == [{"b": 2}]
assert results("del(.missing)", value) == [value]
def test_plain_assignment_uses_original_input_for_each_rhs_result():
assert results(".a = (.b, .b + 1)", {"a": 0, "b": 2}) == [
{"a": 2, "b": 2}, {"a": 3, "b": 2}
]
def test_update_assignment_uses_first_result_and_deletes_on_empty():
assert results(".a |= (.+1, .+2)", {"a": 2}) == [{"a": 3}]
assert results(".a |= empty", {"a": 2}) == [{}]
def test_slice_assignment_replaces_the_selected_range():
assert results('.[1:3] = ["x"]', [0, 1, 2, 3]) == [[0, "x", 3]]
def test_invalid_path_from_single_result_function_reports_value_not_stream():
assert results("try (def x: reverse; x=10) catch .", [0, 1, 2]) == [
"Invalid path expression with result [2,1,0]"
]
@pytest.mark.parametrize("program", ["setpath(0; 1)", "delpaths(0)"])
def test_path_mutation_rejects_non_array_paths(program):
with pytest.raises(RuntimeError):
results(program, {})
Run artifact