bt2: pass custom Python object to Python component's __init__()
[babeltrace.git] / tests / bindings / python / bt2 / test_component.py
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
19 import unittest
20 import copy
21 import bt2
22
23
24 class UserComponentTestCase(unittest.TestCase):
25 @staticmethod
26 def _create_comp(comp_cls, name=None, log_level=bt2.LoggingLevel.NONE):
27 graph = bt2.Graph()
28
29 if name is None:
30 name = 'comp'
31
32 return graph.add_component(comp_cls, name, logging_level=log_level)
33
34 def test_name(self):
35 class MySink(bt2._UserSinkComponent):
36 def __init__(comp_self, params, obj):
37 self.assertEqual(comp_self.name, 'yaes')
38
39 def _user_consume(self):
40 pass
41
42 comp = self._create_comp(MySink, 'yaes')
43
44 def test_logging_level(self):
45 class MySink(bt2._UserSinkComponent):
46 def __init__(comp_self, params, obj):
47 self.assertEqual(comp_self.logging_level, bt2.LoggingLevel.INFO)
48
49 def _user_consume(self):
50 pass
51
52 comp = self._create_comp(MySink, 'yaes', bt2.LoggingLevel.INFO)
53
54 def test_class(self):
55 class MySink(bt2._UserSinkComponent):
56 def __init__(comp_self, params, obj):
57 self.assertEqual(comp_self.cls, MySink)
58
59 def _user_consume(self):
60 pass
61
62 self._create_comp(MySink)
63
64 def test_addr(self):
65 class MySink(bt2._UserSinkComponent):
66 def __init__(comp_self, params, obj):
67 self.assertIsInstance(comp_self.addr, int)
68 self.assertNotEqual(comp_self.addr, 0)
69
70 def _user_consume(self):
71 pass
72
73 self._create_comp(MySink)
74
75 def test_finalize(self):
76 finalized = False
77
78 class MySink(bt2._UserSinkComponent):
79 def _user_consume(self):
80 pass
81
82 def _user_finalize(comp_self):
83 nonlocal finalized
84 finalized = True
85
86 graph = bt2.Graph()
87 comp = graph.add_component(MySink, 'lel')
88
89 del graph
90 del comp
91 self.assertTrue(finalized)
92
93
94 class GenericComponentTestCase(unittest.TestCase):
95 @staticmethod
96 def _create_comp(comp_cls, name=None, log_level=bt2.LoggingLevel.NONE):
97 graph = bt2.Graph()
98
99 if name is None:
100 name = 'comp'
101
102 return graph.add_component(comp_cls, name, logging_level=log_level)
103
104 def test_name(self):
105 class MySink(bt2._UserSinkComponent):
106 def _user_consume(self):
107 pass
108
109 comp = self._create_comp(MySink, 'yaes')
110 self.assertEqual(comp.name, 'yaes')
111
112 def test_logging_level(self):
113 class MySink(bt2._UserSinkComponent):
114 def _user_consume(self):
115 pass
116
117 comp = self._create_comp(MySink, 'yaes', bt2.LoggingLevel.WARNING)
118 self.assertEqual(comp.logging_level, bt2.LoggingLevel.WARNING)
119
120 def test_class(self):
121 class MySink(bt2._UserSinkComponent):
122 def _user_consume(self):
123 pass
124
125 comp = self._create_comp(MySink)
126 self.assertEqual(comp.cls, MySink)
127
128 def test_addr(self):
129 class MySink(bt2._UserSinkComponent):
130 def _user_consume(self):
131 pass
132
133 comp = self._create_comp(MySink)
134 self.assertIsInstance(comp.addr, int)
135 self.assertNotEqual(comp.addr, 0)
This page took 0.033148 seconds and 5 git commands to generate.