1 # SPDX-License-Identifier: BSD-2-Clause
3 # Copyright (c) 2016, Matt Layman
5 from tap
.adapter
import Adapter
6 from tap
.directive
import Directive
8 from tap
.line
import Result
12 def __init__(self
, filename
, suite
):
13 self
._filename
= filename
15 self
._lines
_seen
= {'plan': [], 'test': 0, 'version': []}
17 def check(self
, final_line_count
):
18 """Check the status of all provided data and update the suite."""
19 if self
._lines
_seen
['version']:
20 self
._process
_version
_lines
()
21 self
._process
_plan
_lines
(final_line_count
)
23 def _process_version_lines(self
):
24 """Process version line rules."""
25 if len(self
._lines
_seen
['version']) > 1:
26 self
._add
_error
(_('Multiple version lines appeared.'))
27 elif self
._lines
_seen
['version'][0] != 1:
28 self
._add
_error
(_('The version must be on the first line.'))
30 def _process_plan_lines(self
, final_line_count
):
31 """Process plan line rules."""
32 if not self
._lines
_seen
['plan']:
33 self
._add
_error
(_('Missing a plan.'))
36 if len(self
._lines
_seen
['plan']) > 1:
37 self
._add
_error
(_('Only one plan line is permitted per file.'))
40 plan
, at_line
= self
._lines
_seen
['plan'][0]
41 if not self
._plan
_on
_valid
_line
(at_line
, final_line_count
):
43 _('A plan must appear at the beginning or end of the file.')
47 if plan
.expected_tests
!= self
._lines
_seen
['test']:
50 'Expected {expected_count} tests ' 'but only {seen_count} ran.'
52 expected_count
=plan
.expected_tests
,
53 seen_count
=self
._lines
_seen
['test'],
57 def _plan_on_valid_line(self
, at_line
, final_line_count
):
58 """Check if a plan is on a valid line."""
59 # Put the common cases first.
60 if at_line
== 1 or at_line
== final_line_count
:
63 # The plan may only appear on line 2 if the version is at line 1.
65 self
._lines
_seen
['version']
66 and self
._lines
_seen
['version'][0] == 1
74 def handle_bail(self
, bail
):
75 """Handle a bail line."""
76 self
._add
_error
(_('Bailed: {reason}').format(reason
=bail
.reason
))
78 def handle_file_does_not_exist(self
):
79 """Handle a test file that does not exist."""
80 self
._add
_error
(_('{filename} does not exist.').format(filename
=self
._filename
))
82 def handle_skipping_plan(self
, skip_plan
):
83 """Handle a plan that contains a SKIP directive."""
84 skip_line
= Result(True, None, skip_plan
.directive
.text
, Directive('SKIP'))
85 self
._suite
.addTest(Adapter(self
._filename
, skip_line
))
87 def saw_plan(self
, plan
, at_line
):
88 """Record when a plan line was seen."""
89 self
._lines
_seen
['plan'].append((plan
, at_line
))
92 """Record when a test line was seen."""
93 self
._lines
_seen
['test'] += 1
95 def saw_version_at(self
, line_counter
):
96 """Record when a version line was seen."""
97 self
._lines
_seen
['version'].append(line_counter
)
99 def _add_error(self
, message
):
100 """Add an error test to the suite."""
101 error_line
= Result(False, None, message
, Directive(''))
102 self
._suite
.addTest(Adapter(self
._filename
, error_line
))
This page took 0.031629 seconds and 4 git commands to generate.