Coverage for src \ syriantaxes \ rounder.py: 52%

29 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-12 14:00 +0200

1from decimal import ROUND_DOWN, ROUND_UP, Decimal 

2 

3from .cast import cast_to_decimal 

4from .types import Method, Number 

5 

6 

7class Rounder: 

8 def __init__(self, method: Method, to_nearest: Number) -> None: 

9 self._validate_method(method) 

10 self._method: Method = method 

11 self._to_nearest = cast_to_decimal(to_nearest, lt=Decimal(0)) 

12 

13 def round(self, number: Number) -> Decimal: 

14 number = cast_to_decimal(number, lt=Decimal(0)) / self.to_nearest 

15 methods = {"floor": ROUND_DOWN, "ceil": ROUND_UP} 

16 

17 return ( 

18 number.quantize(self.to_nearest, rounding=methods[self.method]) 

19 * self.to_nearest 

20 ) 

21 

22 @property 

23 def method(self) -> Method: 

24 return self._method 

25 

26 @method.setter 

27 def method(self, value: Method) -> None: 

28 self._validate_method(value) 

29 self._method = value 

30 

31 @property 

32 def to_nearest(self) -> Decimal: 

33 return self._to_nearest 

34 

35 @to_nearest.setter 

36 def to_nearest(self, value: Number) -> None: 

37 self._to_nearest = cast_to_decimal(value, lt=Decimal(0)) 

38 

39 def _validate_method(self, value: Method) -> None: 

40 if value not in ["floor", "ceil"]: 

41 message = "value must be one of 'floor' or 'ceil'" 

42 raise ValueError(message)