Antoine Pitrou | 482e66a | 2010-08-04 15:43:16 +0000 | [diff] [blame] | 1 | import unittest |
Yury Selivanov | 46f7785 | 2016-02-04 14:00:26 -0500 | [diff] [blame] | 2 | import unittest.mock |
Antoine Pitrou | 482e66a | 2010-08-04 15:43:16 +0000 | [diff] [blame] | 3 | import builtins |
| 4 | import rlcompleter |
| 5 | |
| 6 | class CompleteMe: |
| 7 | """ Trivial class used in testing rlcompleter.Completer. """ |
| 8 | spam = 1 |
| 9 | |
| 10 | |
| 11 | class TestRlcompleter(unittest.TestCase): |
| 12 | def setUp(self): |
| 13 | self.stdcompleter = rlcompleter.Completer() |
| 14 | self.completer = rlcompleter.Completer(dict(spam=int, |
| 15 | egg=str, |
| 16 | CompleteMe=CompleteMe)) |
| 17 | |
| 18 | # forces stdcompleter to bind builtins namespace |
| 19 | self.stdcompleter.complete('', 0) |
| 20 | |
| 21 | def test_namespace(self): |
| 22 | class A(dict): |
| 23 | pass |
| 24 | class B(list): |
| 25 | pass |
| 26 | |
| 27 | self.assertTrue(self.stdcompleter.use_main_ns) |
| 28 | self.assertFalse(self.completer.use_main_ns) |
| 29 | self.assertFalse(rlcompleter.Completer(A()).use_main_ns) |
| 30 | self.assertRaises(TypeError, rlcompleter.Completer, B((1,))) |
| 31 | |
| 32 | def test_global_matches(self): |
| 33 | # test with builtins namespace |
Georg Brandl | 50de5f5 | 2010-10-21 07:40:03 +0000 | [diff] [blame] | 34 | self.assertEqual(sorted(self.stdcompleter.global_matches('di')), |
Antoine Pitrou | 482e66a | 2010-08-04 15:43:16 +0000 | [diff] [blame] | 35 | [x+'(' for x in dir(builtins) if x.startswith('di')]) |
Georg Brandl | 50de5f5 | 2010-10-21 07:40:03 +0000 | [diff] [blame] | 36 | self.assertEqual(sorted(self.stdcompleter.global_matches('st')), |
Antoine Pitrou | 482e66a | 2010-08-04 15:43:16 +0000 | [diff] [blame] | 37 | [x+'(' for x in dir(builtins) if x.startswith('st')]) |
| 38 | self.assertEqual(self.stdcompleter.global_matches('akaksajadhak'), []) |
| 39 | |
| 40 | # test with a customized namespace |
| 41 | self.assertEqual(self.completer.global_matches('CompleteM'), |
| 42 | ['CompleteMe(']) |
| 43 | self.assertEqual(self.completer.global_matches('eg'), |
| 44 | ['egg(']) |
| 45 | # XXX: see issue5256 |
| 46 | self.assertEqual(self.completer.global_matches('CompleteM'), |
| 47 | ['CompleteMe(']) |
| 48 | |
| 49 | def test_attr_matches(self): |
| 50 | # test with builtins namespace |
| 51 | self.assertEqual(self.stdcompleter.attr_matches('str.s'), |
| 52 | ['str.{}('.format(x) for x in dir(str) |
| 53 | if x.startswith('s')]) |
| 54 | self.assertEqual(self.stdcompleter.attr_matches('tuple.foospamegg'), []) |
| 55 | |
| 56 | # test with a customized namespace |
| 57 | self.assertEqual(self.completer.attr_matches('CompleteMe.sp'), |
| 58 | ['CompleteMe.spam']) |
| 59 | self.assertEqual(self.completer.attr_matches('Completeme.egg'), []) |
| 60 | |
| 61 | CompleteMe.me = CompleteMe |
| 62 | self.assertEqual(self.completer.attr_matches('CompleteMe.me.me.sp'), |
| 63 | ['CompleteMe.me.me.spam']) |
| 64 | self.assertEqual(self.completer.attr_matches('egg.s'), |
| 65 | ['egg.{}('.format(x) for x in dir(str) |
| 66 | if x.startswith('s')]) |
| 67 | |
Martin Panter | 06622ea | 2015-11-13 22:47:00 +0000 | [diff] [blame] | 68 | def test_excessive_getattr(self): |
| 69 | # Ensure getattr() is invoked no more than once per attribute |
| 70 | class Foo: |
| 71 | calls = 0 |
| 72 | @property |
| 73 | def bar(self): |
| 74 | self.calls += 1 |
| 75 | return None |
| 76 | f = Foo() |
| 77 | completer = rlcompleter.Completer(dict(f=f)) |
| 78 | self.assertEqual(completer.complete('f.b', 0), 'f.bar') |
| 79 | self.assertEqual(f.calls, 1) |
| 80 | |
Yury Selivanov | 46f7785 | 2016-02-04 14:00:26 -0500 | [diff] [blame] | 81 | @unittest.mock.patch('rlcompleter._readline_available', False) |
Berker Peksag | aaf6114 | 2015-07-28 00:06:31 +0300 | [diff] [blame] | 82 | def test_complete(self): |
| 83 | completer = rlcompleter.Completer() |
| 84 | self.assertEqual(completer.complete('', 0), '\t') |
| 85 | self.assertEqual(completer.complete('a', 0), 'and') |
| 86 | self.assertEqual(completer.complete('a', 1), 'as') |
| 87 | self.assertEqual(completer.complete('as', 2), 'assert') |
| 88 | self.assertEqual(completer.complete('an', 0), 'and') |
Antoine Pitrou | 482e66a | 2010-08-04 15:43:16 +0000 | [diff] [blame] | 89 | |
Martin Panter | ed92910 | 2015-11-23 23:50:26 +0000 | [diff] [blame] | 90 | def test_duplicate_globals(self): |
| 91 | namespace = { |
| 92 | 'False': None, # Keyword vs builtin vs namespace |
| 93 | 'assert': None, # Keyword vs namespace |
| 94 | 'try': lambda: None, # Keyword vs callable |
| 95 | 'memoryview': None, # Callable builtin vs non-callable |
| 96 | 'Ellipsis': lambda: None, # Non-callable builtin vs callable |
| 97 | } |
| 98 | completer = rlcompleter.Completer(namespace) |
| 99 | self.assertEqual(completer.complete('False', 0), 'False') |
| 100 | self.assertIsNone(completer.complete('False', 1)) # No duplicates |
| 101 | self.assertEqual(completer.complete('assert', 0), 'assert') |
| 102 | self.assertIsNone(completer.complete('assert', 1)) |
| 103 | self.assertEqual(completer.complete('try', 0), 'try') |
| 104 | self.assertIsNone(completer.complete('try', 1)) |
| 105 | # No opening bracket "(" because we overrode the built-in class |
| 106 | self.assertEqual(completer.complete('memoryview', 0), 'memoryview') |
| 107 | self.assertIsNone(completer.complete('memoryview', 1)) |
| 108 | self.assertEqual(completer.complete('Ellipsis', 0), 'Ellipsis(') |
| 109 | self.assertIsNone(completer.complete('Ellipsis', 1)) |
| 110 | |
Antoine Pitrou | 482e66a | 2010-08-04 15:43:16 +0000 | [diff] [blame] | 111 | if __name__ == '__main__': |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 112 | unittest.main() |