import jsonxox
import io


def test_jsonxox_loads_with_trailing_commas_indented(read: callable):
    """Text the jsonxox.loads() method."""

    contents: str = read("examples/trailing-commas-indented.jsonx")

    assert isinstance(contents, str)

    data = jsonxox.loads(contents)

    assert isinstance(data, dict)

    assert "a" in data
    assert data["a"] == 123

    assert "b" in data
    assert data["b"] == "456"

    assert "c" in data
    assert data["c"] == 789.10


def test_jsonxox_loads_with_trailing_commas_compacted(read: callable):
    """Text the jsonxox.loads() method."""

    contents: str = read("examples/trailing-commas-compacted.jsonx")

    assert isinstance(contents, str)

    data = jsonxox.loads(contents)

    assert isinstance(data, dict)

    assert "a" in data
    assert data["a"] == 123

    assert "b" in data
    assert data["b"] == "456"

    assert "c" in data
    assert data["c"] == 789.10


def test_jsonxox_loads_with_single_line_comments_indented(read: callable):
    """Text the jsonxox.loads() method."""

    contents: str = read("examples/single-line-comments-indented.jsonx")

    assert isinstance(contents, str)

    data = jsonxox.loads(contents)

    assert isinstance(data, dict)

    assert "a" in data
    assert data["a"] == 123

    assert "b" in data
    assert data["b"] == "456"

    assert "c" in data
    assert data["c"] == 789.10


def test_jsonxox_loads_with_single_line_comments_compacted(read: callable):
    """Text the jsonxox.loads() method."""

    contents: str = read("examples/single-line-comments-compacted.jsonx")

    assert isinstance(contents, str)

    data = jsonxox.loads(contents)

    assert isinstance(data, dict)

    assert "a" in data
    assert data["a"] == 123

    assert "b" in data
    assert data["b"] == "456"

    assert "c" in data
    assert data["c"] == 789.10


def test_jsonxox_loads_with_multi_line_comments_indented(read: callable):
    """Text the jsonxox.loads() method."""

    contents: str = read("examples/multi-line-comments-indented.jsonx")

    assert isinstance(contents, str)

    data = jsonxox.loads(contents)

    assert isinstance(data, dict)

    assert "a" in data
    assert data["a"] == 123

    assert "b" in data
    assert data["b"] == "456"

    assert "c" in data
    assert data["c"] == 789.10


def test_jsonxox_loads_with_multi_line_comments_compacted(read: callable):
    """Text the jsonxox.loads() method."""

    contents: str = read("examples/multi-line-comments-compacted.jsonx")

    assert isinstance(contents, str)

    data = jsonxox.loads(contents)

    assert isinstance(data, dict)

    assert "a" in data
    assert data["a"] == 123

    assert "b" in data
    assert data["b"] == "456"

    assert "c" in data
    assert data["c"] == 789.10


def test_jsonxox_load_with_all_combined_indented(path: callable):
    """Text the jsonxox.load() method."""

    filepath: str = path("examples/all-combined-indented.jsonx")

    assert isinstance(filepath, str)

    with open(filepath, "r") as handle:
        data = jsonxox.load(handle)

        assert isinstance(data, dict)

        assert "a" in data
        assert data["a"] == 123

        assert "b" in data
        assert data["b"] == "456"

        assert "c" in data
        assert data["c"] == 789.10


def test_jsonxox_load_with_all_combined_compacted(path: callable):
    """Text the jsonxox.load() method."""

    filepath: str = path("examples/all-combined-compacted.jsonx")

    assert isinstance(filepath, str)

    with open(filepath, "r") as handle:
        data = jsonxox.load(handle)

        assert isinstance(data, dict)

        assert "a" in data
        assert data["a"] == 123

        assert "b" in data
        assert data["b"] == "456"

        assert "c" in data
        assert data["c"] == 789.10


def test_jsonxox_loads_with_all_combined_indented(read: callable):
    """Text the jsonxox.loads() method."""

    contents: str = read("examples/all-combined-indented.jsonx")

    assert isinstance(contents, str)

    data = jsonxox.loads(contents)

    assert isinstance(data, dict)

    assert "a" in data
    assert data["a"] == 123

    assert "b" in data
    assert data["b"] == "456"

    assert "c" in data
    assert data["c"] == 789.10


def test_jsonxox_loads_with_all_combined_compacted(read: callable):
    """Text the jsonxox.loads() method."""

    contents: str = read("examples/all-combined-compacted.jsonx")

    assert isinstance(contents, str)

    data = jsonxox.loads(contents)

    assert isinstance(data, dict)

    assert "a" in data
    assert data["a"] == 123

    assert "b" in data
    assert data["b"] == "456"

    assert "c" in data
    assert data["c"] == 789.10


def test_jsonxox_dump(path: callable, read: callable):
    """Text the jsonxox.dump() method."""

    data = {
        "a": 123,
        "b": "456",
        "c": 789.10,
    }

    assert isinstance(data, dict)

    assert "a" in data
    assert data["a"] == 123

    assert "b" in data
    assert data["b"] == "456"

    assert "c" in data
    assert data["c"] == 789.10

    with io.StringIO() as handle:
        jsonxox.dump(data, handle, indent=2, ensure_ascii=False)

        handle.seek(0)

        dumped: str = handle.read()

        assert isinstance(dumped, str)

        contents: str = read("examples/dumped.json")

        assert isinstance(contents, str)

        assert dumped == contents


def test_jsonxox_dumps(path: callable, read: callable):
    """Text the jsonxox.dumps() method."""

    data = {
        "a": 123,
        "b": "456",
        "c": 789.10,
    }

    assert isinstance(data, dict)

    assert "a" in data
    assert data["a"] == 123

    assert "b" in data
    assert data["b"] == "456"

    assert "c" in data
    assert data["c"] == 789.10

    dumped: str = jsonxox.dumps(data, indent=2, ensure_ascii=False)

    assert isinstance(dumped, str)

    contents: str = read("examples/dumped.json")

    assert isinstance(contents, str)

    assert dumped == contents
