Assertions
The code samples on this page add tests to Kaputt, which can then be launched by executing the function
Test.launch_tests. To use the following code, you have to open
open Kaputt.Abbreviations.
First, some assertions:
let () =
Test.add_simple_test
~title:"miscellaneous"
(fun () ->
Assert.is_true true;
Assert.is_false false;
Assert.is_some (Some 3);
Assert.is_none None;
Assert.raises (fun () -> failwith "msg");
Assert.no_raise ignore;
Assert.make_raises
(function Not_found -> true | _ -> false)
Printexc.to_string
(fun () -> raise Not_found))
let () =
Test.add_simple_test
~title:"not_equal_int"
(fun () -> Assert.not_equal_int 3 3)
Generators
Then, some specification-based tests using generators. The specifications are unsatisfiable to exhibit counterexamples:
let () =
Test.add_random_test
~title:"int * float"
~nb_runs:3
~random_src:(Gen.make_random_seed seed)
(Gen.zip2
(Gen.make_int 1 1000)
(Gen.transform (fun x -> mod_float x 1000.) Gen.float))
(fun (x, y) -> (float x) +. y)
[Spec.always ==> Spec.never]
let () =
Test.add_random_test
~title:"string option"
~nb_runs:3
~random_src:(Gen.make_random_seed seed)
(Gen.option Gen.bool (Gen.word (Gen.make_int 1 9)))
(fun _ -> 0)
[Spec.always ==> Spec.never]
Enumerators
Also some specification-based tests, but using enumerators:
let () =
Test.add_enum_test
~title:"int"
(Enum.int 1 5)
(fun _ -> ())
[Spec.always ==> Spec.never]
let () =
Test.add_enum_test
~title:"[1..10 | even]"
(Enum.filter (fun x -> (x mod 2) = 0) (Enum.int 1 10))
(fun _ -> ())
[Spec.always ==> Spec.never]
let () =
Test.add_enum_test
~title:"[1..10] [13..15]"
(Enum.sequence [Enum.int 1 10; Enum.int 13 15])
(fun _ -> ())
[Spec.always ==> Spec.never]
Shell
Finally, some shell-based tests:
let _ =
let cmd =
Shell.redirect_output
(Shell.pipe
(Shell.cat ["data"])
(Shell.sed "s/line/LINE/g"))
"data2" in
Shell.run cmd
let () =
let diff = Shell.run (Shell.diff ~options:["-q"] "reference" "result") in
if diff <> 0 then begin
print_endline "failed";
exit 1
end else begin
ignore (Shell.run (Shell.rm ~options:["-f"] ["data2"; "data3"; "result"]));
exit 0
end