Coverage for hdl_registers/constant/test/test_float_constant.py: 100%

23 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# Standard libraries 

11from copy import copy 

12 

13# Third party libraries 

14import pytest 

15 

16# First party libraries 

17from hdl_registers.constant.float_constant import FloatConstant 

18 

19 

20def test_constant(): 

21 for value in [3.14, -9.9]: 

22 constant = FloatConstant(name="apa", value=value, description=f"desc {value}") 

23 

24 assert constant.name == "apa" 

25 assert constant.value == value 

26 assert constant.description == f"desc {value}" 

27 

28 

29def test_invalid_data_type(): 

30 with pytest.raises(ValueError) as exception_info: 

31 FloatConstant(name="apa", value=True) 

32 assert ( 

33 str(exception_info.value) 

34 == 'Constant "apa" has invalid data type "<class \'bool\'>". Value: "True".' 

35 ) 

36 

37 

38def test_repr(): 

39 data = FloatConstant(name="apa", value=3.14) 

40 

41 # Check that repr is an actual representation, not just "X object at 0xABCDEF" 

42 assert "apa" in repr(data) 

43 assert repr(data) == repr(copy(data)) 

44 

45 # Different name 

46 other = FloatConstant(name="hest", value=3.14) 

47 assert repr(data) != repr(other) 

48 

49 # Different value 

50 other = FloatConstant(name="apa", value=4.2) 

51 assert repr(data) != repr(other) 

52 

53 # Different description 

54 data = FloatConstant(name="apa", value=3.14, description="X") 

55 assert repr(data) != repr(other)