| 1 |
Require Import Ltac2.Ltac2. |
| 2 |
Require Import Ltac2.Printf. |
| 3 |
|
| 4 |
(* Check that the arguments have type unit *) |
| 5 |
Ltac2 ignore (x : unit) := (). |
| 6 |
|
| 7 |
Ltac2 dummy0 (_ : int) := Message.of_string "dummy". |
| 8 |
Ltac2 dummy (_ : unit) (_ : int) := Message.of_string "dummy". |
| 9 |
|
| 10 |
(** Simple test for all specifications *) |
| 11 |
|
| 12 |
Ltac2 Eval ignore (printf "%i" 42). |
| 13 |
Ltac2 Eval ignore (printf "%s" "abc"). |
| 14 |
Ltac2 Eval ignore (printf "%I" @Foo). |
| 15 |
Ltac2 Eval ignore (printf "%t" '(1 + 1 = 0)). |
| 16 |
Ltac2 Eval ignore (printf "%%"). |
| 17 |
Ltac2 Eval ignore (printf "%a" dummy 18). |
| 18 |
Ltac2 Eval ignore (printf "%A" dummy0 18). |
| 19 |
Ltac2 Eval ignore (printf "%m!" (fprintf "%s %s" "hello" "world")). |
| 20 |
|
| 21 |
(** More complex tests *) |
| 22 |
|
| 23 |
Ltac2 Eval ignore (printf "%I foo%a bar %s" @ok dummy 18 "yes"). |
| 24 |
Ltac2 Eval ignore (printf "%I foo%A bar %s" @ok dummy0 18 "yes"). |
| 25 |
|
| 26 |
Ltac2 Eval Message.print (fprintf "%I foo%a bar %s" @ok dummy 18 "yes"). |
| 27 |
Ltac2 Eval Message.print (fprintf "%I foo%A bar %s" @ok dummy0 18 "yes"). |
| 28 |
|
| 29 |
(** Failure tests *) |
| 30 |
|
| 31 |
Fail Ltac2 Eval printf "%i" "foo". |
| 32 |
Fail Ltac2 Eval printf "%s" 0. |
| 33 |
Fail Ltac2 Eval printf "%I" "foo". |
| 34 |
Fail Ltac2 Eval printf "%t" "foo". |
| 35 |
Fail Ltac2 Eval printf "%a" (fun _ _ => ()). |
| 36 |
Fail Ltac2 Eval printf "%A" (fun _ => ()). |
| 37 |
Fail Ltac2 Eval printf "%a" (fun _ i => Message.of_int i) "foo". |
| 38 |
Fail Ltac2 Eval printf "%A" Message.of_int "foo". |
| 39 |
Fail Ltac2 Eval printf "%m" 0. |
| 40 |
|
| 41 |
Import Message. |
| 42 |
|
| 43 |
Ltac2 Eval |
| 44 |
Control.assert_true ( |
| 45 |
String.equal |
| 46 |
(Message.to_string Message.empty) |
| 47 |
(Message.to_string (Message.of_string "")) |
| 48 |
). |
| 49 |
|
| 50 |
Ltac2 print_if b fmt := |
| 51 |
if b then Format.kfprintf Message.to_string fmt |
| 52 |
else Format.ikfprintf Message.to_string (Message.of_string "") fmt. |
| 53 |
|
| 54 |
Ltac2 Notation "print_if" b(tactic(0)) fmt(format) := print_if b fmt. |
| 55 |
|
| 56 |
Ltac2 Eval Control.assert_true (String.equal "hello friend" (print_if true "hello %s" "friend")). |
| 57 |
|
| 58 |
Ltac2 Eval Control.assert_true (String.equal "" (print_if false "hello %s" "friend")). |
| 59 |
|
| 60 |
Fail Ltac2 Eval print_if true "%a" (fun _ => Control.throw Assertion_failure) (). |
| 61 |
Fail Ltac2 Eval print_if true "%A" (fun _ => Control.throw Assertion_failure) (). |
| 62 |
|
| 63 |
(* ikfprintf doesn't run the closure *) |
| 64 |
Ltac2 Eval print_if false "%a" (fun _ => Control.throw Assertion_failure) (). |
| 65 |
Ltac2 Eval print_if false "%A" (fun _ => Control.throw Assertion_failure) (). |