bt2: add bit array field class and field support
[babeltrace.git] / tests / bindings / python / bt2 / test_field_class.py
index 2b05f0096cfab2bc8213d98834c6bc2033e0b377..e564cac5f2a9c8a6533b1360c6a057186fa40de5 100644 (file)
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #
 
-import bt2.field
 import unittest
 import bt2
-import collections
 from utils import get_default_trace_class
 
 
+class BoolFieldClassTestCase(unittest.TestCase):
+    def setUp(self):
+        tc = get_default_trace_class()
+        self._fc = tc.create_bool_field_class()
+
+    def test_create_default(self):
+        self.assertIsNotNone(self._fc)
+
+
+class BitArrayFieldClassTestCase(unittest.TestCase):
+    def setUp(self):
+        self._tc = get_default_trace_class()
+        self._fc = self._tc.create_bit_array_field_class(17)
+
+    def test_create_default(self):
+        self.assertIsNotNone(self._fc)
+
+    def test_create_length_out_of_range(self):
+        with self.assertRaises(ValueError):
+            self._tc.create_bit_array_field_class(65)
+
+    def test_create_length_zero(self):
+        with self.assertRaises(ValueError):
+            self._tc.create_bit_array_field_class(0)
+
+    def test_create_length_invalid_type(self):
+        with self.assertRaises(TypeError):
+            self._tc.create_bit_array_field_class('lel')
+
+    def test_length_prop(self):
+        self.assertEqual(self._fc.length, 17)
+
+
 class _TestIntegerFieldClassProps:
     def test_create_default(self):
         fc = self._create_func()
@@ -132,7 +163,7 @@ class _EnumerationFieldClassTestCase(_TestIntegerFieldClassProps):
             self._fc.add_mapping('allo', 'meow')
 
     def test_add_mapping_dup_label(self):
-        with self.assertRaises(bt2.Error):
+        with self.assertRaises(ValueError):
             self._fc.add_mapping('a', self._ranges1)
             self._fc.add_mapping('a', self._ranges2)
 
@@ -263,7 +294,7 @@ class _TestElementContainer:
         sub_fc1 = self._tc.create_string_field_class()
         sub_fc2 = self._tc.create_string_field_class()
 
-        with self.assertRaises(bt2.Error):
+        with self.assertRaises(ValueError):
             self._append_element_method(self._fc, 'yes', sub_fc1)
             self._append_element_method(self._fc, 'yes', sub_fc2)
 
@@ -371,25 +402,93 @@ class _TestElementContainer:
 
 
 class StructureFieldClassTestCase(_TestElementContainer, unittest.TestCase):
-    _append_element_method = staticmethod(
-        bt2.field_class._StructureFieldClass.append_member
-    )
-    _at_index_method = staticmethod(
-        bt2.field_class._StructureFieldClass.member_at_index
-    )
+    _append_element_method = staticmethod(bt2._StructureFieldClass.append_member)
+    _at_index_method = staticmethod(bt2._StructureFieldClass.member_at_index)
 
     def _create_default_fc(self):
         return self._tc.create_structure_field_class()
 
 
+class OptionFieldClassTestCase(unittest.TestCase):
+    def setUp(self):
+        self._tc = get_default_trace_class()
+        self._content_fc = self._tc.create_signed_integer_field_class(23)
+        self._tag_fc = self._tc.create_bool_field_class()
+
+    def test_create_default(self):
+        fc = self._tc.create_option_field_class(self._content_fc)
+        self.assertEqual(fc.field_class.addr, self._content_fc.addr)
+        self.assertIsNone(fc.selector_field_path, None)
+
+    def _create_field_class_for_field_path_test(self):
+        fc = self._tc.create_option_field_class(self._content_fc, self._tag_fc)
+
+        foo_fc = self._tc.create_real_field_class()
+        bar_fc = self._tc.create_string_field_class()
+        baz_fc = self._tc.create_string_field_class()
+
+        inner_struct_fc = self._tc.create_structure_field_class()
+        inner_struct_fc.append_member('bar', bar_fc)
+        inner_struct_fc.append_member('baz', baz_fc)
+        inner_struct_fc.append_member('tag', self._tag_fc)
+        inner_struct_fc.append_member('opt', fc)
+
+        opt_struct_array_fc = self._tc.create_option_field_class(inner_struct_fc)
+
+        outer_struct_fc = self._tc.create_structure_field_class()
+        outer_struct_fc.append_member('foo', foo_fc)
+        outer_struct_fc.append_member('inner_opt', opt_struct_array_fc)
+
+        # The path to the selector field class is resolved when the
+        # option field class is actually used, for example in a packet
+        # context.
+        self._tc.create_stream_class(
+            packet_context_field_class=outer_struct_fc, supports_packets=True
+        )
+
+        return fc
+
+    def test_field_path_len(self):
+        fc = self._create_field_class_for_field_path_test()
+        self.assertEqual(len(fc.selector_field_path), 3)
+
+    def test_field_path_iter(self):
+        fc = self._create_field_class_for_field_path_test()
+        path_items = list(fc.selector_field_path)
+
+        self.assertEqual(len(path_items), 3)
+
+        self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem)
+        self.assertEqual(path_items[0].index, 1)
+
+        self.assertIsInstance(path_items[1], bt2._CurrentOptionContentFieldPathItem)
+
+        self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem)
+        self.assertEqual(path_items[2].index, 2)
+
+    def test_field_path_root_scope(self):
+        fc = self._create_field_class_for_field_path_test()
+        self.assertEqual(
+            fc.selector_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT
+        )
+
+    def test_create_invalid_field_class(self):
+        with self.assertRaises(TypeError):
+            self._tc.create_option_field_class(object())
+
+    def test_create_invalid_selector_type(self):
+        with self.assertRaises(TypeError):
+            self._tc.create_option_field_class(self._content_fc, 17)
+
+
 class VariantFieldClassWithoutSelectorTestCase(
     _TestElementContainer, unittest.TestCase
 ):
     _append_element_method = staticmethod(
-        bt2.field_class._VariantFieldClassWithoutSelector.append_option
+        bt2._VariantFieldClassWithoutSelector.append_option
     )
     _at_index_method = staticmethod(
-        bt2.field_class._VariantFieldClassWithoutSelector.option_at_index
+        bt2._VariantFieldClassWithoutSelector.option_at_index
     )
 
     def _create_default_fc(self):
@@ -446,7 +545,7 @@ class _VariantFieldClassWithSelectorTestCase:
         sub_fc1 = self._tc.create_string_field_class()
         sub_fc2 = self._tc.create_string_field_class()
 
-        with self.assertRaises(bt2.Error):
+        with self.assertRaises(ValueError):
             self._fc.append_option('yes', sub_fc1, self._ranges1)
             self._fc.append_option('yes', sub_fc2, self._ranges2)
 
@@ -617,20 +716,18 @@ class _VariantFieldClassWithSelectorTestCase:
 
         self.assertEqual(len(path_items), 3)
 
-        self.assertIsInstance(path_items[0], bt2.field_path._IndexFieldPathItem)
+        self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem)
         self.assertEqual(path_items[0].index, 1)
 
-        self.assertIsInstance(
-            path_items[1], bt2.field_path._CurrentArrayElementFieldPathItem
-        )
+        self.assertIsInstance(path_items[1], bt2._CurrentArrayElementFieldPathItem)
 
-        self.assertIsInstance(path_items[2], bt2.field_path._IndexFieldPathItem)
+        self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem)
         self.assertEqual(path_items[2].index, 0)
 
     def test_selector_field_path_root_scope(self):
         self._fill_default_fc_for_field_path_test()
         self.assertEqual(
-            self._fc.selector_field_path.root_scope, bt2.field_path.Scope.PACKET_CONTEXT
+            self._fc.selector_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT
         )
 
 
@@ -745,20 +842,18 @@ class DynamicArrayFieldClassTestCase(unittest.TestCase):
 
         self.assertEqual(len(path_items), 3)
 
-        self.assertIsInstance(path_items[0], bt2.field_path._IndexFieldPathItem)
+        self.assertIsInstance(path_items[0], bt2._IndexFieldPathItem)
         self.assertEqual(path_items[0].index, 1)
 
-        self.assertIsInstance(
-            path_items[1], bt2.field_path._CurrentArrayElementFieldPathItem
-        )
+        self.assertIsInstance(path_items[1], bt2._CurrentArrayElementFieldPathItem)
 
-        self.assertIsInstance(path_items[2], bt2.field_path._IndexFieldPathItem)
+        self.assertIsInstance(path_items[2], bt2._IndexFieldPathItem)
         self.assertEqual(path_items[2].index, 2)
 
     def test_field_path_root_scope(self):
         fc = self._create_field_class_for_field_path_test()
         self.assertEqual(
-            fc.length_field_path.root_scope, bt2.field_path.Scope.PACKET_CONTEXT
+            fc.length_field_path.root_scope, bt2.FieldPathScope.PACKET_CONTEXT
         )
 
     def test_create_invalid_field_class(self):
This page took 0.028559 seconds and 4 git commands to generate.