Source file derive.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
(** Runtime support functions for [@@deriving hegel_generator].

    These helpers are used by the code generated by the [ppx_hegel_generator]
    PPX deriver. They are not intended for direct use by end users.

    The PPX generates [Internal.test_case -> 'a] functions that call
    {!Hegel.Generators.draw} internally and return typed OCaml values. These
    helpers provide the plumbing for option and list types. *)

open! Core

(** [generate_option tc gen_fn] generates an [option] value.

    Uses {!Generators.booleans} to decide between [Some] and [None]. When [Some]
    is chosen, calls [gen_fn tc] to produce the inner value. *)
let generate_option tc gen_fn =
  let b = Generators.draw tc (Generators.booleans ()) in
  if b then Some (gen_fn tc) else None
;;

(** [generate_list tc gen_fn] generates a list of values.

    Uses {!Generators.integers} to determine the list length (0-20), then calls
    [gen_fn tc] for each element. *)
let generate_list tc gen_fn =
  let len = Generators.draw tc (Generators.integers ~min_value:0 ~max_value:20 ()) in
  List.init len ~f:(fun _ -> gen_fn tc)
;;