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 all commits
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) ->
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_signatures)
(libraries qcheck-alcotest ppxlib ppx_deriving_qcheck qcheck-core)
(preprocess (pps ppxlib.metaquot ppx_deriving_qcheck)))
30 changes: 30 additions & 0 deletions test/ppx_deriving_qcheck/deriver/qcheck2/test_signatures.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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 qcheck2]

type string' = string [@@deriving qcheck2]
end = struct
type t = int [@@deriving qcheck2]

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

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

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

(** {2. Execute tests} *)

let () = Alcotest.run "Test_Signatures"
[("Signatures",
Alcotest.[
test_case "test_int (sig)" `Quick test_int;
test_case "test_string (sig)" `Quick test_string;
])]