10
10
11
11
import re
12
12
13
+ from .error import XCTestCheckerError
14
+
13
15
14
16
def _actual_lines (path ):
15
17
"""
@@ -26,37 +28,64 @@ def _expected_lines_and_line_numbers(path, check_prefix):
26
28
that begins with the given prefix.
27
29
"""
28
30
with open (path ) as f :
29
- for line_number , line in enumerate (f ):
30
- if line .startswith (check_prefix ):
31
- yield line [len (check_prefix ):].strip (), line_number + 1
31
+ for index , line in enumerate (f ):
32
+ if 'RUN:' in line :
33
+ # Ignore lit directives, which may include a call to
34
+ # xctest_checker that specifies a check prefix.
35
+ continue
36
+
37
+ # Note that line numbers are not zero-indexed; we must add one to
38
+ # the loop index.
39
+ line_number = index + 1
40
+
41
+ components = line .split (check_prefix )
42
+ if len (components ) == 2 :
43
+ yield components [1 ].strip (), line_number
44
+ elif len (components ) > 2 :
45
+ # Include a newline, then the file name and line number in the
46
+ # exception in order to have it appear as an inline failure in
47
+ # Xcode.
48
+ raise XCTestCheckerError (
49
+ path , line_number ,
50
+ 'Usage violation: prefix "{}" appears twice in the same '
51
+ 'line.' .format (check_prefix ))
52
+
32
53
33
54
def _add_whitespace_leniency (original_regex ):
34
55
return "^ *" + original_regex + " *$"
35
56
57
+
36
58
def compare (actual , expected , check_prefix ):
37
59
"""
38
60
Compares each line in the two given files.
39
61
If any line in the 'actual' file doesn't match the regex in the 'expected'
40
62
file, raises an AssertionError. Also raises an AssertionError if the number
41
63
of lines in the two files differ.
42
64
"""
43
- for actual_line , expected_line_and_line_number in map (
65
+ for actual_line , expected_line_and_number in map (
44
66
None ,
45
67
_actual_lines (actual ),
46
68
_expected_lines_and_line_numbers (expected , check_prefix )):
47
69
48
- if actual_line is None :
49
- raise AssertionError ( 'There were more lines expected to appear '
50
- 'than there were lines in the actual input.' )
51
- if expected_line_and_line_number is None :
52
- raise AssertionError ( 'There were more lines than expected to '
53
- 'appear.' )
70
+ if expected_line_and_number is None :
71
+ raise XCTestCheckerError (
72
+ expected , 1 ,
73
+ 'The actual output contained more lines of text than the '
74
+ 'expected output. First unexpected line: {}' . format (
75
+ repr ( actual_line )) )
54
76
55
- (expected_line , expectation_source_line_number ) = expected_line_and_line_number
77
+ (expected_line , expectation_line_number ) = expected_line_and_number
78
+
79
+ if actual_line is None :
80
+ raise XCTestCheckerError (
81
+ expected , expectation_line_number ,
82
+ 'There were more lines expected to appear than there were '
83
+ 'lines in the actual input. Unmet expectation: {}' .format (
84
+ repr (expected_line )))
56
85
57
86
if not re .match (_add_whitespace_leniency (expected_line ), actual_line ):
58
- raise AssertionError ( 'Actual line did not match the expected '
59
- 'regular expression. \n '
60
- '{}:{}: Actual: {} \n '
61
- 'Expected : {}\n ' .format (
62
- expected , expectation_source_line_number , repr (actual_line ), repr (expected_line )))
87
+ raise XCTestCheckerError (
88
+ expected , expectation_line_number ,
89
+ 'Actual line did not match the expected regular expression. \n '
90
+ 'Actual : {}\n Expected: {} ' .format (
91
+ repr (actual_line ), repr (expected_line )))
0 commit comments