Skip to content

Commit

Permalink
[Update]: (ocaml (>= 4.13.1))
Browse files Browse the repository at this point in the history
  • Loading branch information
muqiuhan committed Mar 12, 2024
1 parent 91909b0 commit 5d1c7c4
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 70 deletions.
1 change: 1 addition & 0 deletions .github/workflows/Linux.yml
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/MacOS.yml
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/Windows.yml
Expand Up @@ -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
Expand Down
42 changes: 15 additions & 27 deletions README.md
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion dune-project
Expand Up @@ -22,7 +22,7 @@
(description "OCaml Minimalist Testing Library")
(depends
(ocaml
(>= 5.0.0))
(>= 4.13.1))
dune)
(tags
(testing "test framework")))
Expand Down
4 changes: 2 additions & 2 deletions lib/color.ml
Expand Up @@ -28,7 +28,7 @@
*)

type color =
| Ok
| Pass
| Fail
| Fail_info
| Time
Expand All @@ -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";
Expand Down
6 changes: 5 additions & 1 deletion lib/info.ml
Expand Up @@ -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

Expand All @@ -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

Expand Down
8 changes: 4 additions & 4 deletions lib/omtl.ml
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/types.ml
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions lib/utils.ml
Expand Up @@ -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
2 changes: 1 addition & 1 deletion omtl.opam
Expand Up @@ -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}
]
Expand Down
38 changes: 9 additions & 29 deletions test/test_omtl.ml
Expand Up @@ -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"

Expand All @@ -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;
Expand Down

0 comments on commit 5d1c7c4

Please sign in to comment.