2010-08-04 12:43:16 -03:00
|
|
|
import unittest
|
|
|
|
import builtins
|
|
|
|
import rlcompleter
|
|
|
|
|
|
|
|
class CompleteMe:
|
|
|
|
""" Trivial class used in testing rlcompleter.Completer. """
|
|
|
|
spam = 1
|
2015-09-27 07:43:50 -03:00
|
|
|
_ham = 2
|
2010-08-04 12:43:16 -03:00
|
|
|
|
|
|
|
|
|
|
|
class TestRlcompleter(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.stdcompleter = rlcompleter.Completer()
|
|
|
|
self.completer = rlcompleter.Completer(dict(spam=int,
|
|
|
|
egg=str,
|
|
|
|
CompleteMe=CompleteMe))
|
|
|
|
|
|
|
|
# forces stdcompleter to bind builtins namespace
|
|
|
|
self.stdcompleter.complete('', 0)
|
|
|
|
|
|
|
|
def test_namespace(self):
|
|
|
|
class A(dict):
|
|
|
|
pass
|
|
|
|
class B(list):
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.assertTrue(self.stdcompleter.use_main_ns)
|
|
|
|
self.assertFalse(self.completer.use_main_ns)
|
|
|
|
self.assertFalse(rlcompleter.Completer(A()).use_main_ns)
|
|
|
|
self.assertRaises(TypeError, rlcompleter.Completer, B((1,)))
|
|
|
|
|
|
|
|
def test_global_matches(self):
|
|
|
|
# test with builtins namespace
|
2010-10-21 04:40:03 -03:00
|
|
|
self.assertEqual(sorted(self.stdcompleter.global_matches('di')),
|
2010-08-04 12:43:16 -03:00
|
|
|
[x+'(' for x in dir(builtins) if x.startswith('di')])
|
2010-10-21 04:40:03 -03:00
|
|
|
self.assertEqual(sorted(self.stdcompleter.global_matches('st')),
|
2010-08-04 12:43:16 -03:00
|
|
|
[x+'(' for x in dir(builtins) if x.startswith('st')])
|
|
|
|
self.assertEqual(self.stdcompleter.global_matches('akaksajadhak'), [])
|
|
|
|
|
|
|
|
# test with a customized namespace
|
|
|
|
self.assertEqual(self.completer.global_matches('CompleteM'),
|
|
|
|
['CompleteMe('])
|
|
|
|
self.assertEqual(self.completer.global_matches('eg'),
|
|
|
|
['egg('])
|
|
|
|
# XXX: see issue5256
|
|
|
|
self.assertEqual(self.completer.global_matches('CompleteM'),
|
|
|
|
['CompleteMe('])
|
|
|
|
|
|
|
|
def test_attr_matches(self):
|
|
|
|
# test with builtins namespace
|
|
|
|
self.assertEqual(self.stdcompleter.attr_matches('str.s'),
|
|
|
|
['str.{}('.format(x) for x in dir(str)
|
|
|
|
if x.startswith('s')])
|
|
|
|
self.assertEqual(self.stdcompleter.attr_matches('tuple.foospamegg'), [])
|
2015-09-27 07:43:50 -03:00
|
|
|
expected = sorted({'None.%s%s' % (x, '(' if x != '__doc__' else '')
|
|
|
|
for x in dir(None)})
|
|
|
|
self.assertEqual(self.stdcompleter.attr_matches('None.'), expected)
|
|
|
|
self.assertEqual(self.stdcompleter.attr_matches('None._'), expected)
|
|
|
|
self.assertEqual(self.stdcompleter.attr_matches('None.__'), expected)
|
2010-08-04 12:43:16 -03:00
|
|
|
|
|
|
|
# test with a customized namespace
|
|
|
|
self.assertEqual(self.completer.attr_matches('CompleteMe.sp'),
|
|
|
|
['CompleteMe.spam'])
|
|
|
|
self.assertEqual(self.completer.attr_matches('Completeme.egg'), [])
|
2015-09-27 07:43:50 -03:00
|
|
|
self.assertEqual(self.completer.attr_matches('CompleteMe.'),
|
|
|
|
['CompleteMe.mro(', 'CompleteMe.spam'])
|
|
|
|
self.assertEqual(self.completer.attr_matches('CompleteMe._'),
|
|
|
|
['CompleteMe._ham'])
|
|
|
|
matches = self.completer.attr_matches('CompleteMe.__')
|
|
|
|
for x in matches:
|
|
|
|
self.assertTrue(x.startswith('CompleteMe.__'), x)
|
|
|
|
self.assertIn('CompleteMe.__name__', matches)
|
|
|
|
self.assertIn('CompleteMe.__new__(', matches)
|
2010-08-04 12:43:16 -03:00
|
|
|
|
|
|
|
CompleteMe.me = CompleteMe
|
|
|
|
self.assertEqual(self.completer.attr_matches('CompleteMe.me.me.sp'),
|
|
|
|
['CompleteMe.me.me.spam'])
|
|
|
|
self.assertEqual(self.completer.attr_matches('egg.s'),
|
|
|
|
['egg.{}('.format(x) for x in dir(str)
|
|
|
|
if x.startswith('s')])
|
|
|
|
|
2015-07-27 18:06:31 -03:00
|
|
|
def test_complete(self):
|
|
|
|
completer = rlcompleter.Completer()
|
|
|
|
self.assertEqual(completer.complete('', 0), '\t')
|
2015-09-27 07:26:03 -03:00
|
|
|
self.assertEqual(completer.complete('a', 0), 'and ')
|
|
|
|
self.assertEqual(completer.complete('a', 1), 'as ')
|
|
|
|
self.assertEqual(completer.complete('as', 2), 'assert ')
|
|
|
|
self.assertEqual(completer.complete('an', 0), 'and ')
|
|
|
|
self.assertEqual(completer.complete('pa', 0), 'pass')
|
|
|
|
self.assertEqual(completer.complete('Fa', 0), 'False')
|
|
|
|
self.assertEqual(completer.complete('el', 0), 'elif ')
|
|
|
|
self.assertEqual(completer.complete('el', 1), 'else')
|
|
|
|
self.assertEqual(completer.complete('tr', 0), 'try:')
|
2010-08-04 12:43:16 -03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2015-04-13 17:00:43 -03:00
|
|
|
unittest.main()
|