-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
test_dialog_helper.py
182 lines (142 loc) · 6.81 KB
/
test_dialog_helper.py
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
# -*- coding: utf-8 -*-
from io import BytesIO, StringIO
import re
from unittest import TestCase
from cleo.helpers.dialog_helper import DialogHelper
from cleo.helpers.helper_set import HelperSet
from cleo.helpers.formatter_helper import FormatterHelper
from cleo.outputs.stream_output import StreamOutput
from cleo.validators import Choice
from cleo._compat import PY2
class DialogHelperTest(TestCase):
def test_select(self):
"""
DialogHelper.select() behaves properly
"""
dialog = DialogHelper()
helper_set = HelperSet([FormatterHelper()])
dialog.set_helper_set(helper_set)
heroes = ['Superman', 'Batman', 'Spiderman']
dialog.set_input_stream(self.get_input_stream('\n1\nSebastien\n1\nSebastien\nSebastien\n'))
self.assertEqual(2,
dialog.select(self.get_output_stream(),
'What is your favorite superhero?', heroes, '2'))
self.assertEqual(1,
dialog.select(self.get_output_stream(),
'What is your favorite superhero?', heroes))
output = self.get_output_stream()
self.assertEqual(1,
dialog.select(output,
'What is your favorite superhero?', heroes, None,
False, 'Input "%s" is not a superhero!'))
output.get_stream().seek(0)
self.assertTrue(re.match('.*Input "Sebastien" is not a superhero!.*',
output.get_stream().read().decode()) is not None)
output = self.get_output_stream()
self.assertRaises(Exception, dialog.select, output, 'What is your favorite superhero?', heroes, None, 1)
def test_ask(self):
"""
DialogHelper.ask() behaves properly
"""
dialog = DialogHelper()
dialog.set_input_stream(self.get_input_stream('\n8AM\n'))
self.assertEqual('2PM', dialog.ask(self.get_output_stream(), 'What time is it?', '2PM'))
output = self.get_output_stream()
self.assertEqual('8AM', dialog.ask(output, 'What time is it?', '2PM'))
output.get_stream().seek(0)
self.assertEqual('What time is it?', output.get_stream().read().decode())
def test_ask_with_string(self):
"""
DialogHelper.ask() behaves properly with string
"""
dialog = DialogHelper()
dialog.set_input_stream(self.get_string_input_stream('\n8AM\n'))
self.assertEqual('2PM', dialog.ask(self.get_output_stream(), 'What time is it?', '2PM'))
output = self.get_output_stream()
self.assertEqual('8AM', dialog.ask(output, 'What time is it?', '2PM'))
output.get_stream().seek(0)
self.assertEqual('What time is it?', output.get_stream().read().decode())
def test_ask_confirmation(self):
"""
DialogHelper.ask_confirmation() behaves properly
"""
dialog = DialogHelper()
dialog.set_input_stream(self.get_input_stream('\n\n'))
self.assertTrue(dialog.ask_confirmation(self.get_output_stream(), 'Do you like French fries?'))
self.assertFalse(dialog.ask_confirmation(self.get_output_stream(), 'Do you like French fries?', False))
dialog.set_input_stream(self.get_input_stream('y\nyes\n'))
self.assertTrue(dialog.ask_confirmation(self.get_output_stream(), 'Do you like French fries?', False))
self.assertTrue(dialog.ask_confirmation(self.get_output_stream(), 'Do you like French fries?', False))
dialog.set_input_stream(self.get_input_stream('n\nno\n'))
self.assertFalse(dialog.ask_confirmation(self.get_output_stream(), 'Do you like French fries?', True))
self.assertFalse(dialog.ask_confirmation(self.get_output_stream(), 'Do you like French fries?', True))
def test_ask_and_validate(self):
"""
DialogHelper.ask_and_validate() behaves properly
"""
dialog = DialogHelper()
helper_set = HelperSet([FormatterHelper()])
dialog.set_helper_set(helper_set)
question = 'What color was the white horse of Henry IV?'
error = 'This is not a color!'
def validator(color):
if color not in ['white', 'black']:
raise Exception(error)
return color
dialog.set_input_stream(self.get_input_stream('\nblack\n'))
self.assertEqual('white', dialog.ask_and_validate(self.get_output_stream(), question, validator, 2, 'white'))
self.assertEqual('black', dialog.ask_and_validate(self.get_output_stream(), question, validator, 2, 'white'))
dialog.set_input_stream(self.get_input_stream('green\nyellow\norange\n'))
self.assertRaises(Exception,
dialog.ask_and_validate,
self.get_output_stream(), question, validator, 2, 'white')
def test_ask_and_validate_with_validator(self):
"""
DialogHelper.ask_and_validate() behaves properly when passing Validator instances
"""
dialog = DialogHelper()
helper_set = HelperSet([FormatterHelper()])
dialog.set_helper_set(helper_set)
question = 'What color was the white horse of Henry IV?'
error = 'This is not a color!'
def validator(color):
if color not in ['white', 'black']:
raise Exception(error)
return color
dialog.set_input_stream(self.get_input_stream('\nblack\n'))
self.assertEqual('white',
dialog.ask_and_validate(
self.get_output_stream(),
question,
Choice(['white', 'black']),
2,
'white'
))
self.assertEqual('black',
dialog.ask_and_validate(
self.get_output_stream(),
question,
Choice(['white', 'black']),
2,
'white'
))
dialog.set_input_stream(self.get_input_stream('green\nyellow\norange\n'))
self.assertRaises(Exception,
dialog.ask_and_validate,
self.get_output_stream(), question, Choice(['white', 'black']), 2, 'white')
def get_input_stream(self, input_):
stream = BytesIO()
stream.write(input_.encode())
stream.seek(0)
return stream
def get_string_input_stream(self, input_):
stream = StringIO()
if PY2:
stream.write(input_.decode())
else:
stream.write(input_)
stream.seek(0)
return stream
def get_output_stream(self):
stream = BytesIO()
return StreamOutput(stream)