-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_parser_test.py
More file actions
188 lines (144 loc) · 6.39 KB
/
input_parser_test.py
File metadata and controls
188 lines (144 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import unittest
import input_parser
import colors
import exceptions
class InputParserTest(unittest.TestCase):
def test_time_addition_pretty_format(self):
input_type, [seconds] = input_parser.parse_input('+3m')
self.assertEqual(input_parser.InputType.TIME_ADDITION, input_type)
self.assertEqual(180, seconds)
def test_time_addition_clock_format(self):
input_type, [seconds] = input_parser.parse_input('+3:00')
self.assertEqual(input_parser.InputType.TIME_ADDITION, input_type)
self.assertEqual(180, seconds)
def test_time_addition_naked_format(self):
input_type, [seconds] = input_parser.parse_input('+180')
self.assertEqual(input_parser.InputType.TIME_ADDITION, input_type)
self.assertEqual(180, seconds)
def test_time_reduction_pretty_format(self):
input_type, [seconds] = input_parser.parse_input('-3m')
self.assertEqual(input_parser.InputType.TIME_REDUCTION, input_type)
self.assertEqual(180, seconds)
def test_time_reduction_clock_format(self):
input_type, [seconds] = input_parser.parse_input('-3:00')
self.assertEqual(input_parser.InputType.TIME_REDUCTION, input_type)
self.assertEqual(180, seconds)
def test_time_reduction_naked_format(self):
input_type, [seconds] = input_parser.parse_input('-180')
self.assertEqual(input_parser.InputType.TIME_REDUCTION, input_type)
self.assertEqual(180, seconds)
def test_set_time_pretty_format(self):
input_type, [seconds] = input_parser.parse_input('5m')
self.assertEqual(input_parser.InputType.TIME_SET, input_type)
self.assertEqual(300, seconds)
def test_set_time_clock_format(self):
input_type, [seconds] = input_parser.parse_input('5:00')
self.assertEqual(input_parser.InputType.TIME_SET, input_type)
self.assertEqual(300, seconds)
def test_set_time_naked_format(self):
input_type, [seconds] = input_parser.parse_input('300')
self.assertEqual(input_parser.InputType.TIME_SET, input_type)
self.assertEqual(300, seconds)
def test_set_color_option(self):
input_type, [color] = input_parser.parse_input('color_option=red_on_negatives')
self.assertEqual(input_parser.InputType.SET_COLOR_OPTION, input_type)
self.assertEqual(colors.ColorOption.RED_ON_NEGATIVES, color)
def test_set_timer_name(self):
input_type, [key, value] = input_parser.parse_input('timer_name=whatever')
self.assertEqual(
input_parser.InputType.SET_GENERIC_FREE_TEXT_PROPERTY, input_type
)
self.assertEqual('timer_name', key)
self.assertEqual('whatever', value)
def test_set_alarm_command(self):
input_type, [key, value] = input_parser.parse_input('alarm_command=whatever')
self.assertEqual(
input_parser.InputType.SET_GENERIC_FREE_TEXT_PROPERTY, input_type
)
self.assertEqual('alarm_command', key)
self.assertEqual('whatever', value)
def test_set_read_input_command(self):
input_type, [key, value] = input_parser.parse_input(
'read_input_command=whatever'
)
self.assertEqual(
input_parser.InputType.SET_GENERIC_FREE_TEXT_PROPERTY, input_type
)
self.assertEqual('read_input_command', key)
self.assertEqual('whatever', value)
def test_set_running_label(self):
input_type, [key, value] = input_parser.parse_input('running_label=whatever')
self.assertEqual(
input_parser.InputType.SET_GENERIC_FREE_TEXT_PROPERTY, input_type
)
self.assertEqual('running_label', key)
self.assertEqual('whatever', value)
def test_set_stopped_label(self):
input_type, [key, value] = input_parser.parse_input('stopped_label=whatever')
self.assertEqual(
input_parser.InputType.SET_GENERIC_FREE_TEXT_PROPERTY, input_type
)
self.assertEqual('stopped_label', key)
self.assertEqual('whatever', value)
def test_set_paused_label(self):
input_type, [key, value] = input_parser.parse_input('paused_label=whatever')
self.assertEqual(
input_parser.InputType.SET_GENERIC_FREE_TEXT_PROPERTY, input_type
)
self.assertEqual('paused_label', key)
self.assertEqual('whatever', value)
def test_empty_input(self):
input_type, [] = input_parser.parse_input('')
self.assertEqual(
input_parser.InputType.VOID, input_type
)
def test_set_text_format(self):
input_type, [value] = input_parser.parse_input('text_format={elapsed_time}')
self.assertEqual(input_parser.InputType.SET_TEXT_FORMAT, input_type)
self.assertEqual('{elapsed_time}', value)
def test_unknown_property(self):
with self.assertRaisesRegex(exceptions.BadPropertyPattern, 'unknown property'):
input_parser.parse_input('something_non_existent=¯\_(ツ)_/¯')
def test_bad_input(self):
with self.assertRaisesRegex(exceptions.BadValue, 'invalid input'):
input_parser.parse_input('¯\_(ツ)_/¯')
def test_clock_format_to_seconds(self):
cases = [
(300, '05:00'),
(300, '5:00'),
(301, '05:01'),
(301, '5:1'),
(299, '04:59'),
(3600, '1:00:00'),
(3600, '1:0:0'),
(3600 * 24, '24:00:00'),
(3661, '1:01:01'),
(3661, '1:1:1'),
(3601, '1:0:01'),
]
for expected_secs, clock_time in cases:
with self.subTest(
clock_time, clock_time=clock_time, expected_secs=expected_secs
):
self.assertEqual(
expected_secs, input_parser.clock_format_to_seconds(clock_time)
)
def test_pretty_format_to_seconds(self):
cases = [
(300, '5m'),
(301, '5m1s'),
(299, '4m59s'),
(3600, '1h'),
(3600 * 24, '24h'),
(3661, '1h1m1s'),
(3601, '1h1s'),
]
for expected_secs, clock_time in cases:
with self.subTest(
clock_time, clock_time=clock_time, expected_secs=expected_secs
):
self.assertEqual(
expected_secs, input_parser.pretty_time_to_seconds(clock_time)
)
if __name__ == '__main__':
unittest.main()