Show More
@@ -431,7 +431,7 b' class BaseFormatter(Configurable):' | |||||
431 | else: |
|
431 | else: | |
432 | raise KeyError("No registered value for {0!r}".format(typ_key)) |
|
432 | raise KeyError("No registered value for {0!r}".format(typ_key)) | |
433 | else: |
|
433 | else: | |
434 | old = self.deferred_printers.pop(typ) |
|
434 | old = self.deferred_printers.pop(typ_key) | |
435 | else: |
|
435 | else: | |
436 | if typ in self.type_printers: |
|
436 | if typ in self.type_printers: | |
437 | old = self.type_printers.pop(typ) |
|
437 | old = self.type_printers.pop(typ) |
@@ -90,20 +90,18 b' def test_bad_precision():' | |||||
90 | nt.assert_raises(ValueError, set_fp, 'foo') |
|
90 | nt.assert_raises(ValueError, set_fp, 'foo') | |
91 | nt.assert_raises(ValueError, set_fp, -1) |
|
91 | nt.assert_raises(ValueError, set_fp, -1) | |
92 |
|
92 | |||
93 |
|
||||
94 | def test_for_type(): |
|
93 | def test_for_type(): | |
95 | f = PlainTextFormatter() |
|
94 | f = PlainTextFormatter() | |
96 |
|
95 | |||
97 |
# initial return |
|
96 | # initial return, None | |
98 |
assert |
|
97 | nt.assert_is(f.for_type(C, foo_printer), None) | |
99 | # no func queries |
|
98 | # no func queries | |
100 |
assert |
|
99 | nt.assert_is(f.for_type(C), foo_printer) | |
101 | # shouldn't change anything |
|
100 | # shouldn't change anything | |
102 |
assert |
|
101 | nt.assert_is(f.for_type(C), foo_printer) | |
103 | # None should do the same |
|
102 | # None should do the same | |
104 |
assert |
|
103 | nt.assert_is(f.for_type(C, None), foo_printer) | |
105 |
assert |
|
104 | nt.assert_is(f.for_type(C, None), foo_printer) | |
106 |
|
||||
107 |
|
105 | |||
108 | def test_for_type_string(): |
|
106 | def test_for_type_string(): | |
109 | f = PlainTextFormatter() |
|
107 | f = PlainTextFormatter() | |
@@ -112,42 +110,101 b' def test_for_type_string():' | |||||
112 |
|
110 | |||
113 | type_str = '%s.%s' % (C.__module__, 'C') |
|
111 | type_str = '%s.%s' % (C.__module__, 'C') | |
114 |
|
112 | |||
115 |
# initial return |
|
113 | # initial return, None | |
116 |
assert |
|
114 | nt.assert_is(f.for_type(type_str, foo_printer), None) | |
117 | # no func queries |
|
115 | # no func queries | |
118 |
assert |
|
116 | nt.assert_is(f.for_type(type_str), foo_printer) | |
119 |
assert |
|
117 | nt.assert_in(_mod_name_key(C), f.deferred_printers) | |
120 |
assert |
|
118 | nt.assert_is(f.for_type(C), foo_printer) | |
121 |
assert |
|
119 | nt.assert_not_in(_mod_name_key(C), f.deferred_printers) | |
122 |
assert |
|
120 | nt.assert_in(C, f.type_printers) | |
123 |
|
||||
124 |
|
121 | |||
125 | def test_for_type_by_name(): |
|
122 | def test_for_type_by_name(): | |
126 | f = PlainTextFormatter() |
|
123 | f = PlainTextFormatter() | |
127 |
|
124 | |||
128 | mod = C.__module__ |
|
125 | mod = C.__module__ | |
129 |
|
126 | |||
130 |
# initial return |
|
127 | # initial return, None | |
131 |
assert |
|
128 | nt.assert_is(f.for_type_by_name(mod, 'C', foo_printer), None) | |
132 | # no func queries |
|
129 | # no func queries | |
133 |
assert |
|
130 | nt.assert_is(f.for_type_by_name(mod, 'C'), foo_printer) | |
134 | # shouldn't change anything |
|
131 | # shouldn't change anything | |
135 |
assert |
|
132 | nt.assert_is(f.for_type_by_name(mod, 'C'), foo_printer) | |
136 | # None should do the same |
|
133 | # None should do the same | |
137 |
assert |
|
134 | nt.assert_is(f.for_type_by_name(mod, 'C', None), foo_printer) | |
138 |
assert |
|
135 | nt.assert_is(f.for_type_by_name(mod, 'C', None), foo_printer) | |
139 |
|
||||
140 |
|
136 | |||
141 | def test_lookup(): |
|
137 | def test_lookup(): | |
142 | f = PlainTextFormatter() |
|
138 | f = PlainTextFormatter() | |
143 |
|
139 | |||
144 | mod = C.__module__ |
|
140 | f.for_type(C, foo_printer) | |
145 | c = C() |
|
141 | nt.assert_is(f.lookup(C()), foo_printer) | |
|
142 | with nt.assert_raises(KeyError): | |||
|
143 | f.lookup(A()) | |||
|
144 | ||||
|
145 | def test_lookup_string(): | |||
|
146 | f = PlainTextFormatter() | |||
|
147 | type_str = '%s.%s' % (C.__module__, 'C') | |||
|
148 | ||||
|
149 | f.for_type(type_str, foo_printer) | |||
|
150 | nt.assert_is(f.lookup(C()), foo_printer) | |||
|
151 | # should move from deferred to imported dict | |||
|
152 | nt.assert_not_in(_mod_name_key(C), f.deferred_printers) | |||
|
153 | nt.assert_in(C, f.type_printers) | |||
|
154 | ||||
|
155 | def test_lookup_by_type(): | |||
|
156 | f = PlainTextFormatter() | |||
|
157 | f.for_type(C, foo_printer) | |||
|
158 | nt.assert_is(f.lookup_by_type(C), foo_printer) | |||
|
159 | type_str = '%s.%s' % (C.__module__, 'C') | |||
|
160 | with nt.assert_raises(KeyError): | |||
|
161 | f.lookup_by_type(A) | |||
|
162 | ||||
|
163 | def test_lookup_by_type_string(): | |||
|
164 | f = PlainTextFormatter() | |||
|
165 | type_str = '%s.%s' % (C.__module__, 'C') | |||
|
166 | f.for_type(type_str, foo_printer) | |||
|
167 | ||||
|
168 | # verify insertion | |||
|
169 | nt.assert_in(_mod_name_key(C), f.deferred_printers) | |||
|
170 | nt.assert_not_in(C, f.type_printers) | |||
146 |
|
171 | |||
|
172 | nt.assert_is(f.lookup_by_type(C), foo_printer) | |||
|
173 | # should move from deferred to imported dict | |||
|
174 | nt.assert_not_in(_mod_name_key(C), f.deferred_printers) | |||
|
175 | nt.assert_in(C, f.type_printers) | |||
|
176 | ||||
|
177 | def test_pop(): | |||
|
178 | f = PlainTextFormatter() | |||
|
179 | f.for_type(C, foo_printer) | |||
|
180 | nt.assert_is(f.lookup_by_type(C), foo_printer) | |||
|
181 | f.pop(C) | |||
|
182 | with nt.assert_raises(KeyError): | |||
|
183 | f.lookup_by_type(C) | |||
|
184 | with nt.assert_raises(KeyError): | |||
|
185 | f.pop(C) | |||
|
186 | with nt.assert_raises(KeyError): | |||
|
187 | f.pop(A) | |||
|
188 | ||||
|
189 | def test_pop_string(): | |||
|
190 | f = PlainTextFormatter() | |||
147 | type_str = '%s.%s' % (C.__module__, 'C') |
|
191 | type_str = '%s.%s' % (C.__module__, 'C') | |
148 |
|
192 | |||
149 | # initial return is None |
|
193 | with nt.assert_raises(KeyError): | |
150 | assert f.for_type(type_str, foo_printer) is None |
|
194 | f.pop(type_str) | |
151 | # no func queries |
|
195 | ||
152 | assert f.lookup(c) is foo_printer |
|
196 | f.for_type(type_str, foo_printer) | |
|
197 | f.pop(type_str) | |||
|
198 | with nt.assert_raises(KeyError): | |||
|
199 | f.lookup_by_type(C) | |||
|
200 | with nt.assert_raises(KeyError): | |||
|
201 | f.pop(type_str) | |||
|
202 | ||||
|
203 | f.for_type(C, foo_printer) | |||
|
204 | f.pop(type_str) | |||
|
205 | with nt.assert_raises(KeyError): | |||
|
206 | f.lookup_by_type(C) | |||
|
207 | with nt.assert_raises(KeyError): | |||
|
208 | f.pop(type_str) | |||
|
209 | ||||
153 |
|
210 |
General Comments 0
You need to be logged in to leave comments.
Login now