diff --git a/.github/workflows/Linux.yml b/.github/workflows/Linux.yml index bc8de79..e9d9e60 100644 --- a/.github/workflows/Linux.yml +++ b/.github/workflows/Linux.yml @@ -16,6 +16,7 @@ jobs: # Decision on version matrix informed by https://discuss.ocaml.org/t/which-ocaml-compiler-versions-should-we-run-against-in-ci/7933/2 # But has gradually inched up due to signs of bitrot on earlier versions # such as https://github.com/thierry-martinez/stdcompat/issues/26 + - 4.x - 5.x runs-on: ${{ matrix.os }} steps: diff --git a/.github/workflows/MacOS.yml b/.github/workflows/MacOS.yml index 3af0ea3..26982a1 100644 --- a/.github/workflows/MacOS.yml +++ b/.github/workflows/MacOS.yml @@ -16,6 +16,7 @@ jobs: # Decision on version matrix informed by https://discuss.ocaml.org/t/which-ocaml-compiler-versions-should-we-run-against-in-ci/7933/2 # But has gradually inched up due to signs of bitrot on earlier versions # such as https://github.com/thierry-martinez/stdcompat/issues/26 + - 4.x - 5.x runs-on: ${{ matrix.os }} steps: diff --git a/.github/workflows/Windows.yml b/.github/workflows/Windows.yml index a04c540..4dd15e6 100644 --- a/.github/workflows/Windows.yml +++ b/.github/workflows/Windows.yml @@ -16,7 +16,7 @@ jobs: # Decision on version matrix informed by https://discuss.ocaml.org/t/which-ocaml-compiler-versions-should-we-run-against-in-ci/7933/2 # But has gradually inched up due to signs of bitrot on earlier versions # such as https://github.com/thierry-martinez/stdcompat/issues/26 - - 5.x + - 4.x runs-on: ${{ matrix.os }} steps: - name: Checkout code diff --git a/README.md b/README.md index 5d3dfe3..bf5bf98 100644 --- a/README.md +++ b/README.md @@ -21,47 +21,35 @@ __An OCaml minimalist testing framework with zero dependencies.__ ## Usage [./test/test_omtl.ml](./test/test_omtl.ml) ```ocaml -open Omtl - (* A module with functions to test *) -module My_String = struct - let equal = String.equal - - let capitalize = String.capitalize_ascii +module Suit = struct + let pass () = () - let str_concat = String.concat "" + let failed () = "Failed !!!" end (* The tests *) -let test_equal () = - if My_String.equal "hello!" "hello!" then - () - else - fail {| My_String.equal "hello!" = "hello!" |} - -let test_capitalize () = - if String.equal "HELLO!" (My_String.capitalize "hELLO!") then - () - else - fail {| My_String.capitalize "hELLO!" = "HELLO!!" |} +let test_pass () = Suit.pass () |> ok -let test_str_concat () = - if String.equal "foobar" (My_String.str_concat ["foo"; "bar"]) then - () - else - fail {| My_String.str_concat ["foo"; "bar"] = "foobar" |} +let test_failed () = Suit.failed () |> err let test_failure () = failwith "Take it easy, this is just an example of a failed test" + let test_exception () = raise Not_found -let test_function_running_time () = for _ = 0 to 100000 do () done + +let test_function_running_time () = + for _ = 0 to 100000 do + () + done; + Ok () + (* Run it *) let _ = "My_String" +:> [ - "equal" >== test_equal; - "capitalize" >== test_capitalize; - "str_concat" >== test_str_concat; + "pass" >== test_pass; + "failed" >== test_failed; "Examples of test failures" >== test_failure; "Examples of undefined exception" >== test_exception; "Test function running time" >== test_function_running_time; diff --git a/dune-project b/dune-project index 19ec701..e1ad898 100644 --- a/dune-project +++ b/dune-project @@ -22,7 +22,7 @@ (description "OCaml Minimalist Testing Library") (depends (ocaml - (>= 5.0.0)) + (>= 4.13.1)) dune) (tags (testing "test framework"))) diff --git a/lib/color.ml b/lib/color.ml index 3194bde..1e22b01 100644 --- a/lib/color.ml +++ b/lib/color.ml @@ -28,7 +28,7 @@ *) type color = - | Ok + | Pass | Fail | Fail_info | Time @@ -41,7 +41,7 @@ type color = let color_map : (color, string) Hashtbl.t = [ - Ok, "\027[32m"; + Pass, "\027[32m"; Fail, "\027[5;31m"; Fail_info, "\027[31m"; Info_title, "\027[4;36m"; diff --git a/lib/info.ml b/lib/info.ml index 23729e8..998eb62 100644 --- a/lib/info.ml +++ b/lib/info.ml @@ -48,15 +48,18 @@ functor struct let get () : string = M.get_info () |> String.split_on_char '\n' |> M.filter |> M.decorate |> String.concat "\n" + [@@inline always] end module Get_Info = struct module Backtrace = struct let get_info () = Printexc.get_raw_backtrace () |> Printexc.raw_backtrace_to_string + [@@inline always] end module CallStack = struct let get_info () = Printexc.get_callstack 20 |> Printexc.raw_backtrace_to_string + [@@inline always] end end @@ -68,10 +71,11 @@ module Filter = struct (not (String.starts_with ~prefix:"Called from Omtl.test.time" s)) && not (String.equal s "")) backtraces + [@@inline always] end module CallStack = struct - let filter (lst : string list) : string list = lst + let filter (lst : string list) : string list = lst [@@inline always] end end diff --git a/lib/omtl.ml b/lib/omtl.ml index 6e4c0b1..d99a720 100644 --- a/lib/omtl.ml +++ b/lib/omtl.ml @@ -44,7 +44,7 @@ let test status (f : unit -> (unit, string) Result.t) : Test_Result.t = Result.map (fun () -> Standalone_unix.gettimeofday () -. timer) result in match time f with - | Result.Ok time -> Test_Result.Ok time + | Result.Ok time -> Test_Result.Pass time | Result.Error info -> let backtrace = if backtrace then Backtrace.get () else String.empty and callstack = if callstack then CallStack.get () else String.empty in @@ -63,12 +63,12 @@ let test_case status (test_case : test_case) : string = | { backtrace; callstack; force; suit = _ } -> ( let name, f = test_case in match test status f with - | Test_Result.Ok time -> + | Test_Result.Pass time -> Format.sprintf "\t %s- %s...%s %s" - (text ~force ~color:Ok "o") + (text ~force ~color:Pass "o") name - (text ~force ~color:Ok "OK") + (text ~force ~color:Pass "OK") (text ~force ~color:Time (Format.sprintf "(%f ms)" (time *. 1000.))) | Test_Result.Fail (i, b, c) -> Format.sprintf diff --git a/lib/types.ml b/lib/types.ml index 88b2963..9004a71 100644 --- a/lib/types.ml +++ b/lib/types.ml @@ -40,7 +40,7 @@ and test_case = string * (unit -> (unit, string) result) module Test_Result = struct type t = - | Ok of time + | Pass of time | Fail of info * backtraces * callstack and time = float diff --git a/lib/utils.ml b/lib/utils.ml index 0fe3002..87a9381 100644 --- a/lib/utils.ml +++ b/lib/utils.ml @@ -29,10 +29,11 @@ open Types -(** Assist in building test sets and individual test items to improve the readability of test code *) +(* Assist in building test sets and individual test items to improve the readability of test code *) let ( +:> ) (name : string) (test_case_list : test_case list) = name, test_case_list let ( >== ) (name : string) (f : unit -> (unit, string) result) : test_case = name, f -(** Wrapper function to failwith *) -let fail = failwith +let ok v = Ok v + +let err v = Error v diff --git a/omtl.opam b/omtl.opam index cf84f66..e164f66 100644 --- a/omtl.opam +++ b/omtl.opam @@ -10,7 +10,7 @@ homepage: "https://github.com/muqiuhan/omtl" doc: "https://github.com/omtl" bug-reports: "https://github.com/muqiuhan/omtl/issues" depends: [ - "ocaml" {>= "5.0.0"} + "ocaml" {>= "4.13.1"} "dune" {>= "3.0"} "odoc" {with-doc} ] diff --git a/test/test_omtl.ml b/test/test_omtl.ml index 5bcffb8..007304f 100644 --- a/test/test_omtl.ml +++ b/test/test_omtl.ml @@ -30,35 +30,16 @@ open Omtl (* A module with functions to test *) -module My_String = struct - let equal = String.equal +module Suit = struct + let pass () = () - let capitalize = String.capitalize_ascii - - let str_concat = String.concat "" + let failed () = "Failed !!!" end (* The tests *) -let test_equal () = - if My_String.equal "hello!" "hello!" then - Ok () - else - Error {| My_String.equal "hello!" = "hello!" |} - - -let test_capitalize () = - if String.equal "HELLO!" (My_String.capitalize "hELLO!") then - Ok () - else - Error {| My_String.capitalize "hELLO!" = "HELLO!!" |} - - -let test_str_concat () = - if String.equal "foobar" (My_String.str_concat ["foo"; "bar"]) then - Ok () - else - Error {| My_String.str_concat ["foo"; "bar"] = "foobar" |} +let test_pass () = Suit.pass () |> ok +let test_failed () = Suit.failed () |> err let test_failure () = failwith "Take it easy, this is just an example of a failed test" @@ -67,17 +48,16 @@ let test_exception () = raise Not_found let test_function_running_time () = for _ = 0 to 100000 do () - done; - Ok () + done + |> ok (* Run it *) let _ = "My_String" +:> [ - "equal" >== test_equal; - "capitalize" >== test_capitalize; - "str_concat" >== test_str_concat; + "pass" >== test_pass; + "failed" >== test_failed; "Examples of test failures" >== test_failure; "Examples of undefined exception" >== test_exception; "Test function running time" >== test_function_running_time;