Skip to content

Commit

Permalink
Test mapping_type and seq_type
Browse files Browse the repository at this point in the history
  • Loading branch information
nineteendo committed Nov 18, 2024
1 parent d6dd9fe commit be92a87
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/jsonyx/test/test_dumps.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,10 @@ def test_seq_types(json: ModuleType, obj: Sequence[object]) -> None:
# Multiple values
({"a": 1, "b": 2, "c": 3}, '{"a": 1, "b": 2, "c": 3}'),
])
def test_mapping(
def test_dict(
json: ModuleType, obj: dict[str, object], expected: str,
) -> None:
"""Test mapping."""
"""Test dict."""
assert json.dumps(obj, end="") == expected


Expand Down
25 changes: 15 additions & 10 deletions src/jsonyx/test/test_loads.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Copyright (C) 2024 Nice Zombies
"""JSON loads tests."""
# TODO(Nice Zombies): test mapping_type
# TODO(Nice Zombies): test seq_type
from __future__ import annotations

__all__: list[str] = []
Expand Down Expand Up @@ -311,9 +309,12 @@ def test_invalid_unicode_escape(
# Multiple values
("[1, 2, 3]", [1, 2, 3]),
])
def test_array(json: ModuleType, s: str, expected: list[object]) -> None:
@pytest.mark.parametrize("seq_type", [list, tuple])
def test_array(
json: ModuleType, s: str, expected: list[object], seq_type: type,
) -> None:
"""Test JSON array."""
assert json.loads(s) == expected
assert json.loads(s, seq_type=seq_type) == seq_type(expected)


@pytest.mark.parametrize(("s", "expected"), [
Expand Down Expand Up @@ -386,20 +387,24 @@ def test_invalid_array(

@pytest.mark.parametrize(("s", "expected"), [
# Empty object
("{}", {}),
("{}", []),
# One value
('{"": 0}', {"": 0}),
('{"": 0}', [("", 0)]),
# Multiple values
('{"a": 1, "b": 2, "c": 3}', {"a": 1, "b": 2, "c": 3}),
('{"a": 1, "b": 2, "c": 3}', [("a", 1), ("b", 2), ("c", 3)]),
# Duplicate keys
('{"a": 1, "a": 2, "a": 3}', {"a": 3}),
('{"a": 1, "a": 2, "a": 3}', [("a", 1), ("a", 2), ("a", 3)]),
])
def test_object(json: ModuleType, s: str, expected: dict[str, object]) -> None:
@pytest.mark.parametrize("mapping_type", [dict, list])
def test_object(
json: ModuleType, s: str, expected: list[tuple[str, object]],
mapping_type: type,
) -> None:
"""Test JSON object."""
assert json.loads(s) == expected
assert json.loads(s, mapping_type=mapping_type) == mapping_type(expected)


@pytest.mark.parametrize(("s", "expected"), [
Expand Down

0 comments on commit be92a87

Please sign in to comment.