Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sig expander to qcheck2 #286

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/ppx_deriving_qcheck/ppx_deriving_qcheck.ml
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,27 @@ let gen_expander_qcheck = Deriving.Generator.V2.make_noarg create_arbs

let gen_expander_qcheck2 = Deriving.Generator.V2.make_noarg (create_gens `QCheck2)

let _ = Deriving.add "qcheck" ~str_type_decl:gen_expander_qcheck

let _ = Deriving.add "qcheck2" ~str_type_decl:gen_expander_qcheck2
let gen_sig_expander_qcheck2 =
Deriving.Generator.V2.make_noarg
(fun ~ctxt (_rec_flag, tds) ->
mbarbin marked this conversation as resolved.
Show resolved Hide resolved
let loc = Expansion_context.Deriver.derived_item_loc ctxt in
let (module A) = Ast_builder.make loc in
tds |> List.map (fun td ->
let type_name = td.ptype_name.txt in
A.psig_value
(A.value_description
~name:(A.Located.mk (name type_name))
~type_:(A.ptyp_constr (A.Located.mk (Ldot (Ldot (Lident "QCheck2", "Gen"), "t")))
[ A.ptyp_constr (A.Located.mk (Lident type_name)) [] ])
~prim:[])))

let _ =
Deriving.add
"qcheck"
~str_type_decl:gen_expander_qcheck

let _ =
Deriving.add
"qcheck2"
~str_type_decl:gen_expander_qcheck2
~sig_type_decl:gen_sig_expander_qcheck2
3 changes: 2 additions & 1 deletion test/ppx_deriving_qcheck/deriver/qcheck2/dune
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
test_recursive
test_tuple
test_variants
test_record)
test_record
test_sig)
(libraries qcheck-alcotest ppxlib ppx_deriving_qcheck qcheck-core)
(preprocess (pps ppxlib.metaquot ppx_deriving_qcheck)))
42 changes: 42 additions & 0 deletions test/ppx_deriving_qcheck/deriver/qcheck2/test_sig.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
open QCheck2
open Helpers

(* Here we check that deriving qcheck2 works in signature, and cover the cases
where the type is named "t" and when it is not. *)

module T : sig
type t = int [@@deriving_inline qcheck2]
include sig
[@@@ocaml.warning "-32"]
val gen : t QCheck2.Gen.t
end
[@@ocaml.doc "@inline"]
[@@@deriving.end]

type string' = string [@@deriving_inline qcheck2]
include sig
[@@@ocaml.warning "-32"]
val gen_string' : string' QCheck2.Gen.t
end
[@@ocaml.doc "@inline"]
[@@@deriving.end]
end = struct
type t = int [@@deriving qcheck2]

type string' = string [@@deriving qcheck2]
end

let test_int () =
test_compare ~msg:"Gen.int <=> deriving int" ~eq:Alcotest.int Gen.int T.gen

let test_string () =
test_compare ~msg:"Gen.string <=> deriving string" ~eq:Alcotest.string Gen.string T.gen_string'

(** {2. Execute tests} *)

let () = Alcotest.run "Test_Primitives"
[("Primitives",
Alcotest.[
test_case "test_int" `Quick test_int;
test_case "test_unit" `Quick test_string;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All 6 test titles here seem copy-pasted from test_primitives.ml (which is a natural starting point).
To separate their outcome in the log from the other ones, it would make sense to include the keyword "signature" in them.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I am not familiar with alcotest, and had missed the naming pattern in use. I've made an attempt to improve based on your comment. Thank you!

])]