Coverage for hdl_registers/parser/test/test_toml.py: 100%

37 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 import Register 

15from hdl_registers.register_modes import REGISTER_MODES 

16 

17 

18def test_load_nonexistent_toml_file_should_raise_exception(tmp_path): 

19 toml_path = tmp_path / "apa.toml" 

20 with pytest.raises(FileNotFoundError) as exception_info: 

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

22 assert str(exception_info.value) == f"Requested TOML file does not exist: {toml_path}" 

23 

24 

25def test_load_dirty_toml_file_should_raise_exception(tmp_path): 

26 toml = """ 

27a.type = "constant" 

28a.value = 1 

29 

30b.type = "constant" 

31b.value = "2" 

32""" 

33 toml_path = create_file(tmp_path / "apa.toml", toml) 

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

35 

36 toml_path = create_file(tmp_path / "hest.toml", toml + "garbage") 

37 with pytest.raises(ValueError) as exception_info: 

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

39 assert str(exception_info.value).startswith( 

40 f"Error while parsing TOML file {toml_path}:\n" 

41 "expected an equals, found eof at line 7 column 8" 

42 ) 

43 

44 

45def test_default_registers(tmp_path): 

46 toml_path = create_file( 

47 file=tmp_path / "regs.toml", 

48 contents=""" 

49[apa] 

50 

51mode = "w" 

52 

53[hest] 

54 

55mode = "w" 

56""", 

57 ) 

58 register_list = from_toml( 

59 name="", 

60 toml_file=toml_path, 

61 default_registers=[ 

62 Register(name="config", index=0, mode=REGISTER_MODES["r_w"], description=""), 

63 Register(name="status", index=1, mode=REGISTER_MODES["r"], description=""), 

64 ], 

65 ) 

66 

67 # Default registers. 

68 assert register_list.get_register("config").index == 0 

69 assert register_list.get_register("status").index == 1 

70 # TOML registers. 

71 assert register_list.get_register("apa").index == 2 

72 assert register_list.get_register("hest").index == 3 

73 

74 

75def test_two_registers_with_same_name_should_raise_exception(tmp_path): 

76 toml_path = create_file( 

77 file=tmp_path / "regs.toml", 

78 contents=""" 

79[status] 

80 

81mode = "w" 

82 

83[status] 

84 

85mode = "w" 

86""", 

87 ) 

88 

89 with pytest.raises(ValueError) as exception_info: 

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

91 expected = ( 

92 f"Error while parsing TOML file {toml_path}:\n" 

93 "redefinition of table `status` for key `status` at line 6 column 1" 

94 ) 

95 assert str(exception_info.value).startswith(expected) 

96 

97 

98def test_two_fields_with_same_name_should_raise_exception(tmp_path): 

99 toml_path = create_file( 

100 file=tmp_path / "regs.toml", 

101 contents=""" 

102[test_reg] 

103 

104mode = "w" 

105 

106[test_reg.test_bit] 

107 

108type = "bit" 

109description = "Declaration 1" 

110 

111[test_reg.test_bit] 

112 

113type = "bit_vector" 

114description = "Declaration 2" 

115""", 

116 ) 

117 

118 with pytest.raises(ValueError) as exception_info: 

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

120 expected = ( 

121 f"Error while parsing TOML file {toml_path}:\n" 

122 "redefinition of table `test_reg.test_bit` for key " 

123 "`test_reg.test_bit` at line 11 column 1" 

124 ) 

125 assert str(exception_info.value).startswith(expected)