55 # 应当成功,尤其应当是 --zero_linebase(行号从 0 开始)
66 $ python3 check.py --json lang.json --base . --zero_linebase
77
8- """
8+ 检查 rust 项目
99
10+ $ ./lang -d -v --no-need-comment collect rust ../../testdata/rust2 > rust2.json
11+ $ python3 check.py --json lang.json --base . --zero_linebase --implheads
12+ """
1013import json
1114import os
1215import argparse
13- from collections import defaultdict
1416import sys
17+ from collections import defaultdict
1518
1619
1720def trim_multiline (s , max_lines = 5 ):
@@ -35,6 +38,7 @@ def verify_function_content(
3538 filter_files = None ,
3639 filter_funcs = None ,
3740 zero_linebase = False ,
41+ implheads = False ,
3842):
3943 with open (json_path , "r" , encoding = "utf-8" ) as f :
4044 data = json .load (f )
@@ -72,7 +76,6 @@ def verify_function_content(
7276 actual_bytes = content_bytes [start :end ]
7377 actual_content = safe_decode (actual_bytes )
7478
75- # Line check
7679 line_number = func ["Line" ]
7780 content_str = safe_decode (content_bytes )
7881 file_lines = content_str .splitlines ()
@@ -85,12 +88,20 @@ def verify_function_content(
8588 except IndexError :
8689 actual_line_content = "<out of range>"
8790
88- expected_line_start = (
89- expected_content .splitlines ()[0 ].strip () if expected_content else ""
90- )
91-
92- offset_match = actual_content == expected_content
93- line_match = actual_line_content == expected_line_start
91+ if implheads :
92+ offset_match = actual_content in expected_content
93+ line_match = any (
94+ line .strip () == actual_line_content .strip ()
95+ for line in expected_content .splitlines ()
96+ )
97+ else :
98+ offset_match = actual_content == expected_content
99+ expected_line_start = (
100+ expected_content .splitlines ()[0 ].strip ()
101+ if expected_content
102+ else ""
103+ )
104+ line_match = actual_line_content == expected_line_start
94105
95106 print (f"[{ module_name } /{ package_name } ] Checking function: { func_name } " )
96107 if not offset_match :
@@ -100,8 +111,13 @@ def verify_function_content(
100111 if not line_match :
101112 display_line_number = line_number if zero_linebase else line_number
102113 print (f" [Mismatch] Line { display_line_number } mismatch:" )
103- print (f" Expected line: { expected_line_start } " )
104- print (f" Actual line: { actual_line_content } " )
114+ print (f" Expected line (from JSON content):" )
115+ if implheads :
116+ print (f" Any line in expected content matching actual line:" )
117+ else :
118+ print (f" { expected_line_start } " )
119+ print (f" Actual line:" )
120+ print (f" { actual_line_content } " )
105121 if not offset_match or not line_match :
106122 errors [file_name ].append (func_name )
107123 if bail_on_error :
@@ -149,6 +165,11 @@ def verify_function_content(
149165 action = "store_true" ,
150166 help = "Line numbers in JSON are 0-based instead of 1-based" ,
151167 )
168+ parser .add_argument (
169+ "--implheads" ,
170+ action = "store_true" ,
171+ help = "Allow actual content to be a substring of expected content and lines to match any line" ,
172+ )
152173
153174 args = parser .parse_args ()
154175 filter_files = set (args .filter_file .split ("," )) if args .filter_file else None
@@ -161,4 +182,5 @@ def verify_function_content(
161182 filter_files = filter_files ,
162183 filter_funcs = filter_funcs ,
163184 zero_linebase = args .zero_linebase ,
185+ implheads = args .implheads ,
164186 )
0 commit comments