Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scanning for the best intended match #50

Merged
merged 2 commits into from
Dec 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions filecheck/FileCheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class CheckType(Enum):
Check = namedtuple("Check", "check_type match_type expression source_line check_line_idx start_index")


def debug_print(string):
# print(string)
pass


def similar(a, b):
return SequenceMatcher(None, a, b).ratio()

Expand Down Expand Up @@ -81,13 +86,13 @@ def canonicalize_whitespace(input):


def dump_check(check):
print("check dump")
print("\tcheck_type: {}".format(check.check_type))
print("\tmatch_type: {}".format(check.match_type))
print("\texpression: {}".format(check.expression))
print("\tsource_line: {}".format(check.source_line))
print("\tcheck_line_idx: {}".format(check.check_line_idx))
print("\tstart_index: {}".format(check.start_index))
debug_print("check dump")
debug_print("\tcheck_type: {}".format(check.check_type))
debug_print("\tmatch_type: {}".format(check.match_type))
debug_print("\texpression: {}".format(check.expression))
debug_print("\tsource_line: {}".format(check.source_line))
debug_print("\tcheck_line_idx: {}".format(check.check_line_idx))
debug_print("\tstart_index: {}".format(check.start_index))


def main():
Expand Down Expand Up @@ -313,15 +318,6 @@ def main():
if current_check.match_type == MatchType.SUBSTRING:
last_read_line = input_lines[current_scan_base]

candidate_line = None
current_best_ratio = 0
for read_line in input_lines[current_scan_base:]:
similar_ratio = similar(last_read_line, current_check.expression)
if current_best_ratio < similar_ratio:
candidate_line = read_line
current_best_ratio = similar_ratio
assert candidate_line

print("{}:{}:{}: error: CHECK: expected string not found in input"
.format(check_file,
current_check.check_line_idx + 1,
Expand All @@ -333,10 +329,19 @@ def main():
print(last_read_line)
print("^")

caret_pos = len(candidate_line) // 2 + 1
print("<stdin>:{}:{}: note: possible intended match here".format(current_scan_base + 1, caret_pos))
print(candidate_line)
print("^".rjust(caret_pos, ' '))
candidate_line = None
current_best_ratio = 0
for read_line in input_lines[current_scan_base:]:
similar_ratio = similar(read_line, current_check.expression)
if current_best_ratio < similar_ratio:
candidate_line = read_line
current_best_ratio = similar_ratio
if candidate_line:
caret_pos = len(candidate_line) // 2 + 1
print("<stdin>:{}:{}: note: possible intended match here".format(current_scan_base + 1, caret_pos))
print(candidate_line)
print("^".rjust(caret_pos, ' '))

exit(1)

if current_check.match_type == MatchType.REGEX:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
; CHECK: foo
; CHECK: hello2
; CHECK: hello3
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hello1
hello2
fo
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
; RUN: cat %S/filecheck.input | (%FILECHECK_EXEC %S/filecheck.check 2>&1; test $? = 1) | %FILECHECK_TESTER_EXEC %s --match-full-lines
; CHECK: {{^.*}}FileCheck{{(\.py)?$}}
; CHECK-NEXT: {{.*filecheck.check:1:.*: error: CHECK: expected string not found in input$}}
; CHECK-NEXT: {{^}}; CHECK: foo{{$}}
; CHECK-NEXT: {{^}} ^{{$}}
; CHECK-NEXT: {{^<stdin>:.*:.*: note: scanning from here$}}
; CHECK-NEXT: hello1
; CHECK-NEXT: {{^\^$}}
; CHECK-NEXT: {{^<stdin>:.*:.*: note: possible intended match here$}}
; CHECK-NEXT: {{^fo$}}

; TODO: https://github.com/stanislaw/FileCheck.py/issues/38
; CHECK-NEXT: {{^ ?\^$}}
; CHECK-EMPTY:
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
; CHECK: foo
; CHECK: hello2
; CHECK: hello3
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bar
bar
bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
; RUN: cat %S/filecheck.input | (%FILECHECK_EXEC %S/filecheck.check 2>&1; test $? = 1) | %FILECHECK_TESTER_EXEC %s --match-full-lines
; CHECK: {{^.*}}FileCheck{{(\.py)?$}}
; CHECK-NEXT: {{.*filecheck.check:1:.*: error: CHECK: expected string not found in input$}}
; CHECK-NEXT: {{^}}; CHECK: foo{{$}}
; CHECK-NEXT: {{^}} ^{{$}}
; CHECK-NEXT: {{^<stdin>:.*:.*: note: scanning from here$}}
; CHECK-NEXT: {{^bar$}}
; CHECK-NEXT: {{^\^$}}
; CHECK-EMPTY: