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