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

48 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 

14 

15 

16def test_register_field_without_type_property_should_raise_exception(tmp_path): 

17 toml_path = create_file( 

18 file=tmp_path / "regs.toml", 

19 contents=""" 

20[apa] 

21 

22mode = "r_w" 

23 

24hest.width = 4 

25""", 

26 ) 

27 

28 with pytest.raises(ValueError) as exception_info: 

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

30 assert str(exception_info.value) == ( 

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

32 'Missing required property "type".' 

33 ) 

34 

35 

36def test_array_register_field_without_type_property_should_raise_exception(tmp_path): 

37 toml_path = create_file( 

38 file=tmp_path / "regs.toml", 

39 contents=""" 

40[apa] 

41 

42type = "register_array" 

43array_length = 2 

44 

45[apa.hest] 

46 

47mode = "r_w" 

48 

49zebra.width = 4 

50""", 

51 ) 

52 

53 with pytest.raises(ValueError) as exception_info: 

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

55 assert str(exception_info.value) == ( 

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

57 'Missing required property "type".' 

58 ) 

59 

60 

61def test_register_field_with_unknown_type_should_raise_exception(tmp_path): 

62 toml_path = create_file( 

63 file=tmp_path / "regs.toml", 

64 contents=""" 

65[apa] 

66 

67mode = "r_w" 

68 

69hest.type = "bits" 

70hest.width = 4 

71""", 

72 ) 

73 

74 with pytest.raises(ValueError) as exception_info: 

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

76 assert str(exception_info.value) == ( 

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

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

79 ) 

80 

81 

82def test_array_register_field_with_unknown_type_should_raise_exception(tmp_path): 

83 toml_path = create_file( 

84 file=tmp_path / "regs.toml", 

85 contents=""" 

86[apa] 

87 

88type = "register_array" 

89array_length = 2 

90 

91[apa.hest] 

92 

93mode = "r_w" 

94 

95zebra.type = "bits" 

96zebra.width = 4 

97""", 

98 ) 

99 

100 with pytest.raises(ValueError) as exception_info: 

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

102 assert str(exception_info.value) == ( 

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

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

105 ) 

106 

107 

108def test_unknown_bit_field_property_should_raise_exception(tmp_path): 

109 toml_path = create_file( 

110 file=tmp_path / "regs.toml", 

111 contents=""" 

112[dummy_reg] 

113 

114mode = "w" 

115 

116dummy_bit.type = "bit" 

117dummy_bit.description = "Stuff" 

118 

119dummy_bit.dummy_integer.max_value = 3 

120""", 

121 ) 

122 

123 with pytest.raises(ValueError) as exception_info: 

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

125 assert str(exception_info.value) == ( 

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

127 'Unknown property "dummy_integer".' 

128 ) 

129 

130 

131def test_unknown_bit_vector_field_property_should_raise_exception(tmp_path): 

132 toml_path = create_file( 

133 file=tmp_path / "regs.toml", 

134 contents=""" 

135[apa] 

136 

137type = "register_array" 

138array_length = 2 

139 

140[apa.dummy_reg] 

141 

142mode = "w" 

143 

144[apa.dummy_reg.dummy_bit_vector] 

145 

146type = "bit_vector" 

147description = "Stuff" 

148width = 3 

149height = 4 

150 

151""", 

152 ) 

153 

154 with pytest.raises(ValueError) as exception_info: 

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

156 assert str(exception_info.value) == ( 

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

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

159 ) 

160 

161 

162def test_bit_vector_field_without_width_should_raise_exception(tmp_path): 

163 toml_path = create_file( 

164 file=tmp_path / "regs.toml", 

165 contents=""" 

166[test_reg] 

167mode = "w" 

168 

169test_bit_vector.type = "bit_vector" 

170""", 

171 ) 

172 

173 with pytest.raises(ValueError) as exception_info: 

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

175 assert str(exception_info.value) == ( 

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

177 'Missing required property "width".' 

178 ) 

179 

180 

181def test_enumeration_field_without_elements_should_raise_exception(tmp_path): 

182 toml_path = create_file( 

183 file=tmp_path / "regs.toml", 

184 contents=""" 

185[apa] 

186 

187type = "register_array" 

188array_length = 2 

189 

190[apa.test_reg] 

191 

192mode = "w" 

193 

194test.type = "enumeration" 

195""", 

196 ) 

197 

198 with pytest.raises(ValueError) as exception_info: 

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

200 assert str(exception_info.value) == ( 

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

202 'Missing required property "element".' 

203 ) 

204 

205 

206def test_integer_field_without_max_value_should_raise_exception(tmp_path): 

207 toml_path = create_file( 

208 file=tmp_path / "regs.toml", 

209 contents=""" 

210[test_reg] 

211mode = "w" 

212 

213test_integer.type = "integer" 

214test_integer.min_value = 3 

215""", 

216 ) 

217 

218 with pytest.raises(ValueError) as exception_info: 

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

220 assert str(exception_info.value) == ( 

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

222 'Missing required property "max_value".' 

223 )