|
| 1 | +import os |
| 2 | +class raises(): |
| 3 | + def __init__(self, err): |
| 4 | + self.err = err |
| 5 | + |
| 6 | + def __enter__(self): |
| 7 | + pass |
| 8 | + |
| 9 | + def __exit__(self, *args): |
| 10 | + if len(args) < 2 or not isinstance(args[1], self.err): |
| 11 | + raise ValueError("Did not raise") |
| 12 | + return True |
| 13 | + |
| 14 | + |
| 15 | +def istestfunc(func): |
| 16 | + return ( |
| 17 | + hasattr(func, "__call__") |
| 18 | + and getattr(func, "__name__", "<lambda>") != "<lambda>" |
| 19 | + ) |
| 20 | + |
| 21 | +# import attr |
| 22 | + |
| 23 | +# @attr.s(frozen=True) |
| 24 | +class Mark(object): |
| 25 | + |
| 26 | + def __init__(self, name, args, kwargs): |
| 27 | + self.name = name |
| 28 | + self.args = args |
| 29 | + self.kwargs = kwargs |
| 30 | + |
| 31 | + def combined_with(self, other): |
| 32 | + """ |
| 33 | + :param other: the mark to combine with |
| 34 | + :type other: Mark |
| 35 | + :rtype: Mark |
| 36 | +
|
| 37 | + combines by appending args and merging the mappings |
| 38 | + """ |
| 39 | + assert self.name == other.name |
| 40 | + return Mark( |
| 41 | + self.name, self.args + other.args, dict(self.kwargs, **other.kwargs) |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +# @attr.s |
| 46 | +class MarkDecorator(object): |
| 47 | +# mark = attr.ib(validator=attr.validators.instance_of(Mark)) |
| 48 | + |
| 49 | +# name = alias("mark.name") |
| 50 | +# args = alias("mark.args") |
| 51 | +# kwargs = alias("mark.kwargs") |
| 52 | + def __init__(self, mark): |
| 53 | + self.mark = mark |
| 54 | + self.name = mark.name |
| 55 | + self.args = mark.args |
| 56 | + self.kwargs = mark.kwargs |
| 57 | + |
| 58 | + @property |
| 59 | + def markname(self): |
| 60 | + return self.name # for backward-compat (2.4.1 had this attr) |
| 61 | + |
| 62 | + def __eq__(self, other): |
| 63 | + return self.mark == other.mark if isinstance(other, MarkDecorator) else False |
| 64 | + |
| 65 | + def __repr__(self): |
| 66 | + return "<MarkDecorator %r>" % (self.mark,) |
| 67 | + |
| 68 | + def with_args(self, *args, **kwargs): |
| 69 | + """ return a MarkDecorator with extra arguments added |
| 70 | +
|
| 71 | + unlike call this can be used even if the sole argument is a callable/class |
| 72 | +
|
| 73 | + :return: MarkDecorator |
| 74 | + """ |
| 75 | + |
| 76 | + mark = Mark(self.name, args, kwargs) |
| 77 | + return self.__class__(self.mark.combined_with(mark)) |
| 78 | + |
| 79 | + def __call__(self, *args, **kwargs): |
| 80 | + """ if passed a single callable argument: decorate it with mark info. |
| 81 | + otherwise add *args/**kwargs in-place to mark information. """ |
| 82 | + if args and not kwargs: |
| 83 | + func = args[0] |
| 84 | + is_class = False |
| 85 | + if len(args) == 1 and (istestfunc(func) or is_class): |
| 86 | + return func |
| 87 | + return self.with_args(*args, **kwargs) |
| 88 | + |
| 89 | +class MarkGenerator(object): |
| 90 | + _config = None |
| 91 | + _markers = set() |
| 92 | + |
| 93 | + def __getattr__(self, name): |
| 94 | + if name[0] == "_": |
| 95 | + raise AttributeError("Marker name must NOT start with underscore") |
| 96 | + return MarkDecorator(Mark(name, (), {})) |
| 97 | + |
| 98 | +mark = MarkGenerator() |
0 commit comments