1
1
import unittest
2
2
3
- from _colorize import ANSIColors
4
- from _pyrepl .fancycompleter import Completer , DefaultConfig , commonprefix
5
-
6
-
7
- class ConfigForTest (DefaultConfig ):
8
- use_colors = False
9
-
10
- class ColorConfig (DefaultConfig ):
11
- use_colors = True
3
+ from _colorize import ANSIColors , get_theme
4
+ from _pyrepl .fancycompleter import Completer , commonprefix
12
5
13
6
class MockPatch :
14
7
def __init__ (self ):
@@ -41,7 +34,7 @@ def test_commonprefix(self):
41
34
self .assertEqual (commonprefix (['aaa' , 'bbb' ], base = 'x' ), '' )
42
35
43
36
def test_complete_attribute (self ):
44
- compl = Completer ({'a' : None }, ConfigForTest )
37
+ compl = Completer ({'a' : None }, use_colors = False )
45
38
self .assertEqual (compl .attr_matches ('a.' ), ['a.__' ])
46
39
matches = compl .attr_matches ('a.__' )
47
40
self .assertNotIn ('a.__class__' , matches )
@@ -53,23 +46,23 @@ class C(object):
53
46
attr = 1
54
47
_attr = 2
55
48
__attr__attr = 3
56
- compl = Completer ({'a' : C }, ConfigForTest )
49
+ compl = Completer ({'a' : C }, use_colors = False )
57
50
self .assertEqual (compl .attr_matches ('a.' ), ['attr' , 'mro' ])
58
51
self .assertEqual (compl .attr_matches ('a._' ), ['_C__attr__attr' , '_attr' , ' ' ])
59
52
matches = compl .attr_matches ('a.__' )
60
53
self .assertNotIn ('a.__class__' , matches )
61
54
self .assertIn ('__class__' , matches )
62
55
self .assertEqual (compl .attr_matches ('a.__class' ), ['a.__class__' ])
63
56
64
- compl = Completer ({'a' : None }, ConfigForTest )
57
+ compl = Completer ({'a' : None }, use_colors = False )
65
58
self .assertEqual (compl .attr_matches ('a._' ), ['a.__' ])
66
59
67
60
def test_complete_attribute_colored (self ):
68
- compl = Completer ({'a' : 42 }, ColorConfig )
61
+ theme = get_theme ()
62
+ compl = Completer ({'a' : 42 }, use_colors = True )
69
63
matches = compl .attr_matches ('a.__' )
70
64
self .assertGreater (len (matches ), 2 )
71
- expected_color = compl .config .color_by_type .get (type (compl .__class__ ))
72
- self .assertEqual (expected_color , ANSIColors .BOLD_MAGENTA )
65
+ expected_color = theme .fancycompleter .type
73
66
expected_part = f'{ expected_color } __class__{ ANSIColors .RESET } '
74
67
for match in matches :
75
68
if expected_part in match :
@@ -80,28 +73,28 @@ def test_complete_attribute_colored(self):
80
73
81
74
def test_complete_colored_single_match (self ):
82
75
"""No coloring, via commonprefix."""
83
- compl = Completer ({'foobar' : 42 }, ColorConfig )
76
+ compl = Completer ({'foobar' : 42 }, use_colors = True )
84
77
matches = compl .global_matches ('foob' )
85
78
self .assertEqual (matches , ['foobar' ])
86
79
87
80
def test_does_not_color_single_match (self ):
88
81
class obj :
89
82
msgs = []
90
83
91
- compl = Completer ({'obj' : obj }, ColorConfig )
84
+ compl = Completer ({'obj' : obj }, use_colors = True )
92
85
matches = compl .attr_matches ('obj.msgs' )
93
86
self .assertEqual (matches , ['obj.msgs' ])
94
87
95
88
def test_complete_global (self ):
96
- compl = Completer ({'foobar' : 1 , 'foobazzz' : 2 }, ConfigForTest )
89
+ compl = Completer ({'foobar' : 1 , 'foobazzz' : 2 }, use_colors = False )
97
90
self .assertEqual (compl .global_matches ('foo' ), ['fooba' ])
98
91
matches = compl .global_matches ('fooba' )
99
92
self .assertEqual (set (matches ), set (['foobar' , 'foobazzz' ]))
100
93
self .assertEqual (compl .global_matches ('foobaz' ), ['foobazzz' ])
101
94
self .assertEqual (compl .global_matches ('nothing' ), [])
102
95
103
96
def test_complete_global_colored (self ):
104
- compl = Completer ({'foobar' : 1 , 'foobazzz' : 2 }, ColorConfig )
97
+ compl = Completer ({'foobar' : 1 , 'foobazzz' : 2 }, use_colors = True )
105
98
self .assertEqual (compl .global_matches ('foo' ), ['fooba' ])
106
99
matches = compl .global_matches ('fooba' )
107
100
@@ -119,7 +112,7 @@ def test_complete_global_colored(self):
119
112
self .assertEqual (compl .global_matches ('nothing' ), [])
120
113
121
114
def test_complete_global_colored_exception (self ):
122
- compl = Completer ({'tryme' : 42 }, ColorConfig )
115
+ compl = Completer ({'tryme' : 42 }, use_colors = True )
123
116
N0 = f"\x1b [000;00m"
124
117
N1 = f"\x1b [001;00m"
125
118
self .assertEqual (compl .global_matches ('try' ), [
@@ -129,7 +122,7 @@ def test_complete_global_colored_exception(self):
129
122
])
130
123
131
124
def test_complete_with_indexer (self ):
132
- compl = Completer ({'lst' : [None , 2 , 3 ]}, ConfigForTest )
125
+ compl = Completer ({'lst' : [None , 2 , 3 ]}, use_colors = False )
133
126
self .assertEqual (compl .attr_matches ('lst[0].' ), ['lst[0].__' ])
134
127
matches = compl .attr_matches ('lst[0].__' )
135
128
self .assertNotIn ('lst[0].__class__' , matches )
@@ -143,7 +136,7 @@ class A:
143
136
abc_2 = None
144
137
abc_3 = None
145
138
bbb = None
146
- compl = Completer ({'A' : A }, ConfigForTest )
139
+ compl = Completer ({'A' : A }, use_colors = False )
147
140
#
148
141
# In this case, we want to display all attributes which start with
149
142
# 'a'. Moreover, we also include a space to prevent readline to
@@ -164,23 +157,23 @@ class A:
164
157
self .assertEqual (sorted (matches ), [' ' , 'abc_1' , 'abc_2' , 'abc_3' ])
165
158
166
159
def test_complete_exception (self ):
167
- compl = Completer ({}, ConfigForTest )
160
+ compl = Completer ({}, use_colors = False )
168
161
self .assertEqual (compl .attr_matches ('xxx.' ), [])
169
162
170
163
def test_complete_invalid_attr (self ):
171
- compl = Completer ({'str' : str }, ConfigForTest )
164
+ compl = Completer ({'str' : str }, use_colors = False )
172
165
self .assertEqual (compl .attr_matches ('str.xx' ), [])
173
166
174
167
def test_complete_function_skipped (self ):
175
- compl = Completer ({'str' : str }, ConfigForTest )
168
+ compl = Completer ({'str' : str }, use_colors = False )
176
169
self .assertEqual (compl .attr_matches ('str.split().' ), [])
177
170
178
171
def test_unicode_in___dir__ (self ):
179
172
class Foo (object ):
180
173
def __dir__ (self ):
181
174
return ['hello' , 'world' ]
182
175
183
- compl = Completer ({'a' : Foo ()}, ConfigForTest )
176
+ compl = Completer ({'a' : Foo ()}, use_colors = False )
184
177
matches = compl .attr_matches ('a.' )
185
178
self .assertEqual (matches , ['hello' , 'world' ])
186
179
self .assertIs (type (matches [0 ]), str )
0 commit comments