Coverage for tests/test_timer.py: 100%

49 statements  

« prev     ^ index     » next       coverage.py v7.11.1, created at 2026-03-27 20:23 -0700

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_150_minutes(): 

38 assert timer.duration("150m") == 9000 

39 

40 

41def test_duration_25_minutes(): 

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

43 

44 

45def test_duration_3_minute_and_30_seconds(): 

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

47 

48 

49def test_duration_2_minutes_and_8_seconds(): 

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

51 

52 

53def test_get_number_lines_10_seconds(): 

54 # Use size 5 digits for consistent rendering 

55 chars = CHARS_BY_SIZE[5] 

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

57 """ 

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

59 ██ ██ ██ ██ ██ ███ ██ ██ 

60 ██ ██ ██ ██ ██ ██ ██ 

61 ██ ██ ██ ██ ██ ██ ██ ██ 

62 ██████ ██████ ██ ██████ 

63 """ 

64 ).strip("\n") 

65 

66 

67def test_get_number_lines_60_seconds(): 

68 # Use size 5 digits 

69 chars = CHARS_BY_SIZE[5] 

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

71 """ 

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

73 ██ ██ ███ ██ ██ ██ ██ ██ 

74 ██ ██ ██ ██ ██ ██ ██ 

75 ██ ██ ██ ██ ██ ██ ██ ██ 

76 ██████ ██ ██████ ██████ 

77 """ 

78 ).strip("\n") 

79 

80 

81def test_get_number_lines_45_minutes(): 

82 # Use size 5 digits 

83 chars = CHARS_BY_SIZE[5] 

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

85 """ 

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

87 ██ ██ ██ ██ ██ ██ ██ ██ 

88 ██████ ██████ ██ ██ ██ ██ 

89 ██ ██ ██ ██ ██ ██ ██ 

90 ██ ██████ ██████ ██████ 

91 """ 

92 ).strip("\n") 

93 

94 

95def test_get_number_lines_101_minutes(): 

96 # Use size 5 digits 

97 chars = CHARS_BY_SIZE[5] 

98 assert join_lines(timer.get_number_lines(6060, chars)) == ( 

99 " ██ ██████ ██ ██████ ██████\n" 

100 " ███ ██ ██ ███ ██ ██ ██ ██ ██\n" 

101 " ██ ██ ██ ██ ██ ██ ██ ██\n" 

102 " ██ ██ ██ ██ ██ ██ ██ ██ ██\n" 

103 " ██ ██████ ██ ██████ ██████" 

104 ) 

105 

106 

107def test_get_number_lines_17_minutes_and_four_seconds(): 

108 # Use size 5 digits 

109 chars = CHARS_BY_SIZE[5] 

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

111 " ██ ██████ ██████ ██ ██\n" 

112 " ███ ██ ██ ██ ██ ██ ██\n" 

113 " ██ ██ ██ ██ ██████\n" 

114 " ██ ██ ██ ██ ██ ██\n" 

115 " ██ ██ ██████ ██" 

116 ) 

117 

118 

119def test_get_number_lines_8_minutes_and_6_seconds(): 

120 # Use size 5 digits 

121 chars = CHARS_BY_SIZE[5] 

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

123 """ 

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

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

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

127 ██ ██ ██ ██ ██ ██ ██ ██ ██ 

128 ██████ ████ ██████ ██████ 

129 """ 

130 ).strip("\n") 

131 

132 

133def test_get_number_lines_9_minutes(): 

134 # Use size 5 digits 

135 chars = CHARS_BY_SIZE[5] 

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

137 """ 

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

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

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

141 ██ ██ ██ ██ ██ ██ ██ ██ 

142 ██████ █████ ██████ ██████ 

143 """ 

144 ).strip("\n") 

145 

146 

147def test_get_number_lines_3478(): 

148 # Use size 5 digits 

149 chars = CHARS_BY_SIZE[5] 

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

151 """ 

152 ██████ ██████ ██ ████ 

153 ██ ██ ██ ███ ██ ██ 

154 █████ ██████ ██ ████ 

155 ██ ██ ██ ██ ██ ██ 

156 ██████ ██████ ██ ████ 

157 """ 

158 ).strip("\n")