~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_ui.py

  • Committer: Martin Packman
  • Author(s): John Arbash Meinel
  • Date: 2011-09-30 16:29:17 UTC
  • mto: (6015.33.7 2.4)
  • mto: This revision was merged to the branch mainline in revision 6192.
  • Revision ID: martin.packman@canonical.com-20110930162917-zilsfmiuxzsky4so
Return early to avoid malloc(0) in create_delta_index_from_delta

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
116
114
                                      "yes with garbage\nY\n" # True
117
115
                                      "not an answer\nno\n" # False
118
116
                                      "I'm sure!\nyes\n" # True
127
125
        self.assertEqual(False, factory.get_boolean(u""))
128
126
        self.assertEqual(True, factory.get_boolean(u""))
129
127
        self.assertEqual(False, factory.get_boolean(u""))
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())
 
128
        self.assertEqual("foo\n", factory.stdin.read())
 
129
        # stdin should be empty
 
130
        self.assertEqual('', factory.stdin.readline())
201
131
 
202
132
    def test_text_ui_get_integer(self):
203
133
        stdin = tests.StringIOWrapper(
240
170
        output = out.getvalue()
241
171
        self.assertContainsRe(output,
242
172
            "| foo *\r\r  *\r*")
243
 
        self.assertContainsString(output,
244
 
            r"what do you want? ([y]es, [n]o): what do you want? ([y]es, [n]o): ")
 
173
        self.assertContainsRe(output,
 
174
            r"what do you want\? \[y/n\]: what do you want\? \[y/n\]: ")
245
175
        # stdin should have been totally consumed
246
176
        self.assertEqual('', factory.stdin.readline())
247
177