Commit | Line | Data |
---|---|---|
9cf643d1 PP |
1 | from functools import partial, partialmethod |
2 | import operator | |
3 | import unittest | |
4 | import numbers | |
5 | import math | |
6 | import copy | |
7 | import bt2 | |
8 | ||
9 | ||
10 | class _TestFrozen: | |
11 | def test_is_frozen(self): | |
12 | self._def.freeze() | |
13 | self.assertTrue(self._def.is_frozen) | |
14 | ||
15 | def test_frozen(self): | |
16 | self._def.freeze() | |
17 | self.assertTrue(self._def.frozen) | |
18 | ||
19 | def test_frozen_exc(self): | |
20 | self._def.freeze() | |
21 | ||
22 | with self.assertRaisesRegex(bt2.FrozenError, r'.* value object is frozen$') as cm: | |
23 | self._modify_def() | |
24 | ||
25 | self.assertEqual(self._def, self._def_value) | |
26 | ||
27 | def test_get_value_when_frozen(self): | |
28 | self._def.freeze() | |
29 | self.assertEqual(self._def, self._def_value) | |
30 | ||
31 | ||
32 | class _TestFrozenSimple(_TestFrozen): | |
33 | def _modify_def(self): | |
34 | self._def.value = self._def_new_value | |
35 | ||
36 | ||
37 | class _TestCopySimple: | |
38 | def test_copy(self): | |
39 | cpy = copy.copy(self._def) | |
40 | self.assertIsNot(cpy, self._def) | |
41 | self.assertNotEqual(cpy.addr, self._def.addr) | |
42 | self.assertEqual(cpy, self._def) | |
43 | ||
44 | def test_deepcopy(self): | |
45 | cpy = copy.deepcopy(self._def) | |
46 | self.assertIsNot(cpy, self._def) | |
47 | self.assertNotEqual(cpy.addr, self._def.addr) | |
48 | self.assertEqual(cpy, self._def) | |
49 | ||
50 | ||
51 | _COMP_BINOPS = ( | |
52 | operator.eq, | |
53 | operator.ne, | |
54 | ) | |
55 | ||
56 | ||
57 | class _TestNumericValue(_TestFrozenSimple, _TestCopySimple): | |
58 | def _binop(self, op, rhs): | |
59 | rexc = None | |
60 | rvexc = None | |
61 | comp_value = rhs | |
62 | ||
63 | if type(rhs) in (bt2.BoolValue, bt2.IntegerValue, bt2.FloatValue): | |
64 | comp_value = rhs.value | |
65 | ||
66 | try: | |
67 | r = op(self._def, rhs) | |
68 | except Exception as e: | |
69 | rexc = e | |
70 | ||
71 | try: | |
72 | rv = op(self._def_value, comp_value) | |
73 | except Exception as e: | |
74 | rvexc = e | |
75 | ||
76 | if rexc is not None or rvexc is not None: | |
77 | # at least one of the operations raised an exception: in | |
78 | # this case both operations should have raised the same | |
79 | # type of exception (division by zero, bit shift with a | |
80 | # floating point number operand, etc.) | |
81 | self.assertIs(type(rexc), type(rvexc)) | |
82 | return None, None | |
83 | ||
84 | return r, rv | |
85 | ||
86 | def _unaryop(self, op): | |
87 | rexc = None | |
88 | rvexc = None | |
89 | ||
90 | try: | |
91 | r = op(self._def) | |
92 | except Exception as e: | |
93 | rexc = e | |
94 | ||
95 | try: | |
96 | rv = op(self._def_value) | |
97 | except Exception as e: | |
98 | rvexc = e | |
99 | ||
100 | if rexc is not None or rvexc is not None: | |
101 | # at least one of the operations raised an exception: in | |
102 | # this case both operations should have raised the same | |
103 | # type of exception (division by zero, bit shift with a | |
104 | # floating point number operand, etc.) | |
105 | self.assertIs(type(rexc), type(rvexc)) | |
106 | return None, None | |
107 | ||
108 | return r, rv | |
109 | ||
110 | def _test_unaryop_type(self, op): | |
111 | r, rv = self._unaryop(op) | |
112 | ||
113 | if r is None: | |
114 | return | |
115 | ||
116 | self.assertIsInstance(r, type(rv)) | |
117 | ||
118 | def _test_unaryop_value(self, op): | |
119 | r, rv = self._unaryop(op) | |
120 | ||
121 | if r is None: | |
122 | return | |
123 | ||
124 | self.assertEqual(r, rv) | |
125 | ||
126 | def _test_unaryop_addr_same(self, op): | |
127 | addr_before = self._def.addr | |
128 | self._unaryop(op) | |
129 | self.assertEqual(self._def.addr, addr_before) | |
130 | ||
131 | def _test_unaryop_value_same(self, op): | |
132 | value_before = self._def.value | |
133 | self._unaryop(op) | |
134 | self.assertEqual(self._def.value, value_before) | |
135 | ||
136 | def _test_binop_type(self, op, rhs): | |
137 | r, rv = self._binop(op, rhs) | |
138 | ||
139 | if r is None: | |
140 | return | |
141 | ||
142 | if op in _COMP_BINOPS: | |
143 | # __eq__() and __ne__() always return a 'bool' object | |
144 | self.assertIsInstance(r, bool) | |
145 | else: | |
146 | self.assertIsInstance(r, type(rv)) | |
147 | ||
148 | def _test_binop_value(self, op, rhs): | |
149 | r, rv = self._binop(op, rhs) | |
150 | ||
151 | if r is None: | |
152 | return | |
153 | ||
154 | self.assertEqual(r, rv) | |
155 | ||
156 | def _test_binop_lhs_addr_same(self, op, rhs): | |
157 | addr_before = self._def.addr | |
158 | r, rv = self._binop(op, rhs) | |
159 | self.assertEqual(self._def.addr, addr_before) | |
160 | ||
161 | def _test_binop_lhs_value_same(self, op, rhs): | |
162 | value_before = self._def.value | |
163 | r, rv = self._binop(op, rhs) | |
164 | self.assertEqual(self._def.value, value_before) | |
165 | ||
166 | def _test_binop_invalid_unknown(self, op): | |
167 | if op in _COMP_BINOPS: | |
168 | self.skipTest('not testing') | |
169 | ||
170 | class A: | |
171 | pass | |
172 | ||
173 | with self.assertRaises(TypeError): | |
174 | op(self._def, A()) | |
175 | ||
176 | def _test_binop_invalid_none(self, op): | |
177 | if op in _COMP_BINOPS: | |
178 | self.skipTest('not testing') | |
179 | ||
180 | with self.assertRaises(TypeError): | |
181 | op(self._def, None) | |
182 | ||
183 | def _test_ibinop_value(self, op, rhs): | |
184 | r, rv = self._binop(op, rhs) | |
185 | ||
186 | if r is None: | |
187 | return | |
188 | ||
189 | # The inplace operators are special for value objects because | |
190 | # they do not return a new, immutable object like it's the case | |
191 | # for Python numbers. In Python, `a += 2`, where `a` is a number | |
192 | # object, assigns a new number object reference to `a`, dropping | |
193 | # the old reference. Since BT's value objects are mutable, we | |
194 | # modify their internal value with the inplace operators. This | |
195 | # means however that we can lose data in the process, for | |
196 | # example: | |
197 | # | |
198 | # int_value_obj += 3.3 | |
199 | # | |
200 | # Here, if `int_value_obj` is a Python `int` with the value 2, | |
201 | # it would be a `float` object after this, holding the value | |
202 | # 5.3. In our case, if `int_value_obj` is an integer value | |
203 | # object, 3.3 is converted to an `int` object (3) and added to | |
204 | # the current value of `int_value_obj`, so after this the value | |
205 | # of the object is 5. This does not compare to 5.3, which is | |
206 | # why we also use the `int()` type here. | |
207 | if type(self._def) is bt2.IntegerValue: | |
208 | rv = int(rv) | |
209 | ||
210 | self.assertEqual(r, rv) | |
211 | ||
212 | def _test_ibinop_type(self, op, rhs): | |
213 | r, rv = self._binop(op, rhs) | |
214 | ||
215 | if r is None: | |
216 | return | |
217 | ||
218 | self.assertIs(r, self._def) | |
219 | ||
220 | def _test_ibinop_invalid_unknown(self, op): | |
221 | class A: | |
222 | pass | |
223 | ||
224 | with self.assertRaises(TypeError): | |
225 | op(self._def, A()) | |
226 | ||
227 | def _test_ibinop_invalid_none(self, op): | |
228 | with self.assertRaises(TypeError): | |
229 | op(self._def, None) | |
230 | ||
231 | def _test_binop_rhs_false(self, test_cb, op): | |
232 | test_cb(op, False) | |
233 | ||
234 | def _test_binop_rhs_true(self, test_cb, op): | |
235 | test_cb(op, True) | |
236 | ||
237 | def _test_binop_rhs_pos_int(self, test_cb, op): | |
238 | test_cb(op, 2) | |
239 | ||
240 | def _test_binop_rhs_neg_int(self, test_cb, op): | |
241 | test_cb(op, -23) | |
242 | ||
243 | def _test_binop_rhs_zero_int(self, test_cb, op): | |
244 | test_cb(op, 0) | |
245 | ||
246 | def _test_binop_rhs_pos_vint(self, test_cb, op): | |
247 | test_cb(op, bt2.create_value(2)) | |
248 | ||
249 | def _test_binop_rhs_neg_vint(self, test_cb, op): | |
250 | test_cb(op, bt2.create_value(-23)) | |
251 | ||
252 | def _test_binop_rhs_zero_vint(self, test_cb, op): | |
253 | test_cb(op, bt2.create_value(0)) | |
254 | ||
255 | def _test_binop_rhs_pos_float(self, test_cb, op): | |
256 | test_cb(op, 2.2) | |
257 | ||
258 | def _test_binop_rhs_neg_float(self, test_cb, op): | |
259 | test_cb(op, -23.4) | |
260 | ||
261 | def _test_binop_rhs_zero_float(self, test_cb, op): | |
262 | test_cb(op, 0.0) | |
263 | ||
264 | def _test_binop_rhs_pos_vfloat(self, test_cb, op): | |
265 | test_cb(op, bt2.create_value(2.2)) | |
266 | ||
267 | def _test_binop_rhs_neg_vfloat(self, test_cb, op): | |
268 | test_cb(op, bt2.create_value(-23.4)) | |
269 | ||
270 | def _test_binop_rhs_zero_vfloat(self, test_cb, op): | |
271 | test_cb(op, bt2.create_value(0.0)) | |
272 | ||
273 | def _test_binop_type_false(self, op): | |
274 | self._test_binop_rhs_false(self._test_binop_type, op) | |
275 | ||
276 | def _test_binop_type_true(self, op): | |
277 | self._test_binop_rhs_true(self._test_binop_type, op) | |
278 | ||
279 | def _test_binop_type_pos_int(self, op): | |
280 | self._test_binop_rhs_pos_int(self._test_binop_type, op) | |
281 | ||
282 | def _test_binop_type_neg_int(self, op): | |
283 | self._test_binop_rhs_neg_int(self._test_binop_type, op) | |
284 | ||
285 | def _test_binop_type_zero_int(self, op): | |
286 | self._test_binop_rhs_zero_int(self._test_binop_type, op) | |
287 | ||
288 | def _test_binop_type_pos_vint(self, op): | |
289 | self._test_binop_rhs_pos_vint(self._test_binop_type, op) | |
290 | ||
291 | def _test_binop_type_neg_vint(self, op): | |
292 | self._test_binop_rhs_neg_vint(self._test_binop_type, op) | |
293 | ||
294 | def _test_binop_type_zero_vint(self, op): | |
295 | self._test_binop_rhs_zero_vint(self._test_binop_type, op) | |
296 | ||
297 | def _test_binop_type_pos_float(self, op): | |
298 | self._test_binop_rhs_pos_float(self._test_binop_type, op) | |
299 | ||
300 | def _test_binop_type_neg_float(self, op): | |
301 | self._test_binop_rhs_neg_float(self._test_binop_type, op) | |
302 | ||
303 | def _test_binop_type_zero_float(self, op): | |
304 | self._test_binop_rhs_zero_float(self._test_binop_type, op) | |
305 | ||
306 | def _test_binop_type_pos_vfloat(self, op): | |
307 | self._test_binop_rhs_pos_vfloat(self._test_binop_type, op) | |
308 | ||
309 | def _test_binop_type_neg_vfloat(self, op): | |
310 | self._test_binop_rhs_neg_vfloat(self._test_binop_type, op) | |
311 | ||
312 | def _test_binop_type_zero_vfloat(self, op): | |
313 | self._test_binop_rhs_zero_vfloat(self._test_binop_type, op) | |
314 | ||
315 | def _test_binop_value_false(self, op): | |
316 | self._test_binop_rhs_false(self._test_binop_value, op) | |
317 | ||
318 | def _test_binop_value_true(self, op): | |
319 | self._test_binop_rhs_true(self._test_binop_value, op) | |
320 | ||
321 | def _test_binop_value_pos_int(self, op): | |
322 | self._test_binop_rhs_pos_int(self._test_binop_value, op) | |
323 | ||
324 | def _test_binop_value_neg_int(self, op): | |
325 | self._test_binop_rhs_neg_int(self._test_binop_value, op) | |
326 | ||
327 | def _test_binop_value_zero_int(self, op): | |
328 | self._test_binop_rhs_zero_int(self._test_binop_value, op) | |
329 | ||
330 | def _test_binop_value_pos_vint(self, op): | |
331 | self._test_binop_rhs_pos_vint(self._test_binop_value, op) | |
332 | ||
333 | def _test_binop_value_neg_vint(self, op): | |
334 | self._test_binop_rhs_neg_vint(self._test_binop_value, op) | |
335 | ||
336 | def _test_binop_value_zero_vint(self, op): | |
337 | self._test_binop_rhs_zero_vint(self._test_binop_value, op) | |
338 | ||
339 | def _test_binop_value_pos_float(self, op): | |
340 | self._test_binop_rhs_pos_float(self._test_binop_value, op) | |
341 | ||
342 | def _test_binop_value_neg_float(self, op): | |
343 | self._test_binop_rhs_neg_float(self._test_binop_value, op) | |
344 | ||
345 | def _test_binop_value_zero_float(self, op): | |
346 | self._test_binop_rhs_zero_float(self._test_binop_value, op) | |
347 | ||
348 | def _test_binop_value_pos_vfloat(self, op): | |
349 | self._test_binop_rhs_pos_vfloat(self._test_binop_value, op) | |
350 | ||
351 | def _test_binop_value_neg_vfloat(self, op): | |
352 | self._test_binop_rhs_neg_vfloat(self._test_binop_value, op) | |
353 | ||
354 | def _test_binop_value_zero_vfloat(self, op): | |
355 | self._test_binop_rhs_zero_vfloat(self._test_binop_value, op) | |
356 | ||
357 | def _test_binop_lhs_addr_same_false(self, op): | |
358 | self._test_binop_rhs_false(self._test_binop_lhs_addr_same, op) | |
359 | ||
360 | def _test_binop_lhs_addr_same_true(self, op): | |
361 | self._test_binop_rhs_true(self._test_binop_lhs_addr_same, op) | |
362 | ||
363 | def _test_binop_lhs_addr_same_pos_int(self, op): | |
364 | self._test_binop_rhs_pos_int(self._test_binop_lhs_addr_same, op) | |
365 | ||
366 | def _test_binop_lhs_addr_same_neg_int(self, op): | |
367 | self._test_binop_rhs_neg_int(self._test_binop_lhs_addr_same, op) | |
368 | ||
369 | def _test_binop_lhs_addr_same_zero_int(self, op): | |
370 | self._test_binop_rhs_zero_int(self._test_binop_lhs_addr_same, op) | |
371 | ||
372 | def _test_binop_lhs_addr_same_pos_vint(self, op): | |
373 | self._test_binop_rhs_pos_vint(self._test_binop_lhs_addr_same, op) | |
374 | ||
375 | def _test_binop_lhs_addr_same_neg_vint(self, op): | |
376 | self._test_binop_rhs_neg_vint(self._test_binop_lhs_addr_same, op) | |
377 | ||
378 | def _test_binop_lhs_addr_same_zero_vint(self, op): | |
379 | self._test_binop_rhs_zero_vint(self._test_binop_lhs_addr_same, op) | |
380 | ||
381 | def _test_binop_lhs_addr_same_pos_float(self, op): | |
382 | self._test_binop_rhs_pos_float(self._test_binop_lhs_addr_same, op) | |
383 | ||
384 | def _test_binop_lhs_addr_same_neg_float(self, op): | |
385 | self._test_binop_rhs_neg_float(self._test_binop_lhs_addr_same, op) | |
386 | ||
387 | def _test_binop_lhs_addr_same_zero_float(self, op): | |
388 | self._test_binop_rhs_zero_float(self._test_binop_lhs_addr_same, op) | |
389 | ||
390 | def _test_binop_lhs_addr_same_pos_vfloat(self, op): | |
391 | self._test_binop_rhs_pos_vfloat(self._test_binop_lhs_addr_same, op) | |
392 | ||
393 | def _test_binop_lhs_addr_same_neg_vfloat(self, op): | |
394 | self._test_binop_rhs_neg_vfloat(self._test_binop_lhs_addr_same, op) | |
395 | ||
396 | def _test_binop_lhs_addr_same_zero_vfloat(self, op): | |
397 | self._test_binop_rhs_zero_vfloat(self._test_binop_lhs_addr_same, op) | |
398 | ||
399 | def _test_binop_lhs_value_same_false(self, op): | |
400 | self._test_binop_rhs_false(self._test_binop_lhs_value_same, op) | |
401 | ||
402 | def _test_binop_lhs_value_same_true(self, op): | |
403 | self._test_binop_rhs_true(self._test_binop_lhs_value_same, op) | |
404 | ||
405 | def _test_binop_lhs_value_same_pos_int(self, op): | |
406 | self._test_binop_rhs_pos_int(self._test_binop_lhs_value_same, op) | |
407 | ||
408 | def _test_binop_lhs_value_same_neg_int(self, op): | |
409 | self._test_binop_rhs_neg_int(self._test_binop_lhs_value_same, op) | |
410 | ||
411 | def _test_binop_lhs_value_same_zero_int(self, op): | |
412 | self._test_binop_rhs_zero_int(self._test_binop_lhs_value_same, op) | |
413 | ||
414 | def _test_binop_lhs_value_same_pos_vint(self, op): | |
415 | self._test_binop_rhs_pos_vint(self._test_binop_lhs_value_same, op) | |
416 | ||
417 | def _test_binop_lhs_value_same_neg_vint(self, op): | |
418 | self._test_binop_rhs_neg_vint(self._test_binop_lhs_value_same, op) | |
419 | ||
420 | def _test_binop_lhs_value_same_zero_vint(self, op): | |
421 | self._test_binop_rhs_zero_vint(self._test_binop_lhs_value_same, op) | |
422 | ||
423 | def _test_binop_lhs_value_same_pos_float(self, op): | |
424 | self._test_binop_rhs_pos_float(self._test_binop_lhs_value_same, op) | |
425 | ||
426 | def _test_binop_lhs_value_same_neg_float(self, op): | |
427 | self._test_binop_rhs_neg_float(self._test_binop_lhs_value_same, op) | |
428 | ||
429 | def _test_binop_lhs_value_same_zero_float(self, op): | |
430 | self._test_binop_rhs_zero_float(self._test_binop_lhs_value_same, op) | |
431 | ||
432 | def _test_binop_lhs_value_same_pos_vfloat(self, op): | |
433 | self._test_binop_rhs_pos_vfloat(self._test_binop_lhs_value_same, op) | |
434 | ||
435 | def _test_binop_lhs_value_same_neg_vfloat(self, op): | |
436 | self._test_binop_rhs_neg_vfloat(self._test_binop_lhs_value_same, op) | |
437 | ||
438 | def _test_binop_lhs_value_same_zero_vfloat(self, op): | |
439 | self._test_binop_rhs_zero_vfloat(self._test_binop_lhs_value_same, op) | |
440 | ||
441 | def _test_ibinop_type_false(self, op): | |
442 | self._test_binop_rhs_false(self._test_ibinop_type, op) | |
443 | ||
444 | def _test_ibinop_type_true(self, op): | |
445 | self._test_binop_rhs_true(self._test_ibinop_type, op) | |
446 | ||
447 | def _test_ibinop_type_pos_int(self, op): | |
448 | self._test_binop_rhs_pos_int(self._test_ibinop_type, op) | |
449 | ||
450 | def _test_ibinop_type_neg_int(self, op): | |
451 | self._test_binop_rhs_neg_int(self._test_ibinop_type, op) | |
452 | ||
453 | def _test_ibinop_type_zero_int(self, op): | |
454 | self._test_binop_rhs_zero_int(self._test_ibinop_type, op) | |
455 | ||
456 | def _test_ibinop_type_pos_vint(self, op): | |
457 | self._test_binop_rhs_pos_vint(self._test_ibinop_type, op) | |
458 | ||
459 | def _test_ibinop_type_neg_vint(self, op): | |
460 | self._test_binop_rhs_neg_vint(self._test_ibinop_type, op) | |
461 | ||
462 | def _test_ibinop_type_zero_vint(self, op): | |
463 | self._test_binop_rhs_zero_vint(self._test_ibinop_type, op) | |
464 | ||
465 | def _test_ibinop_type_pos_float(self, op): | |
466 | self._test_binop_rhs_pos_float(self._test_ibinop_type, op) | |
467 | ||
468 | def _test_ibinop_type_neg_float(self, op): | |
469 | self._test_binop_rhs_neg_float(self._test_ibinop_type, op) | |
470 | ||
471 | def _test_ibinop_type_zero_float(self, op): | |
472 | self._test_binop_rhs_zero_float(self._test_ibinop_type, op) | |
473 | ||
474 | def _test_ibinop_type_pos_vfloat(self, op): | |
475 | self._test_binop_rhs_pos_vfloat(self._test_ibinop_type, op) | |
476 | ||
477 | def _test_ibinop_type_neg_vfloat(self, op): | |
478 | self._test_binop_rhs_neg_vfloat(self._test_ibinop_type, op) | |
479 | ||
480 | def _test_ibinop_type_zero_vfloat(self, op): | |
481 | self._test_binop_rhs_zero_vfloat(self._test_ibinop_type, op) | |
482 | ||
483 | def _test_ibinop_value_false(self, op): | |
484 | self._test_binop_rhs_false(self._test_ibinop_value, op) | |
485 | ||
486 | def _test_ibinop_value_true(self, op): | |
487 | self._test_binop_rhs_true(self._test_ibinop_value, op) | |
488 | ||
489 | def _test_ibinop_value_pos_int(self, op): | |
490 | self._test_binop_rhs_pos_int(self._test_ibinop_value, op) | |
491 | ||
492 | def _test_ibinop_value_neg_int(self, op): | |
493 | self._test_binop_rhs_neg_int(self._test_ibinop_value, op) | |
494 | ||
495 | def _test_ibinop_value_zero_int(self, op): | |
496 | self._test_binop_rhs_zero_int(self._test_ibinop_value, op) | |
497 | ||
498 | def _test_ibinop_value_pos_vint(self, op): | |
499 | self._test_binop_rhs_pos_vint(self._test_ibinop_value, op) | |
500 | ||
501 | def _test_ibinop_value_neg_vint(self, op): | |
502 | self._test_binop_rhs_neg_vint(self._test_ibinop_value, op) | |
503 | ||
504 | def _test_ibinop_value_zero_vint(self, op): | |
505 | self._test_binop_rhs_zero_vint(self._test_ibinop_value, op) | |
506 | ||
507 | def _test_ibinop_value_pos_float(self, op): | |
508 | self._test_binop_rhs_pos_float(self._test_ibinop_value, op) | |
509 | ||
510 | def _test_ibinop_value_neg_float(self, op): | |
511 | self._test_binop_rhs_neg_float(self._test_ibinop_value, op) | |
512 | ||
513 | def _test_ibinop_value_zero_float(self, op): | |
514 | self._test_binop_rhs_zero_float(self._test_ibinop_value, op) | |
515 | ||
516 | def _test_ibinop_value_pos_vfloat(self, op): | |
517 | self._test_binop_rhs_pos_vfloat(self._test_ibinop_value, op) | |
518 | ||
519 | def _test_ibinop_value_neg_vfloat(self, op): | |
520 | self._test_binop_rhs_neg_vfloat(self._test_ibinop_value, op) | |
521 | ||
522 | def _test_ibinop_value_zero_vfloat(self, op): | |
523 | self._test_binop_rhs_zero_vfloat(self._test_ibinop_value, op) | |
524 | ||
525 | def test_bool_op(self): | |
526 | self.assertEqual(bool(self._def), bool(self._def_value)) | |
527 | ||
528 | def test_int_op(self): | |
529 | self.assertEqual(int(self._def), int(self._def_value)) | |
530 | ||
531 | def test_float_op(self): | |
532 | self.assertEqual(float(self._def), float(self._def_value)) | |
533 | ||
534 | def test_complex_op(self): | |
535 | self.assertEqual(complex(self._def), complex(self._def_value)) | |
536 | ||
537 | def test_str_op(self): | |
538 | self.assertEqual(str(self._def), str(self._def_value)) | |
539 | ||
540 | def test_eq_none(self): | |
541 | self.assertFalse(self._def == None) | |
542 | ||
543 | def test_ne_none(self): | |
544 | self.assertTrue(self._def != None) | |
545 | ||
546 | ||
547 | _BINOPS = ( | |
548 | ('lt', operator.lt), | |
549 | ('le', operator.le), | |
550 | ('eq', operator.eq), | |
551 | ('ne', operator.ne), | |
552 | ('ge', operator.ge), | |
553 | ('gt', operator.gt), | |
554 | ('add', operator.add), | |
555 | ('radd', lambda a, b: operator.add(b, a)), | |
556 | ('and', operator.and_), | |
557 | ('rand', lambda a, b: operator.and_(b, a)), | |
558 | ('floordiv', operator.floordiv), | |
559 | ('rfloordiv', lambda a, b: operator.floordiv(b, a)), | |
560 | ('lshift', operator.lshift), | |
561 | ('rlshift', lambda a, b: operator.lshift(b, a)), | |
562 | ('mod', operator.mod), | |
563 | ('rmod', lambda a, b: operator.mod(b, a)), | |
564 | ('mul', operator.mul), | |
565 | ('rmul', lambda a, b: operator.mul(b, a)), | |
566 | ('or', operator.or_), | |
567 | ('ror', lambda a, b: operator.or_(b, a)), | |
568 | ('pow', operator.pow), | |
569 | ('rpow', lambda a, b: operator.pow(b, a)), | |
570 | ('rshift', operator.rshift), | |
571 | ('rrshift', lambda a, b: operator.rshift(b, a)), | |
572 | ('sub', operator.sub), | |
573 | ('rsub', lambda a, b: operator.sub(b, a)), | |
574 | ('truediv', operator.truediv), | |
575 | ('rtruediv', lambda a, b: operator.truediv(b, a)), | |
576 | ('xor', operator.xor), | |
577 | ('rxor', lambda a, b: operator.xor(b, a)), | |
578 | ) | |
579 | ||
580 | ||
581 | _IBINOPS = ( | |
582 | ('iadd', operator.iadd), | |
583 | ('iand', operator.iand), | |
584 | ('ifloordiv', operator.ifloordiv), | |
585 | ('ilshift', operator.ilshift), | |
586 | ('imod', operator.imod), | |
587 | ('imul', operator.imul), | |
588 | ('ior', operator.ior), | |
589 | ('ipow', operator.ipow), | |
590 | ('irshift', operator.irshift), | |
591 | ('isub', operator.isub), | |
592 | ('itruediv', operator.itruediv), | |
593 | ('ixor', operator.ixor), | |
594 | ) | |
595 | ||
596 | ||
597 | _UNARYOPS = ( | |
598 | ('neg', operator.neg), | |
599 | ('pos', operator.pos), | |
600 | ('abs', operator.abs), | |
601 | ('invert', operator.invert), | |
602 | ('round', round), | |
603 | ('round_0', partial(round, ndigits=0)), | |
604 | ('round_1', partial(round, ndigits=1)), | |
605 | ('round_2', partial(round, ndigits=2)), | |
606 | ('round_3', partial(round, ndigits=3)), | |
607 | ('ceil', math.ceil), | |
608 | ('floor', math.floor), | |
609 | ('trunc', math.trunc), | |
610 | ) | |
611 | ||
612 | ||
613 | def _inject_numeric_testing_methods(cls): | |
614 | def test_binop_name(suffix): | |
615 | return 'test_binop_{}_{}'.format(name, suffix) | |
616 | ||
617 | def test_ibinop_name(suffix): | |
618 | return 'test_ibinop_{}_{}'.format(name, suffix) | |
619 | ||
620 | def test_unaryop_name(suffix): | |
621 | return 'test_unaryop_{}_{}'.format(name, suffix) | |
622 | ||
623 | # inject testing methods for each binary operation | |
624 | for name, binop in _BINOPS: | |
625 | ||
626 | setattr(cls, test_binop_name('invalid_unknown'), partialmethod(_TestNumericValue._test_binop_invalid_unknown, op=binop)) | |
627 | setattr(cls, test_binop_name('invalid_none'), partialmethod(_TestNumericValue._test_binop_invalid_none, op=binop)) | |
628 | setattr(cls, test_binop_name('type_true'), partialmethod(_TestNumericValue._test_binop_type_true, op=binop)) | |
629 | setattr(cls, test_binop_name('type_pos_int'), partialmethod(_TestNumericValue._test_binop_type_pos_int, op=binop)) | |
630 | setattr(cls, test_binop_name('type_pos_vint'), partialmethod(_TestNumericValue._test_binop_type_pos_vint, op=binop)) | |
631 | setattr(cls, test_binop_name('value_true'), partialmethod(_TestNumericValue._test_binop_value_true, op=binop)) | |
632 | setattr(cls, test_binop_name('value_pos_int'), partialmethod(_TestNumericValue._test_binop_value_pos_int, op=binop)) | |
633 | setattr(cls, test_binop_name('value_pos_vint'), partialmethod(_TestNumericValue._test_binop_value_pos_vint, op=binop)) | |
634 | setattr(cls, test_binop_name('lhs_addr_same_true'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_true, op=binop)) | |
635 | setattr(cls, test_binop_name('lhs_addr_same_pos_int'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_pos_int, op=binop)) | |
636 | setattr(cls, test_binop_name('lhs_addr_same_pos_vint'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_pos_vint, op=binop)) | |
637 | setattr(cls, test_binop_name('lhs_value_same_true'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_true, op=binop)) | |
638 | setattr(cls, test_binop_name('lhs_value_same_pos_int'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_pos_int, op=binop)) | |
639 | setattr(cls, test_binop_name('lhs_value_same_pos_vint'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_pos_vint, op=binop)) | |
640 | setattr(cls, test_binop_name('type_neg_int'), partialmethod(_TestNumericValue._test_binop_type_neg_int, op=binop)) | |
641 | setattr(cls, test_binop_name('type_neg_vint'), partialmethod(_TestNumericValue._test_binop_type_neg_vint, op=binop)) | |
642 | setattr(cls, test_binop_name('value_neg_int'), partialmethod(_TestNumericValue._test_binop_value_neg_int, op=binop)) | |
643 | setattr(cls, test_binop_name('value_neg_vint'), partialmethod(_TestNumericValue._test_binop_value_neg_vint, op=binop)) | |
644 | setattr(cls, test_binop_name('lhs_addr_same_neg_int'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_neg_int, op=binop)) | |
645 | setattr(cls, test_binop_name('lhs_addr_same_neg_vint'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_neg_vint, op=binop)) | |
646 | setattr(cls, test_binop_name('lhs_value_same_neg_int'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_neg_int, op=binop)) | |
647 | setattr(cls, test_binop_name('lhs_value_same_neg_vint'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_neg_vint, op=binop)) | |
648 | setattr(cls, test_binop_name('type_false'), partialmethod(_TestNumericValue._test_binop_type_false, op=binop)) | |
649 | setattr(cls, test_binop_name('type_zero_int'), partialmethod(_TestNumericValue._test_binop_type_zero_int, op=binop)) | |
650 | setattr(cls, test_binop_name('type_zero_vint'), partialmethod(_TestNumericValue._test_binop_type_zero_vint, op=binop)) | |
651 | setattr(cls, test_binop_name('value_false'), partialmethod(_TestNumericValue._test_binop_value_false, op=binop)) | |
652 | setattr(cls, test_binop_name('value_zero_int'), partialmethod(_TestNumericValue._test_binop_value_zero_int, op=binop)) | |
653 | setattr(cls, test_binop_name('value_zero_vint'), partialmethod(_TestNumericValue._test_binop_value_zero_vint, op=binop)) | |
654 | setattr(cls, test_binop_name('lhs_addr_same_false'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_false, op=binop)) | |
655 | setattr(cls, test_binop_name('lhs_addr_same_zero_int'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_zero_int, op=binop)) | |
656 | setattr(cls, test_binop_name('lhs_addr_same_zero_vint'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_zero_vint, op=binop)) | |
657 | setattr(cls, test_binop_name('lhs_value_same_false'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_false, op=binop)) | |
658 | setattr(cls, test_binop_name('lhs_value_same_zero_int'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_zero_int, op=binop)) | |
659 | setattr(cls, test_binop_name('lhs_value_same_zero_vint'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_zero_vint, op=binop)) | |
660 | setattr(cls, test_binop_name('type_pos_float'), partialmethod(_TestNumericValue._test_binop_type_pos_float, op=binop)) | |
661 | setattr(cls, test_binop_name('type_neg_float'), partialmethod(_TestNumericValue._test_binop_type_neg_float, op=binop)) | |
662 | setattr(cls, test_binop_name('type_pos_vfloat'), partialmethod(_TestNumericValue._test_binop_type_pos_vfloat, op=binop)) | |
663 | setattr(cls, test_binop_name('type_neg_vfloat'), partialmethod(_TestNumericValue._test_binop_type_neg_vfloat, op=binop)) | |
664 | setattr(cls, test_binop_name('value_pos_float'), partialmethod(_TestNumericValue._test_binop_value_pos_float, op=binop)) | |
665 | setattr(cls, test_binop_name('value_neg_float'), partialmethod(_TestNumericValue._test_binop_value_neg_float, op=binop)) | |
666 | setattr(cls, test_binop_name('value_pos_vfloat'), partialmethod(_TestNumericValue._test_binop_value_pos_vfloat, op=binop)) | |
667 | setattr(cls, test_binop_name('value_neg_vfloat'), partialmethod(_TestNumericValue._test_binop_value_neg_vfloat, op=binop)) | |
668 | setattr(cls, test_binop_name('lhs_addr_same_pos_float'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_pos_float, op=binop)) | |
669 | setattr(cls, test_binop_name('lhs_addr_same_neg_float'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_neg_float, op=binop)) | |
670 | setattr(cls, test_binop_name('lhs_addr_same_pos_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_pos_vfloat, op=binop)) | |
671 | setattr(cls, test_binop_name('lhs_addr_same_neg_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_neg_vfloat, op=binop)) | |
672 | setattr(cls, test_binop_name('lhs_value_same_pos_float'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_pos_float, op=binop)) | |
673 | setattr(cls, test_binop_name('lhs_value_same_neg_float'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_neg_float, op=binop)) | |
674 | setattr(cls, test_binop_name('lhs_value_same_pos_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_pos_vfloat, op=binop)) | |
675 | setattr(cls, test_binop_name('lhs_value_same_neg_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_neg_vfloat, op=binop)) | |
676 | setattr(cls, test_binop_name('type_zero_float'), partialmethod(_TestNumericValue._test_binop_type_zero_float, op=binop)) | |
677 | setattr(cls, test_binop_name('type_zero_vfloat'), partialmethod(_TestNumericValue._test_binop_type_zero_vfloat, op=binop)) | |
678 | setattr(cls, test_binop_name('value_zero_float'), partialmethod(_TestNumericValue._test_binop_value_zero_float, op=binop)) | |
679 | setattr(cls, test_binop_name('value_zero_vfloat'), partialmethod(_TestNumericValue._test_binop_value_zero_vfloat, op=binop)) | |
680 | setattr(cls, test_binop_name('lhs_addr_same_zero_float'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_zero_float, op=binop)) | |
681 | setattr(cls, test_binop_name('lhs_addr_same_zero_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_addr_same_zero_vfloat, op=binop)) | |
682 | setattr(cls, test_binop_name('lhs_value_same_zero_float'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_zero_float, op=binop)) | |
683 | setattr(cls, test_binop_name('lhs_value_same_zero_vfloat'), partialmethod(_TestNumericValue._test_binop_lhs_value_same_zero_vfloat, op=binop)) | |
684 | ||
685 | # inject testing methods for each unary operation | |
686 | for name, unaryop in _UNARYOPS: | |
687 | setattr(cls, test_unaryop_name('type'), partialmethod(_TestNumericValue._test_unaryop_type, op=unaryop)) | |
688 | setattr(cls, test_unaryop_name('value'), partialmethod(_TestNumericValue._test_unaryop_value, op=unaryop)) | |
689 | setattr(cls, test_unaryop_name('addr_same'), partialmethod(_TestNumericValue._test_unaryop_addr_same, op=unaryop)) | |
690 | setattr(cls, test_unaryop_name('value_same'), partialmethod(_TestNumericValue._test_unaryop_value_same, op=unaryop)) | |
691 | ||
692 | # inject testing methods for each inplace binary operation | |
693 | for name, ibinop in _IBINOPS: | |
694 | setattr(cls, test_ibinop_name('invalid_unknown'), partialmethod(_TestNumericValue._test_ibinop_invalid_unknown, op=ibinop)) | |
695 | setattr(cls, test_ibinop_name('invalid_none'), partialmethod(_TestNumericValue._test_ibinop_invalid_none, op=ibinop)) | |
696 | setattr(cls, test_ibinop_name('type_true'), partialmethod(_TestNumericValue._test_ibinop_type_true, op=ibinop)) | |
697 | setattr(cls, test_ibinop_name('value_true'), partialmethod(_TestNumericValue._test_ibinop_value_true, op=ibinop)) | |
698 | setattr(cls, test_ibinop_name('type_pos_int'), partialmethod(_TestNumericValue._test_ibinop_type_pos_int, op=ibinop)) | |
699 | setattr(cls, test_ibinop_name('type_pos_vint'), partialmethod(_TestNumericValue._test_ibinop_type_pos_vint, op=ibinop)) | |
700 | setattr(cls, test_ibinop_name('value_pos_int'), partialmethod(_TestNumericValue._test_ibinop_value_pos_int, op=ibinop)) | |
701 | setattr(cls, test_ibinop_name('value_pos_vint'), partialmethod(_TestNumericValue._test_ibinop_value_pos_vint, op=ibinop)) | |
702 | setattr(cls, test_ibinop_name('type_neg_int'), partialmethod(_TestNumericValue._test_ibinop_type_neg_int, op=ibinop)) | |
703 | setattr(cls, test_ibinop_name('type_neg_vint'), partialmethod(_TestNumericValue._test_ibinop_type_neg_vint, op=ibinop)) | |
704 | setattr(cls, test_ibinop_name('value_neg_int'), partialmethod(_TestNumericValue._test_ibinop_value_neg_int, op=ibinop)) | |
705 | setattr(cls, test_ibinop_name('value_neg_vint'), partialmethod(_TestNumericValue._test_ibinop_value_neg_vint, op=ibinop)) | |
706 | setattr(cls, test_ibinop_name('type_false'), partialmethod(_TestNumericValue._test_ibinop_type_false, op=ibinop)) | |
707 | setattr(cls, test_ibinop_name('value_false'), partialmethod(_TestNumericValue._test_ibinop_value_false, op=ibinop)) | |
708 | setattr(cls, test_ibinop_name('type_zero_int'), partialmethod(_TestNumericValue._test_ibinop_type_zero_int, op=ibinop)) | |
709 | setattr(cls, test_ibinop_name('type_zero_vint'), partialmethod(_TestNumericValue._test_ibinop_type_zero_vint, op=ibinop)) | |
710 | setattr(cls, test_ibinop_name('value_zero_int'), partialmethod(_TestNumericValue._test_ibinop_value_zero_int, op=ibinop)) | |
711 | setattr(cls, test_ibinop_name('value_zero_vint'), partialmethod(_TestNumericValue._test_ibinop_value_zero_vint, op=ibinop)) | |
712 | setattr(cls, test_ibinop_name('type_pos_float'), partialmethod(_TestNumericValue._test_ibinop_type_pos_float, op=ibinop)) | |
713 | setattr(cls, test_ibinop_name('type_neg_float'), partialmethod(_TestNumericValue._test_ibinop_type_neg_float, op=ibinop)) | |
714 | setattr(cls, test_ibinop_name('type_pos_vfloat'), partialmethod(_TestNumericValue._test_ibinop_type_pos_vfloat, op=ibinop)) | |
715 | setattr(cls, test_ibinop_name('type_neg_vfloat'), partialmethod(_TestNumericValue._test_ibinop_type_neg_vfloat, op=ibinop)) | |
716 | setattr(cls, test_ibinop_name('value_pos_float'), partialmethod(_TestNumericValue._test_ibinop_value_pos_float, op=ibinop)) | |
717 | setattr(cls, test_ibinop_name('value_neg_float'), partialmethod(_TestNumericValue._test_ibinop_value_neg_float, op=ibinop)) | |
718 | setattr(cls, test_ibinop_name('value_pos_vfloat'), partialmethod(_TestNumericValue._test_ibinop_value_pos_vfloat, op=ibinop)) | |
719 | setattr(cls, test_ibinop_name('value_neg_vfloat'), partialmethod(_TestNumericValue._test_ibinop_value_neg_vfloat, op=ibinop)) | |
720 | setattr(cls, test_ibinop_name('type_zero_float'), partialmethod(_TestNumericValue._test_ibinop_type_zero_float, op=ibinop)) | |
721 | setattr(cls, test_ibinop_name('type_zero_vfloat'), partialmethod(_TestNumericValue._test_ibinop_type_zero_vfloat, op=ibinop)) | |
722 | setattr(cls, test_ibinop_name('value_zero_float'), partialmethod(_TestNumericValue._test_ibinop_value_zero_float, op=ibinop)) | |
723 | setattr(cls, test_ibinop_name('value_zero_vfloat'), partialmethod(_TestNumericValue._test_ibinop_value_zero_vfloat, op=ibinop)) | |
724 | ||
725 | ||
726 | class CreateValueFuncTestCase(unittest.TestCase): | |
727 | def test_create_none(self): | |
728 | v = bt2.create_value(None) | |
729 | self.assertIsNone(v) | |
730 | ||
731 | def test_create_bool_false(self): | |
732 | v = bt2.create_value(False) | |
733 | self.assertIsInstance(v, bt2.BoolValue) | |
734 | self.assertFalse(v) | |
735 | ||
736 | def test_create_bool_true(self): | |
737 | v = bt2.create_value(True) | |
738 | self.assertIsInstance(v, bt2.BoolValue) | |
739 | self.assertTrue(v) | |
740 | ||
741 | def test_create_int_pos(self): | |
742 | raw = 23 | |
743 | v = bt2.create_value(raw) | |
744 | self.assertIsInstance(v, bt2.IntegerValue) | |
745 | self.assertEqual(v, raw) | |
746 | ||
747 | def test_create_int_neg(self): | |
748 | raw = -23 | |
749 | v = bt2.create_value(raw) | |
750 | self.assertIsInstance(v, bt2.IntegerValue) | |
751 | self.assertEqual(v, raw) | |
752 | ||
753 | def test_create_float_pos(self): | |
754 | raw = 17.5 | |
755 | v = bt2.create_value(raw) | |
756 | self.assertIsInstance(v, bt2.FloatValue) | |
757 | self.assertEqual(v, raw) | |
758 | ||
759 | def test_create_float_neg(self): | |
760 | raw = -17.5 | |
761 | v = bt2.create_value(raw) | |
762 | self.assertIsInstance(v, bt2.FloatValue) | |
763 | self.assertEqual(v, raw) | |
764 | ||
765 | def test_create_string(self): | |
766 | raw = 'salut' | |
767 | v = bt2.create_value(raw) | |
768 | self.assertIsInstance(v, bt2.StringValue) | |
769 | self.assertEqual(v, raw) | |
770 | ||
771 | def test_create_string_empty(self): | |
772 | raw = '' | |
773 | v = bt2.create_value(raw) | |
774 | self.assertIsInstance(v, bt2.StringValue) | |
775 | self.assertEqual(v, raw) | |
776 | ||
777 | def test_create_array_from_list(self): | |
778 | raw = [1, 2, 3] | |
779 | v = bt2.create_value(raw) | |
780 | self.assertIsInstance(v, bt2.ArrayValue) | |
781 | self.assertEqual(v, raw) | |
782 | ||
783 | def test_create_array_from_tuple(self): | |
784 | raw = 4, 5, 6 | |
785 | v = bt2.create_value(raw) | |
786 | self.assertIsInstance(v, bt2.ArrayValue) | |
787 | self.assertEqual(v, raw) | |
788 | ||
789 | def test_create_array_from_empty_list(self): | |
790 | raw = [] | |
791 | v = bt2.create_value(raw) | |
792 | self.assertIsInstance(v, bt2.ArrayValue) | |
793 | self.assertEqual(v, raw) | |
794 | ||
795 | def test_create_array_from_empty_tuple(self): | |
796 | raw = () | |
797 | v = bt2.create_value(raw) | |
798 | self.assertIsInstance(v, bt2.ArrayValue) | |
799 | self.assertEqual(v, raw) | |
800 | ||
801 | def test_create_map(self): | |
802 | raw = {'salut': 23} | |
803 | v = bt2.create_value(raw) | |
804 | self.assertIsInstance(v, bt2.MapValue) | |
805 | self.assertEqual(v, raw) | |
806 | ||
807 | def test_create_map_empty(self): | |
808 | raw = {} | |
809 | v = bt2.create_value(raw) | |
810 | self.assertIsInstance(v, bt2.MapValue) | |
811 | self.assertEqual(v, raw) | |
812 | ||
813 | def test_create_vfalse(self): | |
814 | v = bt2.create_value(bt2.create_value(False)) | |
815 | self.assertIsInstance(v, bt2.BoolValue) | |
816 | self.assertFalse(v) | |
817 | ||
818 | def test_create_invalid(self): | |
819 | class A: | |
820 | pass | |
821 | ||
822 | a = A() | |
823 | ||
824 | with self.assertRaisesRegex(TypeError, "cannot create value object from 'A' object") as cm: | |
825 | v = bt2.create_value(a) | |
826 | ||
827 | ||
828 | class BoolValueTestCase(_TestFrozenSimple, _TestCopySimple, unittest.TestCase): | |
829 | def setUp(self): | |
830 | self._f = bt2.BoolValue(False) | |
831 | self._t = bt2.BoolValue(True) | |
832 | self._def = self._f | |
833 | self._def_value = False | |
834 | self._def_new_value = True | |
835 | ||
836 | def _assert_expecting_bool(self): | |
837 | return self.assertRaisesRegex(TypeError, r"expecting a 'bool' object") | |
838 | ||
839 | def test_create_default(self): | |
840 | b = bt2.BoolValue() | |
841 | self.assertFalse(b) | |
842 | ||
843 | def test_create_false(self): | |
844 | self.assertFalse(self._f.value) | |
845 | self.assertFalse(self._f) | |
846 | ||
847 | def test_create_true(self): | |
848 | self.assertTrue(self._t.value) | |
849 | self.assertTrue(self._t) | |
850 | ||
851 | def test_create_from_vfalse(self): | |
852 | b = bt2.BoolValue(self._f) | |
853 | self.assertFalse(b.value) | |
854 | self.assertFalse(b) | |
855 | ||
856 | def test_create_from_vtrue(self): | |
857 | b = bt2.BoolValue(self._t) | |
858 | self.assertTrue(b.value) | |
859 | self.assertTrue(b) | |
860 | ||
861 | def test_create_from_int_non_zero(self): | |
862 | with self.assertRaises(TypeError): | |
863 | b = bt2.BoolValue(23) | |
864 | ||
865 | def test_create_from_int_zero(self): | |
866 | with self.assertRaises(TypeError): | |
867 | b = bt2.BoolValue(0) | |
868 | ||
869 | def test_assign_true(self): | |
870 | b = bt2.BoolValue() | |
871 | b.value = True | |
872 | self.assertTrue(b) | |
873 | ||
874 | def test_assign_false(self): | |
875 | b = bt2.BoolValue() | |
876 | b.value = False | |
877 | self.assertFalse(b) | |
878 | ||
879 | def test_assign_vtrue(self): | |
880 | b = bt2.BoolValue() | |
881 | b.value = self._t | |
882 | self.assertTrue(b) | |
883 | ||
884 | def test_assign_vfalse(self): | |
885 | b = bt2.BoolValue() | |
886 | b.value = False | |
887 | self.assertFalse(b) | |
888 | ||
889 | def test_assign_int(self): | |
890 | with self.assertRaises(TypeError): | |
891 | b = bt2.BoolValue() | |
892 | b.value = 23 | |
893 | ||
894 | def test_bool_op(self): | |
895 | self.assertEqual(bool(self._def), bool(self._def_value)) | |
896 | ||
897 | def test_str_op(self): | |
898 | self.assertEqual(str(self._def), str(self._def_value)) | |
899 | ||
900 | def test_eq_none(self): | |
901 | self.assertFalse(self._def == None) | |
902 | ||
903 | def test_ne_none(self): | |
904 | self.assertTrue(self._def != None) | |
905 | ||
906 | def test_vfalse_eq_false(self): | |
907 | self.assertEqual(self._f, False) | |
908 | ||
909 | def test_vfalse_ne_true(self): | |
910 | self.assertNotEqual(self._f, True) | |
911 | ||
912 | def test_vtrue_eq_true(self): | |
913 | self.assertEqual(self._t, True) | |
914 | ||
915 | def test_vtrue_ne_false(self): | |
916 | self.assertNotEqual(self._t, False) | |
917 | ||
918 | ||
919 | class IntegerValueTestCase(_TestNumericValue, unittest.TestCase): | |
920 | def setUp(self): | |
921 | self._pv = 23 | |
922 | self._nv = -52 | |
923 | self._ip = bt2.IntegerValue(self._pv) | |
924 | self._in = bt2.IntegerValue(self._nv) | |
925 | self._def = self._ip | |
926 | self._def_value = self._pv | |
927 | self._def_new_value = -101 | |
928 | ||
929 | def _assert_expecting_int(self): | |
930 | return self.assertRaisesRegex(TypeError, r'expecting a number object') | |
931 | ||
932 | def _assert_expecting_int64(self): | |
933 | return self.assertRaisesRegex(ValueError, r"expecting a signed 64-bit integral value") | |
934 | ||
935 | def _assert_expecting_uint64(self): | |
936 | return self.assertRaisesRegex(ValueError, r"expecting an unsigned 64-bit integral value") | |
937 | ||
938 | def test_create_default(self): | |
939 | i = bt2.IntegerValue() | |
940 | self.assertEqual(i.value, 0) | |
941 | ||
942 | def test_create_pos(self): | |
943 | self.assertEqual(self._ip.value, self._pv) | |
944 | self.assertEqual(self._ip, self._pv) | |
945 | ||
946 | def test_create_neg(self): | |
947 | self.assertEqual(self._in.value, self._nv) | |
948 | self.assertEqual(self._in, self._nv) | |
949 | ||
950 | def test_create_pos_too_big(self): | |
951 | with self._assert_expecting_int64(): | |
952 | i = bt2.IntegerValue(2 ** 63) | |
953 | ||
954 | def test_create_neg_too_big(self): | |
955 | with self._assert_expecting_int64(): | |
956 | i = bt2.IntegerValue(-(2 ** 63) - 1) | |
957 | ||
958 | def test_create_from_vint(self): | |
959 | i = bt2.IntegerValue(self._ip) | |
960 | self.assertEqual(i, self._pv) | |
961 | ||
962 | def test_create_from_false(self): | |
963 | i = bt2.IntegerValue(False) | |
964 | self.assertFalse(i.value) | |
965 | self.assertFalse(i) | |
966 | ||
967 | def test_create_from_true(self): | |
968 | i = bt2.IntegerValue(True) | |
969 | self.assertTrue(i.value) | |
970 | self.assertTrue(i) | |
971 | ||
972 | def test_create_from_float(self): | |
973 | i = bt2.IntegerValue(99.6) | |
974 | self.assertEqual(i, 99) | |
975 | ||
976 | def test_create_from_vfloat(self): | |
977 | f = bt2.create_value(17.5) | |
978 | i = bt2.IntegerValue(f) | |
979 | self.assertEqual(i, 17) | |
980 | ||
981 | def test_create_from_unknown(self): | |
982 | class A: | |
983 | pass | |
984 | ||
985 | with self._assert_expecting_int(): | |
986 | i = bt2.IntegerValue(A()) | |
987 | ||
988 | def test_create_from_varray(self): | |
989 | with self._assert_expecting_int(): | |
990 | i = bt2.IntegerValue(bt2.ArrayValue()) | |
991 | ||
992 | def test_assign_true(self): | |
993 | raw = True | |
994 | self._def.value = raw | |
995 | self.assertEqual(self._def, raw) | |
996 | self.assertEqual(self._def.value, raw) | |
997 | ||
998 | def test_assign_false(self): | |
999 | raw = False | |
1000 | self._def.value = raw | |
1001 | self.assertEqual(self._def, raw) | |
1002 | self.assertEqual(self._def.value, raw) | |
1003 | ||
1004 | def test_assign_pos_int(self): | |
1005 | raw = 477 | |
1006 | self._def.value = raw | |
1007 | self.assertEqual(self._def, raw) | |
1008 | self.assertEqual(self._def.value, raw) | |
1009 | ||
1010 | def test_assign_neg_int(self): | |
1011 | raw = -13 | |
1012 | self._def.value = raw | |
1013 | self.assertEqual(self._def, raw) | |
1014 | self.assertEqual(self._def.value, raw) | |
1015 | ||
1016 | def test_assign_vint(self): | |
1017 | raw = 999 | |
1018 | self._def.value = bt2.create_value(raw) | |
1019 | self.assertEqual(self._def, raw) | |
1020 | self.assertEqual(self._def.value, raw) | |
1021 | ||
1022 | def test_assign_vfloat(self): | |
1023 | raw = 123.456 | |
1024 | self._def.value = bt2.create_value(raw) | |
1025 | self.assertEqual(self._def, int(raw)) | |
1026 | self.assertEqual(self._def.value, int(raw)) | |
1027 | ||
1028 | ||
1029 | _inject_numeric_testing_methods(IntegerValueTestCase) | |
1030 | ||
1031 | ||
1032 | class FloatValueTestCase(_TestNumericValue, unittest.TestCase): | |
1033 | def setUp(self): | |
1034 | self._pv = 23.4 | |
1035 | self._nv = -52.7 | |
1036 | self._fp = bt2.FloatValue(self._pv) | |
1037 | self._fn = bt2.FloatValue(self._nv) | |
1038 | self._def = self._fp | |
1039 | self._def_value = self._pv | |
1040 | self._def_new_value = -101.88 | |
1041 | ||
1042 | def _assert_expecting_float(self): | |
1043 | return self.assertRaisesRegex(TypeError, r"expecting a real number object") | |
1044 | ||
1045 | def _test_invalid_op(self, cb): | |
1046 | with self.assertRaises(TypeError): | |
1047 | cb() | |
1048 | ||
1049 | def test_create_default(self): | |
1050 | f = bt2.FloatValue() | |
1051 | self.assertEqual(f.value, 0.0) | |
1052 | ||
1053 | def test_create_pos(self): | |
1054 | self.assertEqual(self._fp.value, self._pv) | |
1055 | self.assertEqual(self._fp, self._pv) | |
1056 | ||
1057 | def test_create_neg(self): | |
1058 | self.assertEqual(self._fn.value, self._nv) | |
1059 | self.assertEqual(self._fn, self._nv) | |
1060 | ||
1061 | def test_create_from_vint(self): | |
1062 | f = bt2.FloatValue(self._fp) | |
1063 | self.assertEqual(f, self._pv) | |
1064 | ||
1065 | def test_create_from_false(self): | |
1066 | f = bt2.FloatValue(False) | |
1067 | self.assertFalse(f.value) | |
1068 | self.assertFalse(f) | |
1069 | ||
1070 | def test_create_from_true(self): | |
1071 | f = bt2.FloatValue(True) | |
1072 | self.assertTrue(f.value) | |
1073 | self.assertTrue(f) | |
1074 | ||
1075 | def test_create_from_int(self): | |
1076 | raw = 17 | |
1077 | f = bt2.FloatValue(raw) | |
1078 | self.assertEqual(f.value, float(raw)) | |
1079 | ||
1080 | def test_create_from_vint(self): | |
1081 | raw = 17 | |
1082 | f = bt2.FloatValue(bt2.create_value(raw)) | |
1083 | self.assertEqual(f.value, float(raw)) | |
1084 | ||
1085 | def test_create_from_vfloat(self): | |
1086 | raw = 17.17 | |
1087 | f = bt2.FloatValue(bt2.create_value(raw)) | |
1088 | self.assertEqual(f.value, raw) | |
1089 | ||
1090 | def test_create_from_unknown(self): | |
1091 | class A: | |
1092 | pass | |
1093 | ||
1094 | with self._assert_expecting_float(): | |
1095 | f = bt2.FloatValue(A()) | |
1096 | ||
1097 | def test_create_from_varray(self): | |
1098 | with self._assert_expecting_float(): | |
1099 | f = bt2.FloatValue(bt2.ArrayValue()) | |
1100 | ||
1101 | def test_assign_true(self): | |
1102 | self._def.value = True | |
1103 | self.assertTrue(self._def) | |
1104 | self.assertTrue(self._def.value) | |
1105 | ||
1106 | def test_assign_false(self): | |
1107 | self._def.value = False | |
1108 | self.assertFalse(self._def) | |
1109 | self.assertFalse(self._def.value) | |
1110 | ||
1111 | def test_assign_pos_int(self): | |
1112 | raw = 477 | |
1113 | self._def.value = raw | |
1114 | self.assertEqual(self._def, float(raw)) | |
1115 | self.assertEqual(self._def.value, float(raw)) | |
1116 | ||
1117 | def test_assign_neg_int(self): | |
1118 | raw = -13 | |
1119 | self._def.value = raw | |
1120 | self.assertEqual(self._def, float(raw)) | |
1121 | self.assertEqual(self._def.value, float(raw)) | |
1122 | ||
1123 | def test_assign_vint(self): | |
1124 | raw = 999 | |
1125 | self._def.value = bt2.create_value(raw) | |
1126 | self.assertEqual(self._def, float(raw)) | |
1127 | self.assertEqual(self._def.value, float(raw)) | |
1128 | ||
1129 | def test_assign_float(self): | |
1130 | raw = -19.23 | |
1131 | self._def.value = raw | |
1132 | self.assertEqual(self._def, raw) | |
1133 | self.assertEqual(self._def.value, raw) | |
1134 | ||
1135 | def test_assign_vfloat(self): | |
1136 | raw = 101.32 | |
1137 | self._def.value = bt2.create_value(raw) | |
1138 | self.assertEqual(self._def, raw) | |
1139 | self.assertEqual(self._def.value, raw) | |
1140 | ||
1141 | def test_invalid_lshift(self): | |
1142 | self._test_invalid_op(lambda: self._def << 23) | |
1143 | ||
1144 | def test_invalid_rshift(self): | |
1145 | self._test_invalid_op(lambda: self._def >> 23) | |
1146 | ||
1147 | def test_invalid_and(self): | |
1148 | self._test_invalid_op(lambda: self._def & 23) | |
1149 | ||
1150 | def test_invalid_or(self): | |
1151 | self._test_invalid_op(lambda: self._def | 23) | |
1152 | ||
1153 | def test_invalid_xor(self): | |
1154 | self._test_invalid_op(lambda: self._def ^ 23) | |
1155 | ||
1156 | def test_invalid_invert(self): | |
1157 | self._test_invalid_op(lambda: ~self._def) | |
1158 | ||
1159 | ||
1160 | _inject_numeric_testing_methods(FloatValueTestCase) | |
1161 | ||
1162 | ||
1163 | class StringValueTestCase(_TestCopySimple, _TestFrozenSimple, unittest.TestCase): | |
1164 | def setUp(self): | |
1165 | self._def_value = 'Hello, World!' | |
1166 | self._def = bt2.StringValue(self._def_value) | |
1167 | self._def_new_value = 'Yes!' | |
1168 | ||
1169 | def _assert_expecting_str(self): | |
1170 | return self.assertRaises(TypeError) | |
1171 | ||
1172 | def test_create_default(self): | |
1173 | s = bt2.StringValue() | |
1174 | self.assertEqual(s.value, '') | |
1175 | ||
1176 | def test_create_from_str(self): | |
1177 | raw = 'liberté' | |
1178 | s = bt2.StringValue(raw) | |
1179 | self.assertEqual(s.value, raw) | |
1180 | ||
1181 | def test_create_from_vstr(self): | |
1182 | raw = 'liberté' | |
1183 | s = bt2.StringValue(bt2.create_value(raw)) | |
1184 | self.assertEqual(s.value, raw) | |
1185 | ||
1186 | def test_create_from_unknown(self): | |
1187 | class A: | |
1188 | pass | |
1189 | ||
1190 | with self._assert_expecting_str(): | |
1191 | i = bt2.StringValue(A()) | |
1192 | ||
1193 | def test_create_from_varray(self): | |
1194 | with self._assert_expecting_str(): | |
1195 | i = bt2.StringValue(bt2.ArrayValue()) | |
1196 | ||
1197 | def test_assign_int(self): | |
1198 | with self._assert_expecting_str(): | |
1199 | self._def.value = 283 | |
1200 | ||
1201 | def test_assign_str(self): | |
1202 | raw = 'zorg' | |
1203 | self._def = raw | |
1204 | self.assertEqual(self._def, raw) | |
1205 | ||
1206 | def test_assign_vstr(self): | |
1207 | raw = 'zorg' | |
1208 | self._def = bt2.create_value(raw) | |
1209 | self.assertEqual(self._def, raw) | |
1210 | ||
1211 | def test_eq(self): | |
1212 | self.assertEqual(self._def, self._def_value) | |
1213 | ||
1214 | def test_eq(self): | |
1215 | self.assertNotEqual(self._def, 23) | |
1216 | ||
1217 | def test_lt_vstring(self): | |
1218 | s1 = bt2.StringValue('allo') | |
1219 | s2 = bt2.StringValue('bateau') | |
1220 | self.assertLess(s1, s2) | |
1221 | ||
1222 | def test_lt_string(self): | |
1223 | s1 = bt2.StringValue('allo') | |
1224 | self.assertLess(s1, 'bateau') | |
1225 | ||
1226 | def test_le_vstring(self): | |
1227 | s1 = bt2.StringValue('allo') | |
1228 | s2 = bt2.StringValue('bateau') | |
1229 | self.assertLessEqual(s1, s2) | |
1230 | ||
1231 | def test_le_string(self): | |
1232 | s1 = bt2.StringValue('allo') | |
1233 | self.assertLessEqual(s1, 'bateau') | |
1234 | ||
1235 | def test_gt_vstring(self): | |
1236 | s1 = bt2.StringValue('allo') | |
1237 | s2 = bt2.StringValue('bateau') | |
1238 | self.assertGreater(s2, s1) | |
1239 | ||
1240 | def test_gt_string(self): | |
1241 | s1 = bt2.StringValue('allo') | |
1242 | self.assertGreater('bateau', s1) | |
1243 | ||
1244 | def test_ge_vstring(self): | |
1245 | s1 = bt2.StringValue('allo') | |
1246 | s2 = bt2.StringValue('bateau') | |
1247 | self.assertGreaterEqual(s2, s1) | |
1248 | ||
1249 | def test_ge_string(self): | |
1250 | s1 = bt2.StringValue('allo') | |
1251 | self.assertGreaterEqual('bateau', s1) | |
1252 | ||
1253 | def test_bool_op(self): | |
1254 | self.assertEqual(bool(self._def), bool(self._def_value)) | |
1255 | ||
1256 | def test_str_op(self): | |
1257 | self.assertEqual(str(self._def), str(self._def_value)) | |
1258 | ||
1259 | def test_len(self): | |
1260 | self.assertEqual(len(self._def), len(self._def_value)) | |
1261 | ||
1262 | def test_getitem(self): | |
1263 | self.assertEqual(self._def[5], self._def_value[5]) | |
1264 | ||
1265 | def test_append_str(self): | |
1266 | to_append = 'meow meow meow' | |
1267 | self._def += to_append | |
1268 | self._def_value += to_append | |
1269 | self.assertEqual(self._def, self._def_value) | |
1270 | ||
1271 | def test_append_vstr(self): | |
1272 | to_append = 'meow meow meow' | |
1273 | self._def += bt2.create_value(to_append) | |
1274 | self._def_value += to_append | |
1275 | self.assertEqual(self._def, self._def_value) | |
1276 | ||
1277 | ||
1278 | class ArrayValueTestCase(_TestFrozen, unittest.TestCase): | |
1279 | def setUp(self): | |
1280 | self._def_value = [None, False, True, -23, 0, 42, -42.4, 23.17, 'yes'] | |
1281 | self._def = bt2.ArrayValue(copy.deepcopy(self._def_value)) | |
1282 | ||
1283 | def _modify_def(self): | |
1284 | self._def[2] = 'xyz' | |
1285 | ||
1286 | def _assert_type_error(self): | |
1287 | return self.assertRaises(TypeError) | |
1288 | ||
1289 | def test_create_default(self): | |
1290 | a = bt2.ArrayValue() | |
1291 | self.assertEqual(len(a), 0) | |
1292 | ||
1293 | def test_create_from_array(self): | |
1294 | self.assertEqual(self._def, self._def_value) | |
1295 | ||
1296 | def test_create_from_tuple(self): | |
1297 | t = 1, 2, False, None | |
1298 | a = bt2.ArrayValue(t) | |
1299 | self.assertEqual(a, t) | |
1300 | ||
1301 | def test_create_from_varray(self): | |
1302 | va = bt2.ArrayValue(copy.deepcopy(self._def_value)) | |
1303 | a = bt2.ArrayValue(va) | |
1304 | self.assertEqual(va, a) | |
1305 | ||
1306 | def test_create_from_unknown(self): | |
1307 | class A: | |
1308 | pass | |
1309 | ||
1310 | with self._assert_type_error(): | |
1311 | a = bt2.ArrayValue(A()) | |
1312 | ||
1313 | def test_bool_op_true(self): | |
1314 | self.assertTrue(bool(self._def)) | |
1315 | ||
1316 | def test_bool_op_false(self): | |
1317 | self.assertFalse(bool(bt2.ArrayValue())) | |
1318 | ||
1319 | def test_len(self): | |
1320 | self.assertEqual(len(self._def), len(self._def_value)) | |
1321 | ||
1322 | def test_copy(self): | |
1323 | to_copy = (1, 2, 'hello', (4, 5.2)) | |
1324 | a = bt2.ArrayValue(to_copy) | |
1325 | cpy = copy.copy(a) | |
1326 | self.assertEqual(a, cpy) | |
1327 | self.assertNotEqual(a.addr, cpy.addr) | |
1328 | self.assertEqual(a[3].addr, cpy[3].addr) | |
1329 | ||
1330 | def test_deepcopy(self): | |
1331 | to_copy = (1, 2, 'hello', (4, 5.2)) | |
1332 | a = bt2.ArrayValue(to_copy) | |
1333 | cpy = copy.deepcopy(a) | |
1334 | self.assertEqual(a, cpy) | |
1335 | self.assertNotEqual(a.addr, cpy.addr) | |
1336 | self.assertNotEqual(a[3].addr, cpy[3].addr) | |
1337 | ||
1338 | def test_eq_int(self): | |
1339 | self.assertNotEqual(self._def, 23) | |
1340 | ||
1341 | def test_eq_diff_len(self): | |
1342 | a1 = bt2.create_value([1, 2, 3]) | |
1343 | a2 = bt2.create_value([1, 2]) | |
1344 | self.assertNotEqual(a1, a2) | |
1345 | ||
1346 | def test_eq_diff_content_same_len(self): | |
1347 | a1 = bt2.create_value([1, 2, 3]) | |
1348 | a2 = bt2.create_value([4, 5, 6]) | |
1349 | self.assertNotEqual(a1, a2) | |
1350 | ||
1351 | def test_eq_same_content_same_len(self): | |
1352 | raw = (3, True, [1, 2.5, None, {'a': 17.6, 'b': None}]) | |
1353 | a1 = bt2.ArrayValue(raw) | |
1354 | a2 = bt2.ArrayValue(copy.deepcopy(raw)) | |
1355 | self.assertEqual(a1, a2) | |
1356 | ||
1357 | def test_setitem_int(self): | |
1358 | raw = 19 | |
1359 | self._def[2] = raw | |
1360 | self.assertEqual(self._def[2], raw) | |
1361 | ||
1362 | def test_setitem_vint(self): | |
1363 | raw = 19 | |
1364 | self._def[2] = bt2.create_value(raw) | |
1365 | self.assertEqual(self._def[2], raw) | |
1366 | ||
1367 | def test_setitem_none(self): | |
1368 | self._def[2] = None | |
1369 | self.assertIsNone(self._def[2]) | |
1370 | ||
1371 | def test_setitem_index_wrong_type(self): | |
1372 | with self._assert_type_error(): | |
1373 | self._def['yes'] = 23 | |
1374 | ||
1375 | def test_setitem_index_neg(self): | |
1376 | with self.assertRaises(IndexError): | |
1377 | self._def[-2] = 23 | |
1378 | ||
1379 | def test_setitem_index_out_of_range(self): | |
1380 | with self.assertRaises(IndexError): | |
1381 | self._def[len(self._def)] = 23 | |
1382 | ||
1383 | def test_append_none(self): | |
1384 | self._def.append(None) | |
1385 | self.assertIsNone(self._def[len(self._def) - 1]) | |
1386 | ||
1387 | def test_append_int(self): | |
1388 | raw = 145 | |
1389 | self._def.append(raw) | |
1390 | self.assertEqual(self._def[len(self._def) - 1], raw) | |
1391 | ||
1392 | def test_append_vint(self): | |
1393 | raw = 145 | |
1394 | self._def.append(bt2.create_value(raw)) | |
1395 | self.assertEqual(self._def[len(self._def) - 1], raw) | |
1396 | ||
1397 | def test_append_unknown(self): | |
1398 | class A: | |
1399 | pass | |
1400 | ||
1401 | with self._assert_type_error(): | |
1402 | self._def.append(A()) | |
1403 | ||
1404 | def test_iadd(self): | |
1405 | raw = 4, 5, True | |
1406 | self._def += raw | |
1407 | self.assertEqual(self._def[len(self._def) - 3], raw[0]) | |
1408 | self.assertEqual(self._def[len(self._def) - 2], raw[1]) | |
1409 | self.assertEqual(self._def[len(self._def) - 1], raw[2]) | |
1410 | ||
1411 | def test_iadd_unknown(self): | |
1412 | class A: | |
1413 | pass | |
1414 | ||
1415 | with self._assert_type_error(): | |
1416 | self._def += A() | |
1417 | ||
1418 | def test_iadd_list_unknown(self): | |
1419 | class A: | |
1420 | pass | |
1421 | ||
1422 | with self._assert_type_error(): | |
1423 | self._def += [A()] | |
1424 | ||
1425 | def test_iter(self): | |
1426 | for velem, elem in zip(self._def, self._def_value): | |
1427 | self.assertEqual(velem, elem) | |
1428 | ||
1429 | ||
1430 | class MapValueTestCase(_TestFrozen, unittest.TestCase): | |
1431 | def setUp(self): | |
1432 | self._def_value = { | |
1433 | 'none': None, | |
1434 | 'false': False, | |
1435 | 'true': True, | |
1436 | 'neg-int': -23, | |
1437 | 'zero': 0, | |
1438 | 'pos-int': 42, | |
1439 | 'neg-float': -42.4, | |
1440 | 'pos-float': 23.17, | |
1441 | 'str': 'yes' | |
1442 | } | |
1443 | self._def = bt2.MapValue(copy.deepcopy(self._def_value)) | |
1444 | ||
1445 | def _modify_def(self): | |
1446 | self._def['zero'] = 1 | |
1447 | ||
1448 | def test_create_default(self): | |
1449 | m = bt2.MapValue() | |
1450 | self.assertEqual(len(m), 0) | |
1451 | ||
1452 | def test_create_from_dict(self): | |
1453 | self.assertEqual(self._def, self._def_value) | |
1454 | ||
1455 | def test_create_from_vmap(self): | |
1456 | vm = bt2.MapValue(copy.deepcopy(self._def_value)) | |
1457 | m = bt2.MapValue(vm) | |
1458 | self.assertEqual(vm, m) | |
1459 | ||
1460 | def test_create_from_unknown(self): | |
1461 | class A: | |
1462 | pass | |
1463 | ||
1464 | with self.assertRaises(AttributeError): | |
1465 | m = bt2.MapValue(A()) | |
1466 | ||
1467 | def test_bool_op_true(self): | |
1468 | self.assertTrue(bool(self._def)) | |
1469 | ||
1470 | def test_bool_op_false(self): | |
1471 | self.assertFalse(bool(bt2.MapValue())) | |
1472 | ||
1473 | def test_len(self): | |
1474 | self.assertEqual(len(self._def), len(self._def_value)) | |
1475 | ||
1476 | def test_copy(self): | |
1477 | to_copy = { | |
1478 | 'yes': 1, | |
1479 | 'no': 2, | |
1480 | 's': 'hello', | |
1481 | 'inner': (4, 5.2) | |
1482 | } | |
1483 | m = bt2.MapValue(to_copy) | |
1484 | cpy = copy.copy(m) | |
1485 | self.assertEqual(m, cpy) | |
1486 | self.assertNotEqual(m.addr, cpy.addr) | |
1487 | self.assertEqual(m['inner'].addr, cpy['inner'].addr) | |
1488 | ||
1489 | def test_deepcopy(self): | |
1490 | to_copy = { | |
1491 | 'yes': 1, | |
1492 | 'no': 2, | |
1493 | 's': 'hello', | |
1494 | 'inner': (4, 5.2) | |
1495 | } | |
1496 | m = bt2.MapValue(to_copy) | |
1497 | cpy = copy.deepcopy(m) | |
1498 | self.assertEqual(m, cpy) | |
1499 | self.assertNotEqual(m.addr, cpy.addr) | |
1500 | self.assertNotEqual(m['inner'].addr, cpy['inner'].addr) | |
1501 | ||
1502 | def test_eq_int(self): | |
1503 | self.assertNotEqual(self._def, 23) | |
1504 | ||
1505 | def test_eq_diff_len(self): | |
1506 | a1 = bt2.create_value({'a': 1, 'b': 2, 'c': 3}) | |
1507 | a2 = bt2.create_value({'a': 1, 'b': 2}) | |
1508 | self.assertNotEqual(a1, a2) | |
1509 | ||
1510 | def test_eq_diff_content_same_len(self): | |
1511 | a1 = bt2.create_value({'a': 1, 'b': 2, 'c': 3}) | |
1512 | a2 = bt2.create_value({'a': 4, 'b': 2, 'c': 3}) | |
1513 | self.assertNotEqual(a1, a2) | |
1514 | ||
1515 | def test_eq_same_content_diff_keys(self): | |
1516 | a1 = bt2.create_value({'a': 1, 'b': 2, 'c': 3}) | |
1517 | a2 = bt2.create_value({'a': 1, 'k': 2, 'c': 3}) | |
1518 | self.assertNotEqual(a1, a2) | |
1519 | ||
1520 | def test_eq_same_content_same_len(self): | |
1521 | raw = { | |
1522 | '3': 3, | |
1523 | 'True': True, | |
1524 | 'array': [1, 2.5, None, {'a': 17.6, 'b': None}] | |
1525 | } | |
1526 | a1 = bt2.MapValue(raw) | |
1527 | a2 = bt2.MapValue(copy.deepcopy(raw)) | |
1528 | self.assertEqual(a1, a2) | |
1529 | self.assertEqual(a1, raw) | |
1530 | ||
1531 | def test_setitem_int(self): | |
1532 | raw = 19 | |
1533 | self._def['pos-int'] = raw | |
1534 | self.assertEqual(self._def['pos-int'], raw) | |
1535 | ||
1536 | def test_setitem_vint(self): | |
1537 | raw = 19 | |
1538 | self._def['pos-int'] = bt2.create_value(raw) | |
1539 | self.assertEqual(self._def['pos-int'], raw) | |
1540 | ||
1541 | def test_setitem_none(self): | |
1542 | self._def['none'] = None | |
1543 | self.assertIsNone(self._def['none']) | |
1544 | ||
1545 | def test_setitem_new_int(self): | |
1546 | old_len = len(self._def) | |
1547 | self._def['new-int'] = 23 | |
1548 | self.assertEqual(self._def['new-int'], 23) | |
1549 | self.assertEqual(len(self._def), old_len + 1) | |
1550 | ||
1551 | def test_setitem_index_wrong_type(self): | |
1552 | with self.assertRaises(TypeError): | |
1553 | self._def[18] = 23 | |
1554 | ||
1555 | def test_iter(self): | |
1556 | for vkey, vval in self._def.items(): | |
1557 | val = self._def_value[vkey] | |
1558 | self.assertEqual(vval, val) | |
1559 | ||
1560 | def test_getitem_wrong_key(self): | |
1561 | with self.assertRaises(KeyError): | |
1562 | self._def['kilojoule'] |