import io
import pytest
from jq_interpreter.command import main
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"),
[
("type", None, "null"),
("type", True, "boolean"),
("type", 1, "number"),
("type", "x", "string"),
("type", [], "array"),
("type", {}, "object"),
("length", None, 0),
("length", -4, 4),
("length", "μ", 1),
("length", [1, 2], 2),
("length", {"a": 1}, 1),
("utf8bytelength", "μ", 2),
("tonumber", " +5.43 ", 5.43),
("toboolean", "false", False),
("tostring", {"a": 1}, '{"a":1}'),
("tojson", "x", '"x"'),
("fromjson", '{"a":1}', {"a": 1}),
("explode", "Aμ", [65, 956]),
("implode", [65, 956], "Aμ"),
],
)
def test_conversion_builtin_returns_expected_value(program, value, expected):
assert result(program, value) == [expected]
def test_implode_replaces_invalid_codepoints_and_rounds_numbers_down():
assert result("implode", [-1, 1.9, 0xD800, 0x10FFFF + 1]) == ["�\x01��"]
@pytest.mark.parametrize(
("program", "value"),
[
("length", True),
("utf8bytelength", 1),
("tonumber", "not-a-number"),
("toboolean", 1),
("fromjson", "{'a': 1}"),
("explode", []),
("implode", ["a"]),
],
)
def test_conversion_builtin_rejects_invalid_input(program, value):
with pytest.raises(RuntimeError):
result(program, value)
def test_command_accepts_jq_special_numeric_input_and_serializes_nan_as_null():
output = io.StringIO()
assert main(["-c", "tojson | fromjson"], io.StringIO('{"a":nan}\n'), output, io.StringIO()) == 0
assert output.getvalue().splitlines() == ['{"a":null}']
Run artifact