Skip to content

Commit 983824a

Browse files
committed
edited by copilot: move from pytest-style to unittest-style
1 parent 30abd71 commit 983824a

1 file changed

Lines changed: 75 additions & 71 deletions

File tree

Lib/test/test_pyrepl/test_fancycompleter.py

Lines changed: 75 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -11,145 +11,153 @@ class ConfigForTest(DefaultConfig):
1111
class ColorConfig(DefaultConfig):
1212
use_colors = True
1313

14+
class MockPatch:
15+
def __init__(self):
16+
self.original_values = {}
17+
18+
def setattr(self, obj, name, value):
19+
if obj not in self.original_values:
20+
self.original_values[obj] = {}
21+
if name not in self.original_values[obj]:
22+
self.original_values[obj][name] = getattr(obj, name)
23+
setattr(obj, name, value)
24+
25+
def restore_all(self):
26+
for obj, attrs in self.original_values.items():
27+
for name, value in attrs.items():
28+
setattr(obj, name, value)
29+
1430
class FancyCompleterTests(unittest.TestCase):
31+
def setUp(self):
32+
self.mock_patch = MockPatch()
1533

16-
def test_commonprefix(self):
17-
assert commonprefix(['isalpha', 'isdigit', 'foo']) == ''
18-
assert commonprefix(['isalpha', 'isdigit']) == 'is'
19-
assert commonprefix(['isalpha', 'isdigit', 'foo'], base='i') == 'is'
20-
assert commonprefix([]) == ''
21-
assert commonprefix(['aaa', 'bbb'], base='x') == ''
34+
def tearDown(self):
35+
self.mock_patch.restore_all()
2236

37+
def test_commonprefix(self):
38+
self.assertEqual(commonprefix(['isalpha', 'isdigit', 'foo']), '')
39+
self.assertEqual(commonprefix(['isalpha', 'isdigit']), 'is')
40+
self.assertEqual(commonprefix(['isalpha', 'isdigit', 'foo'], base='i'), 'is')
41+
self.assertEqual(commonprefix([]), '')
42+
self.assertEqual(commonprefix(['aaa', 'bbb'], base='x'), '')
2343

2444
def test_complete_attribute(self):
2545
compl = Completer({'a': None}, ConfigForTest)
26-
assert compl.attr_matches('a.') == ['a.__']
46+
self.assertEqual(compl.attr_matches('a.'), ['a.__'])
2747
matches = compl.attr_matches('a.__')
28-
assert 'a.__class__' not in matches
29-
assert '__class__' in matches
30-
assert compl.attr_matches('a.__class') == ['a.__class__']
31-
48+
self.assertNotIn('a.__class__', matches)
49+
self.assertIn('__class__', matches)
50+
self.assertEqual(compl.attr_matches('a.__class'), ['a.__class__'])
3251

3352
def test_complete_attribute_prefix(self):
3453
class C(object):
3554
attr = 1
3655
_attr = 2
3756
__attr__attr = 3
3857
compl = Completer({'a': C}, ConfigForTest)
39-
assert compl.attr_matches('a.') == ['attr', 'mro']
40-
assert compl.attr_matches('a._') == ['_C__attr__attr', '_attr', ' ']
58+
self.assertEqual(compl.attr_matches('a.'), ['attr', 'mro'])
59+
self.assertEqual(compl.attr_matches('a._'), ['_C__attr__attr', '_attr', ' '])
4160
matches = compl.attr_matches('a.__')
42-
assert 'a.__class__' not in matches
43-
assert '__class__' in matches
44-
assert compl.attr_matches('a.__class') == ['a.__class__']
61+
self.assertNotIn('a.__class__', matches)
62+
self.assertIn('__class__', matches)
63+
self.assertEqual(compl.attr_matches('a.__class'), ['a.__class__'])
4564

4665
compl = Completer({'a': None}, ConfigForTest)
47-
assert compl.attr_matches('a._') == ['a.__']
48-
66+
self.assertEqual(compl.attr_matches('a._'), ['a.__'])
4967

5068
def test_complete_attribute_colored(self):
5169
compl = Completer({'a': 42}, ColorConfig)
5270
matches = compl.attr_matches('a.__')
53-
assert len(matches) > 2
71+
self.assertGreater(len(matches), 2)
5472
expected_color = compl.config.color_by_type.get(type(compl.__class__))
55-
assert expected_color == '35;01'
73+
self.assertEqual(expected_color, '35;01')
5674
expected_part = Color.set(expected_color, '__class__')
5775
for match in matches:
5876
if expected_part in match:
5977
break
6078
else:
61-
assert False, matches
62-
assert ' ' in matches
63-
79+
self.assertFalse(True, matches)
80+
self.assertIn(' ', matches)
6481

6582
def test_complete_colored_single_match(self):
6683
"""No coloring, via commonprefix."""
6784
compl = Completer({'foobar': 42}, ColorConfig)
6885
matches = compl.global_matches('foob')
69-
assert matches == ['foobar']
70-
86+
self.assertEqual(matches, ['foobar'])
7187

7288
def test_does_not_color_single_match(self):
7389
class obj:
7490
msgs = []
7591

7692
compl = Completer({'obj': obj}, ColorConfig)
7793
matches = compl.attr_matches('obj.msgs')
78-
assert matches == ['obj.msgs']
79-
94+
self.assertEqual(matches, ['obj.msgs'])
8095

8196
def test_complete_global(self):
8297
compl = Completer({'foobar': 1, 'foobazzz': 2}, ConfigForTest)
83-
assert compl.global_matches('foo') == ['fooba']
98+
self.assertEqual(compl.global_matches('foo'), ['fooba'])
8499
matches = compl.global_matches('fooba')
85-
assert set(matches) == set(['foobar', 'foobazzz'])
86-
assert compl.global_matches('foobaz') == ['foobazzz']
87-
assert compl.global_matches('nothing') == []
88-
100+
self.assertEqual(set(matches), set(['foobar', 'foobazzz']))
101+
self.assertEqual(compl.global_matches('foobaz'), ['foobazzz'])
102+
self.assertEqual(compl.global_matches('nothing'), [])
89103

90104
def test_complete_global_colored(self):
91105
compl = Completer({'foobar': 1, 'foobazzz': 2}, ColorConfig)
92-
assert compl.global_matches('foo') == ['fooba']
106+
self.assertEqual(compl.global_matches('foo'), ['fooba'])
93107
matches = compl.global_matches('fooba')
94-
assert set(matches) == {
108+
self.assertEqual(set(matches), {
95109
' ',
96110
'\x1b[001;00m\x1b[33;01mfoobazzz\x1b[00m',
97111
'\x1b[000;00m\x1b[33;01mfoobar\x1b[00m',
98-
}
99-
assert compl.global_matches('foobaz') == ['foobazzz']
100-
assert compl.global_matches('nothing') == []
101-
112+
})
113+
self.assertEqual(compl.global_matches('foobaz'), ['foobazzz'])
114+
self.assertEqual(compl.global_matches('nothing'), [])
102115

103116
def test_complete_global_colored_exception(self):
104117
compl = Completer({'tryme': ValueError()}, ColorConfig)
105118
if sys.version_info >= (3, 6):
106-
assert compl.global_matches('try') == [
119+
self.assertEqual(compl.global_matches('try'), [
107120
'\x1b[000;00m\x1b[37mtry:\x1b[00m',
108121
'\x1b[001;00m\x1b[31;01mtryme\x1b[00m',
109122
' '
110-
]
123+
])
111124
else:
112-
assert compl.global_matches('try') == [
125+
self.assertEqual(compl.global_matches('try'), [
113126
'\x1b[000;00m\x1b[37mtry\x1b[00m',
114127
'\x1b[001;00m\x1b[31;01mtryme\x1b[00m',
115128
' '
116-
]
117-
118-
119-
def test_complete_global_exception(monkeypatchself):
120-
import rlcompleter
129+
])
121130

131+
def test_complete_global_exception(self):
122132
def rlcompleter_global_matches(self, text):
123133
return ['trigger_exception!', 'nameerror', 'valid']
124134

125-
monkeypatch.setattr(rlcompleter.Completer, 'global_matches',
126-
rlcompleter_global_matches)
135+
self.mock_patch.setattr(rlcompleter.Completer, 'global_matches',
136+
rlcompleter_global_matches)
127137

128138
compl = Completer({'valid': 42}, ColorConfig)
129-
assert compl.global_matches("") == [
139+
self.assertEqual(compl.global_matches(""), [
130140
"\x1b[000;00m\x1b[31;01mnameerror\x1b[00m",
131141
"\x1b[001;00m\x1b[31;01mtrigger_exception!\x1b[00m",
132142
"\x1b[002;00m\x1b[33;01mvalid\x1b[00m",
133143
" ",
134-
]
135-
144+
])
136145

137-
def test_color_for_obj(monkeypatchself):
146+
def test_color_for_obj(self):
138147
class Config(ColorConfig):
139148
color_by_type = {}
140149

141150
compl = Completer({}, Config)
142-
assert compl.color_for_obj(1, "foo", "bar") == "\x1b[001;00m\x1b[00mfoo\x1b[00m"
143-
151+
self.assertEqual(compl.color_for_obj(1, "foo", "bar"),
152+
"\x1b[001;00m\x1b[00mfoo\x1b[00m")
144153

145154
def test_complete_with_indexer(self):
146155
compl = Completer({'lst': [None, 2, 3]}, ConfigForTest)
147-
assert compl.attr_matches('lst[0].') == ['lst[0].__']
156+
self.assertEqual(compl.attr_matches('lst[0].'), ['lst[0].__'])
148157
matches = compl.attr_matches('lst[0].__')
149-
assert 'lst[0].__class__' not in matches
150-
assert '__class__' in matches
151-
assert compl.attr_matches('lst[0].__class') == ['lst[0].__class__']
152-
158+
self.assertNotIn('lst[0].__class__', matches)
159+
self.assertIn('__class__', matches)
160+
self.assertEqual(compl.attr_matches('lst[0].__class'), ['lst[0].__class__'])
153161

154162
def test_autocomplete(self):
155163
class A:
@@ -165,34 +173,30 @@ class A:
165173
# automatically insert the common prefix (which will the the ANSI escape
166174
# sequence if we use colors)
167175
matches = compl.attr_matches('A.a')
168-
assert sorted(matches) == [' ', 'aaa', 'abc_1', 'abc_2', 'abc_3']
176+
self.assertEqual(sorted(matches), [' ', 'aaa', 'abc_1', 'abc_2', 'abc_3'])
169177
#
170178
# IF there is an actual common prefix, we return just it, so that readline
171179
# will insert it into place
172180
matches = compl.attr_matches('A.ab')
173-
assert matches == ['A.abc_']
181+
self.assertEqual(matches, ['A.abc_'])
174182
#
175183
# finally, at the next TAB, we display again all the completions available
176184
# for this common prefix. Agai, we insert a spurious space to prevent the
177185
# automatic completion of ANSI sequences
178186
matches = compl.attr_matches('A.abc_')
179-
assert sorted(matches) == [' ', 'abc_1', 'abc_2', 'abc_3']
180-
187+
self.assertEqual(sorted(matches), [' ', 'abc_1', 'abc_2', 'abc_3'])
181188

182189
def test_complete_exception(self):
183190
compl = Completer({}, ConfigForTest)
184-
assert compl.attr_matches('xxx.') == []
185-
191+
self.assertEqual(compl.attr_matches('xxx.'), [])
186192

187193
def test_complete_invalid_attr(self):
188194
compl = Completer({'str': str}, ConfigForTest)
189-
assert compl.attr_matches('str.xx') == []
190-
195+
self.assertEqual(compl.attr_matches('str.xx'), [])
191196

192197
def test_complete_function_skipped(self):
193198
compl = Completer({'str': str}, ConfigForTest)
194-
assert compl.attr_matches('str.split().') == []
195-
199+
self.assertEqual(compl.attr_matches('str.split().'), [])
196200

197201
def test_unicode_in___dir__(self):
198202
class Foo(object):
@@ -201,8 +205,8 @@ def __dir__(self):
201205

202206
compl = Completer({'a': Foo()}, ConfigForTest)
203207
matches = compl.attr_matches('a.')
204-
assert matches == ['hello', 'world']
205-
assert type(matches[0]) is str
208+
self.assertEqual(matches, ['hello', 'world'])
209+
self.assertIs(type(matches[0]), str)
206210

207211

208212
if __name__ == "__main__":

0 commit comments

Comments
 (0)