Skip to main content

tests/test_decoder.py

1"""
2Tests for `javascript_data_files.decoder`.
3"""
5import pytest
7from javascript_data_files.decoder import decode_from_json
10@pytest.mark.parametrize(
11 "json_string",
12 [
13 '{ "sides": 3, "sides": 4 }',
14 '{ "sides": 3, "colour": "blue", "sides": 4 }',
15 '[{ "nested": { "sides": 3, "sides": 4 } }]',
16 ],
18def test_object_with_duplicate_names_is_rejected(json_string: str) -> None:
19 """
20 Trying to decode a JavaScript string which includes an object
21 with duplicate names throws a ValueError.
22 """
23 with pytest.raises(ValueError, match="Found duplicate name in JSON object: sides"):
24 decode_from_json(json_string)
27def test_object_with_multiple_duplicate_names_is_rejected() -> None:
28 """
29 Trying to decode a JavaScript string which includes an object
30 with multiple duplicate names throws a ValueError.
31 """
32 with pytest.raises(ValueError, match="Found duplicate names in JSON object:"):
33 decode_from_json(
34 '{ "sides": 3, "colour": "blue", "sides": 4, "colour": "red" }'
35 )