import pytest
from jq_interpreter.errors import RuntimeError
from jq_interpreter.evaluator import evaluate
from jq_interpreter.parser import parse
def result(program, value):
return list(evaluate(parse(program), value))
@pytest.mark.parametrize("program,value,expected", [
("keys", {"z": 1, "a": 2}, [["a", "z"]]),
("keys", ["a", "b"], [[0, 1]]),
("map(has(1))", [[0], [0, 1]], [[False, True]]),
("to_entries", {"a": 1}, [[{"key": "a", "value": 1}]]),
("from_entries", [{"Key": "a", "Value": 1}], [{"a": 1}]),
("map_values(.+1)", [1, 2], [[2, 3]]),
("map_values(if . == 1 then empty else . end)", [1, 2], [[2]]),
("sort", [3, None, 1], [[None, 1, 3]]),
("unique", [2, 1, 2, 1], [[1, 2]]),
("flatten(1)", [0, [1, [2]]], [[0, 1, [2]]]),
("transpose", [[1], [2, 3]], [[[1, 2], [None, 3]]]),
("contains({a: {b: 1}})", {"a": {"b": 1, "c": 2}}, [True]),
("add", [1, 2, 3], [6]),
("any(. > 2)", [1, 3], [True]),
("all(. > 0)", [1, 3], [True]),
("bsearch(2)", [1, 3], [-2]),
])
def test_collection_builtin_behavior(program, value, expected):
assert result(program, value) == expected
def test_collection_builtins_reject_non_array_sort_input():
with pytest.raises(RuntimeError):
result("sort", {"a": 1})
Run artifact