Commit | Line | Data |
---|---|---|
0235b0db | 1 | # SPDX-License-Identifier: GPL-2.0-only |
d2d857a8 MJ |
2 | # |
3 | # Copyright (C) 2019 EfficiOS Inc. | |
4 | # | |
d2d857a8 | 5 | |
811644b8 | 6 | import unittest |
5995b304 | 7 | |
811644b8 | 8 | import bt2 |
59225a3e | 9 | from bt2 import component as bt2_component |
811644b8 PP |
10 | |
11 | ||
12 | class UserComponentTestCase(unittest.TestCase): | |
13 | @staticmethod | |
e6090a23 | 14 | def _create_comp(comp_cls, name=None, log_level=bt2.LoggingLevel.NONE): |
811644b8 PP |
15 | graph = bt2.Graph() |
16 | ||
17 | if name is None: | |
f5567ea8 | 18 | name = "comp" |
811644b8 | 19 | |
e6090a23 | 20 | return graph.add_component(comp_cls, name, logging_level=log_level) |
811644b8 PP |
21 | |
22 | def test_name(self): | |
23 | class MySink(bt2._UserSinkComponent): | |
59225a3e | 24 | def __init__(comp_self, config, params, obj): |
f5567ea8 | 25 | self.assertEqual(comp_self.name, "yaes") |
811644b8 | 26 | |
6a91742b | 27 | def _user_consume(self): |
a01b452b SM |
28 | pass |
29 | ||
f5567ea8 | 30 | self._create_comp(MySink, "yaes") |
811644b8 | 31 | |
e6090a23 PP |
32 | def test_logging_level(self): |
33 | class MySink(bt2._UserSinkComponent): | |
59225a3e | 34 | def __init__(comp_self, config, params, obj): |
e6090a23 PP |
35 | self.assertEqual(comp_self.logging_level, bt2.LoggingLevel.INFO) |
36 | ||
6a91742b | 37 | def _user_consume(self): |
a01b452b SM |
38 | pass |
39 | ||
f5567ea8 | 40 | self._create_comp(MySink, "yaes", bt2.LoggingLevel.INFO) |
e6090a23 | 41 | |
056deb59 PP |
42 | def test_graph_mip_version(self): |
43 | class MySink(bt2._UserSinkComponent): | |
59225a3e | 44 | def __init__(comp_self, config, params, obj): |
056deb59 PP |
45 | self.assertEqual(comp_self._graph_mip_version, 0) |
46 | ||
47 | def _user_consume(self): | |
48 | pass | |
49 | ||
f5567ea8 | 50 | self._create_comp(MySink, "yaes", bt2.LoggingLevel.INFO) |
056deb59 | 51 | |
811644b8 PP |
52 | def test_class(self): |
53 | class MySink(bt2._UserSinkComponent): | |
59225a3e | 54 | def __init__(comp_self, config, params, obj): |
e8ac1aae | 55 | self.assertEqual(comp_self.cls, MySink) |
811644b8 | 56 | |
6a91742b | 57 | def _user_consume(self): |
a01b452b SM |
58 | pass |
59 | ||
811644b8 PP |
60 | self._create_comp(MySink) |
61 | ||
62 | def test_addr(self): | |
63 | class MySink(bt2._UserSinkComponent): | |
59225a3e | 64 | def __init__(comp_self, config, params, obj): |
811644b8 PP |
65 | self.assertIsInstance(comp_self.addr, int) |
66 | self.assertNotEqual(comp_self.addr, 0) | |
67 | ||
6a91742b | 68 | def _user_consume(self): |
a01b452b SM |
69 | pass |
70 | ||
811644b8 PP |
71 | self._create_comp(MySink) |
72 | ||
73 | def test_finalize(self): | |
74 | finalized = False | |
75 | ||
76 | class MySink(bt2._UserSinkComponent): | |
6a91742b | 77 | def _user_consume(self): |
a01b452b SM |
78 | pass |
79 | ||
6a91742b | 80 | def _user_finalize(comp_self): |
811644b8 PP |
81 | nonlocal finalized |
82 | finalized = True | |
83 | ||
84 | graph = bt2.Graph() | |
f5567ea8 | 85 | comp = graph.add_component(MySink, "lel") |
1c9ed2ff | 86 | |
811644b8 PP |
87 | del graph |
88 | del comp | |
89 | self.assertTrue(finalized) | |
90 | ||
59225a3e SM |
91 | def test_source_component_config(self): |
92 | class MySource( | |
93 | bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator | |
94 | ): | |
95 | def __init__(comp_self, config, params, obj): | |
96 | nonlocal cfg_type | |
97 | cfg_type = type(config) | |
98 | ||
99 | cfg_type = None | |
100 | self._create_comp(MySource) | |
101 | self.assertIs(cfg_type, bt2_component._UserSourceComponentConfiguration) | |
102 | ||
103 | def test_filter_component_config(self): | |
104 | class MyFilter( | |
105 | bt2._UserFilterComponent, 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(MyFilter) | |
113 | self.assertIs(cfg_type, bt2_component._UserFilterComponentConfiguration) | |
114 | ||
115 | def test_sink_component_config(self): | |
116 | class MySink(bt2._UserSinkComponent): | |
117 | def __init__(comp_self, config, params, obj): | |
118 | nonlocal cfg_type | |
119 | cfg_type = type(config) | |
120 | ||
121 | def _user_consume(self): | |
122 | pass | |
123 | ||
124 | cfg_type = None | |
125 | self._create_comp(MySink) | |
126 | self.assertIs(cfg_type, bt2_component._UserSinkComponentConfiguration) | |
127 | ||
811644b8 PP |
128 | |
129 | class GenericComponentTestCase(unittest.TestCase): | |
130 | @staticmethod | |
e6090a23 | 131 | def _create_comp(comp_cls, name=None, log_level=bt2.LoggingLevel.NONE): |
811644b8 PP |
132 | graph = bt2.Graph() |
133 | ||
134 | if name is None: | |
f5567ea8 | 135 | name = "comp" |
811644b8 | 136 | |
e6090a23 | 137 | return graph.add_component(comp_cls, name, logging_level=log_level) |
811644b8 PP |
138 | |
139 | def test_name(self): | |
140 | class MySink(bt2._UserSinkComponent): | |
6a91742b | 141 | def _user_consume(self): |
a01b452b SM |
142 | pass |
143 | ||
f5567ea8 FD |
144 | comp = self._create_comp(MySink, "yaes") |
145 | self.assertEqual(comp.name, "yaes") | |
811644b8 | 146 | |
e6090a23 PP |
147 | def test_logging_level(self): |
148 | class MySink(bt2._UserSinkComponent): | |
6a91742b | 149 | def _user_consume(self): |
a01b452b SM |
150 | pass |
151 | ||
f5567ea8 | 152 | comp = self._create_comp(MySink, "yaes", bt2.LoggingLevel.WARNING) |
770538dd | 153 | self.assertEqual(comp.logging_level, bt2.LoggingLevel.WARNING) |
e6090a23 | 154 | |
811644b8 PP |
155 | def test_class(self): |
156 | class MySink(bt2._UserSinkComponent): | |
6a91742b | 157 | def _user_consume(self): |
a01b452b SM |
158 | pass |
159 | ||
811644b8 | 160 | comp = self._create_comp(MySink) |
e8ac1aae | 161 | self.assertEqual(comp.cls, MySink) |
811644b8 PP |
162 | |
163 | def test_addr(self): | |
164 | class MySink(bt2._UserSinkComponent): | |
6a91742b | 165 | def _user_consume(self): |
a01b452b SM |
166 | pass |
167 | ||
811644b8 PP |
168 | comp = self._create_comp(MySink) |
169 | self.assertIsInstance(comp.addr, int) | |
170 | self.assertNotEqual(comp.addr, 0) | |
d14ddbba SM |
171 | |
172 | ||
f5567ea8 | 173 | if __name__ == "__main__": |
d14ddbba | 174 | unittest.main() |