Coverage for tests/test_timer.py: 100%

44 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-07 20:46 -0800

1"""Test cases for the timer module.""" 

2 

3from textwrap import dedent 

4 

5import pytest 

6 

7from countdown import timer 

8from countdown.digits import CHARS_BY_SIZE 

9 

10 

11def join_lines(lines): 

12 """Given list of lines, return string of lines with whitespace stripped.""" 

13 return "\n".join(line.rstrip(" ") for line in lines) 

14 

15 

16def test_invalid_duration(): 

17 with pytest.raises(ValueError): 

18 timer.duration("10") 

19 

20 

21def test_duration_10_seconds(): 

22 assert timer.duration("10s") == 10 

23 

24 

25def test_duration_60_seconds(): 

26 assert timer.duration("60s") == 60 

27 

28 

29def test_duration_1_minute(): 

30 assert timer.duration("1m") == 60 

31 

32 

33def test_duration_10_minutes(): 

34 assert timer.duration("10m") == 600 

35 

36 

37def test_duration_25_minutes(): 

38 assert timer.duration("25m") == 1500 

39 

40 

41def test_duration_3_minute_and_30_seconds(): 

42 assert timer.duration("3m30s") == 210 

43 

44 

45def test_duration_2_minutes_and_8_seconds(): 

46 assert timer.duration("2m8s") == 128 

47 

48 

49def test_get_number_lines_10_seconds(): 

50 # Use size 5 digits for consistent rendering 

51 chars = CHARS_BY_SIZE[5] 

52 assert join_lines(timer.get_number_lines(10, chars)) == dedent( 

53 """ 

54 ██████ ██████ ██ ██████ 

55 ██ ██ ██ ██ ██ ███ ██ ██ 

56 ██ ██ ██ ██ ██ ██ ██ 

57 ██ ██ ██ ██ ██ ██ ██ ██ 

58 ██████ ██████ ██ ██████ 

59 """ 

60 ).strip("\n") 

61 

62 

63def test_get_number_lines_60_seconds(): 

64 # Use size 5 digits 

65 chars = CHARS_BY_SIZE[5] 

66 assert join_lines(timer.get_number_lines(60, chars)) == dedent( 

67 """ 

68 ██████ ██ ██████ ██████ 

69 ██ ██ ███ ██ ██ ██ ██ ██ 

70 ██ ██ ██ ██ ██ ██ ██ 

71 ██ ██ ██ ██ ██ ██ ██ ██ 

72 ██████ ██ ██████ ██████ 

73 """ 

74 ).strip("\n") 

75 

76 

77def test_get_number_lines_45_minutes(): 

78 # Use size 5 digits 

79 chars = CHARS_BY_SIZE[5] 

80 assert join_lines(timer.get_number_lines(2700, chars)) == dedent( 

81 """ 

82 ██ ██ ██████ ██████ ██████ 

83 ██ ██ ██ ██ ██ ██ ██ ██ 

84 ██████ ██████ ██ ██ ██ ██ 

85 ██ ██ ██ ██ ██ ██ ██ 

86 ██ ██████ ██████ ██████ 

87 """ 

88 ).strip("\n") 

89 

90 

91def test_get_number_lines_17_minutes_and_four_seconds(): 

92 # Use size 5 digits 

93 chars = CHARS_BY_SIZE[5] 

94 assert join_lines(timer.get_number_lines(1024, chars)) == ( 

95 " ██ ██████ ██████ ██ ██\n" 

96 " ███ ██ ██ ██ ██ ██ ██\n" 

97 " ██ ██ ██ ██ ██████\n" 

98 " ██ ██ ██ ██ ██ ██\n" 

99 " ██ ██ ██████ ██" 

100 ) 

101 

102 

103def test_get_number_lines_8_minutes_and_6_seconds(): 

104 # Use size 5 digits 

105 chars = CHARS_BY_SIZE[5] 

106 assert join_lines(timer.get_number_lines(486, chars)) == dedent( 

107 """ 

108 ██████ ████ ██████ ██████ 

109 ██ ██ ██ ██ ██ ██ ██ ██ 

110 ██ ██ ████ ██ ██ ██████ 

111 ██ ██ ██ ██ ██ ██ ██ ██ ██ 

112 ██████ ████ ██████ ██████ 

113 """ 

114 ).strip("\n") 

115 

116 

117def test_get_number_lines_9_minutes(): 

118 # Use size 5 digits 

119 chars = CHARS_BY_SIZE[5] 

120 assert join_lines(timer.get_number_lines(540, chars)) == dedent( 

121 """ 

122 ██████ ██████ ██████ ██████ 

123 ██ ██ ██ ██ ██ ██ ██ ██ ██ 

124 ██ ██ ██████ ██ ██ ██ ██ 

125 ██ ██ ██ ██ ██ ██ ██ ██ 

126 ██████ █████ ██████ ██████ 

127 """ 

128 ).strip("\n") 

129 

130 

131def test_get_number_lines_3478(): 

132 # Use size 5 digits 

133 chars = CHARS_BY_SIZE[5] 

134 assert join_lines(timer.get_number_lines(2118, chars)) == dedent( 

135 """ 

136 ██████ ██████ ██ ████ 

137 ██ ██ ██ ███ ██ ██ 

138 █████ ██████ ██ ████ 

139 ██ ██ ██ ██ ██ ██ 

140 ██████ ██████ ██ ████ 

141 """ 

142 ).strip("\n")