Skip to content

Commit c08b0d9

Browse files
committed
CWStopOnFailure
1 parent 62b29ad commit c08b0d9

File tree

6 files changed

+46
-31
lines changed

6 files changed

+46
-31
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ Run `make .merlin` to create the `.merlin` file.
3535

3636
Note that the `term` should be in parentheses.
3737

38+
- `CWStopOnFailure 0/1`
39+
40+
This command controls whether Coq execution should be stopped after a failed test (`1`) or not (`0`).
41+
The default behavior is to stop after the first failed test.
42+
3843
- `CWGroup string`
3944

4045
Begins a group of tests (outputs `<DESCRIBE::>`).

cw_example/SolutionTest.v

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ Require Solution.
22
Require Import Preloaded.
33
From CW Require Import Loader.
44

5+
CWStopOnFailure 0.
6+
57
CWGroup "Tests for Solution.solution".
68
CWTest "Type test".
79
(* The expected type should be in parentheses *)
8-
Fail CWAssert "Should fail" Solution.solution : (1 + 1 = 2).
10+
CWAssert "Should fail" Solution.solution : (1 + 1 = 2).
911
CWAssert Solution.solution : (1 + 1 = 3).
1012
(* CWEndTest is optional before CWTest or CWEndGroup *)
1113
CWEndTest.
1214
CWTest "Assumptions test".
13-
CWAssert "Testing solution" Solution.solution Assumes test_axiom.
15+
CWAssert "Testing solution" Solution.solution Assumes. (*test_axiom.*)
1416
CWTest "Type test 2".
1517
Definition expected := 1 + 1 = 3.
1618
CWAssert Solution.solution : expected.

cw_example_extraction/SolutionTest.v

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Extract Inductive nat => "int"
1010
Extract Constant plus => "( + )".
1111
Extract Constant mult => "( * )".
1212

13+
(* CWStopOnFailure 0. *)
14+
1315
CWGroup "Group".
1416

1517
CWTest "Successful Extraction Test".

src/coq_cw.ml

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ let format_msg =
1111
let display tag msg =
1212
Feedback.msg_notice (str (Printf.sprintf "\n<%s::>%s\n" tag (format_msg msg)))
1313

14-
let passed = display "PASSED"
15-
16-
let failed = display "FAILED"
14+
let stop_on_failure_flag = ref true
1715

1816
type group = {
1917
tag : string;
2018
start_time : float
2119
}
2220

21+
let stop_on_failure v = stop_on_failure_flag := v <> 0
22+
2323
let mk_group tag t = { tag = tag; start_time = t }
2424

2525
let group_stack = Summary.ref ~name:"open_groups" ([] : group list)
@@ -49,6 +49,15 @@ let rec end_all_groups () =
4949
| [] -> ()
5050
| {tag = tag} :: gs -> end_group tag; end_all_groups ()
5151

52+
let passed = display "PASSED"
53+
54+
let failed msg =
55+
display "FAILED" msg;
56+
if !stop_on_failure_flag then begin
57+
end_all_groups ();
58+
CErrors.user_err (str msg)
59+
end
60+
5261
let locate r =
5362
try
5463
let gr = Smartlocate.locate_global_with_alias r in
@@ -67,9 +76,8 @@ let test_type ?(msg = "Type Test") r c_ty =
6776
let p_actual = Printer.pr_econstr_env env sigma actual_ty in
6877
let p_expected = Printer.pr_econstr_env env sigma expected_ty in
6978
failed (Printf.sprintf "%s\nActual type = %s\nExpected type = %s"
70-
msg (string_of_ppcmds p_actual) (string_of_ppcmds p_expected));
71-
end_all_groups ();
72-
CErrors.user_err (str "Incorrect Type: " ++ Printer.pr_econstr_env env sigma tm)
79+
msg (string_of_ppcmds p_actual) (string_of_ppcmds p_expected))
80+
(* CErrors.user_err (str "Incorrect Type: " ++ Printer.pr_econstr_env env sigma tm) *)
7381

7482
(* Based on the PrintAssumptions code from vernac/vernacentries.ml *)
7583
let assumptions r =
@@ -106,9 +114,8 @@ let test_axioms ?(msg = "Axiom Test") c_ref ax_refs =
106114
| Printer.Axiom (ax, _) ->
107115
if Printer.ContextObjectSet.mem t ax_set then ()
108116
else begin
109-
failed msg;
110-
end_all_groups ();
111-
CErrors.user_err (str "Prohibited Axiom: " ++ pr_axiom env sigma ax ty)
117+
let p_axiom = pr_axiom env sigma ax ty in
118+
failed (Printf.sprintf "%s\nProhibited Axiom: %s" msg (string_of_ppcmds p_axiom))
112119
end
113120
| _ -> ()
114121
in
@@ -123,9 +130,7 @@ let test_file_size ?(fname = solution_file) size =
123130
passed (Format.sprintf "Size %d < %d" stats.Unix.st_size size)
124131
else begin
125132
let msg = Format.sprintf "Size %d >= %d" stats.Unix.st_size size in
126-
failed msg;
127-
end_all_groups ();
128-
CErrors.user_err (str msg)
133+
failed msg
129134
end
130135
with Unix.Unix_error _ -> CErrors.user_err (str ("Bad file name: " ^ fname))
131136

@@ -140,22 +145,15 @@ let test_file_regex ?(fname = solution_file) match_flag regex =
140145
with Not_found -> false in
141146
if matched = match_flag then
142147
passed "OK"
143-
else begin
144-
failed "Bad match";
145-
end_all_groups ();
146-
CErrors.user_err (str "Bad match")
147-
end
148+
else
149+
failed "Bad match"
148150

149-
let run_system_command ?err_msg args =
151+
let run_system_command ?(err_msg = "Failed") args =
150152
let cmd = String.concat " " args in
151153
Printf.printf "Running: %s" cmd;
152-
match Unix.system cmd with
153-
| Unix.WEXITED 0 -> ()
154-
| _ ->
155-
let msg = match err_msg with None -> "Failed" | Some msg -> msg in
156-
failed msg;
157-
end_all_groups ();
158-
CErrors.user_err (str msg)
154+
match Unix.system (cmd ^ " 2>&1") with
155+
| Unix.WEXITED 0 -> true
156+
| _ -> (failed err_msg; false)
159157

160158
let write_file fname str =
161159
let oc = open_out fname in
@@ -165,7 +163,7 @@ let write_file fname str =
165163
(** Compiles and runs the given OCaml source files *)
166164
let compile_and_run files ?(options = "") driver_code =
167165
write_file driver_file driver_code;
168-
run_system_command ~err_msg:"Compilation failed" ([ocaml_compiler; options] @ files @ [driver_file]);
169-
passed "OK";
170-
run_system_command ["./a.out"];
171-
passed "OK"
166+
if run_system_command ~err_msg:"Compilation failed" ([ocaml_compiler; options] @ files @ [driver_file]) then begin
167+
passed "OK";
168+
if run_system_command ["./a.out"] then passed "OK"
169+
end

src/coq_cw.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ val test_file_regex : ?fname:string -> bool -> string -> unit
88

99
val compile_and_run : string list -> ?options:string -> string -> unit
1010

11+
val stop_on_failure : int -> unit
12+
1113
val begin_group : string -> string -> unit
1214

1315
val end_group : string -> unit

src/g_coq_cw.ml4

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ VERNAC COMMAND EXTEND CWAssertAssumptions CLASSIFIED AS QUERY
1414
]
1515
END
1616

17+
VERNAC COMMAND EXTEND CWStopOnFailure CLASSIFIED AS SIDEFF
18+
| [ "CWStopOnFailure" int(flag)] -> [
19+
Coq_cw.stop_on_failure flag
20+
]
21+
END
22+
1723
VERNAC COMMAND EXTEND CWGroup CLASSIFIED AS SIDEFF
1824
| [ "CWGroup" string(msg)] -> [
1925
Coq_cw.begin_group "DESCRIBE" msg

0 commit comments

Comments
 (0)