Home | History | Annotate | Download | only in test
      1 from test import test_support as support
      2 import unittest
      3 import __builtin__ as builtins
      4 import rlcompleter
      5 
      6 class CompleteMe(object):
      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
     34         self.assertEqual(sorted(self.stdcompleter.global_matches('di')),
     35                          [x+'(' for x in dir(builtins) if x.startswith('di')])
     36         self.assertEqual(sorted(self.stdcompleter.global_matches('st')),
     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 
     68 def test_main():
     69     support.run_unittest(TestRlcompleter)
     70 
     71 
     72 if __name__ == '__main__':
     73     test_main()
     74