aboutsummaryrefslogtreecommitdiff
path: root/src/suite/decorator.py
blob: e9f9efa2e2d228388393b0b0fd4af3da9d26c3b6 (plain)
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
# ############################################################################ #
#                                                                              #
#                                                         :::      ::::::::    #
#    decorator.py                                       :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: charles <me@cacharle.xyz>                  +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2020/09/11 12:28:00 by charles           #+#    #+#              #
#    Updated: 2021/01/31 02:13:40 by charles          ###   ########.fr        #
#                                                                              #
# ############################################################################ #

from suite import Suite
from test import Test
import inspect


def suite(groups: list[str] = [], bonus: bool = False):
    """Decorator generator for suites arguments"""

    def suite_wrapper(origin):
        """Decorator for a suite function (fmt: suite_[name]) """

        mod_name = inspect.getmodule(origin).__name__[len("suites."):]
        name = "{}/{}".format(mod_name, origin.__name__[len("suite_"):])
        description = origin.__doc__
        if description is None:
            print("You should had a doc string to the {} suite".format(name))
            description = "no description"
        description = description.split("\n")[0].strip()
        s = Suite(name, groups + [mod_name], bonus, description)

        def test_generator():
            def test(*args, **kwargs):
                s.add(Test(*args, **kwargs))
            origin(test)

        s.generator_func = test_generator
        Suite.available.append(s)
        return test_generator

    return suite_wrapper