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