~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Martin Packman
  • Date: 2012-01-05 09:50:04 UTC
  • mfrom: (6424 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6426.
  • Revision ID: martin.packman@canonical.com-20120105095004-mia9xb7y0efmto0v
Merge bzr.dev to resolve conflicts in bzrlib.builtins

Show diffs side-by-side

added added

removed removed

Lines of Context:
111
111
    def test_text_ui_get_boolean(self):
112
112
        stdin = tests.StringIOWrapper("y\n" # True
113
113
                                      "n\n" # False
 
114
                                      " \n y \n" # True
 
115
                                      " no \n" # False
114
116
                                      "yes with garbage\nY\n" # True
115
117
                                      "not an answer\nno\n" # False
116
118
                                      "I'm sure!\nyes\n" # True
125
127
        self.assertEqual(False, factory.get_boolean(u""))
126
128
        self.assertEqual(True, factory.get_boolean(u""))
127
129
        self.assertEqual(False, factory.get_boolean(u""))
128
 
        self.assertEqual("foo\n", factory.stdin.read())
129
 
        # stdin should be empty
130
 
        self.assertEqual('', factory.stdin.readline())
 
130
        self.assertEqual(True, factory.get_boolean(u""))
 
131
        self.assertEqual(False, factory.get_boolean(u""))
 
132
        self.assertEqual("foo\n", factory.stdin.read())
 
133
        # stdin should be empty
 
134
        self.assertEqual('', factory.stdin.readline())
 
135
        # return false on EOF
 
136
        self.assertEqual(False, factory.get_boolean(u""))
 
137
 
 
138
    def test_text_ui_choose_bad_parameters(self):
 
139
        stdin = tests.StringIOWrapper()
 
140
        stdout = tests.StringIOWrapper()
 
141
        stderr = tests.StringIOWrapper()
 
142
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
 
143
        # invalid default index
 
144
        self.assertRaises(ValueError, factory.choose, u"", u"&Yes\n&No", 3)
 
145
        # duplicated choice
 
146
        self.assertRaises(ValueError, factory.choose, u"", u"&choice\n&ChOiCe")
 
147
        # duplicated shortcut
 
148
        self.assertRaises(ValueError, factory.choose, u"", u"&choice1\nchoi&ce2")
 
149
 
 
150
    def test_text_ui_choose_prompt(self):
 
151
        stdin = tests.StringIOWrapper()
 
152
        stdout = tests.StringIOWrapper()
 
153
        stderr = tests.StringIOWrapper()
 
154
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
 
155
        # choices with explicit shortcuts
 
156
        factory.choose(u"prompt", u"&yes\n&No\nmore &info")
 
157
        self.assertEqual("prompt ([y]es, [N]o, more [i]nfo): \n", factory.stderr.getvalue())
 
158
        # automatic shortcuts
 
159
        factory.stderr.truncate(0)
 
160
        factory.choose(u"prompt", u"yes\nNo\nmore info")
 
161
        self.assertEqual("prompt ([y]es, [N]o, [m]ore info): \n", factory.stderr.getvalue())
 
162
 
 
163
    def test_text_ui_choose_return_values(self):
 
164
        choose = lambda: factory.choose(u"", u"&Yes\n&No\nMaybe\nmore &info", 3)
 
165
        stdin = tests.StringIOWrapper("y\n" # 0
 
166
                                      "n\n" # 1
 
167
                                      " \n" # default: 3
 
168
                                      " no \n" # 1
 
169
                                      "b\na\nd \n" # bad shortcuts, all ignored
 
170
                                      "yes with garbage\nY\n" # 0
 
171
                                      "not an answer\nno\n" # 1
 
172
                                      "info\nmore info\n" # 3
 
173
                                      "Maybe\n" # 2
 
174
                                      "foo\n")
 
175
        stdout = tests.StringIOWrapper()
 
176
        stderr = tests.StringIOWrapper()
 
177
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
 
178
        self.assertEqual(0, choose())
 
179
        self.assertEqual(1, choose())
 
180
        self.assertEqual(3, choose())
 
181
        self.assertEqual(1, choose())
 
182
        self.assertEqual(0, choose())
 
183
        self.assertEqual(1, choose())
 
184
        self.assertEqual(3, choose())
 
185
        self.assertEqual(2, choose())
 
186
        self.assertEqual("foo\n", factory.stdin.read())
 
187
        # stdin should be empty
 
188
        self.assertEqual('', factory.stdin.readline())
 
189
        # return None on EOF
 
190
        self.assertEqual(None, choose())
 
191
 
 
192
    def test_text_ui_choose_no_default(self):
 
193
        stdin = tests.StringIOWrapper(" \n" # no default, invalid!
 
194
                                      " yes \n" # 0
 
195
                                      "foo\n")
 
196
        stdout = tests.StringIOWrapper()
 
197
        stderr = tests.StringIOWrapper()
 
198
        factory = _mod_ui_text.TextUIFactory(stdin, stdout, stderr)
 
199
        self.assertEqual(0, factory.choose(u"", u"&Yes\n&No"))
 
200
        self.assertEqual("foo\n", factory.stdin.read())
131
201
 
132
202
    def test_text_ui_get_integer(self):
133
203
        stdin = tests.StringIOWrapper(
170
240
        output = out.getvalue()
171
241
        self.assertContainsRe(output,
172
242
            "| foo *\r\r  *\r*")
173
 
        self.assertContainsRe(output,
174
 
            r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
 
243
        self.assertContainsString(output,
 
244
            r"what do you want? ([y]es, [n]o): what do you want? ([y]es, [n]o): ")
175
245
        # stdin should have been totally consumed
176
246
        self.assertEqual('', factory.stdin.readline())
177
247