File: test_all_mip_versions.py

package info (click to toggle)
babeltrace2 2.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,660 kB
  • sloc: cpp: 106,162; ansic: 78,276; python: 27,115; sh: 9,053; makefile: 1,807; xml: 46
file content (59 lines) | stat: -rw-r--r-- 1,880 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# SPDX-License-Identifier: GPL-2.0-only
#
# Copyright (C) 2024 EfficiOS, Inc.
#


# Decorator for unittest.TestCast sub-classes to run tests once for
# each existing MIP versions.
#
# Replaces all test_* methods with a test_*_mip_0 and a test_*_mip_1 variant.
#
# For instance, it transforms this:
#
#   @test_all_mip_versions
#   class MyTestCase(unittest.TestCase):
#       test_something(self):
#           pass
#
# into:
#
#   class MyTestcase(unittest.TestCase):
#       test_something_mip_0(self):
#           pass
#
#       test_something_mip_1(self):
#           pass
#
# The test methods are wrapped such that the self._mip_version attribute is
# set to either 0 or 1 during the call to each method.
def test_all_mip_versions(cls: type):
    for attr_name, attr_value in list(cls.__dict__.items()):
        if not attr_name.startswith("test_") or not callable(attr_value):
            continue

        for mip_version in 0, 1:
            # Callable that wraps and replaces test methods in order to
            # temporarily set the _mip_version attribute on the TestCase class.
            def set_mip_version_wrapper_method(self, mip_version, test_method):
                assert not hasattr(self, "_mip_version")
                self._mip_version = mip_version

                try:
                    return test_method(self)
                finally:
                    assert hasattr(self, "_mip_version")
                    del self._mip_version

            def wrap_method(wrapper_method, mip_version, original_method):
                return lambda self: wrapper_method(self, mip_version, original_method)

            setattr(
                cls,
                "{}_mip_{}".format(attr_name, mip_version),
                wrap_method(set_mip_version_wrapper_method, mip_version, attr_value),
            )

        delattr(cls, attr_name)

    return cls