10c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yifrom test import test_support as support
20c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport unittest
30c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport __builtin__ as builtins
40c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiimport rlcompleter
50c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
60c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass CompleteMe(object):
70c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    """ Trivial class used in testing rlcompleter.Completer. """
80c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    spam = 1
90c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
100c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
110c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiclass TestRlcompleter(unittest.TestCase):
120c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def setUp(self):
130c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.stdcompleter = rlcompleter.Completer()
140c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.completer = rlcompleter.Completer(dict(spam=int,
150c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                                                    egg=str,
160c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                                                    CompleteMe=CompleteMe))
170c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
180c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        # forces stdcompleter to bind builtins namespace
190c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.stdcompleter.complete('', 0)
200c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
210c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_namespace(self):
220c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        class A(dict):
230c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            pass
240c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        class B(list):
250c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi            pass
260c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
270c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertTrue(self.stdcompleter.use_main_ns)
280c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertFalse(self.completer.use_main_ns)
290c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertFalse(rlcompleter.Completer(A()).use_main_ns)
300c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertRaises(TypeError, rlcompleter.Completer, B((1,)))
310c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
320c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_global_matches(self):
330c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        # test with builtins namespace
340c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(sorted(self.stdcompleter.global_matches('di')),
350c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                         [x+'(' for x in dir(builtins) if x.startswith('di')])
360c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(sorted(self.stdcompleter.global_matches('st')),
370c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                         [x+'(' for x in dir(builtins) if x.startswith('st')])
380c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(self.stdcompleter.global_matches('akaksajadhak'), [])
390c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
400c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        # test with a customized namespace
410c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(self.completer.global_matches('CompleteM'),
420c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                         ['CompleteMe('])
430c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(self.completer.global_matches('eg'),
440c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                         ['egg('])
450c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        # XXX: see issue5256
460c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(self.completer.global_matches('CompleteM'),
470c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                         ['CompleteMe('])
480c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
490c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    def test_attr_matches(self):
500c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        # test with builtins namespace
510c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(self.stdcompleter.attr_matches('str.s'),
520c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                         ['str.{}('.format(x) for x in dir(str)
530c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                          if x.startswith('s')])
540c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(self.stdcompleter.attr_matches('tuple.foospamegg'), [])
550c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
560c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        # test with a customized namespace
570c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(self.completer.attr_matches('CompleteMe.sp'),
580c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                         ['CompleteMe.spam'])
590c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(self.completer.attr_matches('Completeme.egg'), [])
600c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
610c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        CompleteMe.me = CompleteMe
620c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(self.completer.attr_matches('CompleteMe.me.me.sp'),
630c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                         ['CompleteMe.me.me.spam'])
640c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi        self.assertEqual(self.completer.attr_matches('egg.s'),
650c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                         ['egg.{}('.format(x) for x in dir(str)
660c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi                          if x.startswith('s')])
670c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
680c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yidef test_main():
690c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    support.run_unittest(TestRlcompleter)
700c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
710c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi
720c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yiif __name__ == '__main__':
730c5958b1636c47ed7c284f859c8e805fd06a0e6Bill Yi    test_main()
74