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

62 statements  

« prev     ^ index     » next       coverage.py v7.6.3, created at 2024-10-17 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# -------------------------------------------------------------------------------------------------- 

9 

10# Third party libraries 

11import pytest 

12from tsfpga.system_utils import create_file 

13 

14# First party libraries 

15from hdl_registers.parser.toml import from_toml 

16from hdl_registers.register_modes import REGISTER_MODES 

17 

18 

19def test_register_can_be_specified_with_and_without_type(tmp_path): 

20 toml_path = create_file( 

21 file=tmp_path / "regs.toml", 

22 contents=""" 

23[apa] 

24 

25mode = "w" 

26 

27[hest] 

28 

29type = "register" 

30mode = "r" 

31description = "zebra" 

32""", 

33 ) 

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

35 

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

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

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

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

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

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

42 

43 

44def test_register_with_no_mode_property_should_raise_exception(tmp_path): 

45 toml_path = create_file( 

46 file=tmp_path / "regs.toml", 

47 contents=""" 

48[apa] 

49 

50description = "w" 

51""", 

52 ) 

53 

54 with pytest.raises(ValueError) as exception_info: 

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

56 assert ( 

57 str(exception_info.value) 

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

59 ) 

60 

61 

62def test_unknown_register_property_should_raise_exception(tmp_path): 

63 toml_path = create_file( 

64 file=tmp_path / "regs.toml", 

65 contents=""" 

66[test_reg] 

67 

68mode = "w" 

69dummy = 3 

70""", 

71 ) 

72 

73 with pytest.raises(ValueError) as exception_info: 

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

75 assert str(exception_info.value) == ( 

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

77 ) 

78 

79 

80def test_array_register_can_be_specified_with_and_without_type(tmp_path): 

81 toml_path = create_file( 

82 file=tmp_path / "regs.toml", 

83 contents=""" 

84[apa] 

85 

86type = "register_array" 

87array_length = 2 

88 

89[apa.hest] 

90 

91mode = "r" 

92 

93[apa.zebra] 

94 

95type = "register" 

96mode = "w" 

97description = "stuff" 

98""", 

99 ) 

100 

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

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

103 assert ( 

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

105 == REGISTER_MODES["r"] 

106 ) 

107 assert ( 

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

109 == "" 

110 ) 

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

112 assert ( 

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

114 == REGISTER_MODES["w"] 

115 ) 

116 assert ( 

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

118 == "stuff" 

119 ) 

120 

121 

122def test_array_register_with_bad_type_should_raise_exception(tmp_path): 

123 toml_path = create_file( 

124 file=tmp_path / "regs.toml", 

125 contents=""" 

126[apa] 

127 

128type = "register_array" 

129array_length = 2 

130 

131[apa.hest] 

132 

133type = "constant" 

134""", 

135 ) 

136 

137 with pytest.raises(ValueError) as exception_info: 

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

139 assert str(exception_info.value) == ( 

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

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

142 ) 

143 

144 

145def test_array_register_with_no_mode_property_should_raise_exception(tmp_path): 

146 toml_path = create_file( 

147 file=tmp_path / "regs.toml", 

148 contents=""" 

149[apa] 

150 

151type = "register_array" 

152array_length = 2 

153 

154[apa.hest] 

155 

156description = "nothing" 

157""", 

158 ) 

159 

160 with pytest.raises(ValueError) as exception_info: 

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

162 assert str(exception_info.value) == ( 

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

164 'Missing required property "mode".' 

165 ) 

166 

167 

168def test_unknown_array_register_property_should_raise_exception(tmp_path): 

169 toml_path = create_file( 

170 file=tmp_path / "regs.toml", 

171 contents=""" 

172[test_array] 

173 

174type = "register_array" 

175array_length = 2 

176 

177[test_array.hest] 

178 

179mode = "r" 

180dummy = 3 

181""", 

182 ) 

183 

184 with pytest.raises(ValueError) as exception_info: 

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

186 assert ( 

187 str(exception_info.value) 

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

189 'Got unknown property "dummy".' 

190 ) 

191 

192 

193def test_plain_register_with_array_length_property_should_raise_exception(tmp_path): 

194 toml_path = create_file( 

195 file=tmp_path / "regs.toml", 

196 contents=""" 

197[apa] 

198 

199mode = "r_w" 

200array_length = 4 

201""", 

202 ) 

203 

204 with pytest.raises(ValueError) as exception_info: 

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

206 assert str(exception_info.value) == ( 

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

208 ) 

209 

210 

211def test_unknown_register_mode_should_raise_exception(tmp_path): 

212 toml_path = create_file( 

213 file=tmp_path / "regs.toml", 

214 contents=""" 

215[test_reg] 

216 

217mode = "rw" 

218""", 

219 ) 

220 

221 with pytest.raises(ValueError) as exception_info: 

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

223 assert str(exception_info.value) == ( 

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

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

226 ) 

227 

228 

229def test_unknown_array_register_mode_should_raise_exception(tmp_path): 

230 toml_path = create_file( 

231 file=tmp_path / "regs.toml", 

232 contents=""" 

233[test_array] 

234 

235type = "register_array" 

236array_length = 2 

237 

238[test_array.hest] 

239 

240mode = "r_pulse" 

241""", 

242 ) 

243 

244 with pytest.raises(ValueError) as exception_info: 

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

246 assert ( 

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

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

249 )