X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=blobdiff_plain;f=tests%2Fbindings%2Fpython%2Fbt2%2Ftest_field.py;h=37afffb54a422ee2e7ec0e362fefa6b342958359;hp=429fe2eea37089444eefd1e38b8e4778f99f25a1;hb=0235b0db7de5bcacdb3650c92461f2ce5eb2143d;hpb=cec0261d56a42e810f56b39fcefbe33987c8aab8 diff --git a/tests/bindings/python/bt2/test_field.py b/tests/bindings/python/bt2/test_field.py index 429fe2ee..37afffb5 100644 --- a/tests/bindings/python/bt2/test_field.py +++ b/tests/bindings/python/bt2/test_field.py @@ -1,20 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0-only # # Copyright (C) 2019 EfficiOS Inc. # -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; only version 2 -# of the License. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -# from functools import partial, partialmethod import operator @@ -24,7 +11,7 @@ import copy import itertools import collections import bt2 -from utils import get_default_trace_class +from utils import get_default_trace_class, create_const_field _COMP_BINOPS = (operator.eq, operator.ne) @@ -126,6 +113,52 @@ def _create_struct_array_field(tc, length): return packet.context_field[field_name] +class BitArrayFieldTestCase(unittest.TestCase): + def _create_field(self): + return _create_field(self._tc, self._tc.create_bit_array_field_class(24)) + + def setUp(self): + self._tc = get_default_trace_class() + self._def_value = 15497 + self._def = self._create_field() + self._def.value_as_integer = self._def_value + self._def_new_value = 101542 + + def test_assign_invalid_type(self): + with self.assertRaises(TypeError): + self._def.value_as_integer = 'onze' + + def test_assign(self): + self._def.value_as_integer = 199 + self.assertEqual(self._def.value_as_integer, 199) + + def test_assign_masked(self): + self._def.value_as_integer = 0xE1549BB + self.assertEqual(self._def.value_as_integer, 0xE1549BB & ((1 << 24) - 1)) + + def test_eq(self): + other = self._create_field() + other.value_as_integer = self._def_value + self.assertEqual(self._def, other) + + def test_ne_same_type(self): + other = self._create_field() + other.value_as_integer = self._def_value - 1 + self.assertNotEqual(self._def, other) + + def test_ne_diff_type(self): + self.assertNotEqual(self._def, self._def_value) + + def test_len(self): + self.assertEqual(len(self._def), 24) + + def test_str(self): + self.assertEqual(str(self._def), str(self._def_value)) + + def test_repr(self): + self.assertEqual(repr(self._def), repr(self._def_value)) + + # Base class for numeric field test cases. # # To be compatible with this base class, a derived class must, in its @@ -277,9 +310,8 @@ class _TestNumericField: # Tests that the binary operation `op`, when applied to `self._def`, # does not change its value. - @unittest.skip('copy is not implemented') def _test_binop_lhs_value_same(self, op, rhs): - value_before = copy.copy(self._def) + value_before = copy.copy(self._def_value) r, rv = self._binop(op, rhs) self.assertEqual(self._def, value_before) @@ -298,22 +330,31 @@ class _TestNumericField: # `vint` and `vfloat` mean a signed integer value object and a real # value object. - def _test_binop_invalid_unknown(self, op): - if op in _COMP_BINOPS: - self.skipTest('not testing') - + def _test_binop_unknown(self, op): class A: pass - with self.assertRaises(TypeError): - op(self._def, A()) - - def _test_binop_invalid_none(self, op): - if op in _COMP_BINOPS: - self.skipTest('not testing') - - with self.assertRaises(TypeError): - op(self._def, None) + # Operators == and != are defined when comparing the field to an + # arbitrary object. + if op is operator.eq: + self.assertIs(op(self._def, A()), False) + elif op is operator.ne: + self.assertIs(op(self._def, A()), True) + else: + # But not other operators. + with self.assertRaises(TypeError): + op(self._def, A()) + + def _test_binop_none(self, op): + # Operators == and != are defined when comparing the field to None. + if op is operator.eq: + self.assertIs(op(self._def, None), False) + elif op is operator.ne: + self.assertIs(op(self._def, None), True) + else: + # But not other operators. + with self.assertRaises(TypeError): + op(self._def, None) def _test_binop_rhs_false(self, test_cb, op): test_cb(op, False) @@ -570,6 +611,18 @@ class _TestNumericField: def test_str_op(self): self.assertEqual(str(self._def), str(self._def_value)) + def test_hash_op(self): + with self.assertRaises(TypeError): + hash(self._def) + + def test_const_hash_op(self): + self.assertEqual(hash(self._def_const), hash(self._def_value)) + + def test_const_hash_dict(self): + my_dict = {} + my_dict[self._def_const] = 'my_value' + self.assertEqual(my_dict[self._def_value], 'my_value') + def test_eq_none(self): # Ignore this lint error: # E711 comparison to None should be 'if cond is None:' @@ -668,13 +721,13 @@ def _inject_numeric_testing_methods(cls): for name, binop in _BINOPS: setattr( cls, - test_binop_name('invalid_unknown'), - partialmethod(_TestNumericField._test_binop_invalid_unknown, op=binop), + test_binop_name('unknown'), + partialmethod(_TestNumericField._test_binop_unknown, op=binop), ) setattr( cls, - test_binop_name('invalid_none'), - partialmethod(_TestNumericField._test_binop_invalid_none, op=binop), + test_binop_name('none'), + partialmethod(_TestNumericField._test_binop_none, op=binop), ) setattr( cls, @@ -1078,6 +1131,10 @@ def _inject_numeric_testing_methods(cls): class BoolFieldTestCase(_TestNumericField, unittest.TestCase): + @staticmethod + def _const_value_setter(field): + field.value = True + def _create_fc(self, tc): return tc.create_bool_field_class() @@ -1086,8 +1143,15 @@ class BoolFieldTestCase(_TestNumericField, unittest.TestCase): self._def = _create_field(self._tc, self._create_fc(self._tc)) self._def.value = True self._def_value = True + self._def_const = create_const_field( + self._tc, self._tc.create_bool_field_class(), self._const_value_setter + ) self._def_new_value = False + def test_classes(self): + self.assertIs(type(self._def), bt2._BoolField) + self.assertIs(type(self._def_const), bt2._BoolFieldConst) + def test_assign_true(self): raw = True self._def.value = raw @@ -1171,12 +1235,35 @@ class _TestIntegerFieldCommon(_TestNumericField): field.value = (2 ** 53) + 1 self.assertEqual(field, raw) - def test_assign_uint_invalid_neg(self): - uint_fc = self._tc.create_unsigned_integer_field_class(32) + def test_assign_uint_out_of_range(self): + uint_fc = self._tc.create_unsigned_integer_field_class(8) field = _create_field(self._tc, uint_fc) - with self.assertRaises(ValueError): - field.value = -23 + with self.assertRaises(ValueError) as ctx: + field.value = 256 + self.assertEqual( + str(ctx.exception), 'Value 256 is outside valid range [0, 255]' + ) + + with self.assertRaises(ValueError) as ctx: + field.value = -1 + self.assertEqual(str(ctx.exception), 'Value -1 is outside valid range [0, 255]') + + def test_assign_int_out_of_range(self): + int_fc = self._tc.create_signed_integer_field_class(8) + field = _create_field(self._tc, int_fc) + + with self.assertRaises(ValueError) as ctx: + field.value = 128 + self.assertEqual( + str(ctx.exception), 'Value 128 is outside valid range [-128, 127]' + ) + + with self.assertRaises(ValueError) as ctx: + field.value = -129 + self.assertEqual( + str(ctx.exception), 'Value -129 is outside valid range [-128, 127]' + ) def test_str_op(self): self.assertEqual(str(self._def), str(self._def_value)) @@ -1186,6 +1273,10 @@ _inject_numeric_testing_methods(_TestIntegerFieldCommon) class SignedIntegerFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase): + @staticmethod + def _const_value_setter(field): + field.value = 17 + def _create_fc(self, tc): return tc.create_signed_integer_field_class(25) @@ -1196,10 +1287,17 @@ class SignedIntegerFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase): self._def = _create_field(self._tc, self._create_fc(self._tc)) self._def.value = 17 self._def_value = 17 + self._def_const = create_const_field( + self._tc, self._create_fc(self._tc), self._const_value_setter + ) self._def_new_value = -101 class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase): + @staticmethod + def _const_value_setter(field): + field.value = 17 + def _create_fc(self, tc): fc = tc.create_signed_enumeration_field_class(32) fc.add_mapping('something', bt2.SignedIntegerRangeSet([(17, 17)])) @@ -1217,6 +1315,9 @@ class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase) self._def = _create_field(self._tc, self._create_fc(self._tc)) self._def.value = 17 self._def_value = 17 + self._def_const = create_const_field( + self._tc, self._create_fc(self._tc), self._const_value_setter + ) self._def_new_value = -101 def test_str_op(self): @@ -1240,14 +1341,119 @@ class SignedEnumerationFieldTestCase(_TestIntegerFieldCommon, unittest.TestCase) self.assertEqual(labels, ['something', 'whole range', 'zip']) -class RealFieldTestCase(_TestNumericField, unittest.TestCase): +class SingleRealFieldTestCase(_TestNumericField, unittest.TestCase): + @staticmethod + def _const_value_setter(field): + field.value = 52.0 + def _create_fc(self, tc): - return tc.create_real_field_class() + return tc.create_single_precision_real_field_class() def setUp(self): self._tc = get_default_trace_class() self._field = _create_field(self._tc, self._create_fc(self._tc)) self._def = _create_field(self._tc, self._create_fc(self._tc)) + self._def_const = create_const_field( + self._tc, + self._tc.create_single_precision_real_field_class(), + self._const_value_setter, + ) + self._def.value = 52.0 + self._def_value = 52.0 + self._def_new_value = -17.0 + + def _test_invalid_op(self, cb): + with self.assertRaises(TypeError): + cb() + + def test_assign_true(self): + self._def.value = True + self.assertTrue(self._def) + + def test_assign_false(self): + self._def.value = False + self.assertFalse(self._def) + + def test_assign_pos_int(self): + raw = 477 + self._def.value = raw + self.assertEqual(self._def, float(raw)) + + def test_assign_neg_int(self): + raw = -13 + self._def.value = raw + self.assertEqual(self._def, float(raw)) + + def test_assign_int_field(self): + int_fc = self._tc.create_signed_integer_field_class(32) + int_field = _create_field(self._tc, int_fc) + raw = 999 + int_field.value = raw + self._def.value = int_field + self.assertEqual(self._def, float(raw)) + + def test_assign_float(self): + raw = -19.23 + self._def.value = raw + # It's expected to have some lost of precision because of the field + # that is in single precision. + self.assertEqual(round(self._def, 5), raw) + + def test_assign_float_field(self): + field = _create_field(self._tc, self._create_fc(self._tc)) + raw = 101.32 + field.value = raw + self._def.value = field + # It's expected to have some lost of precision because of the field + # that is in single precision. + self.assertEqual(round(self._def, 5), raw) + + def test_assign_invalid_type(self): + with self.assertRaises(TypeError): + self._def.value = 'yes' + + def test_invalid_lshift(self): + self._test_invalid_op(lambda: self._def << 23) + + def test_invalid_rshift(self): + self._test_invalid_op(lambda: self._def >> 23) + + def test_invalid_and(self): + self._test_invalid_op(lambda: self._def & 23) + + def test_invalid_or(self): + self._test_invalid_op(lambda: self._def | 23) + + def test_invalid_xor(self): + self._test_invalid_op(lambda: self._def ^ 23) + + def test_invalid_invert(self): + self._test_invalid_op(lambda: ~self._def) + + def test_str_op(self): + self.assertEqual(str(round(self._def, 5)), str(self._def_value)) + + +_inject_numeric_testing_methods(SingleRealFieldTestCase) + + +class DoubleRealFieldTestCase(_TestNumericField, unittest.TestCase): + @staticmethod + def _const_value_setter(field): + field.value = 52.7 + + def _create_fc(self, tc): + return tc.create_double_precision_real_field_class() + + def setUp(self): + self._tc = get_default_trace_class() + self._field = _create_field(self._tc, self._create_fc(self._tc)) + self._def = _create_field(self._tc, self._create_fc(self._tc)) + self._def_const = create_const_field( + self._tc, + self._tc.create_double_precision_real_field_class(), + self._const_value_setter, + ) self._def.value = 52.7 self._def_value = 52.7 self._def_new_value = -17.164857 @@ -1320,14 +1526,21 @@ class RealFieldTestCase(_TestNumericField, unittest.TestCase): self.assertEqual(str(self._def), str(self._def_value)) -_inject_numeric_testing_methods(RealFieldTestCase) +_inject_numeric_testing_methods(DoubleRealFieldTestCase) class StringFieldTestCase(unittest.TestCase): + @staticmethod + def _const_value_setter(field): + field.value = 'Hello, World!' + def setUp(self): self._tc = get_default_trace_class() self._def_value = 'Hello, World!' self._def = _create_string_field(self._tc) + self._def_const = create_const_field( + self._tc, self._tc.create_string_field_class(), self._const_value_setter + ) self._def.value = self._def_value self._def_new_value = 'Yes!' @@ -1344,6 +1557,9 @@ class StringFieldTestCase(unittest.TestCase): def test_eq(self): self.assertEqual(self._def, self._def_value) + def test_const_eq(self): + self.assertEqual(self._def_const, self._def_value) + def test_not_eq(self): self.assertNotEqual(self._def, 23) @@ -1407,12 +1623,21 @@ class StringFieldTestCase(unittest.TestCase): def test_getitem(self): self.assertEqual(self._def[5], self._def_value[5]) + def test_const_getitem(self): + self.assertEqual(self._def_const[5], self._def_value[5]) + def test_append_str(self): to_append = 'meow meow meow' self._def += to_append self._def_value += to_append self.assertEqual(self._def, self._def_value) + def test_const_append_str(self): + to_append = 'meow meow meow' + with self.assertRaises(TypeError): + self._def_const += to_append + self.assertEqual(self._def_const, self._def_value) + def test_append_string_field(self): field = _create_string_field(self._tc) to_append = 'meow meow meow' @@ -1421,6 +1646,18 @@ class StringFieldTestCase(unittest.TestCase): self._def_value += to_append self.assertEqual(self._def, self._def_value) + def test_hash_op(self): + with self.assertRaises(TypeError): + hash(self._def) + + def test_const_hash_op(self): + self.assertEqual(hash(self._def_const), hash(self._def_value)) + + def test_const_hash_dict(self): + my_dict = {} + my_dict[self._def_const] = 'my_value' + self.assertEqual(my_dict[self._def_value], 'my_value') + class _TestArrayFieldCommon: def _modify_def(self): @@ -1440,6 +1677,11 @@ class _TestArrayFieldCommon: self.assertIs(type(field), bt2._SignedIntegerField) self.assertEqual(field, 1847) + def test_const_getitem(self): + field = self._def_const[1] + self.assertIs(type(field), bt2._SignedIntegerFieldConst) + self.assertEqual(field, 1847) + def test_eq(self): field = _create_int_array_field(self._tc, 3) field[0] = 45 @@ -1504,10 +1746,18 @@ class _TestArrayFieldCommon: with self.assertRaises(IndexError): self._def[len(self._def)] = 134679 + def test_const_setitem(self): + with self.assertRaises(TypeError): + self._def_const[0] = 134679 + def test_iter(self): for field, value in zip(self._def, (45, 1847, 1948754)): self.assertEqual(field, value) + def test_const_iter(self): + for field, value in zip(self._def_const, (45, 1847, 1948754)): + self.assertEqual(field, value) + def test_value_int_field(self): values = [45646, 145, 12145] self._def.value = values @@ -1553,6 +1803,10 @@ class _TestArrayFieldCommon: class StaticArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase): + @staticmethod + def _const_value_setter(field): + field.value = [45, 1847, 1948754] + def setUp(self): self._tc = get_default_trace_class() self._def = _create_int_array_field(self._tc, 3) @@ -1560,6 +1814,13 @@ class StaticArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase): self._def[1] = 1847 self._def[2] = 1948754 self._def_value = [45, 1847, 1948754] + self._def_const = create_const_field( + self._tc, + self._tc.create_static_array_field_class( + self._tc.create_signed_integer_field_class(32), 3 + ), + self._const_value_setter, + ) def test_value_wrong_len(self): values = [45, 1847] @@ -1568,6 +1829,10 @@ class StaticArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase): class DynamicArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase): + @staticmethod + def _const_value_setter(field): + field.value = [45, 1847, 1948754] + def setUp(self): self._tc = get_default_trace_class() self._def = _create_dynamic_array(self._tc) @@ -1575,6 +1840,13 @@ class DynamicArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase): self._def[1] = 1847 self._def[2] = 1948754 self._def_value = [45, 1847, 1948754] + self._def_const = create_const_field( + self._tc, + self._tc.create_dynamic_array_field_class( + self._tc.create_signed_integer_field_class(32) + ), + self._const_value_setter, + ) def test_value_resize(self): new_values = [1, 2, 3, 4] @@ -1586,12 +1858,28 @@ class DynamicArrayFieldTestCase(_TestArrayFieldCommon, unittest.TestCase): self._def[3] = 0 self.assertEqual(len(self._def), 4) + def test_const_set_length(self): + with self.assertRaises(AttributeError): + self._def_const.length = 4 + self.assertEqual(len(self._def), 3) + def test_set_invalid_length(self): with self.assertRaises(TypeError): self._def.length = 'cheval' class StructureFieldTestCase(unittest.TestCase): + @staticmethod + def _const_value_setter(field): + field.value = { + 'A': -1872, + 'B': 'salut', + 'C': 17.5, + 'D': 16497, + 'E': {}, + 'F': {'F_1': 52}, + } + def _create_fc(self, tc): fc = tc.create_structure_field_class() fc.append_member('A', self._fc0_fn()) @@ -1608,7 +1896,7 @@ class StructureFieldTestCase(unittest.TestCase): self._tc = get_default_trace_class() self._fc0_fn = self._tc.create_signed_integer_field_class self._fc1_fn = self._tc.create_string_field_class - self._fc2_fn = self._tc.create_real_field_class + self._fc2_fn = self._tc.create_double_precision_real_field_class self._fc3_fn = self._tc.create_signed_integer_field_class self._fc4_fn = self._tc.create_structure_field_class self._fc5_fn = self._tc.create_structure_field_class @@ -1631,6 +1919,10 @@ class StructureFieldTestCase(unittest.TestCase): 'F': {'F_1': 52}, } + self._def_const = create_const_field( + self._tc, self._create_fc(self._tc), self._const_value_setter + ) + def _modify_def(self): self._def['B'] = 'hola' @@ -1645,9 +1937,56 @@ class StructureFieldTestCase(unittest.TestCase): self.assertEqual(len(self._def), len(self._def_value)) def test_getitem(self): - field = self._def['A'] - self.assertIs(type(field), bt2._SignedIntegerField) - self.assertEqual(field, -1872) + field1 = self._def['A'] + field2 = self._def['B'] + field3 = self._def['C'] + field4 = self._def['D'] + field5 = self._def['E'] + field6 = self._def['F'] + + self.assertIs(type(field1), bt2._SignedIntegerField) + self.assertEqual(field1, -1872) + + self.assertIs(type(field2), bt2._StringField) + self.assertEqual(field2, 'salut') + + self.assertIs(type(field3), bt2._DoublePrecisionRealField) + self.assertEqual(field3, 17.5) + + self.assertIs(type(field4), bt2._SignedIntegerField) + self.assertEqual(field4, 16497) + + self.assertIs(type(field5), bt2._StructureField) + self.assertEqual(field5, {}) + + self.assertIs(type(field6), bt2._StructureField) + self.assertEqual(field6, {'F_1': 52}) + + def test_const_getitem(self): + field1 = self._def_const['A'] + field2 = self._def_const['B'] + field3 = self._def_const['C'] + field4 = self._def_const['D'] + field5 = self._def_const['E'] + field6 = self._def_const['F'] + + self.assertIs(type(field1), bt2._SignedIntegerFieldConst) + self.assertEqual(field1, -1872) + + self.assertIs(type(field2), bt2._StringFieldConst) + self.assertEqual(field2, 'salut') + + self.assertIs(type(field3), bt2._DoublePrecisionRealFieldConst) + self.assertEqual(field3, 17.5) + + self.assertIs(type(field4), bt2._SignedIntegerFieldConst) + self.assertEqual(field4, 16497) + + self.assertIs(type(field5), bt2._StructureFieldConst) + self.assertEqual(field5, {}) + + self.assertIs(type(field6), bt2._StructureFieldConst) + self.assertEqual(field6, {'F_1': 52}) def test_member_at_index_out_of_bounds_after(self): with self.assertRaises(IndexError): @@ -1663,6 +2002,16 @@ class StructureFieldTestCase(unittest.TestCase): field['F'] = {'F_1': 52} self.assertEqual(self._def, field) + def test_const_eq(self): + field = _create_field(self._tc, self._create_fc(self._tc)) + field['A'] = -1872 + field['B'] = 'salut' + field['C'] = 17.5 + field['D'] = 16497 + field['E'] = {} + field['F'] = {'F_1': 52} + self.assertEqual(self._def_const, field) + def test_eq_invalid_type(self): self.assertNotEqual(self._def, 23) @@ -1726,6 +2075,10 @@ class StructureFieldTestCase(unittest.TestCase): self._def['C'] = -18.47 self.assertEqual(self._def['C'], -18.47) + def test_const_setitem(self): + with self.assertRaises(TypeError): + self._def_const['A'] = 134679 + def test_setitem_int_field(self): int_fc = self._tc.create_signed_integer_field_class(32) int_field = _create_field(self._tc, int_fc) @@ -1758,6 +2111,9 @@ class StructureFieldTestCase(unittest.TestCase): def test_member_at_index(self): self.assertEqual(self._def.member_at_index(1), 'salut') + def test_const_member_at_index(self): + self.assertEqual(self._def_const.member_at_index(1), 'salut') + def test_iter(self): orig_values = { 'A': -1872, @@ -1824,8 +2180,14 @@ class StructureFieldTestCase(unittest.TestCase): class OptionFieldTestCase(unittest.TestCase): + @staticmethod + def _const_value_setter(field): + field.value = {'opt_field': 'hiboux'} + def _create_fc(self, tc): - fc = tc.create_option_field_class(tc.create_string_field_class()) + fc = tc.create_option_without_selector_field_class( + tc.create_string_field_class() + ) top_fc = tc.create_structure_field_class() top_fc.append_member('opt_field', fc) return top_fc @@ -1834,17 +2196,29 @@ class OptionFieldTestCase(unittest.TestCase): self._tc = get_default_trace_class() fld = _create_field(self._tc, self._create_fc(self._tc)) self._def = fld['opt_field'] + self._def_value = 'hiboux' + self._def_const = create_const_field( + self._tc, self._create_fc(self._tc), self._const_value_setter + )['opt_field'] def test_value_prop(self): self._def.value = 'hiboux' self.assertEqual(self._def.field, 'hiboux') + self.assertIs(type(self._def), bt2._OptionField) + self.assertIs(type(self._def.field), bt2._StringField) self.assertTrue(self._def.has_field) + def test_const_value_prop(self): + self.assertEqual(self._def_const.field, 'hiboux') + self.assertIs(type(self._def_const), bt2._OptionFieldConst) + self.assertIs(type(self._def_const.field), bt2._StringFieldConst) + self.assertTrue(self._def_const.has_field) + def test_has_field_prop_true(self): self._def.has_field = True self.assertTrue(self._def.has_field) - def test_has_field_prop_true(self): + def test_has_field_prop_false(self): self._def.has_field = False self.assertFalse(self._def.has_field) @@ -1852,7 +2226,7 @@ class OptionFieldTestCase(unittest.TestCase): self._def.value = 'allo' self.assertTrue(self._def) - def test_bool_op_true(self): + def test_bool_op_false(self): self._def.has_field = False self.assertFalse(self._def) @@ -1866,6 +2240,13 @@ class OptionFieldTestCase(unittest.TestCase): field = self._def.field self.assertIsNone(field) + def test_const_field_prop(self): + with self.assertRaises(AttributeError): + self._def_const.has_field = False + + self.assertEqual(self._def_const, self._def_value) + self.assertTrue(self._def_const.has_field) + def test_field_prop_existing_then_none(self): self._def.value = 'meow' field = self._def.field @@ -1881,6 +2262,13 @@ class OptionFieldTestCase(unittest.TestCase): self._def.value = 'walk' self.assertEqual(self._def, field) + def test_const_eq(self): + field = _create_field(self._tc, self._create_fc(self._tc)) + field = field['opt_field'] + field.value = 'hiboux' + self.assertEqual(self._def_const, field) + self.assertEqual(self._def_const, self._def_value) + def test_eq_invalid_type(self): self._def.value = 'gerry' self.assertNotEqual(self._def, 23) @@ -1895,10 +2283,15 @@ class OptionFieldTestCase(unittest.TestCase): class VariantFieldTestCase(unittest.TestCase): + @staticmethod + def _const_value_setter(field): + field.selected_option_index = 3 + field.value = 1334 + def _create_fc(self, tc): ft0 = tc.create_signed_integer_field_class(32) ft1 = tc.create_string_field_class() - ft2 = tc.create_real_field_class() + ft2 = tc.create_double_precision_real_field_class() ft3 = tc.create_signed_integer_field_class(17) fc = tc.create_variant_field_class() fc.append_option('corner', ft0) @@ -1914,6 +2307,15 @@ class VariantFieldTestCase(unittest.TestCase): fld = _create_field(self._tc, self._create_fc(self._tc)) self._def = fld['variant_field'] + self._def_value = 1334 + self._def_selected_index = 3 + const_fc = self._create_fc(self._tc)['variant_field'] + + fld_const = create_const_field( + self._tc, const_fc.field_class, self._const_value_setter + ) + self._def_const = fld_const + def test_bool_op(self): self._def.selected_option_index = 2 self._def.value = -17.34 @@ -1924,14 +2326,35 @@ class VariantFieldTestCase(unittest.TestCase): self._def.selected_option_index = 2 self.assertEqual(self._def.selected_option_index, 2) + def test_selected_option_index_above_range(self): + with self.assertRaises(IndexError): + self._def.selected_option_index = 4 + + def test_selected_option_index_below_range(self): + with self.assertRaises(IndexError): + self._def.selected_option_index = -1 + + def test_const_selected_option_index(self): + with self.assertRaises(AttributeError): + self._def_const.selected_option_index = 2 + self.assertEqual(self._def_const.selected_option_index, 3) + def test_selected_option(self): self._def.selected_option_index = 2 self._def.value = -17.34 self.assertEqual(self._def.selected_option, -17.34) + self.assertEqual(type(self._def.selected_option), bt2._DoublePrecisionRealField) self._def.selected_option_index = 3 self._def.value = 1921 self.assertEqual(self._def.selected_option, 1921) + self.assertEqual(type(self._def.selected_option), bt2._SignedIntegerField) + + def test_const_selected_option(self): + self.assertEqual(self._def_const.selected_option, 1334) + self.assertEqual( + type(self._def_const.selected_option), bt2._SignedIntegerFieldConst + ) def test_eq(self): field = _create_field(self._tc, self._create_fc(self._tc)) @@ -1942,6 +2365,16 @@ class VariantFieldTestCase(unittest.TestCase): self._def.value = 1774 self.assertEqual(self._def, field) + def test_const_eq(self): + field = _create_field(self._tc, self._create_fc(self._tc)) + field = field['variant_field'] + field.selected_option_index = 3 + field.value = 1334 + self.assertEqual(self._def_const, field) + + def test_len(self): + self.assertEqual(len(self._def), 4) + def test_eq_invalid_type(self): self._def.selected_option_index = 1 self._def.value = 'gerry' @@ -1979,3 +2412,7 @@ class VariantFieldTestCase(unittest.TestCase): other_field.selected_option_index = 2 other_field.value = 14.4245 self.assertEqual(str(field), str(other_field)) + + +if __name__ == '__main__': + unittest.main()