Skip to content

Commit

Permalink
Add sig expander to qcheck2
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbin committed Jan 20, 2024
1 parent 15a2477 commit 806699e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
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_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;
])]

0 comments on commit 806699e

Please sign in to comment.