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