@@ -36,10 +36,54 @@ module StringSet = Set.Make(String)
3636module SectionMap = Map. Make (Section )
3737
3838module Reports = struct
39+
40+ type report_info = {
41+ filepath : string ;
42+ line_nb : int ;
43+ value : string ;
44+ }
45+
46+ let line_of_report_info ri =
47+ Printf. sprintf " %s:%d:%s" ri.filepath ri.line_nb ri.value
48+
49+ (* Format of report lines is : "file_path:line_number: value"
50+ with value possibly containing ':'. In case the line comes from
51+ the direct report of dca (is_res_line), the filepath will be relocated
52+ to correspond to filepaths coming from expected reports *)
53+ let report_info_of_line ~is_res_line line =
54+ let report_line_format = " filepath:line_nb:value" in
55+ match String. split_on_char ':' line with
56+ | [] | _ ::[] | _ ::_ ::[] ->
57+ let err =
58+ Printf. sprintf
59+ " Unrecognized report line format. Expected : '%s'"
60+ report_line_format
61+ in
62+ PP. error ~err ~ctx: line () ;
63+ None
64+ | filepath ::line_number ::value ->
65+ try
66+ let line_nb = int_of_string line_number in
67+ let filepath = (* relocate to match expected paths *)
68+ if is_res_line then Path. relocate filepath
69+ else filepath
70+ in
71+ let filepath = Path. normalize filepath in
72+ let value = String. concat " :" value in
73+ Some {filepath; line_nb; value}
74+ with Failure _int_of_string ->
75+ let err =
76+ Printf. sprintf
77+ " Is not an int. Expected report line format is : '%s'"
78+ report_line_format
79+ in
80+ PP. error ~err ~ctx: line_number () ;
81+ None
82+
3983 type t = {
4084 current_filepath : string option ; (* file containg current expected reports *)
41- remaining_content : string list ; (* expected reports in filename not
42- observed yet *)
85+ remaining_content : report_info list ; (* expected reports in filename not
86+ observed yet *)
4387 root : string ; (* directory containing the expected reports files*)
4488 files_map : StringSet .t SectionMap .t (* remaining files containing expected
4589 reports. Once a file is consumed it
@@ -130,28 +174,30 @@ module State = struct
130174 let scores = Scores. incr_fn state.scores in
131175 {state with scores}
132176
133- let report_fn exp_line state =
134- PP. error ~err: " Not detected" ~ctx: exp_line () ;
177+ let report_fn ri state =
178+ let ctx = Reports. line_of_report_info ri in
179+ PP. error ~err: " Not detected" ~ctx () ;
135180 incr_fn state
136181
137182 let incr_fp state =
138183 let scores = Scores. incr_fp state.scores in
139184 {state with scores}
140185
141- let report_fp res_line state =
142- PP. error ~err: " Should not be detected" ~ctx: res_line () ;
186+ let report_fp ri state =
187+ let ctx = Reports. line_of_report_info ri in
188+ PP. error ~err: " Should not be detected" ~ctx () ;
143189 incr_fp state
144190
145191 let incr_success state =
146192 let scores = Scores. incr_success state.scores in
147193 {state with scores}
148194
149- let report_success res_line state =
150- print_endline res_line;
195+ let report_success ri state =
196+ let line = Reports. line_of_report_info ri in
197+ print_endline line;
151198 incr_success state
152199
153200 let update_remaining_content state remaining_content =
154- let remaining_content = List. filter (( <> ) " " ) remaining_content in
155201 let expected_reports = {state.expected_reports with remaining_content} in
156202 {state with expected_reports}
157203
@@ -206,6 +252,8 @@ module State = struct
206252 let current_filepath = Some exp_filepath in
207253 let state =
208254 In_channel. with_open_text exp_filepath In_channel. input_lines
255+ |> List. filter (( <> ) " " )
256+ |> List. filter_map (Reports. report_info_of_line ~is_res_line: false )
209257 |> update_remaining_content state
210258 in
211259 let expected_reports =
@@ -269,82 +317,39 @@ module State = struct
269317
270318end
271319
272- (* Format of report lines is : "file_path:line_number: report_info"
273- with report_info possibly containing ':'. In case the line comes from
274- the direct report of dca (is_res_line), the filepath will be relocated
275- to correspond to filepaths coming from expected reports *)
276- let infos_of_report_line ~is_res_line line =
277- let report_line_format = " filepath:line_nb:report_info" in
278- match String. split_on_char ':' line with
279- | [] | _ ::[] | _ ::_ ::[] ->
280- let err =
281- Printf. sprintf
282- " Unrecognized report line format. Expected : '%s'"
283- report_line_format
284- in
285- PP. error ~err ~ctx: line () ;
286- None
287- | filepath ::line_number ::report_info ->
288- try
289- let line_nb = int_of_string line_number in
290- let filepath = (* relocate to match expected paths *)
291- if is_res_line then Path. relocate filepath
292- else filepath
293- in
294- let filepath = Path. normalize filepath in
295- let report_info = String. concat " :" report_info in
296- let line = (* recontruct the line with updated fields *)
297- if is_res_line then
298- String. concat " :" [filepath; line_number; report_info]
299- else line
300- in
301- Some (filepath, line_nb, report_info, line)
302- with Failure _int_of_string ->
303- let err =
304- Printf. sprintf
305- " Is not an int. Expected report line format is : '%s'"
306- report_line_format
307- in
308- PP. error ~err ~ctx: line_number () ;
309- None
310320
311- let rec process_report_line state (filepath , line_number , report_info , res_line ) =
312- let state = State. maybe_change_file filepath state in
321+ let rec process_report_line state (got : Reports. report_info ) =
322+ let state = State. maybe_change_file got. filepath state in
313323 match state.expected_reports.remaining_content with
314- | [] -> State. report_fp res_line state
315- | exp_line ::remaining_content when exp_line = res_line ->
324+ | [] -> State. report_fp got state
325+ | expected ::remaining_content when expected = got ->
316326 State. update_remaining_content state remaining_content
317- |> State. report_success res_line
318- | exp_line ::remaining_content ->
319- match infos_of_report_line ~is_res_line: false exp_line with
320- | None ->
321- (* exp_line reported in infos_of_report_line as misformatted *)
322- state
323- | Some (exp_filepath , exp_line_number , _ , exp_line ) ->
324- let compare =
325- let paths_compare = String. compare exp_filepath filepath in
326- if paths_compare = 0 then exp_line_number - line_number
327- else paths_compare
327+ |> State. report_success expected
328+ | expected ::remaining_content ->
329+ let compare =
330+ let paths_compare = String. compare expected.filepath got.filepath in
331+ if paths_compare = 0 then expected.line_nb - got.line_nb
332+ else paths_compare
333+ in
334+ if compare > 0 then State. report_fp got state
335+ else if compare < 0 then
336+ let state =
337+ State. update_remaining_content state remaining_content
338+ |> State. report_fn expected
328339 in
329- if compare > 0 then State. report_fp res_line state
330- else if compare < 0 then
331- let state =
332- State. update_remaining_content state remaining_content
333- |> State. report_fn exp_line
334- in
335- process_report_line state (filepath, line_number, report_info, res_line)
336- else
337- (* The location is fine but report_info does not match.
338- The reports are not organized according to the report_info but
339- only the locations (including the column which is not reported.
340- Check if the current line exists in the remaining_content.
341- If so, then it is a successful report which can be removed from
342- the remaining content. Otherwise, it is a fp. *)
343- if List. mem res_line remaining_content then
344- List. filter (( <> ) res_line) remaining_content
345- |> State. update_remaining_content state
346- |> State. report_success res_line
347- else State. report_fp res_line state
340+ process_report_line state got
341+ else
342+ (* The location is fine but report_info does not match.
343+ The reports are not organized according to the report_info but
344+ only the locations (including the column which is not reported.
345+ Check if the current line exists in the remaining_content.
346+ If so, then it is a successful report which can be removed from
347+ the remaining content. Otherwise, it is a fp. *)
348+ if List. mem got remaining_content then
349+ List. filter (( <> ) got) remaining_content
350+ |> State. update_remaining_content state
351+ |> State. report_success got
352+ else State. report_fp got state
348353
349354let process state res_line =
350355 let is_report_line, state =
@@ -359,12 +364,12 @@ let process state res_line =
359364 | Some _ as sec ->
360365 false , State. change_section sec state
361366 | None -> (* res_line is a report line *)
362- match infos_of_report_line ~is_res_line: true res_line with
367+ match Reports. report_info_of_line ~is_res_line: true res_line with
363368 | None ->
364369 (* res_line reported in infos_of_report_line as misformatted *)
365370 false , state
366- | Some infos ->
367- true , process_report_line state infos
371+ | Some got ->
372+ true , process_report_line state got
368373 in
369374 if not is_report_line then print_endline res_line;
370375 state
0 commit comments