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