Coverage for hdl_registers/parser/test/test_parser/test_parser_register.py: 100%

62 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-12 11:11 +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# -------------------------------------------------------------------------------------------------- 

9 

10import pytest 

11from tsfpga.system_utils import create_file 

12 

13from hdl_registers.parser.toml import from_toml 

14from hdl_registers.register_modes import REGISTER_MODES 

15 

16 

17def test_register_can_be_specified_with_and_without_type(tmp_path): 

18 toml_path = create_file( 

19 file=tmp_path / "regs.toml", 

20 contents=""" 

21[apa] 

22 

23mode = "w" 

24 

25[hest] 

26 

27type = "register" 

28mode = "r" 

29description = "zebra" 

30""", 

31 ) 

32 register_list = from_toml(name="", toml_file=toml_path) 

33 

34 assert register_list.get_register("apa").index == 0 

35 assert register_list.get_register("apa").mode == REGISTER_MODES["w"] 

36 assert register_list.get_register("apa").description == "" 

37 assert register_list.get_register("hest").index == 1 

38 assert register_list.get_register("hest").mode == REGISTER_MODES["r"] 

39 assert register_list.get_register("hest").description == "zebra" 

40 

41 

42def test_register_with_no_mode_property_should_raise_exception(tmp_path): 

43 toml_path = create_file( 

44 file=tmp_path / "regs.toml", 

45 contents=""" 

46[apa] 

47 

48description = "w" 

49""", 

50 ) 

51 

52 with pytest.raises(ValueError) as exception_info: 

53 from_toml(name="", toml_file=toml_path) 

54 assert ( 

55 str(exception_info.value) 

56 == f'Error while parsing register "apa" in {toml_path}: Missing required property "mode".' 

57 ) 

58 

59 

60def test_unknown_register_property_should_raise_exception(tmp_path): 

61 toml_path = create_file( 

62 file=tmp_path / "regs.toml", 

63 contents=""" 

64[test_reg] 

65 

66mode = "w" 

67dummy = 3 

68""", 

69 ) 

70 

71 with pytest.raises(ValueError) as exception_info: 

72 from_toml(name="", toml_file=toml_path) 

73 assert str(exception_info.value) == ( 

74 f'Error while parsing register "test_reg" in {toml_path}: Got unknown property "dummy".' 

75 ) 

76 

77 

78def test_array_register_can_be_specified_with_and_without_type(tmp_path): 

79 toml_path = create_file( 

80 file=tmp_path / "regs.toml", 

81 contents=""" 

82[apa] 

83 

84type = "register_array" 

85array_length = 2 

86 

87[apa.hest] 

88 

89mode = "r" 

90 

91[apa.zebra] 

92 

93type = "register" 

94mode = "w" 

95description = "stuff" 

96""", 

97 ) 

98 

99 register_list = from_toml(name="", toml_file=toml_path) 

100 assert register_list.get_register(register_name="hest", register_array_name="apa").index == 0 

101 assert ( 

102 register_list.get_register(register_name="hest", register_array_name="apa").mode 

103 == REGISTER_MODES["r"] 

104 ) 

105 assert ( 

106 register_list.get_register(register_name="hest", register_array_name="apa").description 

107 == "" 

108 ) 

109 assert register_list.get_register(register_name="zebra", register_array_name="apa").index == 1 

110 assert ( 

111 register_list.get_register(register_name="zebra", register_array_name="apa").mode 

112 == REGISTER_MODES["w"] 

113 ) 

114 assert ( 

115 register_list.get_register(register_name="zebra", register_array_name="apa").description 

116 == "stuff" 

117 ) 

118 

119 

120def test_array_register_with_bad_type_should_raise_exception(tmp_path): 

121 toml_path = create_file( 

122 file=tmp_path / "regs.toml", 

123 contents=""" 

124[apa] 

125 

126type = "register_array" 

127array_length = 2 

128 

129[apa.hest] 

130 

131type = "constant" 

132""", 

133 ) 

134 

135 with pytest.raises(ValueError) as exception_info: 

136 from_toml(name="", toml_file=toml_path) 

137 assert str(exception_info.value) == ( 

138 f'Error while parsing register "hest" within array "apa" in {toml_path}: ' 

139 'Got unknown type "constant". Expected "register".' 

140 ) 

141 

142 

143def test_array_register_with_no_mode_property_should_raise_exception(tmp_path): 

144 toml_path = create_file( 

145 file=tmp_path / "regs.toml", 

146 contents=""" 

147[apa] 

148 

149type = "register_array" 

150array_length = 2 

151 

152[apa.hest] 

153 

154description = "nothing" 

155""", 

156 ) 

157 

158 with pytest.raises(ValueError) as exception_info: 

159 from_toml(name="", toml_file=toml_path) 

160 assert str(exception_info.value) == ( 

161 f'Error while parsing register "hest" within array "apa" in {toml_path}: ' 

162 'Missing required property "mode".' 

163 ) 

164 

165 

166def test_unknown_array_register_property_should_raise_exception(tmp_path): 

167 toml_path = create_file( 

168 file=tmp_path / "regs.toml", 

169 contents=""" 

170[test_array] 

171 

172type = "register_array" 

173array_length = 2 

174 

175[test_array.hest] 

176 

177mode = "r" 

178dummy = 3 

179""", 

180 ) 

181 

182 with pytest.raises(ValueError) as exception_info: 

183 from_toml(name="", toml_file=toml_path) 

184 assert ( 

185 str(exception_info.value) 

186 == f'Error while parsing register "hest" within array "test_array" in {toml_path}: ' 

187 'Got unknown property "dummy".' 

188 ) 

189 

190 

191def test_plain_register_with_array_length_property_should_raise_exception(tmp_path): 

192 toml_path = create_file( 

193 file=tmp_path / "regs.toml", 

194 contents=""" 

195[apa] 

196 

197mode = "r_w" 

198array_length = 4 

199""", 

200 ) 

201 

202 with pytest.raises(ValueError) as exception_info: 

203 from_toml(name="", toml_file=toml_path) 

204 assert str(exception_info.value) == ( 

205 f'Error while parsing register "apa" in {toml_path}: Got unknown property "array_length".' 

206 ) 

207 

208 

209def test_unknown_register_mode_should_raise_exception(tmp_path): 

210 toml_path = create_file( 

211 file=tmp_path / "regs.toml", 

212 contents=""" 

213[test_reg] 

214 

215mode = "rw" 

216""", 

217 ) 

218 

219 with pytest.raises(ValueError) as exception_info: 

220 from_toml(name="", toml_file=toml_path) 

221 assert str(exception_info.value) == ( 

222 f'Error while parsing register "test_reg" in {toml_path}: ' 

223 'Got unknown mode "rw". Expected one of "r", "w", "r_w", "wpulse", "r_wpulse".' 

224 ) 

225 

226 

227def test_unknown_array_register_mode_should_raise_exception(tmp_path): 

228 toml_path = create_file( 

229 file=tmp_path / "regs.toml", 

230 contents=""" 

231[test_array] 

232 

233type = "register_array" 

234array_length = 2 

235 

236[test_array.hest] 

237 

238mode = "r_pulse" 

239""", 

240 ) 

241 

242 with pytest.raises(ValueError) as exception_info: 

243 from_toml(name="", toml_file=toml_path) 

244 assert ( 

245 str(exception_info.value) == f'Error while parsing register "hest" in {toml_path}: ' 

246 'Got unknown mode "r_pulse". Expected one of "r", "w", "r_w", "wpulse", "r_wpulse".' 

247 )