Coverage for hdl_registers/parser/test/test_json.py: 100%
42 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-07 20:51 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-07 20:51 +0000
1# --------------------------------------------------------------------------------------------------
2# Copyright (c) Lukas Vik. All rights reserved.
3#
4# This file is part of the hdl-registers project, an HDL register generator fast enough to run
5# in real time.
6# https://hdl-registers.com
7# https://github.com/hdl-registers/hdl-registers
8# --------------------------------------------------------------------------------------------------
10# Third party libraries
11import pytest
12from tsfpga.system_utils import create_file
14# First party libraries
15from hdl_registers.parser.json import from_json
16from hdl_registers.register import Register
17from hdl_registers.register_modes import REGISTER_MODES
20def test_load_nonexistent_json_file_should_raise_exception(tmp_path):
21 json_path = tmp_path / "apa.json"
22 with pytest.raises(FileNotFoundError) as exception_info:
23 from_json(name="", json_file=json_path)
24 assert str(exception_info.value) == f"Requested JSON file does not exist: {json_path}"
27def test_load_dirty_json_file_should_raise_exception(tmp_path):
28 json = """
29{
30 "config": {
31 "mode": "r_w"
32 }
33}
34"""
35 json_path = create_file(tmp_path / "apa.json", json)
36 from_json(name="", json_file=json_path)
38 json_path = create_file(tmp_path / "hest.json", json + "garbage")
39 with pytest.raises(ValueError) as exception_info:
40 from_json(name="", json_file=json_path)
41 assert str(exception_info.value).startswith(
42 f"Error while parsing JSON file {json_path}:\nExtra data: "
43 )
46def test_default_registers(tmp_path):
47 json_path = create_file(
48 file=tmp_path / "regs.json",
49 contents="""
50{
51 "apa": {
52 "mode": "r_w",
53 "description": "Apa.",
54 "enable": {
55 "type": "bit",
56 "description": "Enable.",
57 "default_value": "1"
58 }
59 },
60 "hest": {
61 "mode": "r",
62 "description": "Hest.",
63 "disable": {
64 "type": "bit",
65 "description": "Disable."
66 }
67 }
68}
69""",
70 )
71 register_list = from_json(
72 name="",
73 json_file=json_path,
74 default_registers=[
75 Register(name="config", index=0, mode=REGISTER_MODES["r_w"], description=""),
76 Register(name="status", index=1, mode=REGISTER_MODES["r"], description=""),
77 ],
78 )
80 # Default registers.
81 assert register_list.get_register("config").index == 0
82 assert register_list.get_register("status").index == 1
84 # json registers.
85 assert register_list.get_register("apa").index == 2
86 assert register_list.get_register("apa").mode == REGISTER_MODES["r_w"]
87 assert register_list.get_register("apa").description == "Apa."
88 assert len(register_list.get_register("apa").fields) == 1
89 assert register_list.get_register("apa").fields[0].name == "enable"
90 assert register_list.get_register("apa").fields[0].description == "Enable."
91 assert register_list.get_register("apa").fields[0].default_value == "1"
93 assert register_list.get_register("hest").index == 3
94 assert register_list.get_register("hest").mode == REGISTER_MODES["r"]
95 assert register_list.get_register("hest").description == "Hest."
96 assert len(register_list.get_register("hest").fields) == 1
97 assert register_list.get_register("hest").fields[0].name == "disable"
98 assert register_list.get_register("hest").fields[0].description == "Disable."
99 assert register_list.get_register("hest").fields[0].default_value == "0"
102def test_two_registers_with_same_name_does_not_raise_exception(tmp_path):
103 # Limitation in the JSON file format, unlike TOML.
104 # We would highly prefer if this raised exception.
105 json_path = create_file(
106 file=tmp_path / "regs.json",
107 contents="""
108{
109 "apa": {
110 "mode": "r"
111 },
112 "apa": {
113 "mode": "r_w"
114 }
115}
116""",
117 )
119 register_list = from_json(name="", json_file=json_path)
120 assert len(register_list.register_objects) == 1
121 assert register_list.register_objects[0].mode == REGISTER_MODES["r_w"]