File: helper.py

package info (click to toggle)
anytree 2.12.1-3.1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 872 kB
  • sloc: python: 4,044; makefile: 12
file content (49 lines) | stat: -rw-r--r-- 1,265 bytes parent folder | download
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
"""Helper Methods for testing."""
from contextlib import contextmanager

import six


def eq_(one, other):
    assert one == other, "{one} != {other}".format(one=one, other=other)


# hack own assert_raises, because py26 has a different impelmentation
@contextmanager
def assert_raises(exccls, msg):
    """Check exception of class `exccls` to be raised with message `msg`."""
    try:
        yield
        assert False, "%r not raised" % exccls
    except Exception as exc:
        assert isinstance(exc, exccls), "%r is not a %r" % (exc, exccls)
        eq_(str(exc), msg)


def with_setup(setup=None, teardown=None):
    def decorate(func, setup=setup, teardown=teardown):
        if setup:
            if hasattr(func, "setup"):
                _old_s = func.setup

                def _s():
                    setup()
                    _old_s()

                func.setup = _s
            else:
                func.setup = setup
        if teardown:
            if hasattr(func, "teardown"):
                _old_t = func.teardown

                def _t():
                    _old_t()
                    teardown()

                func.teardown = _t
            else:
                func.teardown = teardown
        return func

    return decorate