|
5 | 5 | from collections import namedtuple |
6 | 6 | from test.support import requires |
7 | 7 | from tkinter import Tk, Text |
| 8 | +import time |
8 | 9 |
|
9 | 10 | Editor = editor.EditorWindow |
10 | 11 |
|
@@ -239,41 +240,62 @@ def test_rclick(self): |
239 | 240 |
|
240 | 241 | class DeleteWantTest(unittest.TestCase): |
241 | 242 |
|
| 243 | + data = [ |
| 244 | + ("abcde" + 10000 * "\t" + 10000 * " ", [ |
| 245 | + (30000, 4, "abcde" + 7499 * "\t"), |
| 246 | + (41005, 4, "abcde" + 10000 * "\t" + 1001 * " "), |
| 247 | + (3, 4, "abcde"), |
| 248 | + (6, 4, "abcde"), |
| 249 | + (30002, 4, "abcde" + 7499 * "\t"), |
| 250 | + ]), |
| 251 | + ("abcde\tabd\t\t", [ |
| 252 | + (7, 4, "abcde\tabd"), |
| 253 | + (12, 4, "abcde\tabd\t"), |
| 254 | + (13, 4, "abcde\tabd\t"), |
| 255 | + (16, 4, "abcde\tabd\t"), |
| 256 | + ]), |
| 257 | + ("abcde\tabd\t \ta", [ |
| 258 | + (7, 4, "abcde\tabd"), |
| 259 | + (12, 4, "abcde\tabd\t"), |
| 260 | + (13, 4, "abcde\tabd\t "), |
| 261 | + (16, 4, "abcde\tabd\t \t"), |
| 262 | + ]), |
| 263 | + ] |
| 264 | + |
| 265 | + def mock_delete_trail_char_and_space(self, want, chars, tabwidth): |
| 266 | + ncharsdeleted = 0 |
| 267 | + while True: |
| 268 | + chars = chars[:-1] |
| 269 | + ncharsdeleted = ncharsdeleted + 1 |
| 270 | + have = len(chars.expandtabs(tabwidth)) |
| 271 | + if have <= want or chars[-1] not in " \t": |
| 272 | + break |
| 273 | + return ncharsdeleted, chars |
| 274 | + |
242 | 275 | def test_delete_trail_char_and_space(self): |
243 | 276 | with unittest.mock.patch.object(Editor, '__init__', return_value=None) as mock_init: |
| 277 | + initial_time_new = time.time() |
244 | 278 | ew = Editor() |
245 | | - |
246 | | - test_str = "abcde" + 10000 * "\t" + 10000 * " " |
247 | | - res_str = ew.delete_trail_char_and_space(30000, test_str, 4)[1] |
248 | | - self.assertEqual(res_str, "abcde" + 7499 * "\t") |
249 | | - res_str = ew.delete_trail_char_and_space(41005, test_str, 4)[1] |
250 | | - self.assertEqual(res_str, "abcde" + 10000 * "\t" + 1001 * " ") |
251 | | - res_str = ew.delete_trail_char_and_space(3, test_str, 4)[1] |
252 | | - self.assertEqual(res_str, "abcde") |
253 | | - res_str = ew.delete_trail_char_and_space(6, test_str, 4)[1] |
254 | | - self.assertEqual(res_str, "abcde") |
255 | | - res_str = ew.delete_trail_char_and_space(30002, test_str, 4)[1] |
256 | | - self.assertEqual(res_str, "abcde" + 7499 * "\t") |
257 | | - |
258 | | - test_str = "abcde\tabd\t\t" |
259 | | - res_str = ew.delete_trail_char_and_space(7, test_str, 4)[1] |
260 | | - self.assertEqual(res_str, "abcde\tabd") |
261 | | - res_str = ew.delete_trail_char_and_space(12, test_str, 4)[1] |
262 | | - self.assertEqual(res_str, "abcde\tabd\t") |
263 | | - res_str = ew.delete_trail_char_and_space(13, test_str, 4)[1] |
264 | | - self.assertEqual(res_str, "abcde\tabd\t") |
265 | | - res_str = ew.delete_trail_char_and_space(16, test_str, 4)[1] |
266 | | - self.assertEqual(res_str, "abcde\tabd\t") |
267 | | - |
268 | | - test_str = "abcde\tabd\t \ta" |
269 | | - res_str = ew.delete_trail_char_and_space(7, test_str, 4)[1] |
270 | | - self.assertEqual(res_str, "abcde\tabd") |
271 | | - res_str = ew.delete_trail_char_and_space(12, test_str, 4)[1] |
272 | | - self.assertEqual(res_str, "abcde\tabd\t") |
273 | | - res_str = ew.delete_trail_char_and_space(13, test_str, 4)[1] |
274 | | - self.assertEqual(res_str, "abcde\tabd\t ") |
275 | | - res_str = ew.delete_trail_char_and_space(16, test_str, 4)[1] |
276 | | - self.assertEqual(res_str, "abcde\tabd\t \t") |
| 279 | + for dat in self.data: |
| 280 | + test_str = dat[0] |
| 281 | + for da in dat[1]: |
| 282 | + with self.subTest(want=da[0], tabwidth=da[1], input=repr(test_str)): |
| 283 | + res_str = ew.delete_trail_char_and_space(da[0], test_str, da[1])[1] |
| 284 | + self.assertEqual(res_str, da[2]) |
| 285 | + time_new = time.time() - initial_time_new |
| 286 | + |
| 287 | + initial_time_old = time.time() |
| 288 | + with unittest.mock.patch.object(Editor, 'delete_trail_char_and_space', self.mock_delete_trail_char_and_space): |
| 289 | + ew = Editor() |
| 290 | + for dat in self.data: |
| 291 | + test_str = dat[0] |
| 292 | + for da in dat[1]: |
| 293 | + with self.subTest(want=da[0], tabwidth=da[1], input=repr(test_str)): |
| 294 | + res_str = ew.delete_trail_char_and_space(da[0], test_str, da[1])[1] |
| 295 | + self.assertEqual(res_str, da[2]) |
| 296 | + time_old = time.time() - initial_time_old |
| 297 | + |
| 298 | + self.assertGreaterEqual(time_old / time_new, 10) |
277 | 299 |
|
278 | 300 | if __name__ == '__main__': |
279 | 301 | unittest.main(verbosity=2) |
0 commit comments