bt2: pass custom Python object to Python component's __init__()
[babeltrace.git] / tests / bindings / python / bt2 / test_component.py
CommitLineData
32d2d479
MJ
1#
2# Copyright (C) 2019 EfficiOS Inc.
3#
4# This program is free software; you can redistribute it and/or
5# modify it under the terms of the GNU General Public License
6# as published by the Free Software Foundation; only version 2
7# of the License.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17#
18
f6a5e476
PP
19import unittest
20import copy
21import bt2
22
23
24class UserComponentTestCase(unittest.TestCase):
25 @staticmethod
9bc3e785 26 def _create_comp(comp_cls, name=None, log_level=bt2.LoggingLevel.NONE):
f6a5e476
PP
27 graph = bt2.Graph()
28
29 if name is None:
30 name = 'comp'
31
9bc3e785 32 return graph.add_component(comp_cls, name, logging_level=log_level)
f6a5e476
PP
33
34 def test_name(self):
35 class MySink(bt2._UserSinkComponent):
b20382e2 36 def __init__(comp_self, params, obj):
f6a5e476
PP
37 self.assertEqual(comp_self.name, 'yaes')
38
819d0ae7 39 def _user_consume(self):
8a08af82
SM
40 pass
41
f6a5e476
PP
42 comp = self._create_comp(MySink, 'yaes')
43
9bc3e785
PP
44 def test_logging_level(self):
45 class MySink(bt2._UserSinkComponent):
b20382e2 46 def __init__(comp_self, params, obj):
9bc3e785
PP
47 self.assertEqual(comp_self.logging_level, bt2.LoggingLevel.INFO)
48
819d0ae7 49 def _user_consume(self):
8a08af82
SM
50 pass
51
9bc3e785
PP
52 comp = self._create_comp(MySink, 'yaes', bt2.LoggingLevel.INFO)
53
f6a5e476
PP
54 def test_class(self):
55 class MySink(bt2._UserSinkComponent):
b20382e2 56 def __init__(comp_self, params, obj):
c88be1c8 57 self.assertEqual(comp_self.cls, MySink)
f6a5e476 58
819d0ae7 59 def _user_consume(self):
8a08af82
SM
60 pass
61
f6a5e476
PP
62 self._create_comp(MySink)
63
64 def test_addr(self):
65 class MySink(bt2._UserSinkComponent):
b20382e2 66 def __init__(comp_self, params, obj):
f6a5e476
PP
67 self.assertIsInstance(comp_self.addr, int)
68 self.assertNotEqual(comp_self.addr, 0)
69
819d0ae7 70 def _user_consume(self):
8a08af82
SM
71 pass
72
f6a5e476
PP
73 self._create_comp(MySink)
74
75 def test_finalize(self):
76 finalized = False
77
78 class MySink(bt2._UserSinkComponent):
819d0ae7 79 def _user_consume(self):
8a08af82
SM
80 pass
81
819d0ae7 82 def _user_finalize(comp_self):
f6a5e476
PP
83 nonlocal finalized
84 finalized = True
85
86 graph = bt2.Graph()
bc5c9924 87 comp = graph.add_component(MySink, 'lel')
e34fb94a 88
f6a5e476
PP
89 del graph
90 del comp
91 self.assertTrue(finalized)
92
93
94class GenericComponentTestCase(unittest.TestCase):
95 @staticmethod
9bc3e785 96 def _create_comp(comp_cls, name=None, log_level=bt2.LoggingLevel.NONE):
f6a5e476
PP
97 graph = bt2.Graph()
98
99 if name is None:
100 name = 'comp'
101
9bc3e785 102 return graph.add_component(comp_cls, name, logging_level=log_level)
f6a5e476
PP
103
104 def test_name(self):
105 class MySink(bt2._UserSinkComponent):
819d0ae7 106 def _user_consume(self):
8a08af82
SM
107 pass
108
f6a5e476
PP
109 comp = self._create_comp(MySink, 'yaes')
110 self.assertEqual(comp.name, 'yaes')
111
9bc3e785
PP
112 def test_logging_level(self):
113 class MySink(bt2._UserSinkComponent):
819d0ae7 114 def _user_consume(self):
8a08af82
SM
115 pass
116
e9d0e821
PP
117 comp = self._create_comp(MySink, 'yaes', bt2.LoggingLevel.WARNING)
118 self.assertEqual(comp.logging_level, bt2.LoggingLevel.WARNING)
9bc3e785 119
f6a5e476
PP
120 def test_class(self):
121 class MySink(bt2._UserSinkComponent):
819d0ae7 122 def _user_consume(self):
8a08af82
SM
123 pass
124
f6a5e476 125 comp = self._create_comp(MySink)
c88be1c8 126 self.assertEqual(comp.cls, MySink)
f6a5e476
PP
127
128 def test_addr(self):
129 class MySink(bt2._UserSinkComponent):
819d0ae7 130 def _user_consume(self):
8a08af82
SM
131 pass
132
f6a5e476
PP
133 comp = self._create_comp(MySink)
134 self.assertIsInstance(comp.addr, int)
135 self.assertNotEqual(comp.addr, 0)
This page took 0.042171 seconds and 4 git commands to generate.