-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
85 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
])] |