summaryrefslogtreecommitdiffstats
path: root/BaseTools/Plugin/DebugMacroCheck/tests/MacroTest.py
blob: 3b966d31ffaadc401e353b77d87012fbc74984dd (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# @file MacroTest.py
#
# Contains the data classes that are used to compose debug macro tests.
#
# All data classes inherit from a single abstract base class that expects
# the subclass to define the category of test it represents.
#
# Copyright (c) Microsoft Corporation. All rights reserved.
# SPDX-License-Identifier: BSD-2-Clause-Patent
##

from dataclasses import dataclass, field
from os import linesep
from typing import Tuple

import abc


@dataclass(frozen=True)
class MacroTest(abc.ABC):
    """Abstract base class for an individual macro test case."""

    macro: str
    result: Tuple[int, int, int]
    description: str = field(default='')

    @property
    @abc.abstractmethod
    def category(self) -> str:
        """Returns the test class category identifier.

        Example: 'equal_specifier_equal_argument_macro_test'

        This string is used to bind test objects against this class.

        Returns:
            str: Test category identifier string.
        """
        pass

    @property
    def category_description(self) -> str:
        """Returns the test class category description.

        Example: 'Test case with equal count of print specifiers to arguments.'

        This string is a human readable description of the test category.

        Returns:
            str: String describing the test category.
        """
        return self.__doc__

    def __str__(self):
        """Returns a macro test case description string."""

        s = [
            f"{linesep}",
            "=" * 80,
            f"Macro Test Type:  {self.category_description}",
            f"{linesep}Macro:            {self.macro}",
            f"{linesep}Expected Result:  {self.result}"
        ]

        if self.description:
            s.insert(3, f"Test Description: {self.description}")

        return f'{linesep}'.join(s)


@dataclass(frozen=True)
class NoSpecifierNoArgumentMacroTest(MacroTest):
    """Test case with no print specifier and no arguments."""

    @property
    def category(self) -> str:
        return "no_specifier_no_argument_macro_test"


@dataclass(frozen=True)
class EqualSpecifierEqualArgumentMacroTest(MacroTest):
    """Test case with equal count of print specifiers to arguments."""

    @property
    def category(self) -> str:
        return "equal_specifier_equal_argument_macro_test"


@dataclass(frozen=True)
class MoreSpecifiersThanArgumentsMacroTest(MacroTest):
    """Test case with more print specifiers than arguments."""

    @property
    def category(self) -> str:
        return "more_specifiers_than_arguments_macro_test"


@dataclass(frozen=True)
class LessSpecifiersThanArgumentsMacroTest(MacroTest):
    """Test case with less print specifiers than arguments."""

    @property
    def category(self) -> str:
        return "less_specifiers_than_arguments_macro_test"


@dataclass(frozen=True)
class IgnoredSpecifiersMacroTest(MacroTest):
    """Test case to test ignored print specifiers."""

    @property
    def category(self) -> str:
        return "ignored_specifiers_macro_test"


@dataclass(frozen=True)
class SpecialParsingMacroTest(MacroTest):
    """Test case with special (complicated) parsing scenarios."""

    @property
    def category(self) -> str:
        return "special_parsing_macro_test"


@dataclass(frozen=True)
class CodeSnippetMacroTest(MacroTest):
    """Test case within a larger code snippet."""

    @property
    def category(self) -> str:
        return "code_snippet_macro_test"