diff --git a/src/lib/CoolUtils.mli b/src/lib/CoolUtils.mli index f045e062ac79fdd744ac585052e1be642571a45f..438c4042fee2e2f011c21993fe97c151b31f6073 100644 --- a/src/lib/CoolUtils.mli +++ b/src/lib/CoolUtils.mli @@ -25,7 +25,6 @@ module TList : sig val combinations : 'a list list -> 'a list list end -(* TODO Document, test *) (** Command line argument parser *) module Args : sig type 'a param_parser = 'a -> 'a diff --git a/src/unit-tests/CoolUtils_tests.ml b/src/unit-tests/CoolUtils_tests.ml index 7e570a964a7e8635f42a8559dc9cf247e9cd2da1..3266675602423114a8f4edf18de599a3774946c7 100644 --- a/src/unit-tests/CoolUtils_tests.ml +++ b/src/unit-tests/CoolUtils_tests.ml @@ -1,11 +1,11 @@ open OUnit2 +let string_of_list (printer: 'a -> string) (lst: 'a list) : string = + "[" ^ (String.concat ";" (List.map printer lst)) ^ "]" + module TList_tests : (sig val tests : test end) = struct open CoolUtils.TList - let string_of_list (printer: 'a -> string) (lst: 'a list) : string = - "[" ^ (String.concat ";" (List.map printer lst)) ^ "]" - let combinations_test _ = let list_printer = string_of_list (string_of_list string_of_int) in assert_equal ~msg:"empty list" ~printer:list_printer [] (combinations []); @@ -17,6 +17,63 @@ module TList_tests : (sig val tests : test end) = struct let tests = "TList" >: ("combinations" >:: combinations_test) 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 : *)