Skip to content

Commit

Permalink
Fix and test variants
Browse files Browse the repository at this point in the history
  • Loading branch information
vch9 committed Dec 13, 2021
1 parent 01bf684 commit f7570c9
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/ppx_deriving_qcheck/QCheck_generators.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ open Ppxlib

(** {2. Type} *)

let ty = "QCheck.Gen.t"
let ty = Ldot (Ldot (Lident "QCheck", "Gen"), "t")

(** {2. Primitive generators} *)

Expand Down
2 changes: 1 addition & 1 deletion src/ppx_deriving_qcheck/ppx_deriving_qcheck.ml
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ and gen_from_variant ~loc ~env rws =
in
let gen = gen_sized ~loc is_rec to_gen rws in
let typ_t = A.ptyp_constr (A.Located.mk @@ Lident env.curr_type) [] in
let typ_gen = A.Located.mk @@ Lident G.ty in
let typ_gen = A.Located.mk G.ty in
let typ = A.ptyp_constr typ_gen [ typ_t ] in
[%expr ([%e gen] : [%t typ])]
Expand Down
3 changes: 2 additions & 1 deletion test/ppx_deriving_qcheck/deriver/dune
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
test_primitives
test_qualified_names
test_recursive
test_tuple)
test_tuple
test_variants)
(libraries qcheck-alcotest ppxlib ppx_deriving_qcheck qcheck)
(preprocess (pps ppxlib.metaquot ppx_deriving_qcheck)))
81 changes: 81 additions & 0 deletions test/ppx_deriving_qcheck/deriver/test_variants.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
open QCheck
open Helpers

(** {1. Test variants and polymorphic variants derivation} *)

(** {2. Variants} *)

type colors = Red | Green | Blue [@@deriving qcheck]

let pp_colors fmt x =
let open Format in
match x with
| Red -> fprintf fmt "Red"
| Green -> fprintf fmt "Green"
| Blue -> fprintf fmt "Blue"

let eq_colors = Alcotest.of_pp pp_colors

let gen = Gen.oneofl [Red; Green; Blue]

let test_variants () =
test_compare ~msg:"Gen.oneofl <=> deriving variants" ~eq:eq_colors gen gen_colors

type poly_colors = [`Red | `Green | `Blue] [@@deriving qcheck]

let pp_poly_colors fmt x =
let open Format in
match x with
| `Red -> fprintf fmt "`Red"
| `Green -> fprintf fmt "`Green"
| `Blue -> fprintf fmt "`Blue"

let eq_poly_colors = Alcotest.of_pp pp_poly_colors

let gen_poly : poly_colors Gen.t = Gen.oneofl [`Red; `Green; `Blue]

let test_poly_variants () =
test_compare ~msg:"Gen.oneofl <=> deriving variants"
~eq:eq_poly_colors gen_poly gen_poly_colors

(** {2. Tests weight} *)

type letters =
| A [@weight 0]
| B
[@@deriving qcheck]

let test_weight =
Test.make ~name:"gen_letters always produces B"
(make gen_letters)
(function
| A -> false
| B -> true)
|>
QCheck_alcotest.to_alcotest

type poly_letters = [
| `A [@weight 0]
| `B
]
[@@deriving qcheck]

let test_weight_poly =
Test.make ~name:"gen_poly_letters always produces B"
(make gen_poly_letters)
(function
| `A -> false
| `B -> true)
|>
QCheck_alcotest.to_alcotest

(** {2. Execute tests} *)

let () = Alcotest.run "Test_Variant"
[("Variants",
Alcotest.[
test_case "test_variants" `Quick test_variants;
test_case "test_poly_variants" `Quick test_poly_variants;
test_weight;
test_weight_poly
])]

0 comments on commit f7570c9

Please sign in to comment.