Skip to content

Commit

Permalink
Initial implementation of nestable test API
Browse files Browse the repository at this point in the history
  • Loading branch information
craigfe committed May 4, 2021
1 parent cf54cde commit 60a8a28
Show file tree
Hide file tree
Showing 150 changed files with 3,158 additions and 1,390 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
all:
dune build

doc:
dune build @doc --force
cp -r _build/default/_doc/_html doc/static/api

test:
dune runtest

Expand Down
2 changes: 1 addition & 1 deletion alcotest-async.opam
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ homepage: "https://github.com/mirage/alcotest"
doc: "https://mirage.github.io/alcotest"
bug-reports: "https://github.com/mirage/alcotest/issues"
depends: [
"dune" {>= "2.2"}
"dune" {>= "2.4"}
"re" {with-test}
"fmt" {with-test}
"cmdliner" {with-test}
Expand Down
2 changes: 1 addition & 1 deletion alcotest-lwt.opam
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ homepage: "https://github.com/mirage/alcotest"
doc: "https://mirage.github.io/alcotest"
bug-reports: "https://github.com/mirage/alcotest/issues"
depends: [
"dune" {>= "2.2"}
"dune" {>= "2.4"}
"re" {with-test}
"cmdliner" {with-test}
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion alcotest-mirage.opam
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ homepage: "https://github.com/mirage/alcotest"
doc: "https://mirage.github.io/alcotest"
bug-reports: "https://github.com/mirage/alcotest/issues"
depends: [
"dune" {>= "2.2"}
"dune" {>= "2.4"}
"re" {with-test}
"cmdliner" {with-test}
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion alcotest.opam
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ homepage: "https://github.com/mirage/alcotest"
doc: "https://mirage.github.io/alcotest"
bug-reports: "https://github.com/mirage/alcotest/issues"
depends: [
"dune" {>= "2.2"}
"dune" {>= "2.4"}
"ocaml" {>= "4.03.0"}
"fmt" {>= "0.8.7"}
"astring"
Expand Down
7 changes: 5 additions & 2 deletions dune-project
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(lang dune 2.2)
(implicit_transitive_deps false)
(lang dune 2.4)
(generate_opam_files true)
(using mdx 0.1)

(name alcotest)
(source (github mirage/alcotest))
Expand Down Expand Up @@ -80,3 +80,6 @@ tests to run.
duration
lwt
logs))

(package
(name ppx_alcotest))
2 changes: 2 additions & 0 deletions examples/bad/bad.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ For more information, please refer to <http://unlicense.org/>

(* Run with [dune exec ./examples/bad/bad.exe] *)

module Alcotest = Alcotest.V1

(* A module with functions to test *)
module To_test = struct
let capitalise = Astring.String.Ascii.uppercase
Expand Down
2 changes: 1 addition & 1 deletion examples/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(tests
(names simple floats)
(names simple floats new)
(package alcotest)
(libraries alcotest))
2 changes: 2 additions & 0 deletions examples/floats.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*)

module Alcotest = Alcotest.V1

let e = epsilon_float

let nan () =
Expand Down
1 change: 1 addition & 0 deletions examples/lwt/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ For more information, please refer to <http://unlicense.org/>
*)

open Lwt.Infix
module Alcotest_lwt = Alcotest_lwt.V1

exception Library_exception

Expand Down
46 changes: 46 additions & 0 deletions examples/new.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
(* A module with functions to test *)
module To_test = struct
let lowercase = String.lowercase_ascii
let capitalize = String.capitalize_ascii
let str_concat = String.concat ""
let list_concat = List.append
end

(* The tests *)
let test_lowercase () =
Alcotest.(check string) "same string" "hello!" (To_test.lowercase "hELLO!")

let test_capitalize () =
Alcotest.(check string) "same string" "World." (To_test.capitalize "world.")

let test_str_concat () =
Alcotest.(check string)
"same string" "foobar"
(To_test.str_concat [ "foo"; "bar" ])

let test_list_concat () =
Alcotest.(check (list int))
"same lists" [ 1; 2; 3 ]
(To_test.list_concat [ 1 ] [ 2; 3 ])

let () =
let open Alcotest in
print_endline "";
run
~config:(Config.v ~and_exit:false ())
~name:__FILE__
[
group ~name:"string-case"
[
test ~name:"Lower case" test_lowercase;
group ~name:"Further_nested"
[
test ~name:"alpha" test_lowercase;
test ~name:"beta" test_lowercase;
];
test ~name:"Capitalization" test_capitalize;
];
group ~name:"string-concat"
[ test ~name:"String mashing" test_str_concat ];
group ~name:"list-concat" [ test ~name:"List mashing" test_list_concat ];
]
6 changes: 4 additions & 2 deletions examples/simple.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*)

module Alcotest = Alcotest.V1

(* A module with functions to test *)
module To_test = struct
let lowercase = String.lowercase_ascii
Expand Down Expand Up @@ -54,9 +56,9 @@ let test_list_concat () =
let () =
Alcotest.run "Utils"
[
( "string-case",
( "String case",
[
Alcotest.test_case "Lower case" `Quick test_lowercase;
Alcotest.test_case "Lower" `Quick test_lowercase;
Alcotest.test_case "Capitalization" `Quick test_capitalize;
] );
( "string-concat",
Expand Down
25 changes: 25 additions & 0 deletions ppx_alcotest.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
maintainer: ["[email protected]"]
authors: ["Thomas Gazagnaire"]
license: "ISC"
homepage: "https://github.com/mirage/alcotest"
bug-reports: "https://github.com/mirage/alcotest/issues"
depends: [
"dune" {>= "2.4"}
]
build: [
["dune" "subst"] {pinned}
[
"dune"
"build"
"-p"
name
"-j"
jobs
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
]
dev-repo: "git+https://github.com/mirage/alcotest.git"
39 changes: 24 additions & 15 deletions src/alcotest-async/alcotest_async.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@ open Core
open Async_kernel
open Async_unix

module Tester =
Alcotest_engine.Cli.Make
(Alcotest.Unix)
(struct
include Deferred
module M = struct
include Deferred

let bind x f = bind x ~f
let bind x f = bind x ~f

let catch t on_error =
try_with t >>= function Ok a -> return a | Error exn -> on_error exn
end)

include Tester

let test_case_sync n s f = test_case n s (fun x -> Deferred.return (f x))
let catch t on_error =
try_with t >>= function Ok a -> return a | Error exn -> on_error exn
end

let run_test timeout name fn args =
Clock.with_timeout timeout (fn args) >>| function
Expand All @@ -26,5 +19,21 @@ let run_test timeout name fn args =
(Printf.sprintf "%s timed out after %s" name
(Time.Span.to_string_hum timeout))

let test_case ?(timeout = sec 2.) name s f =
test_case name s (run_test timeout name f)
module Tester = Alcotest_engine.Cli.Make (Alcotest.Unix) (M)
include Tester

let test_sync ?here ?tag ?tags ~name fn =
test ?here ?tag ?tags ~name (fun x -> Deferred.return (fn x))

let test ?here ?tag ?tags ~name ?(timeout = sec 2.) fn =
test ?here ?tag ?tags ~name (run_test timeout name fn)

module V1 = struct
module Tester = Alcotest_engine.Cli.Make_v1 (Alcotest.Unix) (M)
include Tester

let test_case_sync n s f = test_case n s (fun x -> Deferred.return (f x))

let test_case ?(timeout = sec 2.) name s f =
test_case name s (run_test timeout name f)
end
33 changes: 25 additions & 8 deletions src/alcotest-async/alcotest_async.mli
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,31 @@
(** [Alcotest_async] enables testing functions which return an Async deferred.
{!run} returns a deferred which will run the tests when scheduled. *)

include Alcotest_engine.Cli.S with type return = unit Async_kernel.Deferred.t
include
Alcotest_engine.Cli.S
with type 'a m := 'a Async_kernel.Deferred.t
and type 'a test_args := 'a
and type config := Alcotest_engine.Config.User.t
and type tag := Alcotest_engine.Tag.t
and type tag_set := Alcotest_engine.Tag.Set.t

val test_case :
?timeout:Core_kernel.Time.Span.t ->
string ->
Alcotest.speed_level ->
val test :
(?timeout:Core_kernel.Time.Span.t ->
('a -> unit Async_kernel.Deferred.t) ->
'a test_case
'a test)
Alcotest_engine.Core.identified

val test_case_sync :
string -> Alcotest.speed_level -> ('a -> unit) -> 'a test_case
val test_sync : (('a -> unit) -> 'a test) Alcotest_engine.Core.identified

module V1 : sig
include Alcotest_engine.Cli.V1 with type return = unit Async_kernel.Deferred.t

val test_case :
?timeout:Core_kernel.Time.Span.t ->
string ->
speed_level ->
('a -> unit Async_kernel.Deferred.t) ->
'a test_case

val test_case_sync : string -> speed_level -> ('a -> unit) -> 'a test_case
end
3 changes: 3 additions & 0 deletions src/alcotest-engine/.ocamlformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module-item-spacing = compact
# break-separators = before
# dock-collection-brackets = false
9 changes: 6 additions & 3 deletions src/alcotest-engine/alcotest_engine.ml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
module Test = Test
module Core = Core
module Cli = Cli
module Config = Config
module Core = Core
module Monad = Monad
module Platform = Platform
module Source_code_position = Source_code_position
module Stdlib_ext = Stdlib_ext
module Tag = Tag
module Test = Test

module Private = struct
module Utils = Utils
module Pp = Pp
end
6 changes: 5 additions & 1 deletion src/alcotest-engine/alcotest_engine.mli
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ module Monad = Monad
module Platform = Platform
(** Defines platform-dependent functions. *)

module Tag = Tag
module Config = Config
module Source_code_position = Source_code_position
module Stdlib_ext = Stdlib_ext

(** These modules are exposed for use internally by other Alcotest packages.
They do not provide a stable interface. *)
module Private : sig
module Utils = Utils
module Pp = Pp
end
Loading

0 comments on commit 60a8a28

Please sign in to comment.