aboutsummaryrefslogtreecommitdiff
path: root/minishell_test/suite/decorator.py
blob: 7cfd29c64b55f1e517a7c9788173cae1145adc05 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# ############################################################################ #
#                                                                              #
#                                                         :::      ::::::::    #
#    decorator.py                                       :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: charles <me@cacharle.xyz>                  +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2020/09/11 12:28:00 by charles           #+#    #+#              #
#    Updated: 2021/03/06 11:31:33 by cacharle         ###   ########.fr        #
#                                                                              #
# ############################################################################ #

import inspect
from typing import List

from minishell_test.suite import Suite
from minishell_test.test import Test


class SuiteRegistrationException(Exception):
    def __init__(self, function_name: str, message: str):
        self._function_name = function_name
        self._message = message

    def __str__(self) -> str:
        return "Error during the registration of {self._function_name} as a suite: {self._message}"


_SUITE_FUNCTION_PREFIX = "suite_"


def suite(bonus: bool = False):  # type: ignore
    """Decorator generator for suites arguments"""

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

        # get the function name
        function_name = origin.__name__
        if not function_name.startswith(_SUITE_FUNCTION_PREFIX):
            raise SuiteRegistrationException(function_name, f"Function need to start with {_SUITE_FUNCTION_PREFIX}")
        function_name = function_name[len(_SUITE_FUNCTION_PREFIX):]
        # get the module name
        module = inspect.getmodule(origin)
        if module is None:
            raise SuiteRegistrationException(function_name, "Could not get function module")
        module_name = module.__name__[len("minishell_test.suites."):]
        # get the first line of the function docstring as the suite description
        description = origin.__doc__
        if description is None:
            warnings.warn(f"You should had a doc string to the {name} suite")
            description = "no description"
        description = description.splitlines()[0].strip()

        suite = Suite(origin, function_name, module_name, bonus, description)
        Suite._available.append(suite)

        # def test_generator():
        #     def test(*args, **kwargs):
        #         suite.append_test(Test(*args, **kwargs))
        #     origin(test)
        # suite.generator_func = test_generator
        return origin

    return suite_wrapper