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

42 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-19 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.yaml import from_yaml 

16from hdl_registers.register import Register 

17from hdl_registers.register_modes import REGISTER_MODES 

18 

19 

20def test_load_nonexistent_yaml_file_should_raise_exception(tmp_path): 

21 yaml_path = tmp_path / "apa.yaml" 

22 with pytest.raises(FileNotFoundError) as exception_info: 

23 from_yaml(name="", yaml_file=yaml_path) 

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

25 

26 

27def test_load_dirty_yaml_file_should_raise_exception(tmp_path): 

28 yaml = """ 

29apa: 

30 mode: r_w 

31""" 

32 yaml_path = create_file(tmp_path / "apa.yaml", yaml) 

33 from_yaml(name="", yaml_file=yaml_path) 

34 

35 yaml_path = create_file(tmp_path / "hest.yaml", yaml + "garbage") 

36 with pytest.raises(ValueError) as exception_info: 

37 from_yaml(name="", yaml_file=yaml_path) 

38 assert str(exception_info.value).startswith( 

39 f"Error while parsing YAML file {yaml_path}:\nwhile scanning a simple key\n" 

40 ) 

41 

42 

43def test_default_registers(tmp_path): 

44 yaml_path = create_file( 

45 file=tmp_path / "regs.yaml", 

46 contents=""" 

47apa: 

48 mode: r_w 

49 description: Apa. 

50 enable: 

51 type: bit 

52 default_value: '1' 

53 description: Enable. 

54 

55hest: 

56 mode: r 

57 description: Hest. 

58 disable: 

59 type: bit 

60 description: Disable. 

61""", 

62 ) 

63 register_list = from_yaml( 

64 name="", 

65 yaml_file=yaml_path, 

66 default_registers=[ 

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

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

69 ], 

70 ) 

71 

72 # Default registers. 

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

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

75 

76 # yaml registers. 

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

78 assert register_list.get_register("apa").mode == REGISTER_MODES["r_w"] 

79 assert register_list.get_register("apa").description == "Apa." 

80 assert len(register_list.get_register("apa").fields) == 1 

81 assert register_list.get_register("apa").fields[0].name == "enable" 

82 assert register_list.get_register("apa").fields[0].description == "Enable." 

83 assert register_list.get_register("apa").fields[0].default_value == "1" 

84 

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

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

87 assert register_list.get_register("hest").description == "Hest." 

88 assert len(register_list.get_register("hest").fields) == 1 

89 assert register_list.get_register("hest").fields[0].name == "disable" 

90 assert register_list.get_register("hest").fields[0].description == "Disable." 

91 assert register_list.get_register("hest").fields[0].default_value == "0" 

92 

93 

94def test_two_registers_with_same_name_does_not_raise_exception(tmp_path): 

95 # Limitation in the YAML Python parser: https://github.com/yaml/pyyaml/issues/165 

96 # This is not a problem for TOML. 

97 # We would highly prefer if this raised exception. 

98 yaml_path = create_file( 

99 file=tmp_path / "regs.yaml", 

100 contents=""" 

101apa: 

102 mode: r_w 

103 

104apa: 

105 mode: r 

106""", 

107 ) 

108 

109 register_list = from_yaml(name="", yaml_file=yaml_path) 

110 assert len(register_list.register_objects) == 1 

111 assert register_list.register_objects[0].mode == REGISTER_MODES["r"]