summaryrefslogtreecommitdiff
path: root/testsuite/tests/lib-arg/testerror.ml
blob: 6bcf0fdeb893f1bdb045e56824657b1991718c3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
(* TEST
   * native
*)

(** Test that the right message errors are emitted by Arg *)


let usage= "Arg module testing"

let test total i (spec,anon,argv) =
  let argv = Array.of_list ("testerror" :: argv) in
  try Arg.parse_argv ~current:(ref 0) argv spec anon usage with
  | Arg.Bad s-> Printf.printf "(%d/%d) Bad:\n%s\n" (i+1) total s
  | Arg.Help s -> Printf.printf "(%d/%d) Help:\n%s\n" (i+1) total s


let tests = [
(** missing argument error *)
  ["-s",  Arg.String ignore, "missing arg"], ignore, ["-s"]

(** No argument expected *)
; ["-set",  Arg.Set (ref false), "no argument expected"], ignore, ["-set=true"]

(** help message *)
; [], ignore, ["-help" ]

(** wrong argument type *)
; ["-int", Arg.Int ignore, "wrong argument type" ], ignore,
  ["-int"; "not_an_int" ]

(** unknown option *)
; [], ignore, [ "-an-unknown-option" ]

(** user-error in anon fun *)
; [], (fun _ -> raise @@ Arg.Bad("User-raised error")), [ "argument" ]

(** user-error in anon fun *)
; ["-error",
   Arg.Unit (fun () -> raise @@ Arg.Bad("User-raised error bis")),
   "user raised error"]
, ignore, [ "-error" ]

(* bad keyword in various places*)
; [ "-rest", Arg.Rest ignore, "help"], ignore, [ "-rest=1" ]
; [ "-tuple", Arg.Tuple [Arg.Int print_int; Arg.Int print_int ], "help" ]
  , ignore, [ "-tuple=1" ]
; [ "-unit", Arg.Unit ignore, "" ], ignore, [ "-unit=1" ]
]

let () =
  let n = List.length tests in
  List.iteri (test n) tests