2 # Copyright (C) 2019 EfficiOS Inc.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; only version 2
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 from functools
import partial
, partialmethod
28 class _TestCopySimple
:
30 with self
.assertRaises(NotImplementedError):
33 def test_deepcopy(self
):
34 with self
.assertRaises(NotImplementedError):
35 copy
.deepcopy(self
._def
)
44 class _TestNumericValue(_TestCopySimple
):
45 def _binop(self
, op
, rhs
):
51 r
= op(self
._def
, rhs
)
52 except Exception as e
:
56 rv
= op(self
._def
_value
, comp_value
)
57 except Exception as e
:
60 if rexc
is not None or rvexc
is not None:
61 # at least one of the operations raised an exception: in
62 # this case both operations should have raised the same
63 # type of exception (division by zero, bit shift with a
64 # floating point number operand, etc.)
65 self
.assertIs(type(rexc
), type(rvexc
))
70 def _unaryop(self
, op
):
76 except Exception as e
:
80 rv
= op(self
._def
_value
)
81 except Exception as e
:
84 if rexc
is not None or rvexc
is not None:
85 # at least one of the operations raised an exception: in
86 # this case both operations should have raised the same
87 # type of exception (division by zero, bit shift with a
88 # floating point number operand, etc.)
89 self
.assertIs(type(rexc
), type(rvexc
))
94 def _test_unaryop_type(self
, op
):
95 r
, rv
= self
._unaryop
(op
)
100 self
.assertIsInstance(r
, type(rv
))
102 def _test_unaryop_value(self
, op
):
103 r
, rv
= self
._unaryop
(op
)
108 self
.assertEqual(r
, rv
)
110 def _test_unaryop_addr_same(self
, op
):
111 addr_before
= self
._def
.addr
113 self
.assertEqual(self
._def
.addr
, addr_before
)
115 def _test_unaryop_value_same(self
, op
):
116 value_before
= self
._def
.__class
__(self
._def
)
118 self
.assertEqual(self
._def
, value_before
)
120 def _test_binop_type(self
, op
, rhs
):
121 r
, rv
= self
._binop
(op
, rhs
)
126 if op
in _COMP_BINOPS
:
127 # __eq__() and __ne__() always return a 'bool' object
128 self
.assertIsInstance(r
, bool)
130 self
.assertIsInstance(r
, type(rv
))
132 def _test_binop_value(self
, op
, rhs
):
133 r
, rv
= self
._binop
(op
, rhs
)
138 self
.assertEqual(r
, rv
)
140 def _test_binop_lhs_addr_same(self
, op
, rhs
):
141 addr_before
= self
._def
.addr
142 r
, rv
= self
._binop
(op
, rhs
)
143 self
.assertEqual(self
._def
.addr
, addr_before
)
145 def _test_binop_lhs_value_same(self
, op
, rhs
):
146 value_before
= self
._def
.__class
__(self
._def
)
147 r
, rv
= self
._binop
(op
, rhs
)
148 self
.assertEqual(self
._def
, value_before
)
150 def _test_binop_invalid_unknown(self
, op
):
151 if op
in _COMP_BINOPS
:
152 self
.skipTest('not testing')
157 with self
.assertRaises(TypeError):
160 def _test_binop_invalid_none(self
, op
):
161 if op
in _COMP_BINOPS
:
162 self
.skipTest('not testing')
164 with self
.assertRaises(TypeError):
167 def _test_binop_rhs_false(self
, test_cb
, op
):
170 def _test_binop_rhs_true(self
, test_cb
, op
):
173 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
176 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
179 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
182 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
183 test_cb(op
, bt2
.create_value(2))
185 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
186 test_cb(op
, bt2
.create_value(-23))
188 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
189 test_cb(op
, bt2
.create_value(0))
191 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
194 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
197 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
200 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
201 test_cb(op
, bt2
.create_value(2.2))
203 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
204 test_cb(op
, bt2
.create_value(-23.4))
206 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
207 test_cb(op
, bt2
.create_value(0.0))
209 def _test_binop_type_false(self
, op
):
210 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
212 def _test_binop_type_true(self
, op
):
213 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
215 def _test_binop_type_pos_int(self
, op
):
216 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
218 def _test_binop_type_neg_int(self
, op
):
219 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
221 def _test_binop_type_zero_int(self
, op
):
222 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
224 def _test_binop_type_pos_vint(self
, op
):
225 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
227 def _test_binop_type_neg_vint(self
, op
):
228 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
230 def _test_binop_type_zero_vint(self
, op
):
231 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
233 def _test_binop_type_pos_float(self
, op
):
234 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
236 def _test_binop_type_neg_float(self
, op
):
237 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
239 def _test_binop_type_zero_float(self
, op
):
240 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
242 def _test_binop_type_pos_vfloat(self
, op
):
243 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
245 def _test_binop_type_neg_vfloat(self
, op
):
246 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
248 def _test_binop_type_zero_vfloat(self
, op
):
249 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
251 def _test_binop_value_false(self
, op
):
252 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
254 def _test_binop_value_true(self
, op
):
255 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
257 def _test_binop_value_pos_int(self
, op
):
258 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
260 def _test_binop_value_neg_int(self
, op
):
261 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
263 def _test_binop_value_zero_int(self
, op
):
264 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
266 def _test_binop_value_pos_vint(self
, op
):
267 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
269 def _test_binop_value_neg_vint(self
, op
):
270 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
272 def _test_binop_value_zero_vint(self
, op
):
273 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
275 def _test_binop_value_pos_float(self
, op
):
276 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
278 def _test_binop_value_neg_float(self
, op
):
279 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
281 def _test_binop_value_zero_float(self
, op
):
282 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
284 def _test_binop_value_pos_vfloat(self
, op
):
285 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
287 def _test_binop_value_neg_vfloat(self
, op
):
288 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
290 def _test_binop_value_zero_vfloat(self
, op
):
291 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
293 def _test_binop_lhs_addr_same_false(self
, op
):
294 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
296 def _test_binop_lhs_addr_same_true(self
, op
):
297 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
299 def _test_binop_lhs_addr_same_pos_int(self
, op
):
300 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
302 def _test_binop_lhs_addr_same_neg_int(self
, op
):
303 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
305 def _test_binop_lhs_addr_same_zero_int(self
, op
):
306 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
308 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
309 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
311 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
312 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
314 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
315 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
317 def _test_binop_lhs_addr_same_pos_float(self
, op
):
318 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
320 def _test_binop_lhs_addr_same_neg_float(self
, op
):
321 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
323 def _test_binop_lhs_addr_same_zero_float(self
, op
):
324 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
326 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
327 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
329 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
330 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
332 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
333 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
335 def _test_binop_lhs_value_same_false(self
, op
):
336 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
338 def _test_binop_lhs_value_same_true(self
, op
):
339 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
341 def _test_binop_lhs_value_same_pos_int(self
, op
):
342 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
344 def _test_binop_lhs_value_same_neg_int(self
, op
):
345 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
347 def _test_binop_lhs_value_same_zero_int(self
, op
):
348 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
350 def _test_binop_lhs_value_same_pos_vint(self
, op
):
351 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
353 def _test_binop_lhs_value_same_neg_vint(self
, op
):
354 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
356 def _test_binop_lhs_value_same_zero_vint(self
, op
):
357 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
359 def _test_binop_lhs_value_same_pos_float(self
, op
):
360 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
362 def _test_binop_lhs_value_same_neg_float(self
, op
):
363 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
365 def _test_binop_lhs_value_same_zero_float(self
, op
):
366 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
368 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
369 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
371 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
372 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
374 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
375 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
377 def test_bool_op(self
):
378 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
380 def test_int_op(self
):
381 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
383 def test_float_op(self
):
384 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
386 def test_complex_op(self
):
387 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
389 def test_str_op(self
):
390 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
392 def test_eq_none(self
):
393 self
.assertFalse(self
._def
== None)
395 def test_ne_none(self
):
396 self
.assertTrue(self
._def
!= None)
406 ('add', operator
.add
),
407 ('radd', lambda a
, b
: operator
.add(b
, a
)),
408 ('and', operator
.and_
),
409 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
410 ('floordiv', operator
.floordiv
),
411 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
412 ('lshift', operator
.lshift
),
413 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
414 ('mod', operator
.mod
),
415 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
416 ('mul', operator
.mul
),
417 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
418 ('or', operator
.or_
),
419 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
420 ('pow', operator
.pow),
421 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
422 ('rshift', operator
.rshift
),
423 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
424 ('sub', operator
.sub
),
425 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
426 ('truediv', operator
.truediv
),
427 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
428 ('xor', operator
.xor
),
429 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
434 ('neg', operator
.neg
),
435 ('pos', operator
.pos
),
436 ('abs', operator
.abs),
437 ('invert', operator
.invert
),
439 ('round_0', partial(round, ndigits
=0)),
440 ('round_1', partial(round, ndigits
=1)),
441 ('round_2', partial(round, ndigits
=2)),
442 ('round_3', partial(round, ndigits
=3)),
444 ('floor', math
.floor
),
445 ('trunc', math
.trunc
),
449 def _inject_numeric_testing_methods(cls
, has_neg
=True):
450 def test_binop_name(suffix
):
451 return 'test_binop_{}_{}'.format(name
, suffix
)
453 def test_unaryop_name(suffix
):
454 return 'test_unaryop_{}_{}'.format(name
, suffix
)
456 # inject testing methods for each binary operation
457 for name
, binop
in _BINOPS
:
458 setattr(cls
, test_binop_name('invalid_unknown'), partialmethod(_TestNumericValue
._test
_binop
_invalid
_unknown
, op
=binop
))
459 setattr(cls
, test_binop_name('invalid_none'), partialmethod(_TestNumericValue
._test
_binop
_invalid
_none
, op
=binop
))
460 setattr(cls
, test_binop_name('type_true'), partialmethod(_TestNumericValue
._test
_binop
_type
_true
, op
=binop
))
461 setattr(cls
, test_binop_name('type_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_int
, op
=binop
))
462 setattr(cls
, test_binop_name('type_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_vint
, op
=binop
))
463 setattr(cls
, test_binop_name('value_true'), partialmethod(_TestNumericValue
._test
_binop
_value
_true
, op
=binop
))
464 setattr(cls
, test_binop_name('value_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_int
, op
=binop
))
465 setattr(cls
, test_binop_name('value_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_vint
, op
=binop
))
466 setattr(cls
, test_binop_name('lhs_addr_same_true'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
))
467 setattr(cls
, test_binop_name('lhs_addr_same_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
))
468 setattr(cls
, test_binop_name('lhs_addr_same_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
))
469 setattr(cls
, test_binop_name('lhs_value_same_true'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_true
, op
=binop
))
470 setattr(cls
, test_binop_name('lhs_value_same_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
))
471 setattr(cls
, test_binop_name('lhs_value_same_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
))
474 setattr(cls
, test_binop_name('type_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_int
, op
=binop
))
475 setattr(cls
, test_binop_name('type_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_vint
, op
=binop
))
476 setattr(cls
, test_binop_name('value_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_int
, op
=binop
))
477 setattr(cls
, test_binop_name('value_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_vint
, op
=binop
))
478 setattr(cls
, test_binop_name('lhs_addr_same_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
))
479 setattr(cls
, test_binop_name('lhs_addr_same_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
))
480 setattr(cls
, test_binop_name('lhs_value_same_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
))
481 setattr(cls
, test_binop_name('lhs_value_same_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
))
483 setattr(cls
, test_binop_name('type_false'), partialmethod(_TestNumericValue
._test
_binop
_type
_false
, op
=binop
))
484 setattr(cls
, test_binop_name('type_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_int
, op
=binop
))
485 setattr(cls
, test_binop_name('type_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_vint
, op
=binop
))
486 setattr(cls
, test_binop_name('value_false'), partialmethod(_TestNumericValue
._test
_binop
_value
_false
, op
=binop
))
487 setattr(cls
, test_binop_name('value_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_int
, op
=binop
))
488 setattr(cls
, test_binop_name('value_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_vint
, op
=binop
))
489 setattr(cls
, test_binop_name('lhs_addr_same_false'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
))
490 setattr(cls
, test_binop_name('lhs_addr_same_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
))
491 setattr(cls
, test_binop_name('lhs_addr_same_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
))
492 setattr(cls
, test_binop_name('lhs_value_same_false'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_false
, op
=binop
))
493 setattr(cls
, test_binop_name('lhs_value_same_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
))
494 setattr(cls
, test_binop_name('lhs_value_same_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
))
497 setattr(cls
, test_binop_name('type_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_float
, op
=binop
))
498 setattr(cls
, test_binop_name('type_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_vfloat
, op
=binop
))
499 setattr(cls
, test_binop_name('value_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_float
, op
=binop
))
500 setattr(cls
, test_binop_name('value_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_vfloat
, op
=binop
))
501 setattr(cls
, test_binop_name('lhs_addr_same_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
))
502 setattr(cls
, test_binop_name('lhs_addr_same_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
))
503 setattr(cls
, test_binop_name('lhs_value_same_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
))
504 setattr(cls
, test_binop_name('lhs_value_same_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
))
506 setattr(cls
, test_binop_name('type_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_float
, op
=binop
))
507 setattr(cls
, test_binop_name('type_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_vfloat
, op
=binop
))
508 setattr(cls
, test_binop_name('value_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_float
, op
=binop
))
509 setattr(cls
, test_binop_name('value_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_vfloat
, op
=binop
))
510 setattr(cls
, test_binop_name('lhs_addr_same_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
))
511 setattr(cls
, test_binop_name('lhs_addr_same_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
))
512 setattr(cls
, test_binop_name('lhs_value_same_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
))
513 setattr(cls
, test_binop_name('lhs_value_same_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
))
514 setattr(cls
, test_binop_name('type_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_float
, op
=binop
))
515 setattr(cls
, test_binop_name('type_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_vfloat
, op
=binop
))
516 setattr(cls
, test_binop_name('value_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_float
, op
=binop
))
517 setattr(cls
, test_binop_name('value_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_vfloat
, op
=binop
))
518 setattr(cls
, test_binop_name('lhs_addr_same_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
))
519 setattr(cls
, test_binop_name('lhs_addr_same_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
))
520 setattr(cls
, test_binop_name('lhs_value_same_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
))
521 setattr(cls
, test_binop_name('lhs_value_same_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
))
523 # inject testing methods for each unary operation
524 for name
, unaryop
in _UNARYOPS
:
525 setattr(cls
, test_unaryop_name('type'), partialmethod(_TestNumericValue
._test
_unaryop
_type
, op
=unaryop
))
526 setattr(cls
, test_unaryop_name('value'), partialmethod(_TestNumericValue
._test
_unaryop
_value
, op
=unaryop
))
527 setattr(cls
, test_unaryop_name('addr_same'), partialmethod(_TestNumericValue
._test
_unaryop
_addr
_same
, op
=unaryop
))
528 setattr(cls
, test_unaryop_name('value_same'), partialmethod(_TestNumericValue
._test
_unaryop
_value
_same
, op
=unaryop
))
531 class CreateValueFuncTestCase(unittest
.TestCase
):
532 def test_create_none(self
):
533 v
= bt2
.create_value(None)
536 def test_create_bool_false(self
):
537 v
= bt2
.create_value(False)
538 self
.assertIsInstance(v
, bt2
.BoolValue
)
541 def test_create_bool_true(self
):
542 v
= bt2
.create_value(True)
543 self
.assertIsInstance(v
, bt2
.BoolValue
)
546 def test_create_int_pos(self
):
548 v
= bt2
.create_value(raw
)
549 self
.assertIsInstance(v
, bt2
.SignedIntegerValue
)
550 self
.assertEqual(v
, raw
)
552 def test_create_int_neg(self
):
554 v
= bt2
.create_value(raw
)
555 self
.assertIsInstance(v
, bt2
.SignedIntegerValue
)
556 self
.assertEqual(v
, raw
)
558 def test_create_float_pos(self
):
560 v
= bt2
.create_value(raw
)
561 self
.assertIsInstance(v
, bt2
.RealValue
)
562 self
.assertEqual(v
, raw
)
564 def test_create_float_neg(self
):
566 v
= bt2
.create_value(raw
)
567 self
.assertIsInstance(v
, bt2
.RealValue
)
568 self
.assertEqual(v
, raw
)
570 def test_create_string(self
):
572 v
= bt2
.create_value(raw
)
573 self
.assertIsInstance(v
, bt2
.StringValue
)
574 self
.assertEqual(v
, raw
)
576 def test_create_string_empty(self
):
578 v
= bt2
.create_value(raw
)
579 self
.assertIsInstance(v
, bt2
.StringValue
)
580 self
.assertEqual(v
, raw
)
582 def test_create_array_from_list(self
):
584 v
= bt2
.create_value(raw
)
585 self
.assertIsInstance(v
, bt2
.ArrayValue
)
586 self
.assertEqual(v
, raw
)
588 def test_create_array_from_tuple(self
):
590 v
= bt2
.create_value(raw
)
591 self
.assertIsInstance(v
, bt2
.ArrayValue
)
592 self
.assertEqual(v
, raw
)
594 def test_create_array_from_empty_list(self
):
596 v
= bt2
.create_value(raw
)
597 self
.assertIsInstance(v
, bt2
.ArrayValue
)
598 self
.assertEqual(v
, raw
)
600 def test_create_array_from_empty_tuple(self
):
602 v
= bt2
.create_value(raw
)
603 self
.assertIsInstance(v
, bt2
.ArrayValue
)
604 self
.assertEqual(v
, raw
)
606 def test_create_map(self
):
608 v
= bt2
.create_value(raw
)
609 self
.assertIsInstance(v
, bt2
.MapValue
)
610 self
.assertEqual(v
, raw
)
612 def test_create_map_empty(self
):
614 v
= bt2
.create_value(raw
)
615 self
.assertIsInstance(v
, bt2
.MapValue
)
616 self
.assertEqual(v
, raw
)
618 def test_create_vfalse(self
):
619 v
= bt2
.create_value(bt2
.create_value(False))
620 self
.assertIsInstance(v
, bt2
.BoolValue
)
623 def test_create_invalid(self
):
629 with self
.assertRaisesRegex(TypeError, "cannot create value object from 'A' object") as cm
:
630 v
= bt2
.create_value(a
)
633 class BoolValueTestCase(_TestCopySimple
, unittest
.TestCase
):
635 self
._f
= bt2
.BoolValue(False)
636 self
._t
= bt2
.BoolValue(True)
638 self
._def
_value
= False
639 self
._def
_new
_value
= True
646 def _assert_expecting_bool(self
):
647 return self
.assertRaisesRegex(TypeError, r
"expecting a 'bool' object")
649 def test_create_default(self
):
653 def test_create_false(self
):
654 self
.assertFalse(self
._f
)
656 def test_create_true(self
):
657 self
.assertTrue(self
._t
)
659 def test_create_from_vfalse(self
):
660 b
= bt2
.BoolValue(self
._f
)
663 def test_create_from_vtrue(self
):
664 b
= bt2
.BoolValue(self
._t
)
667 def test_create_from_int_non_zero(self
):
668 with self
.assertRaises(TypeError):
669 b
= bt2
.BoolValue(23)
671 def test_create_from_int_zero(self
):
672 with self
.assertRaises(TypeError):
675 def test_assign_true(self
):
680 def test_assign_false(self
):
685 def test_assign_vtrue(self
):
690 def test_assign_vfalse(self
):
695 def test_assign_int(self
):
696 with self
.assertRaises(TypeError):
700 def test_bool_op(self
):
701 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
703 def test_str_op(self
):
704 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
706 def test_eq_none(self
):
707 self
.assertFalse(self
._def
== None)
709 def test_ne_none(self
):
710 self
.assertTrue(self
._def
!= None)
712 def test_vfalse_eq_false(self
):
713 self
.assertEqual(self
._f
, False)
715 def test_vfalse_ne_true(self
):
716 self
.assertNotEqual(self
._f
, True)
718 def test_vtrue_eq_true(self
):
719 self
.assertEqual(self
._t
, True)
721 def test_vtrue_ne_false(self
):
722 self
.assertNotEqual(self
._t
, False)
725 class _TestIntegerValue(_TestNumericValue
):
728 self
._ip
= self
._CLS
(self
._pv
)
730 self
._def
_value
= self
._pv
731 self
._def
_new
_value
= 101
738 def _assert_expecting_int(self
):
739 return self
.assertRaisesRegex(TypeError, r
'expecting a number object')
741 def _assert_expecting_int64(self
):
742 return self
.assertRaisesRegex(ValueError, r
"expecting a signed 64-bit integral value")
744 def _assert_expecting_uint64(self
):
745 return self
.assertRaisesRegex(ValueError, r
"expecting an unsigned 64-bit integral value")
747 def test_create_default(self
):
749 self
.assertEqual(i
, 0)
751 def test_create_pos(self
):
752 self
.assertEqual(self
._ip
, self
._pv
)
754 def test_create_neg(self
):
755 self
.assertEqual(self
._in
, self
._nv
)
757 def test_create_from_vint(self
):
758 i
= self
._CLS
(self
._ip
)
759 self
.assertEqual(i
, self
._pv
)
761 def test_create_from_false(self
):
765 def test_create_from_true(self
):
769 def test_create_from_float(self
):
771 self
.assertEqual(i
, 99)
773 def test_create_from_vfloat(self
):
774 f
= bt2
.create_value(17.5)
776 self
.assertEqual(i
, 17)
778 def test_create_from_unknown(self
):
782 with self
._assert
_expecting
_int
():
785 def test_create_from_varray(self
):
786 with self
._assert
_expecting
_int
():
787 i
= self
._CLS
(bt2
.ArrayValue())
789 def test_assign_true(self
):
791 self
._def
.value
= raw
792 self
.assertEqual(self
._def
, raw
)
794 def test_assign_false(self
):
796 self
._def
.value
= raw
797 self
.assertEqual(self
._def
, raw
)
799 def test_assign_pos_int(self
):
801 self
._def
.value
= raw
802 self
.assertEqual(self
._def
, raw
)
804 def test_assign_vint(self
):
806 self
._def
.value
= bt2
.create_value(raw
)
807 self
.assertEqual(self
._def
, raw
)
809 def test_assign_vfloat(self
):
811 self
._def
.value
= bt2
.create_value(raw
)
812 self
.assertEqual(self
._def
, int(raw
))
815 class SignedIntegerValueTestCase(_TestIntegerValue
, unittest
.TestCase
):
816 _CLS
= bt2
.SignedIntegerValue
821 self
._in
= self
._CLS
(self
._nv
)
822 self
._def
_new
_value
= -101
828 def test_create_neg(self
):
829 self
.assertEqual(self
._in
, self
._nv
)
831 def test_create_pos_too_big(self
):
832 with self
._assert
_expecting
_int
64():
833 i
= self
._CLS
(2 ** 63)
835 def test_create_neg_too_big(self
):
836 with self
._assert
_expecting
_int
64():
837 i
= self
._CLS
(-(2 ** 63) - 1)
839 def test_assign_neg_int(self
):
841 self
._def
.value
= raw
842 self
.assertEqual(self
._def
, raw
)
844 def test_compare_big_int(self
):
845 # Larger than the IEEE 754 double-precision exact representation of
848 v
= bt2
.create_value(raw
)
849 self
.assertEqual(v
, raw
)
852 _inject_numeric_testing_methods(SignedIntegerValueTestCase
)
855 class UnsignedIntegerValueTestCase(_TestIntegerValue
, unittest
.TestCase
):
856 _CLS
= bt2
.UnsignedIntegerValue
858 def test_create_pos_too_big(self
):
859 with self
._assert
_expecting
_uint
64():
860 i
= self
._CLS
(2 ** 64)
862 def test_create_neg(self
):
863 with self
._assert
_expecting
_uint
64():
867 _inject_numeric_testing_methods(UnsignedIntegerValueTestCase
, False)
870 class RealValueTestCase(_TestNumericValue
, unittest
.TestCase
):
874 self
._fp
= bt2
.RealValue(self
._pv
)
875 self
._fn
= bt2
.RealValue(self
._nv
)
877 self
._def
_value
= self
._pv
878 self
._def
_new
_value
= -101.88
886 def _assert_expecting_float(self
):
887 return self
.assertRaisesRegex(TypeError, r
"expecting a real number object")
889 def _test_invalid_op(self
, cb
):
890 with self
.assertRaises(TypeError):
893 def test_create_default(self
):
895 self
.assertEqual(f
, 0.0)
897 def test_create_pos(self
):
898 self
.assertEqual(self
._fp
, self
._pv
)
900 def test_create_neg(self
):
901 self
.assertEqual(self
._fn
, self
._nv
)
903 def test_create_from_vint(self
):
904 f
= bt2
.RealValue(self
._fp
)
905 self
.assertEqual(f
, self
._pv
)
907 def test_create_from_false(self
):
908 f
= bt2
.RealValue(False)
911 def test_create_from_true(self
):
912 f
= bt2
.RealValue(True)
915 def test_create_from_int(self
):
917 f
= bt2
.RealValue(raw
)
918 self
.assertEqual(f
, float(raw
))
920 def test_create_from_vint(self
):
922 f
= bt2
.RealValue(bt2
.create_value(raw
))
923 self
.assertEqual(f
, float(raw
))
925 def test_create_from_vfloat(self
):
927 f
= bt2
.RealValue(bt2
.create_value(raw
))
928 self
.assertEqual(f
, raw
)
930 def test_create_from_unknown(self
):
934 with self
._assert
_expecting
_float
():
935 f
= bt2
.RealValue(A())
937 def test_create_from_varray(self
):
938 with self
._assert
_expecting
_float
():
939 f
= bt2
.RealValue(bt2
.ArrayValue())
941 def test_assign_true(self
):
942 self
._def
.value
= True
943 self
.assertTrue(self
._def
)
945 def test_assign_false(self
):
946 self
._def
.value
= False
947 self
.assertFalse(self
._def
)
949 def test_assign_pos_int(self
):
951 self
._def
.value
= raw
952 self
.assertEqual(self
._def
, float(raw
))
954 def test_assign_neg_int(self
):
956 self
._def
.value
= raw
957 self
.assertEqual(self
._def
, float(raw
))
959 def test_assign_vint(self
):
961 self
._def
.value
= bt2
.create_value(raw
)
962 self
.assertEqual(self
._def
, float(raw
))
964 def test_assign_float(self
):
966 self
._def
.value
= raw
967 self
.assertEqual(self
._def
, raw
)
969 def test_assign_vfloat(self
):
971 self
._def
.value
= bt2
.create_value(raw
)
972 self
.assertEqual(self
._def
, raw
)
974 def test_invalid_lshift(self
):
975 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
977 def test_invalid_rshift(self
):
978 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
980 def test_invalid_and(self
):
981 self
._test
_invalid
_op
(lambda: self
._def
& 23)
983 def test_invalid_or(self
):
984 self
._test
_invalid
_op
(lambda: self
._def |
23)
986 def test_invalid_xor(self
):
987 self
._test
_invalid
_op
(lambda: self
._def ^
23)
989 def test_invalid_invert(self
):
990 self
._test
_invalid
_op
(lambda: ~self
._def
)
993 _inject_numeric_testing_methods(RealValueTestCase
)
996 class StringValueTestCase(_TestCopySimple
, unittest
.TestCase
):
998 self
._def
_value
= 'Hello, World!'
999 self
._def
= bt2
.StringValue(self
._def
_value
)
1000 self
._def
_new
_value
= 'Yes!'
1005 def _assert_expecting_str(self
):
1006 return self
.assertRaises(TypeError)
1008 def test_create_default(self
):
1009 s
= bt2
.StringValue()
1010 self
.assertEqual(s
, '')
1012 def test_create_from_str(self
):
1014 s
= bt2
.StringValue(raw
)
1015 self
.assertEqual(s
, raw
)
1017 def test_create_from_vstr(self
):
1019 s
= bt2
.StringValue(bt2
.create_value(raw
))
1020 self
.assertEqual(s
, raw
)
1022 def test_create_from_unknown(self
):
1026 with self
._assert
_expecting
_str
():
1027 i
= bt2
.StringValue(A())
1029 def test_create_from_varray(self
):
1030 with self
._assert
_expecting
_str
():
1031 i
= bt2
.StringValue(bt2
.ArrayValue())
1033 def test_assign_int(self
):
1034 with self
._assert
_expecting
_str
():
1035 self
._def
.value
= 283
1037 def test_assign_str(self
):
1040 self
.assertEqual(self
._def
, raw
)
1042 def test_assign_vstr(self
):
1044 self
._def
= bt2
.create_value(raw
)
1045 self
.assertEqual(self
._def
, raw
)
1048 self
.assertEqual(self
._def
, self
._def
_value
)
1051 self
.assertNotEqual(self
._def
, 23)
1053 def test_lt_vstring(self
):
1054 s1
= bt2
.StringValue('allo')
1055 s2
= bt2
.StringValue('bateau')
1056 self
.assertLess(s1
, s2
)
1058 def test_lt_string(self
):
1059 s1
= bt2
.StringValue('allo')
1060 self
.assertLess(s1
, 'bateau')
1062 def test_le_vstring(self
):
1063 s1
= bt2
.StringValue('allo')
1064 s2
= bt2
.StringValue('bateau')
1065 self
.assertLessEqual(s1
, s2
)
1067 def test_le_string(self
):
1068 s1
= bt2
.StringValue('allo')
1069 self
.assertLessEqual(s1
, 'bateau')
1071 def test_gt_vstring(self
):
1072 s1
= bt2
.StringValue('allo')
1073 s2
= bt2
.StringValue('bateau')
1074 self
.assertGreater(s2
, s1
)
1076 def test_gt_string(self
):
1077 s1
= bt2
.StringValue('allo')
1078 self
.assertGreater('bateau', s1
)
1080 def test_ge_vstring(self
):
1081 s1
= bt2
.StringValue('allo')
1082 s2
= bt2
.StringValue('bateau')
1083 self
.assertGreaterEqual(s2
, s1
)
1085 def test_ge_string(self
):
1086 s1
= bt2
.StringValue('allo')
1087 self
.assertGreaterEqual('bateau', s1
)
1089 def test_bool_op(self
):
1090 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1092 def test_str_op(self
):
1093 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1096 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1098 def test_getitem(self
):
1099 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1101 def test_append_str(self
):
1102 to_append
= 'meow meow meow'
1103 self
._def
+= to_append
1104 self
._def
_value
+= to_append
1105 self
.assertEqual(self
._def
, self
._def
_value
)
1107 def test_append_vstr(self
):
1108 to_append
= 'meow meow meow'
1109 self
._def
+= bt2
.create_value(to_append
)
1110 self
._def
_value
+= to_append
1111 self
.assertEqual(self
._def
, self
._def
_value
)
1114 class ArrayValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1116 self
._def
_value
= [None, False, True, -23, 0, 42, -42.4, 23.17, 'yes']
1117 self
._def
= bt2
.ArrayValue(copy
.deepcopy(self
._def
_value
))
1122 def _modify_def(self
):
1123 self
._def
[2] = 'xyz'
1125 def _assert_type_error(self
):
1126 return self
.assertRaises(TypeError)
1128 def test_create_default(self
):
1129 a
= bt2
.ArrayValue()
1130 self
.assertEqual(len(a
), 0)
1132 def test_create_from_array(self
):
1133 self
.assertEqual(self
._def
, self
._def
_value
)
1135 def test_create_from_tuple(self
):
1136 t
= 1, 2, False, None
1137 a
= bt2
.ArrayValue(t
)
1138 self
.assertEqual(a
, t
)
1140 def test_create_from_varray(self
):
1141 va
= bt2
.ArrayValue(copy
.deepcopy(self
._def
_value
))
1142 a
= bt2
.ArrayValue(va
)
1143 self
.assertEqual(va
, a
)
1145 def test_create_from_unknown(self
):
1149 with self
._assert
_type
_error
():
1150 a
= bt2
.ArrayValue(A())
1152 def test_bool_op_true(self
):
1153 self
.assertTrue(bool(self
._def
))
1155 def test_bool_op_false(self
):
1156 self
.assertFalse(bool(bt2
.ArrayValue()))
1159 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1161 def test_eq_int(self
):
1162 self
.assertNotEqual(self
._def
, 23)
1164 def test_eq_diff_len(self
):
1165 a1
= bt2
.create_value([1, 2, 3])
1166 a2
= bt2
.create_value([1, 2])
1167 self
.assertNotEqual(a1
, a2
)
1169 def test_eq_diff_content_same_len(self
):
1170 a1
= bt2
.create_value([1, 2, 3])
1171 a2
= bt2
.create_value([4, 5, 6])
1172 self
.assertNotEqual(a1
, a2
)
1174 def test_eq_same_content_same_len(self
):
1175 raw
= (3, True, [1, 2.5, None, {'a': 17.6, 'b': None}])
1176 a1
= bt2
.ArrayValue(raw
)
1177 a2
= bt2
.ArrayValue(copy
.deepcopy(raw
))
1178 self
.assertEqual(a1
, a2
)
1180 def test_setitem_int(self
):
1183 self
.assertEqual(self
._def
[2], raw
)
1185 def test_setitem_vint(self
):
1187 self
._def
[2] = bt2
.create_value(raw
)
1188 self
.assertEqual(self
._def
[2], raw
)
1190 def test_setitem_none(self
):
1192 self
.assertIsNone(self
._def
[2])
1194 def test_setitem_index_wrong_type(self
):
1195 with self
._assert
_type
_error
():
1196 self
._def
['yes'] = 23
1198 def test_setitem_index_neg(self
):
1199 with self
.assertRaises(IndexError):
1202 def test_setitem_index_out_of_range(self
):
1203 with self
.assertRaises(IndexError):
1204 self
._def
[len(self
._def
)] = 23
1206 def test_append_none(self
):
1207 self
._def
.append(None)
1208 self
.assertIsNone(self
._def
[len(self
._def
) - 1])
1210 def test_append_int(self
):
1212 self
._def
.append(raw
)
1213 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
)
1215 def test_append_vint(self
):
1217 self
._def
.append(bt2
.create_value(raw
))
1218 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
)
1220 def test_append_unknown(self
):
1224 with self
._assert
_type
_error
():
1225 self
._def
.append(A())
1227 def test_iadd(self
):
1230 self
.assertEqual(self
._def
[len(self
._def
) - 3], raw
[0])
1231 self
.assertEqual(self
._def
[len(self
._def
) - 2], raw
[1])
1232 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
[2])
1234 def test_iadd_unknown(self
):
1238 with self
._assert
_type
_error
():
1241 def test_iadd_list_unknown(self
):
1245 with self
._assert
_type
_error
():
1248 def test_iter(self
):
1249 for velem
, elem
in zip(self
._def
, self
._def
_value
):
1250 self
.assertEqual(velem
, elem
)
1253 class MapValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1266 self
._def
= bt2
.MapValue(copy
.deepcopy(self
._def
_value
))
1271 def _modify_def(self
):
1272 self
._def
['zero'] = 1
1274 def test_create_default(self
):
1276 self
.assertEqual(len(m
), 0)
1278 def test_create_from_dict(self
):
1279 self
.assertEqual(self
._def
, self
._def
_value
)
1281 def test_create_from_vmap(self
):
1282 vm
= bt2
.MapValue(copy
.deepcopy(self
._def
_value
))
1283 m
= bt2
.MapValue(vm
)
1284 self
.assertEqual(vm
, m
)
1286 def test_create_from_unknown(self
):
1290 with self
.assertRaises(AttributeError):
1291 m
= bt2
.MapValue(A())
1293 def test_bool_op_true(self
):
1294 self
.assertTrue(bool(self
._def
))
1296 def test_bool_op_false(self
):
1297 self
.assertFalse(bool(bt2
.MapValue()))
1300 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1302 def test_eq_int(self
):
1303 self
.assertNotEqual(self
._def
, 23)
1305 def test_eq_diff_len(self
):
1306 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1307 a2
= bt2
.create_value({'a': 1, 'b': 2})
1308 self
.assertNotEqual(a1
, a2
)
1310 def test_eq_diff_content_same_len(self
):
1311 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1312 a2
= bt2
.create_value({'a': 4, 'b': 2, 'c': 3})
1313 self
.assertNotEqual(a1
, a2
)
1315 def test_eq_same_content_diff_keys(self
):
1316 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1317 a2
= bt2
.create_value({'a': 1, 'k': 2, 'c': 3})
1318 self
.assertNotEqual(a1
, a2
)
1320 def test_eq_same_content_same_len(self
):
1324 'array': [1, 2.5, None, {'a': 17.6, 'b': None}]
1326 a1
= bt2
.MapValue(raw
)
1327 a2
= bt2
.MapValue(copy
.deepcopy(raw
))
1328 self
.assertEqual(a1
, a2
)
1329 self
.assertEqual(a1
, raw
)
1331 def test_setitem_int(self
):
1333 self
._def
['pos-int'] = raw
1334 self
.assertEqual(self
._def
['pos-int'], raw
)
1336 def test_setitem_vint(self
):
1338 self
._def
['pos-int'] = bt2
.create_value(raw
)
1339 self
.assertEqual(self
._def
['pos-int'], raw
)
1341 def test_setitem_none(self
):
1342 self
._def
['none'] = None
1343 self
.assertIsNone(self
._def
['none'])
1345 def test_setitem_new_int(self
):
1346 old_len
= len(self
._def
)
1347 self
._def
['new-int'] = 23
1348 self
.assertEqual(self
._def
['new-int'], 23)
1349 self
.assertEqual(len(self
._def
), old_len
+ 1)
1351 def test_setitem_index_wrong_type(self
):
1352 with self
.assertRaises(TypeError):
1355 def test_iter(self
):
1356 for vkey
, vval
in self
._def
.items():
1357 val
= self
._def
_value
[vkey
]
1358 self
.assertEqual(vval
, val
)
1360 def test_getitem_wrong_key(self
):
1361 with self
.assertRaises(KeyError):
1362 self
._def
['kilojoule']