| 1 | # SPDX-License-Identifier: BSD-2-Clause |
| 2 | # |
| 3 | # Copyright (c) 2016, Matt Layman |
| 4 | |
| 5 | |
| 6 | class Line(object): |
| 7 | """Base type for TAP data. |
| 8 | |
| 9 | TAP is a line based protocol. Thus, the most primitive type is a line. |
| 10 | """ |
| 11 | |
| 12 | @property |
| 13 | def category(self): |
| 14 | raise NotImplementedError |
| 15 | |
| 16 | |
| 17 | class Result(Line): |
| 18 | """Information about an individual test line.""" |
| 19 | |
| 20 | def __init__( |
| 21 | self, ok, number=None, description='', directive=None, diagnostics=None |
| 22 | ): |
| 23 | self._ok = ok |
| 24 | if number: |
| 25 | self._number = int(number) |
| 26 | else: |
| 27 | # The number may be an empty string so explicitly set to None. |
| 28 | self._number = None |
| 29 | self._description = description |
| 30 | self.directive = directive |
| 31 | self.diagnostics = diagnostics |
| 32 | |
| 33 | @property |
| 34 | def category(self): |
| 35 | """:returns: ``test``""" |
| 36 | return 'test' |
| 37 | |
| 38 | @property |
| 39 | def ok(self): |
| 40 | """Get the ok status. |
| 41 | |
| 42 | :rtype: bool |
| 43 | """ |
| 44 | return self._ok |
| 45 | |
| 46 | @property |
| 47 | def number(self): |
| 48 | """Get the test number. |
| 49 | |
| 50 | :rtype: int |
| 51 | """ |
| 52 | return self._number |
| 53 | |
| 54 | @property |
| 55 | def description(self): |
| 56 | """Get the description.""" |
| 57 | return self._description |
| 58 | |
| 59 | @property |
| 60 | def skip(self): |
| 61 | """Check if this test was skipped. |
| 62 | |
| 63 | :rtype: bool |
| 64 | """ |
| 65 | return self.directive.skip |
| 66 | |
| 67 | @property |
| 68 | def todo(self): |
| 69 | """Check if this test was a TODO. |
| 70 | |
| 71 | :rtype: bool |
| 72 | """ |
| 73 | return self.directive.todo |
| 74 | |
| 75 | def __str__(self): |
| 76 | is_not = '' |
| 77 | if not self.ok: |
| 78 | is_not = 'not ' |
| 79 | directive = '' |
| 80 | if self.directive is not None and self.directive.text: |
| 81 | directive = ' # {0}'.format(self.directive.text) |
| 82 | diagnostics = '' |
| 83 | if self.diagnostics is not None: |
| 84 | diagnostics = '\n' + self.diagnostics.rstrip() |
| 85 | return "{0}ok {1} - {2}{3}{4}".format( |
| 86 | is_not, self.number, self.description, directive, diagnostics |
| 87 | ) |
| 88 | |
| 89 | |
| 90 | class Plan(Line): |
| 91 | """A plan line to indicate how many tests to expect.""" |
| 92 | |
| 93 | def __init__(self, expected_tests, directive=None): |
| 94 | self._expected_tests = expected_tests |
| 95 | self.directive = directive |
| 96 | |
| 97 | @property |
| 98 | def category(self): |
| 99 | """:returns: ``plan``""" |
| 100 | return 'plan' |
| 101 | |
| 102 | @property |
| 103 | def expected_tests(self): |
| 104 | """Get the number of expected tests. |
| 105 | |
| 106 | :rtype: int |
| 107 | """ |
| 108 | return self._expected_tests |
| 109 | |
| 110 | @property |
| 111 | def skip(self): |
| 112 | """Check if this plan should skip the file. |
| 113 | |
| 114 | :rtype: bool |
| 115 | """ |
| 116 | return self.directive.skip |
| 117 | |
| 118 | |
| 119 | class Diagnostic(Line): |
| 120 | """A diagnostic line (i.e. anything starting with a hash).""" |
| 121 | |
| 122 | def __init__(self, text): |
| 123 | self._text = text |
| 124 | |
| 125 | @property |
| 126 | def category(self): |
| 127 | """:returns: ``diagnostic``""" |
| 128 | return 'diagnostic' |
| 129 | |
| 130 | @property |
| 131 | def text(self): |
| 132 | """Get the text.""" |
| 133 | return self._text |
| 134 | |
| 135 | |
| 136 | class Bail(Line): |
| 137 | """A bail out line (i.e. anything starting with 'Bail out!').""" |
| 138 | |
| 139 | def __init__(self, reason): |
| 140 | self._reason = reason |
| 141 | |
| 142 | @property |
| 143 | def category(self): |
| 144 | """:returns: ``bail``""" |
| 145 | return 'bail' |
| 146 | |
| 147 | @property |
| 148 | def reason(self): |
| 149 | """Get the reason.""" |
| 150 | return self._reason |
| 151 | |
| 152 | |
| 153 | class Version(Line): |
| 154 | """A version line (i.e. of the form 'TAP version 13').""" |
| 155 | |
| 156 | def __init__(self, version): |
| 157 | self._version = version |
| 158 | |
| 159 | @property |
| 160 | def category(self): |
| 161 | """:returns: ``version``""" |
| 162 | return 'version' |
| 163 | |
| 164 | @property |
| 165 | def version(self): |
| 166 | """Get the version number. |
| 167 | |
| 168 | :rtype: int |
| 169 | """ |
| 170 | return self._version |
| 171 | |
| 172 | |
| 173 | class Unknown(Line): |
| 174 | """A line that represents something that is not a known TAP line. |
| 175 | |
| 176 | This exists for the purpose of a Null Object pattern. |
| 177 | """ |
| 178 | |
| 179 | @property |
| 180 | def category(self): |
| 181 | """:returns: ``unknown``""" |
| 182 | return 'unknown' |