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