1from test import test_support as support
2import unittest
3import __builtin__ as builtins
4import rlcompleter
5
6class CompleteMe(object):
7    """ Trivial class used in testing rlcompleter.Completer. """
8    spam = 1
9
10
11class 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_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
81def test_main():
82    support.run_unittest(TestRlcompleter)
83
84    def test_duplicate_globals(self):
85        namespace = {
86            'False': None,  # Keyword vs builtin vs namespace
87            'assert': None,  # Keyword vs namespace
88            'try': lambda: None,  # Keyword vs callable
89            'memoryview': None,  # Callable builtin vs non-callable
90            'Ellipsis': lambda: None,  # Non-callable builtin vs callable
91        }
92        completer = rlcompleter.Completer(namespace)
93        self.assertEqual(completer.complete('False', 0), 'False')
94        self.assertIsNone(completer.complete('False', 1))  # No duplicates
95        self.assertEqual(completer.complete('assert', 0), 'assert')
96        self.assertIsNone(completer.complete('assert', 1))
97        self.assertEqual(completer.complete('try', 0), 'try')
98        self.assertIsNone(completer.complete('try', 1))
99        # No opening bracket "(" because we overrode the built-in class
100        self.assertEqual(completer.complete('memoryview', 0), 'memoryview')
101        self.assertIsNone(completer.complete('memoryview', 1))
102        self.assertEqual(completer.complete('Ellipsis', 0), 'Ellipsis(')
103        self.assertIsNone(completer.complete('Ellipsis', 1))
104
105
106if __name__ == '__main__':
107    test_main()
108