Skip to content

Commit 95967c8

Browse files
committed
Skip tests using only_if and Alcotest filter
1 parent 1fe0783 commit 95967c8

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

test/test.cppo.ml

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -408,19 +408,21 @@ end = struct
408408
end
409409
410410
411-
module List = struct
412-
include List
413-
414-
let filter_map f =
415-
let rec aux accu = function
416-
| [] -> rev accu
417-
| x :: l ->
418-
match f x with
419-
| None -> aux accu l
420-
| Some v -> aux (v :: accu) l
421-
in
422-
aux []
423-
end
411+
let fold_left_map f accu l =
412+
let rec aux accu l_accu = function
413+
| [] -> accu, List.rev l_accu
414+
| x :: l ->
415+
let accu, x = f accu x in
416+
aux accu (x :: l_accu) l in
417+
aux accu [] l
418+
419+
let fold_left_mapi f accu l =
420+
let rec aux i accu l_accu = function
421+
| [] -> accu, List.rev l_accu
422+
| x :: l ->
423+
let accu, x = f i accu x in
424+
aux (i + 1) accu (x :: l_accu) l in
425+
aux 0 accu [] l
424426
425427
426428
open Lwt.Infix
@@ -429,6 +431,7 @@ type suite = {
429431
suite_name : string;
430432
suite_tests : unit Alcotest_lwt.test_case list;
431433
skip_suite_if_this_is_false : unit -> bool;
434+
skip_indexes : int list;
432435
}
433436
434437
let suite name ?(only_if = fun () -> true) tests =
@@ -443,25 +446,37 @@ let suite name ?(only_if = fun () -> true) tests =
443446
let b = run () in
444447
Alcotest.(check bool) "success" b true)
445448
in
446-
let tests =
447-
List.filter_map (fun test ->
448-
if test.skip_if_this_is_false () then
449-
Some (to_test_case test)
450-
else None)
451-
tests
449+
let skip_indexes, tests =
450+
fold_left_mapi (fun i skip_indexes test ->
451+
if test.skip_if_this_is_false () then skip_indexes, to_test_case test
452+
else i :: skip_indexes, to_test_case test)
453+
[] (tests : test list)
452454
in
453455
{suite_name = name;
454456
suite_tests = tests;
455-
skip_suite_if_this_is_false = only_if}
457+
skip_suite_if_this_is_false = only_if;
458+
skip_indexes}
456459
457460
let run library_name suites =
458-
let tests =
459-
List.filter_map (fun suite ->
461+
let skip = Hashtbl.create 16 in
462+
let skip_names, tests =
463+
fold_left_map (fun skip_names suite ->
460464
if suite.skip_suite_if_this_is_false () then
461-
Some (suite.suite_name, suite.suite_tests)
462-
else None)
463-
suites in
464-
Alcotest_lwt.run library_name tests
465+
begin
466+
Hashtbl.add skip suite.suite_name suite.skip_indexes;
467+
skip_names, (suite.suite_name, suite.suite_tests)
468+
end
469+
else
470+
suite.suite_name :: skip_names, (suite.suite_name, suite.suite_tests))
471+
[] suites in
472+
let filter ~name ~index =
473+
if List.mem name skip_names then `Skip
474+
else
475+
let skip_indexes = Hashtbl.find skip name in
476+
if List.mem index skip_indexes then `Skip
477+
else `Run
478+
in
479+
Alcotest_lwt.run ~filter library_name tests
465480
466481
let run library_name suites = Lwt_main.run @@ run library_name suites
467482
let concurrent = run

0 commit comments

Comments
 (0)