Skip to content
Snippets Groups Projects
Commit 7ed16bab authored by Hans-Peter Deifel's avatar Hans-Peter Deifel Committed by Hans-Peter Deifel
Browse files

Add tests for command line option parser

parent 4c23563e
Branches
No related tags found
No related merge requests found
...@@ -25,7 +25,6 @@ module TList : sig ...@@ -25,7 +25,6 @@ module TList : sig
val combinations : 'a list list -> 'a list list val combinations : 'a list list -> 'a list list
end end
(* TODO Document, test *)
(** Command line argument parser *) (** Command line argument parser *)
module Args : sig module Args : sig
type 'a param_parser = 'a -> 'a type 'a param_parser = 'a -> 'a
......
open OUnit2 open OUnit2
module TList_tests : (sig val tests : test end) = struct
open CoolUtils.TList
let string_of_list (printer: 'a -> string) (lst: 'a list) : string = let string_of_list (printer: 'a -> string) (lst: 'a list) : string =
"[" ^ (String.concat ";" (List.map printer lst)) ^ "]" "[" ^ (String.concat ";" (List.map printer lst)) ^ "]"
module TList_tests : (sig val tests : test end) = struct
open CoolUtils.TList
let combinations_test _ = let combinations_test _ =
let list_printer = string_of_list (string_of_list string_of_int) in let list_printer = string_of_list (string_of_list string_of_int) in
assert_equal ~msg:"empty list" ~printer:list_printer [] (combinations []); assert_equal ~msg:"empty list" ~printer:list_printer [] (combinations []);
...@@ -17,6 +17,63 @@ module TList_tests : (sig val tests : test end) = struct ...@@ -17,6 +17,63 @@ module TList_tests : (sig val tests : test end) = struct
let tests = "TList" >: ("combinations" >:: combinations_test) let tests = "TList" >: ("combinations" >:: combinations_test)
end end
let tests = "CoolUtils" >: TList_tests.tests module Args_tests : (sig val tests : test end) = struct
open CoolUtils.Args
type opt = { verbose : bool; file : string option }
let def_opts = { verbose = false; file = None }
let options =
[ { long = "verbose"
; short = Some 'v'
; description = "Print verbose ouput"
; argument = No_arg (fun o -> { o with verbose = true })
}
; { long = "file"
; short = None
; description = "File to load"
; argument = Required_arg ("FILE", fun s o -> { o with file = Some s })
}
]
let print_opts { verbose; file } = match file with
| None -> Printf.sprintf "{ verbose = \"%B\"; file = None}" verbose
| Some f -> Printf.sprintf "{ verbose = \"%B\"; file = Some \"%s\"}" verbose f
let parse_test _ =
let print_maybe = function
| Error e -> "Error \"" ^ e ^ "\""
| Ok x -> "Ok " ^ print_opts x
in
let cmp_maybe a b = match (a, b) with
| Error _, Error _ -> true
| Ok x, Ok y -> x = y
| _ -> false
in
let assert_parse argv res =
assert_equal ~msg:(string_of_list (fun x -> x) argv)
~printer:print_maybe
~cmp:cmp_maybe
res (parse (Array.of_list argv) 0 options def_opts)
in
assert_parse ["--verbose"] (Ok { verbose = true; file = None });
assert_parse ["-v"] (Ok { verbose = true; file = None });
assert_parse [] (Ok { verbose = false; file = None });
assert_parse ["--file"; "foo"] (Ok { verbose = false; file = Some "foo" });
assert_parse ["--verbose"; "--file"; "foo"]
(Ok { verbose = true; file = Some "foo" });
assert_parse ["--file"; "foo"; "--verbose"]
(Ok { verbose = true; file = Some "foo" });
assert_parse ["--verbuse"] (Error "");
assert_parse ["--file"] (Error "");
assert_parse ["file"] (Error "")
let tests = "Args" >: ("parse" >:: parse_test)
end
let tests = "CoolUtils" >::: [TList_tests.tests; Args_tests.tests]
(* vim: set et sw=2 sts=2 ts=8 : *) (* vim: set et sw=2 sts=2 ts=8 : *)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment