X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=tests%2Fbindings%2Fpython%2Fbt2%2Ftest_trace_class.py;h=69397c633d08cd414723c268631a351d8c162329;hb=f0a42b33ac3951cd5cb2ee0f66ac04437a681621;hp=97ede58424d8e1499b29dda3206a81b126d34579;hpb=57081273d1191fc79edc101af619fab96b72460d;p=babeltrace.git diff --git a/tests/bindings/python/bt2/test_trace_class.py b/tests/bindings/python/bt2/test_trace_class.py index 97ede584..69397c63 100644 --- a/tests/bindings/python/bt2/test_trace_class.py +++ b/tests/bindings/python/bt2/test_trace_class.py @@ -17,10 +17,27 @@ # import unittest -from utils import run_in_component_init, get_default_trace_class +from utils import ( + run_in_component_init, + get_default_trace_class, + get_const_stream_beginning_message, +) +from bt2 import stream_class as bt2_stream_class +from bt2 import trace_class as bt2_trace_class class TraceClassTestCase(unittest.TestCase): + def assertRaisesInComponentInit(self, expected_exc_type, user_code): + def f(comp_self): + try: + user_code(comp_self) + except Exception as exc: + return type(exc) + + exc_type = run_in_component_init(f) + self.assertIsNotNone(exc_type) + self.assertEqual(exc_type, expected_exc_type) + def test_create_default(self): def f(comp_self): return comp_self._create_trace_class() @@ -28,7 +45,28 @@ class TraceClassTestCase(unittest.TestCase): tc = run_in_component_init(f) self.assertEqual(len(tc), 0) + self.assertIs(type(tc), bt2_trace_class._TraceClass) self.assertTrue(tc.assigns_automatic_stream_class_id) + self.assertEqual(len(tc.user_attributes), 0) + + def test_create_user_attributes(self): + def f(comp_self): + return comp_self._create_trace_class(user_attributes={'salut': 23}) + + tc = run_in_component_init(f) + self.assertEqual(tc.user_attributes, {'salut': 23}) + + def test_create_invalid_user_attributes(self): + def f(comp_self): + return comp_self._create_trace_class(user_attributes=object()) + + self.assertRaisesInComponentInit(TypeError, f) + + def test_create_invalid_user_attributes_value_type(self): + def f(comp_self): + return comp_self._create_trace_class(user_attributes=23) + + self.assertRaisesInComponentInit(TypeError, f) def test_automatic_stream_class_id(self): def f(comp_self): @@ -41,6 +79,8 @@ class TraceClassTestCase(unittest.TestCase): sc1 = tc.create_stream_class() sc2 = tc.create_stream_class() + self.assertIs(type(sc1), bt2_stream_class._StreamClass) + self.assertIs(type(sc2), bt2_stream_class._StreamClass) self.assertNotEqual(sc1.id, sc2.id) def test_automatic_stream_class_id_raises(self): @@ -51,7 +91,7 @@ class TraceClassTestCase(unittest.TestCase): self.assertTrue(tc.assigns_automatic_stream_class_id) with self.assertRaises(ValueError): - sc1 = tc.create_stream_class(23) + tc.create_stream_class(23) def test_no_assigns_automatic_stream_class_id(self): def f(comp_self): @@ -93,8 +133,13 @@ class TraceClassTestCase(unittest.TestCase): def test_getitem(self): tc, _, _, sc3 = self._create_trace_class_with_some_stream_classes() + self.assertIs(type(tc[2018]), bt2_stream_class._StreamClass) self.assertEqual(tc[2018].addr, sc3.addr) + def test_const_getitem(self): + const_tc = get_const_stream_beginning_message().stream.trace.cls + self.assertIs(type(const_tc[0]), bt2_stream_class._StreamClassConst) + def test_getitem_wrong_key_type(self): tc, _, _, _ = self._create_trace_class_with_some_stream_classes() with self.assertRaises(TypeError): @@ -116,12 +161,18 @@ class TraceClassTestCase(unittest.TestCase): for sc_id, stream_class in tc.items(): if sc_id == 12: + self.assertIs(type(stream_class), bt2_stream_class._StreamClass) self.assertEqual(stream_class.addr, sc1.addr) elif sc_id == 54: self.assertEqual(stream_class.addr, sc2.addr) elif sc_id == 2018: self.assertEqual(stream_class.addr, sc3.addr) + def test_const_iter(self): + const_tc = get_const_stream_beginning_message().stream.trace.cls + const_sc = list(const_tc.values())[0] + self.assertIs(type(const_sc), bt2_stream_class._StreamClassConst) + def test_destruction_listener(self): def on_trace_class_destruction(trace_class): nonlocal trace_class_destroyed