| 1 | # SPDX-License-Identifier: BSD-2-Clause |
| 2 | # |
| 3 | # Copyright (c) 2016, Matt Layman |
| 4 | |
| 5 | |
| 6 | class Adapter(object): |
| 7 | """The adapter processes a TAP test line and updates a unittest result. |
| 8 | |
| 9 | It is an alternative to TestCase to collect TAP results. |
| 10 | """ |
| 11 | failureException = AssertionError |
| 12 | |
| 13 | def __init__(self, filename, line): |
| 14 | self._filename = filename |
| 15 | self._line = line |
| 16 | |
| 17 | def shortDescription(self): |
| 18 | """Get the short description for verbeose results.""" |
| 19 | return self._line.description |
| 20 | |
| 21 | def __call__(self, result): |
| 22 | """Update test result with the lines in the TAP file. |
| 23 | |
| 24 | Provide the interface that TestCase provides to a suite or runner. |
| 25 | """ |
| 26 | result.startTest(self) |
| 27 | |
| 28 | if self._line.skip: |
| 29 | result.addSkip(None, self._line.directive.reason) |
| 30 | return |
| 31 | |
| 32 | if self._line.todo: |
| 33 | if self._line.ok: |
| 34 | result.addUnexpectedSuccess(self) |
| 35 | else: |
| 36 | result.addExpectedFailure(self, (Exception, Exception(), None)) |
| 37 | return |
| 38 | |
| 39 | if self._line.ok: |
| 40 | result.addSuccess(self) |
| 41 | else: |
| 42 | self.addFailure(result) |
| 43 | |
| 44 | def addFailure(self, result): |
| 45 | """Add a failure to the result.""" |
| 46 | result.addFailure(self, (Exception, Exception(), None)) |
| 47 | # Since TAP will not provide assertion data, clean up the assertion |
| 48 | # section so it is not so spaced out. |
| 49 | test, err = result.failures[-1] |
| 50 | result.failures[-1] = (test, '') |
| 51 | |
| 52 | def __repr__(self): |
| 53 | return '<file={filename}>'.format(filename=self._filename) |