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

23 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-15 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 

10from copy import copy 

11 

12import pytest 

13 

14from hdl_registers.constant.integer_constant import IntegerConstant 

15 

16 

17def test_constant(): 

18 for value in [123, -9]: 

19 constant = IntegerConstant(name="apa", value=value, description=f"desc {value}") 

20 

21 assert constant.name == "apa" 

22 assert constant.value == value 

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

24 

25 

26def test_invalid_data_type(): 

27 with pytest.raises(TypeError) as exception_info: 

28 IntegerConstant(name="apa", value=3.5) 

29 assert ( 

30 str(exception_info.value) 

31 == 'Constant "apa" has invalid data type "<class \'float\'>". Value: "3.5".' 

32 ) 

33 

34 

35def test_repr(): 

36 data = IntegerConstant(name="apa", value=3) 

37 

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

39 assert "apa" in repr(data) 

40 assert repr(data) == repr(copy(data)) 

41 

42 # Different name 

43 other = IntegerConstant(name="hest", value=3) 

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

45 

46 # Different value 

47 other = IntegerConstant(name="apa", value=4) 

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

49 

50 # Different description 

51 data = IntegerConstant(name="apa", value=3, description="X") 

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