lib: pass config objects to component init methods
[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 19import unittest
f6a5e476 20import bt2
e3250e61 21from bt2 import component as bt2_component
f6a5e476
PP
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):
e3250e61 36 def __init__(comp_self, config, params, obj):
f6a5e476
PP
37 self.assertEqual(comp_self.name, 'yaes')
38
819d0ae7 39 def _user_consume(self):
8a08af82
SM
40 pass
41
36153ada 42 self._create_comp(MySink, 'yaes')
f6a5e476 43
9bc3e785
PP
44 def test_logging_level(self):
45 class MySink(bt2._UserSinkComponent):
e3250e61 46 def __init__(comp_self, config, 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
36153ada 52 self._create_comp(MySink, 'yaes', bt2.LoggingLevel.INFO)
9bc3e785 53
253a9ecc
PP
54 def test_graph_mip_version(self):
55 class MySink(bt2._UserSinkComponent):
e3250e61 56 def __init__(comp_self, config, params, obj):
253a9ecc
PP
57 self.assertEqual(comp_self._graph_mip_version, 0)
58
59 def _user_consume(self):
60 pass
61
36153ada 62 self._create_comp(MySink, 'yaes', bt2.LoggingLevel.INFO)
253a9ecc 63
f6a5e476
PP
64 def test_class(self):
65 class MySink(bt2._UserSinkComponent):
e3250e61 66 def __init__(comp_self, config, params, obj):
c88be1c8 67 self.assertEqual(comp_self.cls, MySink)
f6a5e476 68
819d0ae7 69 def _user_consume(self):
8a08af82
SM
70 pass
71
f6a5e476
PP
72 self._create_comp(MySink)
73
74 def test_addr(self):
75 class MySink(bt2._UserSinkComponent):
e3250e61 76 def __init__(comp_self, config, params, obj):
f6a5e476
PP
77 self.assertIsInstance(comp_self.addr, int)
78 self.assertNotEqual(comp_self.addr, 0)
79
819d0ae7 80 def _user_consume(self):
8a08af82
SM
81 pass
82
f6a5e476
PP
83 self._create_comp(MySink)
84
85 def test_finalize(self):
86 finalized = False
87
88 class MySink(bt2._UserSinkComponent):
819d0ae7 89 def _user_consume(self):
8a08af82
SM
90 pass
91
819d0ae7 92 def _user_finalize(comp_self):
f6a5e476
PP
93 nonlocal finalized
94 finalized = True
95
96 graph = bt2.Graph()
bc5c9924 97 comp = graph.add_component(MySink, 'lel')
e34fb94a 98
f6a5e476
PP
99 del graph
100 del comp
101 self.assertTrue(finalized)
102
e3250e61
SM
103 def test_source_component_config(self):
104 class MySource(
105 bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator
106 ):
107 def __init__(comp_self, config, params, obj):
108 nonlocal cfg_type
109 cfg_type = type(config)
110
111 cfg_type = None
112 self._create_comp(MySource)
113 self.assertIs(cfg_type, bt2_component._UserSourceComponentConfiguration)
114
115 def test_filter_component_config(self):
116 class MyFilter(
117 bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator
118 ):
119 def __init__(comp_self, config, params, obj):
120 nonlocal cfg_type
121 cfg_type = type(config)
122
123 cfg_type = None
124 self._create_comp(MyFilter)
125 self.assertIs(cfg_type, bt2_component._UserFilterComponentConfiguration)
126
127 def test_sink_component_config(self):
128 class MySink(bt2._UserSinkComponent):
129 def __init__(comp_self, config, params, obj):
130 nonlocal cfg_type
131 cfg_type = type(config)
132
133 def _user_consume(self):
134 pass
135
136 cfg_type = None
137 self._create_comp(MySink)
138 self.assertIs(cfg_type, bt2_component._UserSinkComponentConfiguration)
139
f6a5e476
PP
140
141class GenericComponentTestCase(unittest.TestCase):
142 @staticmethod
9bc3e785 143 def _create_comp(comp_cls, name=None, log_level=bt2.LoggingLevel.NONE):
f6a5e476
PP
144 graph = bt2.Graph()
145
146 if name is None:
147 name = 'comp'
148
9bc3e785 149 return graph.add_component(comp_cls, name, logging_level=log_level)
f6a5e476
PP
150
151 def test_name(self):
152 class MySink(bt2._UserSinkComponent):
819d0ae7 153 def _user_consume(self):
8a08af82
SM
154 pass
155
f6a5e476
PP
156 comp = self._create_comp(MySink, 'yaes')
157 self.assertEqual(comp.name, 'yaes')
158
9bc3e785
PP
159 def test_logging_level(self):
160 class MySink(bt2._UserSinkComponent):
819d0ae7 161 def _user_consume(self):
8a08af82
SM
162 pass
163
e9d0e821
PP
164 comp = self._create_comp(MySink, 'yaes', bt2.LoggingLevel.WARNING)
165 self.assertEqual(comp.logging_level, bt2.LoggingLevel.WARNING)
9bc3e785 166
f6a5e476
PP
167 def test_class(self):
168 class MySink(bt2._UserSinkComponent):
819d0ae7 169 def _user_consume(self):
8a08af82
SM
170 pass
171
f6a5e476 172 comp = self._create_comp(MySink)
c88be1c8 173 self.assertEqual(comp.cls, MySink)
f6a5e476
PP
174
175 def test_addr(self):
176 class MySink(bt2._UserSinkComponent):
819d0ae7 177 def _user_consume(self):
8a08af82
SM
178 pass
179
f6a5e476
PP
180 comp = self._create_comp(MySink)
181 self.assertIsInstance(comp.addr, int)
182 self.assertNotEqual(comp.addr, 0)
3db06b1d
SM
183
184
185if __name__ == '__main__':
186 unittest.main()
This page took 0.047188 seconds and 4 git commands to generate.