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 # The value object classes explicitly do not implement the copy methods,
29 # raising `NotImplementedError`, just in case we decide to implement
31 class _TestCopySimple
:
33 with self
.assertRaises(NotImplementedError):
36 def test_deepcopy(self
):
37 with self
.assertRaises(NotImplementedError):
38 copy
.deepcopy(self
._def
)
47 # Base class for numeric value test cases.
49 # To be compatible with this base class, a derived class must, in its
52 # * Set `self._def` to a value object with an arbitrary raw value.
53 # * Set `self._def_value` to the equivalent raw value of `self._def`.
54 class _TestNumericValue(_TestCopySimple
):
55 # Tries the binary operation `op`:
57 # 1. Between `self._def`, which is a value object, and `rhs`.
58 # 2. Between `self._def_value`, which is the raw value of
59 # `self._def`, and `rhs`.
61 # Returns the results of 1. and 2.
63 # If there's an exception while performing 1. or 2., asserts that
64 # both operations raised exceptions, that both exceptions have the
65 # same type, and returns `None` for both results.
66 def _binop(self
, op
, rhs
):
71 # try with value object
73 r
= op(self
._def
, rhs
)
74 except Exception as e
:
79 rv
= op(self
._def
_value
, comp_value
)
80 except Exception as e
:
83 if type_rexc
is not None or type_rvexc
is not None:
84 # at least one of the operations raised an exception: in
85 # this case both operations should have raised the same
86 # type of exception (division by zero, bit shift with a
87 # floating point number operand, etc.)
88 self
.assertIs(type_rexc
, type_rvexc
)
93 # Tries the unary operation `op`:
95 # 1. On `self._def`, which is a value object.
96 # 2. On `self._def_value`, which is the raw value of `self._def`.
98 # Returns the results of 1. and 2.
100 # If there's an exception while performing 1. or 2., asserts that
101 # both operations raised exceptions, that both exceptions have the
102 # same type, and returns `None` for both results.
103 def _unaryop(self
, op
):
107 # try with value object
110 except Exception as e
:
115 rv
= op(self
._def
_value
)
116 except Exception as e
:
119 if type_rexc
is not None or type_rvexc
is not None:
120 # at least one of the operations raised an exception: in
121 # this case both operations should have raised the same
122 # type of exception (division by zero, bit shift with a
123 # floating point number operand, etc.)
124 self
.assertIs(type_rexc
, type_rvexc
)
129 # Tests that the unary operation `op` gives results with the same
130 # type for both `self._def` and `self._def_value`.
131 def _test_unaryop_type(self
, op
):
132 r
, rv
= self
._unaryop
(op
)
137 self
.assertIsInstance(r
, type(rv
))
139 # Tests that the unary operation `op` gives results with the same
140 # value for both `self._def` and `self._def_value`. This uses the
141 # __eq__() operator of `self._def`.
142 def _test_unaryop_value(self
, op
):
143 r
, rv
= self
._unaryop
(op
)
148 self
.assertEqual(r
, rv
)
150 # Tests that the unary operation `op`, when applied to `self._def`,
151 # does not change its underlying BT object address.
152 def _test_unaryop_addr_same(self
, op
):
153 addr_before
= self
._def
.addr
155 self
.assertEqual(self
._def
.addr
, addr_before
)
157 # Tests that the unary operation `op`, when applied to `self._def`,
158 # does not change its value.
159 def _test_unaryop_value_same(self
, op
):
160 value_before
= self
._def
.__class
__(self
._def
)
162 self
.assertEqual(self
._def
, value_before
)
164 # Tests that the binary operation `op` gives results with the same
165 # type for both `self._def` and `self._def_value`.
166 def _test_binop_type(self
, op
, rhs
):
167 r
, rv
= self
._binop
(op
, rhs
)
172 if op
in _COMP_BINOPS
:
173 # __eq__() and __ne__() always return a 'bool' object
174 self
.assertIsInstance(r
, bool)
176 self
.assertIsInstance(r
, type(rv
))
178 # Tests that the binary operation `op` gives results with the same
179 # value for both `self._def` and `self._def_value`. This uses the
180 # __eq__() operator of `self._def`.
181 def _test_binop_value(self
, op
, rhs
):
182 r
, rv
= self
._binop
(op
, rhs
)
187 self
.assertEqual(r
, rv
)
189 # Tests that the binary operation `op`, when applied to `self._def`,
190 # does not change its underlying BT object address.
191 def _test_binop_lhs_addr_same(self
, op
, rhs
):
192 addr_before
= self
._def
.addr
193 r
, rv
= self
._binop
(op
, rhs
)
194 self
.assertEqual(self
._def
.addr
, addr_before
)
196 # Tests that the binary operation `op`, when applied to `self._def`,
197 # does not change its value.
198 def _test_binop_lhs_value_same(self
, op
, rhs
):
199 value_before
= self
._def
.__class
__(self
._def
)
200 r
, rv
= self
._binop
(op
, rhs
)
201 self
.assertEqual(self
._def
, value_before
)
203 # The methods below which take the `test_cb` and `op` parameters
204 # are meant to be used with one of the _test_binop_*() functions
205 # above as `test_cb` and a binary operator function as `op`.
209 # self._test_binop_rhs_pos_int(self._test_binop_value,
212 # This tests that a numeric value object added to a positive integer
213 # raw value gives a result with the expected value.
215 # `vint` and `vfloat` mean a signed integer value object and a real
218 def _test_binop_invalid_unknown(self
, op
):
219 if op
in _COMP_BINOPS
:
220 self
.skipTest('not testing')
222 with self
.assertRaises(TypeError):
223 op(self
._def
, object())
225 def _test_binop_invalid_none(self
, op
):
226 if op
in _COMP_BINOPS
:
227 self
.skipTest('not testing')
229 with self
.assertRaises(TypeError):
232 def _test_binop_rhs_false(self
, test_cb
, op
):
235 def _test_binop_rhs_true(self
, test_cb
, op
):
238 def _test_binop_rhs_pos_int(self
, test_cb
, op
):
241 def _test_binop_rhs_neg_int(self
, test_cb
, op
):
244 def _test_binop_rhs_zero_int(self
, test_cb
, op
):
247 def _test_binop_rhs_pos_vint(self
, test_cb
, op
):
248 test_cb(op
, bt2
.create_value(2))
250 def _test_binop_rhs_neg_vint(self
, test_cb
, op
):
251 test_cb(op
, bt2
.create_value(-23))
253 def _test_binop_rhs_zero_vint(self
, test_cb
, op
):
254 test_cb(op
, bt2
.create_value(0))
256 def _test_binop_rhs_pos_float(self
, test_cb
, op
):
259 def _test_binop_rhs_neg_float(self
, test_cb
, op
):
262 def _test_binop_rhs_zero_float(self
, test_cb
, op
):
265 def _test_binop_rhs_pos_vfloat(self
, test_cb
, op
):
266 test_cb(op
, bt2
.create_value(2.2))
268 def _test_binop_rhs_neg_vfloat(self
, test_cb
, op
):
269 test_cb(op
, bt2
.create_value(-23.4))
271 def _test_binop_rhs_zero_vfloat(self
, test_cb
, op
):
272 test_cb(op
, bt2
.create_value(0.0))
274 def _test_binop_type_false(self
, op
):
275 self
._test
_binop
_rhs
_false
(self
._test
_binop
_type
, op
)
277 def _test_binop_type_true(self
, op
):
278 self
._test
_binop
_rhs
_true
(self
._test
_binop
_type
, op
)
280 def _test_binop_type_pos_int(self
, op
):
281 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_type
, op
)
283 def _test_binop_type_neg_int(self
, op
):
284 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_type
, op
)
286 def _test_binop_type_zero_int(self
, op
):
287 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_type
, op
)
289 def _test_binop_type_pos_vint(self
, op
):
290 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_type
, op
)
292 def _test_binop_type_neg_vint(self
, op
):
293 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_type
, op
)
295 def _test_binop_type_zero_vint(self
, op
):
296 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_type
, op
)
298 def _test_binop_type_pos_float(self
, op
):
299 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_type
, op
)
301 def _test_binop_type_neg_float(self
, op
):
302 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_type
, op
)
304 def _test_binop_type_zero_float(self
, op
):
305 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_type
, op
)
307 def _test_binop_type_pos_vfloat(self
, op
):
308 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_type
, op
)
310 def _test_binop_type_neg_vfloat(self
, op
):
311 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_type
, op
)
313 def _test_binop_type_zero_vfloat(self
, op
):
314 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_type
, op
)
316 def _test_binop_value_false(self
, op
):
317 self
._test
_binop
_rhs
_false
(self
._test
_binop
_value
, op
)
319 def _test_binop_value_true(self
, op
):
320 self
._test
_binop
_rhs
_true
(self
._test
_binop
_value
, op
)
322 def _test_binop_value_pos_int(self
, op
):
323 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_value
, op
)
325 def _test_binop_value_neg_int(self
, op
):
326 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_value
, op
)
328 def _test_binop_value_zero_int(self
, op
):
329 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_value
, op
)
331 def _test_binop_value_pos_vint(self
, op
):
332 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_value
, op
)
334 def _test_binop_value_neg_vint(self
, op
):
335 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_value
, op
)
337 def _test_binop_value_zero_vint(self
, op
):
338 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_value
, op
)
340 def _test_binop_value_pos_float(self
, op
):
341 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_value
, op
)
343 def _test_binop_value_neg_float(self
, op
):
344 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_value
, op
)
346 def _test_binop_value_zero_float(self
, op
):
347 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_value
, op
)
349 def _test_binop_value_pos_vfloat(self
, op
):
350 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_value
, op
)
352 def _test_binop_value_neg_vfloat(self
, op
):
353 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_value
, op
)
355 def _test_binop_value_zero_vfloat(self
, op
):
356 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_value
, op
)
358 def _test_binop_lhs_addr_same_false(self
, op
):
359 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_addr
_same
, op
)
361 def _test_binop_lhs_addr_same_true(self
, op
):
362 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_addr
_same
, op
)
364 def _test_binop_lhs_addr_same_pos_int(self
, op
):
365 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
367 def _test_binop_lhs_addr_same_neg_int(self
, op
):
368 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
370 def _test_binop_lhs_addr_same_zero_int(self
, op
):
371 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_addr
_same
, op
)
373 def _test_binop_lhs_addr_same_pos_vint(self
, op
):
374 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
376 def _test_binop_lhs_addr_same_neg_vint(self
, op
):
377 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
379 def _test_binop_lhs_addr_same_zero_vint(self
, op
):
380 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_addr
_same
, op
)
382 def _test_binop_lhs_addr_same_pos_float(self
, op
):
383 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
385 def _test_binop_lhs_addr_same_neg_float(self
, op
):
386 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
388 def _test_binop_lhs_addr_same_zero_float(self
, op
):
389 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_addr
_same
, op
)
391 def _test_binop_lhs_addr_same_pos_vfloat(self
, op
):
392 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
394 def _test_binop_lhs_addr_same_neg_vfloat(self
, op
):
395 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
397 def _test_binop_lhs_addr_same_zero_vfloat(self
, op
):
398 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_addr
_same
, op
)
400 def _test_binop_lhs_value_same_false(self
, op
):
401 self
._test
_binop
_rhs
_false
(self
._test
_binop
_lhs
_value
_same
, op
)
403 def _test_binop_lhs_value_same_true(self
, op
):
404 self
._test
_binop
_rhs
_true
(self
._test
_binop
_lhs
_value
_same
, op
)
406 def _test_binop_lhs_value_same_pos_int(self
, op
):
407 self
._test
_binop
_rhs
_pos
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
409 def _test_binop_lhs_value_same_neg_int(self
, op
):
410 self
._test
_binop
_rhs
_neg
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
412 def _test_binop_lhs_value_same_zero_int(self
, op
):
413 self
._test
_binop
_rhs
_zero
_int
(self
._test
_binop
_lhs
_value
_same
, op
)
415 def _test_binop_lhs_value_same_pos_vint(self
, op
):
416 self
._test
_binop
_rhs
_pos
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
418 def _test_binop_lhs_value_same_neg_vint(self
, op
):
419 self
._test
_binop
_rhs
_neg
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
421 def _test_binop_lhs_value_same_zero_vint(self
, op
):
422 self
._test
_binop
_rhs
_zero
_vint
(self
._test
_binop
_lhs
_value
_same
, op
)
424 def _test_binop_lhs_value_same_pos_float(self
, op
):
425 self
._test
_binop
_rhs
_pos
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
427 def _test_binop_lhs_value_same_neg_float(self
, op
):
428 self
._test
_binop
_rhs
_neg
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
430 def _test_binop_lhs_value_same_zero_float(self
, op
):
431 self
._test
_binop
_rhs
_zero
_float
(self
._test
_binop
_lhs
_value
_same
, op
)
433 def _test_binop_lhs_value_same_pos_vfloat(self
, op
):
434 self
._test
_binop
_rhs
_pos
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
436 def _test_binop_lhs_value_same_neg_vfloat(self
, op
):
437 self
._test
_binop
_rhs
_neg
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
439 def _test_binop_lhs_value_same_zero_vfloat(self
, op
):
440 self
._test
_binop
_rhs
_zero
_vfloat
(self
._test
_binop
_lhs
_value
_same
, op
)
442 def test_bool_op(self
):
443 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
445 def test_int_op(self
):
446 self
.assertEqual(int(self
._def
), int(self
._def
_value
))
448 def test_float_op(self
):
449 self
.assertEqual(float(self
._def
), float(self
._def
_value
))
451 def test_complex_op(self
):
452 self
.assertEqual(complex(self
._def
), complex(self
._def
_value
))
454 def test_str_op(self
):
455 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
457 def test_eq_none(self
):
458 self
.assertFalse(self
._def
== None)
460 def test_ne_none(self
):
461 self
.assertTrue(self
._def
!= None)
464 # This is a list of binary operators used for
465 # _inject_numeric_testing_methods().
467 # Each entry is a pair of binary operator name (used as part of the
468 # created testing method's name) and operator function.
476 ('add', operator
.add
),
477 ('radd', lambda a
, b
: operator
.add(b
, a
)),
478 ('and', operator
.and_
),
479 ('rand', lambda a
, b
: operator
.and_(b
, a
)),
480 ('floordiv', operator
.floordiv
),
481 ('rfloordiv', lambda a
, b
: operator
.floordiv(b
, a
)),
482 ('lshift', operator
.lshift
),
483 ('rlshift', lambda a
, b
: operator
.lshift(b
, a
)),
484 ('mod', operator
.mod
),
485 ('rmod', lambda a
, b
: operator
.mod(b
, a
)),
486 ('mul', operator
.mul
),
487 ('rmul', lambda a
, b
: operator
.mul(b
, a
)),
488 ('or', operator
.or_
),
489 ('ror', lambda a
, b
: operator
.or_(b
, a
)),
490 ('pow', operator
.pow),
491 ('rpow', lambda a
, b
: operator
.pow(b
, a
)),
492 ('rshift', operator
.rshift
),
493 ('rrshift', lambda a
, b
: operator
.rshift(b
, a
)),
494 ('sub', operator
.sub
),
495 ('rsub', lambda a
, b
: operator
.sub(b
, a
)),
496 ('truediv', operator
.truediv
),
497 ('rtruediv', lambda a
, b
: operator
.truediv(b
, a
)),
498 ('xor', operator
.xor
),
499 ('rxor', lambda a
, b
: operator
.xor(b
, a
)),
503 # This is a list of unary operators used for
504 # _inject_numeric_testing_methods().
506 # Each entry is a pair of unary operator name (used as part of the
507 # created testing method's name) and operator function.
509 ('neg', operator
.neg
),
510 ('pos', operator
.pos
),
511 ('abs', operator
.abs),
512 ('invert', operator
.invert
),
514 ('round_0', partial(round, ndigits
=0)),
515 ('round_1', partial(round, ndigits
=1)),
516 ('round_2', partial(round, ndigits
=2)),
517 ('round_3', partial(round, ndigits
=3)),
519 ('floor', math
.floor
),
520 ('trunc', math
.trunc
),
524 # This function injects a bunch of testing methods to a numeric
527 # It is meant to be used like this:
529 # _inject_numeric_testing_methods(MyNumericValueTestCase)
531 # This function injects:
533 # * One testing method for each _TestNumericValue._test_binop_*()
534 # method, for each binary operator in the _BINOPS tuple.
536 # * One testing method for each _TestNumericValue._test_unaryop*()
537 # method, for each unary operator in the _UNARYOPS tuple.
538 def _inject_numeric_testing_methods(cls
):
539 def test_binop_name(suffix
):
540 return 'test_binop_{}_{}'.format(name
, suffix
)
542 def test_unaryop_name(suffix
):
543 return 'test_unaryop_{}_{}'.format(name
, suffix
)
545 # inject testing methods for each binary operation
546 for name
, binop
in _BINOPS
:
547 setattr(cls
, test_binop_name('invalid_unknown'), partialmethod(_TestNumericValue
._test
_binop
_invalid
_unknown
, op
=binop
))
548 setattr(cls
, test_binop_name('invalid_none'), partialmethod(_TestNumericValue
._test
_binop
_invalid
_none
, op
=binop
))
549 setattr(cls
, test_binop_name('type_true'), partialmethod(_TestNumericValue
._test
_binop
_type
_true
, op
=binop
))
550 setattr(cls
, test_binop_name('type_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_int
, op
=binop
))
551 setattr(cls
, test_binop_name('type_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_vint
, op
=binop
))
552 setattr(cls
, test_binop_name('value_true'), partialmethod(_TestNumericValue
._test
_binop
_value
_true
, op
=binop
))
553 setattr(cls
, test_binop_name('value_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_int
, op
=binop
))
554 setattr(cls
, test_binop_name('value_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_vint
, op
=binop
))
555 setattr(cls
, test_binop_name('lhs_addr_same_true'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_true
, op
=binop
))
556 setattr(cls
, test_binop_name('lhs_addr_same_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_int
, op
=binop
))
557 setattr(cls
, test_binop_name('lhs_addr_same_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_vint
, op
=binop
))
558 setattr(cls
, test_binop_name('lhs_value_same_true'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_true
, op
=binop
))
559 setattr(cls
, test_binop_name('lhs_value_same_pos_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_int
, op
=binop
))
560 setattr(cls
, test_binop_name('lhs_value_same_pos_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_vint
, op
=binop
))
561 setattr(cls
, test_binop_name('type_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_int
, op
=binop
))
562 setattr(cls
, test_binop_name('type_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_vint
, op
=binop
))
563 setattr(cls
, test_binop_name('value_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_int
, op
=binop
))
564 setattr(cls
, test_binop_name('value_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_vint
, op
=binop
))
565 setattr(cls
, test_binop_name('lhs_addr_same_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_int
, op
=binop
))
566 setattr(cls
, test_binop_name('lhs_addr_same_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_vint
, op
=binop
))
567 setattr(cls
, test_binop_name('lhs_value_same_neg_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_int
, op
=binop
))
568 setattr(cls
, test_binop_name('lhs_value_same_neg_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_vint
, op
=binop
))
569 setattr(cls
, test_binop_name('type_false'), partialmethod(_TestNumericValue
._test
_binop
_type
_false
, op
=binop
))
570 setattr(cls
, test_binop_name('type_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_int
, op
=binop
))
571 setattr(cls
, test_binop_name('type_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_vint
, op
=binop
))
572 setattr(cls
, test_binop_name('value_false'), partialmethod(_TestNumericValue
._test
_binop
_value
_false
, op
=binop
))
573 setattr(cls
, test_binop_name('value_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_int
, op
=binop
))
574 setattr(cls
, test_binop_name('value_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_vint
, op
=binop
))
575 setattr(cls
, test_binop_name('lhs_addr_same_false'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_false
, op
=binop
))
576 setattr(cls
, test_binop_name('lhs_addr_same_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_int
, op
=binop
))
577 setattr(cls
, test_binop_name('lhs_addr_same_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_vint
, op
=binop
))
578 setattr(cls
, test_binop_name('lhs_value_same_false'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_false
, op
=binop
))
579 setattr(cls
, test_binop_name('lhs_value_same_zero_int'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_int
, op
=binop
))
580 setattr(cls
, test_binop_name('lhs_value_same_zero_vint'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_vint
, op
=binop
))
581 setattr(cls
, test_binop_name('type_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_float
, op
=binop
))
582 setattr(cls
, test_binop_name('type_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_type
_neg
_vfloat
, op
=binop
))
583 setattr(cls
, test_binop_name('value_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_float
, op
=binop
))
584 setattr(cls
, test_binop_name('value_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_value
_neg
_vfloat
, op
=binop
))
585 setattr(cls
, test_binop_name('lhs_addr_same_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_float
, op
=binop
))
586 setattr(cls
, test_binop_name('lhs_addr_same_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_neg
_vfloat
, op
=binop
))
587 setattr(cls
, test_binop_name('lhs_value_same_neg_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_float
, op
=binop
))
588 setattr(cls
, test_binop_name('lhs_value_same_neg_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_neg
_vfloat
, op
=binop
))
589 setattr(cls
, test_binop_name('type_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_float
, op
=binop
))
590 setattr(cls
, test_binop_name('type_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_type
_pos
_vfloat
, op
=binop
))
591 setattr(cls
, test_binop_name('value_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_float
, op
=binop
))
592 setattr(cls
, test_binop_name('value_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_value
_pos
_vfloat
, op
=binop
))
593 setattr(cls
, test_binop_name('lhs_addr_same_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_float
, op
=binop
))
594 setattr(cls
, test_binop_name('lhs_addr_same_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_pos
_vfloat
, op
=binop
))
595 setattr(cls
, test_binop_name('lhs_value_same_pos_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_float
, op
=binop
))
596 setattr(cls
, test_binop_name('lhs_value_same_pos_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_pos
_vfloat
, op
=binop
))
597 setattr(cls
, test_binop_name('type_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_float
, op
=binop
))
598 setattr(cls
, test_binop_name('type_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_type
_zero
_vfloat
, op
=binop
))
599 setattr(cls
, test_binop_name('value_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_float
, op
=binop
))
600 setattr(cls
, test_binop_name('value_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_value
_zero
_vfloat
, op
=binop
))
601 setattr(cls
, test_binop_name('lhs_addr_same_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_float
, op
=binop
))
602 setattr(cls
, test_binop_name('lhs_addr_same_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_addr
_same
_zero
_vfloat
, op
=binop
))
603 setattr(cls
, test_binop_name('lhs_value_same_zero_float'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_float
, op
=binop
))
604 setattr(cls
, test_binop_name('lhs_value_same_zero_vfloat'), partialmethod(_TestNumericValue
._test
_binop
_lhs
_value
_same
_zero
_vfloat
, op
=binop
))
606 # inject testing methods for each unary operation
607 for name
, unaryop
in _UNARYOPS
:
608 setattr(cls
, test_unaryop_name('type'), partialmethod(_TestNumericValue
._test
_unaryop
_type
, op
=unaryop
))
609 setattr(cls
, test_unaryop_name('value'), partialmethod(_TestNumericValue
._test
_unaryop
_value
, op
=unaryop
))
610 setattr(cls
, test_unaryop_name('addr_same'), partialmethod(_TestNumericValue
._test
_unaryop
_addr
_same
, op
=unaryop
))
611 setattr(cls
, test_unaryop_name('value_same'), partialmethod(_TestNumericValue
._test
_unaryop
_value
_same
, op
=unaryop
))
614 class CreateValueFuncTestCase(unittest
.TestCase
):
615 def test_create_none(self
):
616 v
= bt2
.create_value(None)
619 def test_create_bool_false(self
):
620 v
= bt2
.create_value(False)
621 self
.assertIsInstance(v
, bt2
.BoolValue
)
624 def test_create_bool_true(self
):
625 v
= bt2
.create_value(True)
626 self
.assertIsInstance(v
, bt2
.BoolValue
)
629 def test_create_int_pos(self
):
631 v
= bt2
.create_value(raw
)
632 self
.assertIsInstance(v
, bt2
.SignedIntegerValue
)
633 self
.assertEqual(v
, raw
)
635 def test_create_int_neg(self
):
637 v
= bt2
.create_value(raw
)
638 self
.assertIsInstance(v
, bt2
.SignedIntegerValue
)
639 self
.assertEqual(v
, raw
)
641 def test_create_float_pos(self
):
643 v
= bt2
.create_value(raw
)
644 self
.assertIsInstance(v
, bt2
.RealValue
)
645 self
.assertEqual(v
, raw
)
647 def test_create_float_neg(self
):
649 v
= bt2
.create_value(raw
)
650 self
.assertIsInstance(v
, bt2
.RealValue
)
651 self
.assertEqual(v
, raw
)
653 def test_create_string(self
):
655 v
= bt2
.create_value(raw
)
656 self
.assertIsInstance(v
, bt2
.StringValue
)
657 self
.assertEqual(v
, raw
)
659 def test_create_string_empty(self
):
661 v
= bt2
.create_value(raw
)
662 self
.assertIsInstance(v
, bt2
.StringValue
)
663 self
.assertEqual(v
, raw
)
665 def test_create_array_from_list(self
):
667 v
= bt2
.create_value(raw
)
668 self
.assertIsInstance(v
, bt2
.ArrayValue
)
669 self
.assertEqual(v
, raw
)
671 def test_create_array_from_tuple(self
):
673 v
= bt2
.create_value(raw
)
674 self
.assertIsInstance(v
, bt2
.ArrayValue
)
675 self
.assertEqual(v
, raw
)
677 def test_create_array_from_empty_list(self
):
679 v
= bt2
.create_value(raw
)
680 self
.assertIsInstance(v
, bt2
.ArrayValue
)
681 self
.assertEqual(v
, raw
)
683 def test_create_array_from_empty_tuple(self
):
685 v
= bt2
.create_value(raw
)
686 self
.assertIsInstance(v
, bt2
.ArrayValue
)
687 self
.assertEqual(v
, raw
)
689 def test_create_map(self
):
691 v
= bt2
.create_value(raw
)
692 self
.assertIsInstance(v
, bt2
.MapValue
)
693 self
.assertEqual(v
, raw
)
695 def test_create_map_empty(self
):
697 v
= bt2
.create_value(raw
)
698 self
.assertIsInstance(v
, bt2
.MapValue
)
699 self
.assertEqual(v
, raw
)
701 def test_create_vfalse(self
):
702 v
= bt2
.create_value(bt2
.create_value(False))
703 self
.assertIsInstance(v
, bt2
.BoolValue
)
706 def test_create_invalid(self
):
712 with self
.assertRaisesRegex(TypeError, "cannot create value object from 'A' object") as cm
:
713 v
= bt2
.create_value(a
)
716 class BoolValueTestCase(_TestCopySimple
, unittest
.TestCase
):
718 self
._f
= bt2
.BoolValue(False)
719 self
._t
= bt2
.BoolValue(True)
721 self
._def
_value
= False
722 self
._def
_new
_value
= True
729 def _assert_expecting_bool(self
):
730 return self
.assertRaisesRegex(TypeError, r
"expecting a 'bool' object")
732 def test_create_default(self
):
736 def test_create_false(self
):
737 self
.assertFalse(self
._f
)
739 def test_create_true(self
):
740 self
.assertTrue(self
._t
)
742 def test_create_from_vfalse(self
):
743 b
= bt2
.BoolValue(self
._f
)
746 def test_create_from_vtrue(self
):
747 b
= bt2
.BoolValue(self
._t
)
750 def test_create_from_int_non_zero(self
):
751 with self
.assertRaises(TypeError):
752 b
= bt2
.BoolValue(23)
754 def test_create_from_int_zero(self
):
755 with self
.assertRaises(TypeError):
758 def test_assign_true(self
):
763 def test_assign_false(self
):
768 def test_assign_vtrue(self
):
773 def test_assign_vfalse(self
):
778 def test_assign_int(self
):
779 with self
.assertRaises(TypeError):
783 def test_bool_op(self
):
784 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
786 def test_str_op(self
):
787 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
789 def test_eq_none(self
):
790 self
.assertFalse(self
._def
== None)
792 def test_ne_none(self
):
793 self
.assertTrue(self
._def
!= None)
795 def test_vfalse_eq_false(self
):
796 self
.assertEqual(self
._f
, False)
798 def test_vfalse_ne_true(self
):
799 self
.assertNotEqual(self
._f
, True)
801 def test_vtrue_eq_true(self
):
802 self
.assertEqual(self
._t
, True)
804 def test_vtrue_ne_false(self
):
805 self
.assertNotEqual(self
._t
, False)
808 class _TestIntegerValue(_TestNumericValue
):
811 self
._ip
= self
._CLS
(self
._pv
)
813 self
._def
_value
= self
._pv
814 self
._def
_new
_value
= 101
821 def _assert_expecting_int(self
):
822 return self
.assertRaisesRegex(TypeError, r
'expecting an integral number object')
824 def _assert_expecting_int64(self
):
825 return self
.assertRaisesRegex(ValueError, r
"expecting a signed 64-bit integral value")
827 def _assert_expecting_uint64(self
):
828 return self
.assertRaisesRegex(ValueError, r
"expecting an unsigned 64-bit integral value")
830 def test_create_default(self
):
832 self
.assertEqual(i
, 0)
834 def test_create_pos(self
):
835 self
.assertEqual(self
._ip
, self
._pv
)
837 def test_create_neg(self
):
838 self
.assertEqual(self
._in
, self
._nv
)
840 def test_create_from_vint(self
):
841 i
= self
._CLS
(self
._ip
)
842 self
.assertEqual(i
, self
._pv
)
844 def test_create_from_false(self
):
848 def test_create_from_true(self
):
852 def test_create_from_unknown(self
):
856 with self
._assert
_expecting
_int
():
859 def test_create_from_varray(self
):
860 with self
._assert
_expecting
_int
():
861 i
= self
._CLS
(bt2
.ArrayValue())
863 def test_assign_true(self
):
865 self
._def
.value
= raw
866 self
.assertEqual(self
._def
, raw
)
868 def test_assign_false(self
):
870 self
._def
.value
= raw
871 self
.assertEqual(self
._def
, raw
)
873 def test_assign_pos_int(self
):
875 self
._def
.value
= raw
876 self
.assertEqual(self
._def
, raw
)
878 def test_assign_vint(self
):
880 self
._def
.value
= bt2
.create_value(raw
)
881 self
.assertEqual(self
._def
, raw
)
884 class SignedIntegerValueTestCase(_TestIntegerValue
, unittest
.TestCase
):
885 _CLS
= bt2
.SignedIntegerValue
890 self
._in
= self
._CLS
(self
._nv
)
891 self
._def
_new
_value
= -101
897 def test_create_neg(self
):
898 self
.assertEqual(self
._in
, self
._nv
)
900 def test_create_pos_too_big(self
):
901 with self
._assert
_expecting
_int
64():
902 i
= self
._CLS
(2 ** 63)
904 def test_create_neg_too_big(self
):
905 with self
._assert
_expecting
_int
64():
906 i
= self
._CLS
(-(2 ** 63) - 1)
908 def test_assign_neg_int(self
):
910 self
._def
.value
= raw
911 self
.assertEqual(self
._def
, raw
)
913 def test_compare_big_int(self
):
914 # Larger than the IEEE 754 double-precision exact representation of
917 v
= bt2
.create_value(raw
)
918 self
.assertEqual(v
, raw
)
921 _inject_numeric_testing_methods(SignedIntegerValueTestCase
)
924 class UnsignedIntegerValueTestCase(_TestIntegerValue
, unittest
.TestCase
):
925 _CLS
= bt2
.UnsignedIntegerValue
927 def test_create_pos_too_big(self
):
928 with self
._assert
_expecting
_uint
64():
929 i
= self
._CLS
(2 ** 64)
931 def test_create_neg(self
):
932 with self
._assert
_expecting
_uint
64():
936 _inject_numeric_testing_methods(UnsignedIntegerValueTestCase
)
939 class RealValueTestCase(_TestNumericValue
, unittest
.TestCase
):
943 self
._fp
= bt2
.RealValue(self
._pv
)
944 self
._fn
= bt2
.RealValue(self
._nv
)
946 self
._def
_value
= self
._pv
947 self
._def
_new
_value
= -101.88
955 def _assert_expecting_float(self
):
956 return self
.assertRaisesRegex(TypeError, r
"expecting a real number object")
958 def _test_invalid_op(self
, cb
):
959 with self
.assertRaises(TypeError):
962 def test_create_default(self
):
964 self
.assertEqual(f
, 0.0)
966 def test_create_pos(self
):
967 self
.assertEqual(self
._fp
, self
._pv
)
969 def test_create_neg(self
):
970 self
.assertEqual(self
._fn
, self
._nv
)
972 def test_create_from_vint(self
):
973 f
= bt2
.RealValue(self
._fp
)
974 self
.assertEqual(f
, self
._pv
)
976 def test_create_from_false(self
):
977 f
= bt2
.RealValue(False)
980 def test_create_from_true(self
):
981 f
= bt2
.RealValue(True)
984 def test_create_from_int(self
):
986 f
= bt2
.RealValue(raw
)
987 self
.assertEqual(f
, float(raw
))
989 def test_create_from_vint(self
):
991 f
= bt2
.RealValue(bt2
.create_value(raw
))
992 self
.assertEqual(f
, float(raw
))
994 def test_create_from_vfloat(self
):
996 f
= bt2
.RealValue(bt2
.create_value(raw
))
997 self
.assertEqual(f
, raw
)
999 def test_create_from_unknown(self
):
1003 with self
._assert
_expecting
_float
():
1004 f
= bt2
.RealValue(A())
1006 def test_create_from_varray(self
):
1007 with self
._assert
_expecting
_float
():
1008 f
= bt2
.RealValue(bt2
.ArrayValue())
1010 def test_assign_true(self
):
1011 self
._def
.value
= True
1012 self
.assertTrue(self
._def
)
1014 def test_assign_false(self
):
1015 self
._def
.value
= False
1016 self
.assertFalse(self
._def
)
1018 def test_assign_pos_int(self
):
1020 self
._def
.value
= raw
1021 self
.assertEqual(self
._def
, float(raw
))
1023 def test_assign_neg_int(self
):
1025 self
._def
.value
= raw
1026 self
.assertEqual(self
._def
, float(raw
))
1028 def test_assign_vint(self
):
1030 self
._def
.value
= bt2
.create_value(raw
)
1031 self
.assertEqual(self
._def
, float(raw
))
1033 def test_assign_float(self
):
1035 self
._def
.value
= raw
1036 self
.assertEqual(self
._def
, raw
)
1038 def test_assign_vfloat(self
):
1040 self
._def
.value
= bt2
.create_value(raw
)
1041 self
.assertEqual(self
._def
, raw
)
1043 def test_invalid_lshift(self
):
1044 self
._test
_invalid
_op
(lambda: self
._def
<< 23)
1046 def test_invalid_rshift(self
):
1047 self
._test
_invalid
_op
(lambda: self
._def
>> 23)
1049 def test_invalid_and(self
):
1050 self
._test
_invalid
_op
(lambda: self
._def
& 23)
1052 def test_invalid_or(self
):
1053 self
._test
_invalid
_op
(lambda: self
._def |
23)
1055 def test_invalid_xor(self
):
1056 self
._test
_invalid
_op
(lambda: self
._def ^
23)
1058 def test_invalid_invert(self
):
1059 self
._test
_invalid
_op
(lambda: ~self
._def
)
1062 _inject_numeric_testing_methods(RealValueTestCase
)
1065 class StringValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1067 self
._def
_value
= 'Hello, World!'
1068 self
._def
= bt2
.StringValue(self
._def
_value
)
1069 self
._def
_new
_value
= 'Yes!'
1074 def _assert_expecting_str(self
):
1075 return self
.assertRaises(TypeError)
1077 def test_create_default(self
):
1078 s
= bt2
.StringValue()
1079 self
.assertEqual(s
, '')
1081 def test_create_from_str(self
):
1083 s
= bt2
.StringValue(raw
)
1084 self
.assertEqual(s
, raw
)
1086 def test_create_from_vstr(self
):
1088 s
= bt2
.StringValue(bt2
.create_value(raw
))
1089 self
.assertEqual(s
, raw
)
1091 def test_create_from_unknown(self
):
1095 with self
._assert
_expecting
_str
():
1096 i
= bt2
.StringValue(A())
1098 def test_create_from_varray(self
):
1099 with self
._assert
_expecting
_str
():
1100 i
= bt2
.StringValue(bt2
.ArrayValue())
1102 def test_assign_int(self
):
1103 with self
._assert
_expecting
_str
():
1104 self
._def
.value
= 283
1106 def test_assign_str(self
):
1109 self
.assertEqual(self
._def
, raw
)
1111 def test_assign_vstr(self
):
1113 self
._def
= bt2
.create_value(raw
)
1114 self
.assertEqual(self
._def
, raw
)
1117 self
.assertEqual(self
._def
, self
._def
_value
)
1120 self
.assertNotEqual(self
._def
, 23)
1122 def test_lt_vstring(self
):
1123 s1
= bt2
.StringValue('allo')
1124 s2
= bt2
.StringValue('bateau')
1125 self
.assertLess(s1
, s2
)
1127 def test_lt_string(self
):
1128 s1
= bt2
.StringValue('allo')
1129 self
.assertLess(s1
, 'bateau')
1131 def test_le_vstring(self
):
1132 s1
= bt2
.StringValue('allo')
1133 s2
= bt2
.StringValue('bateau')
1134 self
.assertLessEqual(s1
, s2
)
1136 def test_le_string(self
):
1137 s1
= bt2
.StringValue('allo')
1138 self
.assertLessEqual(s1
, 'bateau')
1140 def test_gt_vstring(self
):
1141 s1
= bt2
.StringValue('allo')
1142 s2
= bt2
.StringValue('bateau')
1143 self
.assertGreater(s2
, s1
)
1145 def test_gt_string(self
):
1146 s1
= bt2
.StringValue('allo')
1147 self
.assertGreater('bateau', s1
)
1149 def test_ge_vstring(self
):
1150 s1
= bt2
.StringValue('allo')
1151 s2
= bt2
.StringValue('bateau')
1152 self
.assertGreaterEqual(s2
, s1
)
1154 def test_ge_string(self
):
1155 s1
= bt2
.StringValue('allo')
1156 self
.assertGreaterEqual('bateau', s1
)
1158 def test_bool_op(self
):
1159 self
.assertEqual(bool(self
._def
), bool(self
._def
_value
))
1161 def test_str_op(self
):
1162 self
.assertEqual(str(self
._def
), str(self
._def
_value
))
1165 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1167 def test_getitem(self
):
1168 self
.assertEqual(self
._def
[5], self
._def
_value
[5])
1170 def test_append_str(self
):
1171 to_append
= 'meow meow meow'
1172 self
._def
+= to_append
1173 self
._def
_value
+= to_append
1174 self
.assertEqual(self
._def
, self
._def
_value
)
1176 def test_append_vstr(self
):
1177 to_append
= 'meow meow meow'
1178 self
._def
+= bt2
.create_value(to_append
)
1179 self
._def
_value
+= to_append
1180 self
.assertEqual(self
._def
, self
._def
_value
)
1183 class ArrayValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1185 self
._def
_value
= [None, False, True, -23, 0, 42, -42.4, 23.17, 'yes']
1186 self
._def
= bt2
.ArrayValue(copy
.deepcopy(self
._def
_value
))
1191 def _modify_def(self
):
1192 self
._def
[2] = 'xyz'
1194 def _assert_type_error(self
):
1195 return self
.assertRaises(TypeError)
1197 def test_create_default(self
):
1198 a
= bt2
.ArrayValue()
1199 self
.assertEqual(len(a
), 0)
1201 def test_create_from_array(self
):
1202 self
.assertEqual(self
._def
, self
._def
_value
)
1204 def test_create_from_tuple(self
):
1205 t
= 1, 2, False, None
1206 a
= bt2
.ArrayValue(t
)
1207 self
.assertEqual(a
, t
)
1209 def test_create_from_varray(self
):
1210 va
= bt2
.ArrayValue(copy
.deepcopy(self
._def
_value
))
1211 a
= bt2
.ArrayValue(va
)
1212 self
.assertEqual(va
, a
)
1214 def test_create_from_unknown(self
):
1218 with self
._assert
_type
_error
():
1219 a
= bt2
.ArrayValue(A())
1221 def test_bool_op_true(self
):
1222 self
.assertTrue(bool(self
._def
))
1224 def test_bool_op_false(self
):
1225 self
.assertFalse(bool(bt2
.ArrayValue()))
1228 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1230 def test_eq_int(self
):
1231 self
.assertNotEqual(self
._def
, 23)
1233 def test_eq_diff_len(self
):
1234 a1
= bt2
.create_value([1, 2, 3])
1235 a2
= bt2
.create_value([1, 2])
1236 self
.assertNotEqual(a1
, a2
)
1238 def test_eq_diff_content_same_len(self
):
1239 a1
= bt2
.create_value([1, 2, 3])
1240 a2
= bt2
.create_value([4, 5, 6])
1241 self
.assertNotEqual(a1
, a2
)
1243 def test_eq_same_content_same_len(self
):
1244 raw
= (3, True, [1, 2.5, None, {'a': 17.6, 'b': None}])
1245 a1
= bt2
.ArrayValue(raw
)
1246 a2
= bt2
.ArrayValue(copy
.deepcopy(raw
))
1247 self
.assertEqual(a1
, a2
)
1249 def test_setitem_int(self
):
1252 self
.assertEqual(self
._def
[2], raw
)
1254 def test_setitem_vint(self
):
1256 self
._def
[2] = bt2
.create_value(raw
)
1257 self
.assertEqual(self
._def
[2], raw
)
1259 def test_setitem_none(self
):
1261 self
.assertIsNone(self
._def
[2])
1263 def test_setitem_index_wrong_type(self
):
1264 with self
._assert
_type
_error
():
1265 self
._def
['yes'] = 23
1267 def test_setitem_index_neg(self
):
1268 with self
.assertRaises(IndexError):
1271 def test_setitem_index_out_of_range(self
):
1272 with self
.assertRaises(IndexError):
1273 self
._def
[len(self
._def
)] = 23
1275 def test_append_none(self
):
1276 self
._def
.append(None)
1277 self
.assertIsNone(self
._def
[len(self
._def
) - 1])
1279 def test_append_int(self
):
1281 self
._def
.append(raw
)
1282 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
)
1284 def test_append_vint(self
):
1286 self
._def
.append(bt2
.create_value(raw
))
1287 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
)
1289 def test_append_unknown(self
):
1293 with self
._assert
_type
_error
():
1294 self
._def
.append(A())
1296 def test_iadd(self
):
1299 self
.assertEqual(self
._def
[len(self
._def
) - 3], raw
[0])
1300 self
.assertEqual(self
._def
[len(self
._def
) - 2], raw
[1])
1301 self
.assertEqual(self
._def
[len(self
._def
) - 1], raw
[2])
1303 def test_iadd_unknown(self
):
1307 with self
._assert
_type
_error
():
1310 def test_iadd_list_unknown(self
):
1314 with self
._assert
_type
_error
():
1317 def test_iter(self
):
1318 for velem
, elem
in zip(self
._def
, self
._def
_value
):
1319 self
.assertEqual(velem
, elem
)
1322 class MapValueTestCase(_TestCopySimple
, unittest
.TestCase
):
1335 self
._def
= bt2
.MapValue(copy
.deepcopy(self
._def
_value
))
1340 def _modify_def(self
):
1341 self
._def
['zero'] = 1
1343 def test_create_default(self
):
1345 self
.assertEqual(len(m
), 0)
1347 def test_create_from_dict(self
):
1348 self
.assertEqual(self
._def
, self
._def
_value
)
1350 def test_create_from_vmap(self
):
1351 vm
= bt2
.MapValue(copy
.deepcopy(self
._def
_value
))
1352 m
= bt2
.MapValue(vm
)
1353 self
.assertEqual(vm
, m
)
1355 def test_create_from_unknown(self
):
1359 with self
.assertRaises(AttributeError):
1360 m
= bt2
.MapValue(A())
1362 def test_bool_op_true(self
):
1363 self
.assertTrue(bool(self
._def
))
1365 def test_bool_op_false(self
):
1366 self
.assertFalse(bool(bt2
.MapValue()))
1369 self
.assertEqual(len(self
._def
), len(self
._def
_value
))
1371 def test_eq_int(self
):
1372 self
.assertNotEqual(self
._def
, 23)
1374 def test_eq_diff_len(self
):
1375 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1376 a2
= bt2
.create_value({'a': 1, 'b': 2})
1377 self
.assertNotEqual(a1
, a2
)
1379 def test_eq_diff_content_same_len(self
):
1380 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1381 a2
= bt2
.create_value({'a': 4, 'b': 2, 'c': 3})
1382 self
.assertNotEqual(a1
, a2
)
1384 def test_eq_same_content_diff_keys(self
):
1385 a1
= bt2
.create_value({'a': 1, 'b': 2, 'c': 3})
1386 a2
= bt2
.create_value({'a': 1, 'k': 2, 'c': 3})
1387 self
.assertNotEqual(a1
, a2
)
1389 def test_eq_same_content_same_len(self
):
1393 'array': [1, 2.5, None, {'a': 17.6, 'b': None}]
1395 a1
= bt2
.MapValue(raw
)
1396 a2
= bt2
.MapValue(copy
.deepcopy(raw
))
1397 self
.assertEqual(a1
, a2
)
1398 self
.assertEqual(a1
, raw
)
1400 def test_setitem_int(self
):
1402 self
._def
['pos-int'] = raw
1403 self
.assertEqual(self
._def
['pos-int'], raw
)
1405 def test_setitem_vint(self
):
1407 self
._def
['pos-int'] = bt2
.create_value(raw
)
1408 self
.assertEqual(self
._def
['pos-int'], raw
)
1410 def test_setitem_none(self
):
1411 self
._def
['none'] = None
1412 self
.assertIsNone(self
._def
['none'])
1414 def test_setitem_new_int(self
):
1415 old_len
= len(self
._def
)
1416 self
._def
['new-int'] = 23
1417 self
.assertEqual(self
._def
['new-int'], 23)
1418 self
.assertEqual(len(self
._def
), old_len
+ 1)
1420 def test_setitem_index_wrong_type(self
):
1421 with self
.assertRaises(TypeError):
1424 def test_iter(self
):
1425 for vkey
, vval
in self
._def
.items():
1426 val
= self
._def
_value
[vkey
]
1427 self
.assertEqual(vval
, val
)
1429 def test_getitem_wrong_key(self
):
1430 with self
.assertRaises(KeyError):
1431 self
._def
['kilojoule']