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

48 statements  

« 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# -------------------------------------------------------------------------------------------------- 

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 

16 

17 

18def test_register_field_without_type_property_should_raise_exception(tmp_path): 

19 toml_path = create_file( 

20 file=tmp_path / "regs.toml", 

21 contents=""" 

22[apa] 

23 

24mode = "r_w" 

25 

26hest.width = 4 

27""", 

28 ) 

29 

30 with pytest.raises(ValueError) as exception_info: 

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

32 assert str(exception_info.value) == ( 

33 f'Error while parsing field "hest" in register "apa" in {toml_path}: ' 

34 'Missing required property "type".' 

35 ) 

36 

37 

38def test_array_register_field_without_type_property_should_raise_exception(tmp_path): 

39 toml_path = create_file( 

40 file=tmp_path / "regs.toml", 

41 contents=""" 

42[apa] 

43 

44type = "register_array" 

45array_length = 2 

46 

47[apa.hest] 

48 

49mode = "r_w" 

50 

51zebra.width = 4 

52""", 

53 ) 

54 

55 with pytest.raises(ValueError) as exception_info: 

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

57 assert str(exception_info.value) == ( 

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

59 'Missing required property "type".' 

60 ) 

61 

62 

63def test_register_field_with_unknown_type_should_raise_exception(tmp_path): 

64 toml_path = create_file( 

65 file=tmp_path / "regs.toml", 

66 contents=""" 

67[apa] 

68 

69mode = "r_w" 

70 

71hest.type = "bits" 

72hest.width = 4 

73""", 

74 ) 

75 

76 with pytest.raises(ValueError) as exception_info: 

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

78 assert str(exception_info.value) == ( 

79 f'Error while parsing field "hest" in register "apa" in {toml_path}: ' 

80 'Unknown field type "bits". Expected one of "bit", "bit_vector", "enumeration", "integer".' 

81 ) 

82 

83 

84def test_array_register_field_with_unknown_type_should_raise_exception(tmp_path): 

85 toml_path = create_file( 

86 file=tmp_path / "regs.toml", 

87 contents=""" 

88[apa] 

89 

90type = "register_array" 

91array_length = 2 

92 

93[apa.hest] 

94 

95mode = "r_w" 

96 

97zebra.type = "bits" 

98zebra.width = 4 

99""", 

100 ) 

101 

102 with pytest.raises(ValueError) as exception_info: 

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

104 assert str(exception_info.value) == ( 

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

106 'Unknown field type "bits". Expected one of "bit", "bit_vector", "enumeration", "integer".' 

107 ) 

108 

109 

110def test_unknown_bit_field_property_should_raise_exception(tmp_path): 

111 toml_path = create_file( 

112 file=tmp_path / "regs.toml", 

113 contents=""" 

114[dummy_reg] 

115 

116mode = "w" 

117 

118dummy_bit.type = "bit" 

119dummy_bit.description = "Stuff" 

120 

121dummy_bit.dummy_integer.max_value = 3 

122""", 

123 ) 

124 

125 with pytest.raises(ValueError) as exception_info: 

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

127 assert str(exception_info.value) == ( 

128 f'Error while parsing field "dummy_bit" in register "dummy_reg" in {toml_path}: ' 

129 'Unknown property "dummy_integer".' 

130 ) 

131 

132 

133def test_unknown_bit_vector_field_property_should_raise_exception(tmp_path): 

134 toml_path = create_file( 

135 file=tmp_path / "regs.toml", 

136 contents=""" 

137[apa] 

138 

139type = "register_array" 

140array_length = 2 

141 

142[apa.dummy_reg] 

143 

144mode = "w" 

145 

146[apa.dummy_reg.dummy_bit_vector] 

147 

148type = "bit_vector" 

149description = "Stuff" 

150width = 3 

151height = 4 

152 

153""", 

154 ) 

155 

156 with pytest.raises(ValueError) as exception_info: 

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

158 assert str(exception_info.value) == ( 

159 f'Error while parsing field "dummy_bit_vector" in register "dummy_reg" in ' 

160 f'{toml_path}: Unknown property "height".' 

161 ) 

162 

163 

164def test_bit_vector_field_without_width_should_raise_exception(tmp_path): 

165 toml_path = create_file( 

166 file=tmp_path / "regs.toml", 

167 contents=""" 

168[test_reg] 

169mode = "w" 

170 

171test_bit_vector.type = "bit_vector" 

172""", 

173 ) 

174 

175 with pytest.raises(ValueError) as exception_info: 

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

177 assert str(exception_info.value) == ( 

178 f'Error while parsing field "test_bit_vector" in register "test_reg" in {toml_path}: ' 

179 'Missing required property "width".' 

180 ) 

181 

182 

183def test_enumeration_field_without_elements_should_raise_exception(tmp_path): 

184 toml_path = create_file( 

185 file=tmp_path / "regs.toml", 

186 contents=""" 

187[apa] 

188 

189type = "register_array" 

190array_length = 2 

191 

192[apa.test_reg] 

193 

194mode = "w" 

195 

196test.type = "enumeration" 

197""", 

198 ) 

199 

200 with pytest.raises(ValueError) as exception_info: 

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

202 assert str(exception_info.value) == ( 

203 f'Error while parsing field "test" in register "test_reg" in {toml_path}: ' 

204 'Missing required property "element".' 

205 ) 

206 

207 

208def test_integer_field_without_max_value_should_raise_exception(tmp_path): 

209 toml_path = create_file( 

210 file=tmp_path / "regs.toml", 

211 contents=""" 

212[test_reg] 

213mode = "w" 

214 

215test_integer.type = "integer" 

216test_integer.min_value = 3 

217""", 

218 ) 

219 

220 with pytest.raises(ValueError) as exception_info: 

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

222 assert str(exception_info.value) == ( 

223 f'Error while parsing field "test_integer" in register "test_reg" in {toml_path}: ' 

224 'Missing required property "max_value".' 

225 )