test_calltips.py 7.57 KB
Newer Older
1
import unittest
2
import idlelib.calltips as ct
3
import textwrap
4 5
import types

6
default_tip = ct._default_callable_argspec
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

# Test Class TC is used in multiple get_argspec test methods
class TC():
    'doc'
    tip = "(ai=None, *b)"
    def __init__(self, ai=None, *b): 'doc'
    __init__.tip = "(self, ai=None, *b)"
    def t1(self): 'doc'
    t1.tip = "(self)"
    def t2(self, ai, b=None): 'doc'
    t2.tip = "(self, ai, b=None)"
    def t3(self, ai, *args): 'doc'
    t3.tip = "(self, ai, *args)"
    def t4(self, *args): 'doc'
    t4.tip = "(self, *args)"
    def t5(self, ai, b=None, *args, **kw): 'doc'
    t5.tip = "(self, ai, b=None, *args, **kw)"
    def t6(no, self): 'doc'
    t6.tip = "(no, self)"
    def __call__(self, ci): 'doc'
    __call__.tip = "(self, ci)"
    # attaching .tip to wrapped methods does not work
    @classmethod
    def cm(cls, a): 'doc'
    @staticmethod
    def sm(b): 'doc'

tc = TC()

signature = ct.get_argspec  # 2.7 and 3.x use different functions
class Get_signatureTest(unittest.TestCase):
38 39 40 41 42 43
    # The signature function must return a string, even if blank.
    # Test a variety of objects to be sure that none cause it to raise
    # (quite aside from getting as correct an answer as possible).
    # The tests of builtins may break if inspect or the docstrings change,
    # but a red buildbot is better than a user crash (as has happened).
    # For a simple mismatch, change the expected output to the actual.
44 45

    def test_builtins(self):
46

47 48
        # Python class that inherits builtin methods
        class List(list): "List() doc"
49

50
        # Simulate builtin with no docstring for default tip test
51 52 53 54 55
        class SB:  __call__ = None

        def gtest(obj, out):
            self.assertEqual(signature(obj), out)

56
        if List.__doc__ is not None:
57 58
            gtest(List, '(iterable=(), /)' + ct._argument_positional + '\n' +
                  List.__doc__)
59
        gtest(list.__new__,
60
               '(*args, **kwargs)\nCreate and return a new object.  See help(type) for accurate signature.')
61
        gtest(list.__init__,
62
               '(self, /, *args, **kwargs)' + ct._argument_positional + '\n' +
63
               'Initialize self.  See help(type(self)) for accurate signature.')
64 65 66 67
        append_doc = ct._argument_positional + "\nAppend object to the end of the list."
        gtest(list.append, '(self, object, /)' + append_doc)
        gtest(List.append, '(self, object, /)' + append_doc)
        gtest([].append, '(object, /)' + append_doc)
68 69

        gtest(types.MethodType, "method(function, instance)")
70
        gtest(SB(), default_tip)
71 72 73 74 75 76 77 78
        import re
        p = re.compile('')
        gtest(re.sub, '''(pattern, repl, string, count=0, flags=0)\nReturn the string obtained by replacing the leftmost
non-overlapping occurrences of the pattern in string by the
replacement repl.  repl can be either a string or a callable;
if a string, backslash escapes in it are processed.  If it is
a callable, it's passed the match object and must return''')
        gtest(p.sub, '''(repl, string, count=0)\nReturn the string obtained by replacing the leftmost non-overlapping occurrences o...''')
79

80
    def test_signature_wrap(self):
81 82
        if textwrap.TextWrapper.__doc__ is not None:
            self.assertEqual(signature(textwrap.TextWrapper), '''\
83 84
(width=70, initial_indent='', subsequent_indent='', expand_tabs=True,
    replace_whitespace=True, fix_sentence_endings=False, break_long_words=True,
85 86
    drop_whitespace=True, break_on_hyphens=True, tabsize=8, *, max_lines=None,
    placeholder=' [...]')''')
87 88 89 90 91 92

    def test_docline_truncation(self):
        def f(): pass
        f.__doc__ = 'a'*300
        self.assertEqual(signature(f), '()\n' + 'a' * (ct._MAX_COLS-3) + '...')

93 94
    def test_multiline_docstring(self):
        # Test fewer lines than max.
95 96 97
        self.assertEqual(signature(range),
                "range(stop) -> range object\n"
                "range(start, stop[, step]) -> range object")
98

99 100 101 102 103 104 105 106 107 108 109 110
        # Test max lines
        self.assertEqual(signature(bytes), '''\
bytes(iterable_of_ints) -> bytes
bytes(string, encoding[, errors]) -> bytes
bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
bytes(int) -> bytes object of size given by the parameter initialized with null bytes
bytes() -> empty bytes object''')

        # Test more than max lines
        def f(): pass
        f.__doc__ = 'a\n' * 15
        self.assertEqual(signature(f), '()' + '\na' * ct._MAX_LINES)
111

112 113 114 115 116 117 118 119 120 121 122 123
    def test_functions(self):
        def t1(): 'doc'
        t1.tip = "()"
        def t2(a, b=None): 'doc'
        t2.tip = "(a, b=None)"
        def t3(a, *args): 'doc'
        t3.tip = "(a, *args)"
        def t4(*args): 'doc'
        t4.tip = "(*args)"
        def t5(a, b=None, *args, **kw): 'doc'
        t5.tip = "(a, b=None, *args, **kw)"

124
        doc = '\ndoc' if t1.__doc__ is not None else ''
125
        for func in (t1, t2, t3, t4, t5, TC):
126
            self.assertEqual(signature(func), func.tip + doc)
127 128

    def test_methods(self):
129
        doc = '\ndoc' if TC.__doc__ is not None else ''
130
        for meth in (TC.t1, TC.t2, TC.t3, TC.t4, TC.t5, TC.t6, TC.__call__):
131 132 133
            self.assertEqual(signature(meth), meth.tip + doc)
        self.assertEqual(signature(TC.cm), "(a)" + doc)
        self.assertEqual(signature(TC.sm), "(b)" + doc)
134 135 136

    def test_bound_methods(self):
        # test that first parameter is correctly removed from argspec
137
        doc = '\ndoc' if TC.__doc__ is not None else ''
138
        for meth, mtip  in ((tc.t1, "()"), (tc.t4, "(*args)"), (tc.t6, "(self)"),
139
                            (tc.__call__, '(ci)'), (tc, '(ci)'), (TC.cm, "(a)"),):
140
            self.assertEqual(signature(meth), mtip + doc)
141

142 143 144 145 146
    def test_starred_parameter(self):
        # test that starred first parameter is *not* removed from argspec
        class C:
            def m1(*args): pass
        c = C()
147
        for meth, mtip  in ((C.m1, '(*args)'), (c.m1, "(*args)"),):
148 149
            self.assertEqual(signature(meth), mtip)

150 151 152 153 154 155 156 157 158 159
    def test_invalid_method_signature(self):
        class C:
            def m2(**kwargs): pass
        class Test:
            def __call__(*, a): pass

        mtip = ct._invalid_method
        self.assertEqual(signature(C().m2), mtip)
        self.assertEqual(signature(Test()), mtip)

160 161 162
    def test_non_ascii_name(self):
        # test that re works to delete a first parameter name that
        # includes non-ascii chars, such as various forms of A.
163 164 165 166
        uni = "(A\u0391\u0410\u05d0\u0627\u0905\u1e00\u3042, a)"
        assert ct._first_param.sub('', uni) == '(a)'

    def test_no_docstring(self):
167 168
        def nd(s):
            pass
169 170 171 172 173
        TC.nd = nd
        self.assertEqual(signature(nd), "(s)")
        self.assertEqual(signature(TC.nd), "(s)")
        self.assertEqual(signature(tc.nd), "()")

174 175 176 177
    def test_attribute_exception(self):
        class NoCall:
            def __getattr__(self, name):
                raise BaseException
178 179 180 181
        class CallA(NoCall):
            def __call__(oui, a, b, c):
                pass
        class CallB(NoCall):
182 183
            def __call__(self, ci):
                pass
184 185 186 187

        for meth, mtip  in ((NoCall, default_tip), (CallA, default_tip),
                            (NoCall(), ''), (CallA(), '(a, b, c)'),
                            (CallB(), '(ci)')):
188 189
            self.assertEqual(signature(meth), mtip)

190 191 192
    def test_non_callables(self):
        for obj in (0, 0.0, '0', b'0', [], {}):
            self.assertEqual(signature(obj), '')
193

194

195
class Get_entityTest(unittest.TestCase):
196 197 198 199 200 201 202
    def test_bad_entity(self):
        self.assertIsNone(ct.get_entity('1/0'))
    def test_good_entity(self):
        self.assertIs(ct.get_entity('int'), int)

if __name__ == '__main__':
    unittest.main(verbosity=2, exit=False)