Skip to content

Commit

Permalink
Re-order tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nineteendo committed Sep 19, 2024
1 parent addff60 commit e25540a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 65 deletions.
40 changes: 20 additions & 20 deletions src/jsonyx/test/test_loads.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,26 +417,6 @@ def test_object_comments(
assert json.loads(s, allow=COMMENTS) == expected


@pytest.mark.parametrize(("s", "msg", "colno", "end_colno"), [
("{", "Unterminated object", 1, 2),
('{"": 1, "": 2, "": 3}', "Duplicate keys are not allowed", 9, 11),
('{""}', "Expecting colon", 4, -1),
('{"": 0', "Unterminated object", 1, 7),
('{"a": 1"b": 2"c": 3}', "Expecting comma", 8, -1),
('{"a": 1 "b": 2 "c": 3}', "Missing commas are not allowed", 8, -1),
('{"": 0,', "Unterminated object", 1, 8),
('{"": 0,}', "Trailing comma is not allowed", 7, 8),
])
def test_invalid_object(
json: ModuleType, s: str, msg: str, colno: int, end_colno: int,
) -> None:
"""Test invalid JSON object."""
with pytest.raises(json.JSONSyntaxError) as exc_info:
json.loads(s)

check_syntax_err(exc_info, msg, colno, end_colno)


@pytest.mark.parametrize("key", [
# First character
"A", "_", "\u16ee", "\u1885", "\u2118",
Expand Down Expand Up @@ -481,6 +461,26 @@ def test_invalid_unquoted_key(json: ModuleType, key: str) -> None:
check_syntax_err(exc_info, "Expecting key", 2)


@pytest.mark.parametrize(("s", "msg", "colno", "end_colno"), [
("{", "Unterminated object", 1, 2),
('{"": 1, "": 2, "": 3}', "Duplicate keys are not allowed", 9, 11),
('{""}', "Expecting colon", 4, -1),
('{"": 0', "Unterminated object", 1, 7),
('{"a": 1"b": 2"c": 3}', "Expecting comma", 8, -1),
('{"a": 1 "b": 2 "c": 3}', "Missing commas are not allowed", 8, -1),
('{"": 0,', "Unterminated object", 1, 8),
('{"": 0,}', "Trailing comma is not allowed", 7, 8),
])
def test_invalid_object(
json: ModuleType, s: str, msg: str, colno: int, end_colno: int,
) -> None:
"""Test invalid JSON object."""
with pytest.raises(json.JSONSyntaxError) as exc_info:
json.loads(s)

check_syntax_err(exc_info, msg, colno, end_colno)


def test_duplicate_key(json: ModuleType) -> None:
"""Test duplicate key."""
s: str = '{"": 1, "": 2, "": 3}'
Expand Down
90 changes: 45 additions & 45 deletions src/jsonyx/test/test_run_select_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ def test_property(key: str) -> None:
assert run_select_query(([{}], 0), f"$.{key}") == [({}, key)]


@pytest.mark.parametrize("key", [
# First character
"\x00", " ", "!", "$", "0", "\xb2", "\u0300", "\u037a", "\u0488",
# Remaining characters
"A\xb2", "A\u037a", "A\u0488",
])
def test_invalid_property(key: str) -> None:
"""Test invalid property."""
with pytest.raises(JSONSyntaxError) as exc_info:
run_select_query([], f"$.{key}")

check_syntax_err(exc_info, "Expecting property", 3)


@pytest.mark.parametrize("query", ["$.a.b", "$.a[0]"])
def test_list_property(query: str) -> None:
"""Test property on a list."""
Expand All @@ -87,21 +102,6 @@ def test_list_property_mapping(query: str) -> None:
run_select_query(([[]], 0), query, mapping=True)


@pytest.mark.parametrize("key", [
# First character
"\x00", " ", "!", "$", "0", "\xb2", "\u0300", "\u037a", "\u0488",
# Remaining characters
"A\xb2", "A\u037a", "A\u0488",
])
def test_invalid_property(key: str) -> None:
"""Test invalid property."""
with pytest.raises(JSONSyntaxError) as exc_info:
run_select_query([], f"$.{key}")

check_syntax_err(exc_info, "Expecting property", 3)


@pytest.mark.parametrize(("query", "expected"), [
# Slice
("$[:]", _slicer[:]),
Expand Down Expand Up @@ -140,6 +140,28 @@ def test_slice(query: str, expected: slice) -> None:
assert run_select_query(node, query, allow_slice=True) == [([], expected)]


@pytest.mark.parametrize(("query", "msg", "colno"), [
# Start
("$[{big_num}:]", "Start is too big", 3),
("$[{big_num}::]", "Start is too big", 3),
# Stop
("$[:{big_num}]", "Stop is too big", 4),
("$[:{big_num}:]", "Stop is too big", 4),
# Step
("$[::{big_num}]", "Step is too big", 5),
])
def test_too_big_slice_idx(
big_num: str, query: str, msg: str, colno: int,
) -> None:
"""Test too big slice index."""
with pytest.raises(JSONSyntaxError) as exc_info:
run_select_query([], query.format(big_num=big_num))

check_syntax_err(exc_info, msg, colno, colno + len(big_num))


@pytest.mark.parametrize("query", [
# At the end
"$[:]",
Expand All @@ -166,28 +188,6 @@ def test_dict_slice(query: str) -> None:
run_select_query(([{}], 0), query)


@pytest.mark.parametrize(("query", "msg", "colno"), [
# Start
("$[{big_num}:]", "Start is too big", 3),
("$[{big_num}::]", "Start is too big", 3),
# Stop
("$[:{big_num}]", "Stop is too big", 4),
("$[:{big_num}:]", "Stop is too big", 4),
# Step
("$[::{big_num}]", "Step is too big", 5),
])
def test_too_big_slice_idx(
big_num: str, query: str, msg: str, colno: int,
) -> None:
"""Test too big slice index."""
with pytest.raises(JSONSyntaxError) as exc_info:
run_select_query([], query.format(big_num=big_num))

check_syntax_err(exc_info, msg, colno, colno + len(big_num))


@pytest.mark.parametrize("num", [
# Sign
"-1",
Expand All @@ -200,6 +200,14 @@ def test_idx(num: str) -> None:
assert run_select_query(([[]], 0), f"$[{num}]") == [([], int(num))]


def test_too_big_idx(big_num: str) -> None:
"""Test too big index."""
with pytest.raises(JSONSyntaxError) as exc_info:
run_select_query([], f"$[{big_num}]")

check_syntax_err(exc_info, "Index is too big", 3, 3 + len(big_num))


@pytest.mark.parametrize("query", [
# At the end
"$[0]",
Expand All @@ -213,14 +221,6 @@ def test_dict_idx(query: str) -> None:
run_select_query(([{}], 0), query)


def test_too_big_idx(big_num: str) -> None:
"""Test too big index."""
with pytest.raises(JSONSyntaxError) as exc_info:
run_select_query([], f"$[{big_num}]")

check_syntax_err(exc_info, "Index is too big", 3, 3 + len(big_num))


@pytest.mark.parametrize(("obj", "query", "keys"), [
([1, 2, 3], "$[@]", [0, 1, 2]),
({"a": 1, "b": 2, "c": 3}, "$[@]", ["a", "b", "c"]),
Expand Down

0 comments on commit e25540a

Please sign in to comment.