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

37 statements  

« prev     ^ index     » next       coverage.py v7.6.8, created at 2024-12-01 20:50 +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 import Register 

17from hdl_registers.register_modes import REGISTER_MODES 

18 

19 

20def test_load_nonexistent_toml_file_should_raise_exception(tmp_path): 

21 toml_path = tmp_path / "apa.toml" 

22 with pytest.raises(FileNotFoundError) as exception_info: 

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

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

25 

26 

27def test_load_dirty_toml_file_should_raise_exception(tmp_path): 

28 toml = """ 

29a.type = "constant" 

30a.value = 1 

31 

32b.type = "constant" 

33b.value = "2" 

34""" 

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

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

37 

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

39 with pytest.raises(ValueError) as exception_info: 

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

41 assert str(exception_info.value).startswith( 

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

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

44 ) 

45 

46 

47def test_default_registers(tmp_path): 

48 toml_path = create_file( 

49 file=tmp_path / "regs.toml", 

50 contents=""" 

51[apa] 

52 

53mode = "w" 

54 

55[hest] 

56 

57mode = "w" 

58""", 

59 ) 

60 register_list = from_toml( 

61 name="", 

62 toml_file=toml_path, 

63 default_registers=[ 

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

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

66 ], 

67 ) 

68 

69 # Default registers. 

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

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

72 # TOML registers. 

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

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

75 

76 

77def test_two_registers_with_same_name_should_raise_exception(tmp_path): 

78 toml_path = create_file( 

79 file=tmp_path / "regs.toml", 

80 contents=""" 

81[status] 

82 

83mode = "w" 

84 

85[status] 

86 

87mode = "w" 

88""", 

89 ) 

90 

91 with pytest.raises(ValueError) as exception_info: 

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

93 expected = ( 

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

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

96 ) 

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

98 

99 

100def test_two_fields_with_same_name_should_raise_exception(tmp_path): 

101 toml_path = create_file( 

102 file=tmp_path / "regs.toml", 

103 contents=""" 

104[test_reg] 

105 

106mode = "w" 

107 

108[test_reg.test_bit] 

109 

110type = "bit" 

111description = "Declaration 1" 

112 

113[test_reg.test_bit] 

114 

115type = "bit_vector" 

116description = "Declaration 2" 

117""", 

118 ) 

119 

120 with pytest.raises(ValueError) as exception_info: 

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

122 expected = ( 

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

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

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

126 ) 

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