Commit | Line | Data |
---|---|---|
d2d857a8 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 | ||
811644b8 | 19 | import unittest |
811644b8 | 20 | import bt2 |
59225a3e | 21 | from bt2 import component as bt2_component |
811644b8 PP |
22 | |
23 | ||
24 | class UserComponentTestCase(unittest.TestCase): | |
25 | @staticmethod | |
e6090a23 | 26 | def _create_comp(comp_cls, name=None, log_level=bt2.LoggingLevel.NONE): |
811644b8 PP |
27 | graph = bt2.Graph() |
28 | ||
29 | if name is None: | |
30 | name = 'comp' | |
31 | ||
e6090a23 | 32 | return graph.add_component(comp_cls, name, logging_level=log_level) |
811644b8 PP |
33 | |
34 | def test_name(self): | |
35 | class MySink(bt2._UserSinkComponent): | |
59225a3e | 36 | def __init__(comp_self, config, params, obj): |
811644b8 PP |
37 | self.assertEqual(comp_self.name, 'yaes') |
38 | ||
6a91742b | 39 | def _user_consume(self): |
a01b452b SM |
40 | pass |
41 | ||
082db648 | 42 | self._create_comp(MySink, 'yaes') |
811644b8 | 43 | |
e6090a23 PP |
44 | def test_logging_level(self): |
45 | class MySink(bt2._UserSinkComponent): | |
59225a3e | 46 | def __init__(comp_self, config, params, obj): |
e6090a23 PP |
47 | self.assertEqual(comp_self.logging_level, bt2.LoggingLevel.INFO) |
48 | ||
6a91742b | 49 | def _user_consume(self): |
a01b452b SM |
50 | pass |
51 | ||
082db648 | 52 | self._create_comp(MySink, 'yaes', bt2.LoggingLevel.INFO) |
e6090a23 | 53 | |
056deb59 PP |
54 | def test_graph_mip_version(self): |
55 | class MySink(bt2._UserSinkComponent): | |
59225a3e | 56 | def __init__(comp_self, config, params, obj): |
056deb59 PP |
57 | self.assertEqual(comp_self._graph_mip_version, 0) |
58 | ||
59 | def _user_consume(self): | |
60 | pass | |
61 | ||
082db648 | 62 | self._create_comp(MySink, 'yaes', bt2.LoggingLevel.INFO) |
056deb59 | 63 | |
811644b8 PP |
64 | def test_class(self): |
65 | class MySink(bt2._UserSinkComponent): | |
59225a3e | 66 | def __init__(comp_self, config, params, obj): |
e8ac1aae | 67 | self.assertEqual(comp_self.cls, MySink) |
811644b8 | 68 | |
6a91742b | 69 | def _user_consume(self): |
a01b452b SM |
70 | pass |
71 | ||
811644b8 PP |
72 | self._create_comp(MySink) |
73 | ||
74 | def test_addr(self): | |
75 | class MySink(bt2._UserSinkComponent): | |
59225a3e | 76 | def __init__(comp_self, config, params, obj): |
811644b8 PP |
77 | self.assertIsInstance(comp_self.addr, int) |
78 | self.assertNotEqual(comp_self.addr, 0) | |
79 | ||
6a91742b | 80 | def _user_consume(self): |
a01b452b SM |
81 | pass |
82 | ||
811644b8 PP |
83 | self._create_comp(MySink) |
84 | ||
85 | def test_finalize(self): | |
86 | finalized = False | |
87 | ||
88 | class MySink(bt2._UserSinkComponent): | |
6a91742b | 89 | def _user_consume(self): |
a01b452b SM |
90 | pass |
91 | ||
6a91742b | 92 | def _user_finalize(comp_self): |
811644b8 PP |
93 | nonlocal finalized |
94 | finalized = True | |
95 | ||
96 | graph = bt2.Graph() | |
894a8df5 | 97 | comp = graph.add_component(MySink, 'lel') |
1c9ed2ff | 98 | |
811644b8 PP |
99 | del graph |
100 | del comp | |
101 | self.assertTrue(finalized) | |
102 | ||
59225a3e 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 | ||
811644b8 PP |
140 | |
141 | class GenericComponentTestCase(unittest.TestCase): | |
142 | @staticmethod | |
e6090a23 | 143 | def _create_comp(comp_cls, name=None, log_level=bt2.LoggingLevel.NONE): |
811644b8 PP |
144 | graph = bt2.Graph() |
145 | ||
146 | if name is None: | |
147 | name = 'comp' | |
148 | ||
e6090a23 | 149 | return graph.add_component(comp_cls, name, logging_level=log_level) |
811644b8 PP |
150 | |
151 | def test_name(self): | |
152 | class MySink(bt2._UserSinkComponent): | |
6a91742b | 153 | def _user_consume(self): |
a01b452b SM |
154 | pass |
155 | ||
811644b8 PP |
156 | comp = self._create_comp(MySink, 'yaes') |
157 | self.assertEqual(comp.name, 'yaes') | |
158 | ||
e6090a23 PP |
159 | def test_logging_level(self): |
160 | class MySink(bt2._UserSinkComponent): | |
6a91742b | 161 | def _user_consume(self): |
a01b452b SM |
162 | pass |
163 | ||
770538dd PP |
164 | comp = self._create_comp(MySink, 'yaes', bt2.LoggingLevel.WARNING) |
165 | self.assertEqual(comp.logging_level, bt2.LoggingLevel.WARNING) | |
e6090a23 | 166 | |
811644b8 PP |
167 | def test_class(self): |
168 | class MySink(bt2._UserSinkComponent): | |
6a91742b | 169 | def _user_consume(self): |
a01b452b SM |
170 | pass |
171 | ||
811644b8 | 172 | comp = self._create_comp(MySink) |
e8ac1aae | 173 | self.assertEqual(comp.cls, MySink) |
811644b8 PP |
174 | |
175 | def test_addr(self): | |
176 | class MySink(bt2._UserSinkComponent): | |
6a91742b | 177 | def _user_consume(self): |
a01b452b SM |
178 | pass |
179 | ||
811644b8 PP |
180 | comp = self._create_comp(MySink) |
181 | self.assertIsInstance(comp.addr, int) | |
182 | self.assertNotEqual(comp.addr, 0) | |
d14ddbba SM |
183 | |
184 | ||
185 | if __name__ == '__main__': | |
186 | unittest.main() |