File: fields_test.ml

package info (click to toggle)
ppx-fields-conv 0.17.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 276 kB
  • sloc: ml: 2,177; sh: 37; makefile: 14
file content (244 lines) | stat: -rw-r--r-- 6,600 bytes parent folder | download
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
module Simple : sig
  type t =
    { x : int
    ; w : int
    }
  [@@deriving
    fields
      ~iterators:(create, fold_right, for_all, exists, map, to_list, fold, iter)
      ~direct_iterators:(for_all, exists, map, to_list, fold, fold_right, iter)]
end = struct
  type t =
    { x : int
    ; w : int
    }
  [@@deriving
    fields
      ~iterators:(create, fold_right, for_all, exists, map, to_list, fold, iter)
      ~direct_iterators:(for_all, exists, map, to_list, fold, fold_right, iter)]

  let%test _ = Fields.create ~x:2 ~w:4 = { x = 2; w = 4 }
  let all_even = { x = 2; w = 4 }
  let some_even = { x = 1; w = 4 }
  let none_even = { x = 1; w = 3 }
  let is_even t field = Fieldslib.Field.get field t mod 2 = 0
  let%test _ = Fields.for_all ~x:(is_even all_even) ~w:(is_even all_even) = true
  let%test _ = Fields.for_all ~x:(is_even some_even) ~w:(is_even some_even) = false
  let%test _ = Fields.exists ~x:(is_even some_even) ~w:(is_even some_even) = true
  let%test _ = Fields.exists ~x:(is_even none_even) ~w:(is_even none_even) = false

  let is_even field t n =
    assert (Fieldslib.Field.get field t = n);
    n mod 2 = 0
  ;;

  let%test _ = Fields.Direct.for_all all_even ~x:is_even ~w:is_even = true
  let%test _ = Fields.Direct.for_all some_even ~x:is_even ~w:is_even = false
  let%test _ = Fields.Direct.exists some_even ~x:is_even ~w:is_even = true
  let%test _ = Fields.Direct.exists none_even ~x:is_even ~w:is_even = false
  let t = { x = 1; w = 3 }
  let add_one t field = Fieldslib.Field.get field t + 1
  let%test _ = Fields.map ~x:(add_one t) ~w:(add_one t) = { x = 2; w = 4 }
  let%test _ = Fields.to_list ~x:(add_one t) ~w:(add_one t) = [ 2; 4 ]

  let add_one field t n =
    assert (Fieldslib.Field.get field t = n);
    n + 1
  ;;

  let%test _ = Fields.Direct.map t ~x:add_one ~w:add_one = { x = 2; w = 4 }
  let%test _ = Fields.Direct.to_list t ~x:add_one ~w:add_one = [ 2; 4 ]
  let fold_one t acc field = (Fieldslib.Field.get field t + 1) :: acc
  let%test _ = Fields.fold ~init:[] ~x:(fold_one t) ~w:(fold_one t) = [ 4; 2 ]
  let fold_one t field acc = (Fieldslib.Field.get field t + 1) :: acc
  let%test _ = Fields.fold_right ~x:(fold_one t) ~w:(fold_one t) ~init:[] = [ 2; 4 ]

  let fold_one acc field t n =
    assert (Fieldslib.Field.get field t = n);
    (n + 1) :: acc
  ;;

  let%test _ = Fields.Direct.fold t ~init:[] ~x:fold_one ~w:fold_one = [ 4; 2 ]

  let fold_one field t n acc =
    assert (Fieldslib.Field.get field t = n);
    (n + 1) :: acc
  ;;

  let%test _ = Fields.Direct.fold_right t ~x:fold_one ~w:fold_one ~init:[] = [ 2; 4 ]
  let iter_one t buf field = buf := (Fieldslib.Field.get field t + 1) :: !buf

  let%test _ =
    let buf = ref [] in
    Fields.iter ~x:(iter_one t buf) ~w:(iter_one t buf);
    !buf = [ 4; 2 ]
  ;;

  let iter_one buf field t n =
    assert (Fieldslib.Field.get field t = n);
    buf := (n + 1) :: !buf
  ;;

  let%test _ =
    let buf = ref [] in
    Fields.Direct.iter t ~x:(iter_one buf) ~w:(iter_one buf);
    !buf = [ 4; 2 ]
  ;;
end

module Rec = struct
  type a = { something1 : b }
  and b = A of a [@@deriving fields ~getters]

  let _ = something1
end

module Multiple_names = struct
  type a = { a : int }
  and b = { b : int } [@@deriving fields ~getters ~fields]

  let%test _ = b { b = 1 } = 1
  let%test _ = a { a = 1 } = 1
  let _ = Fields_of_a.a
  let _ = Fields_of_b.b
  let _ = (Fields_of_a.a : (_, _) Fieldslib.Field.t :> (_, _) Fieldslib.Field.readonly_t)
end

module Private : sig
  type t = private
    { a : int
    ; mutable b : int
    }
  [@@deriving fields ~fields ~iterators:(fold, map_poly)]
end = struct
  type u =
    { a : int
    ; mutable b : int
    }

  type t = u = private
    { a : int
    ; mutable b : int
    }
  [@@deriving fields ~fields ~iterators:(fold, map_poly)]

  (* let _ = Fieldslib.Field.setter Fields.a *)
end

(* let _ = Fieldslib.Field.setter Private.Fields.a *)
let _ = Private.Fields.fold
let _ = Private.Fields.a
let _ = Fieldslib.Field.name Private.Fields.a
let (_ : Private.t -> int) = Fieldslib.Field.get Private.Fields.a

let _ =
  Private.Fields.map_poly
    { Fieldslib.Field.f =
        (fun f ->
          let (_ : Private.t -> _) = Fieldslib.Field.get f in
          ())
    }
;;

module Warnings : sig
  (* could generate an unused warning but for crazy reasons, only
     when the type is private *)
  type t = private { foo : int } [@@deriving fields ~getters]

  val foo : string
end = struct
  type t = { foo : int } [@@deriving fields ~getters]

  let foo = "a"
end

module Wildcard : sig
  type _ t =
    { x : int
    ; y : string
    }
end = struct
  type _ t =
    { x : int
    ; y : string
    }
  [@@deriving fields ~getters]

  let _ = x
  let _ = y
end

let%test_module "set_all_mutable_fields" =
  (module struct
    module M : sig
      type 'a t =
        { mutable a : int
        ; b : string
        ; mutable c : 'a
        }
      [@@deriving fields ~direct_iterators:set_all_mutable_fields]
    end = struct
      type 'a t =
        { mutable a : int
        ; b : string
        ; mutable c : 'a
        }
      [@@deriving fields ~direct_iterators:set_all_mutable_fields]
    end

    open M

    let%test_unit _ =
      let t : _ t = { a = 0; b = ""; c = nan } in
      let final_t : _ t = { a = 12; b = t.b; c = 12. } in
      Fields.Direct.set_all_mutable_fields t ~a:final_t.a ~c:final_t.c;
      assert (t = final_t)
    ;;
  end)
;;

(* Sometimes it's convenient for the type of the accumulator to change as you handle
   the individual fields. *)
module M (F1 : sig
  type t =
    { a : int
    ; b : string
    ; c : bool
    }
  [@@deriving fields ~iterators:(create, fold) ~direct_iterators:fold]
end) (F2 : sig
  type t =
    { a : int
    ; b : string
    }
  [@@deriving fields ~iterators:create]
end) =
struct
  let convert : F1.t -> F2.t =
    F1.Fields.Direct.fold
      ~init:F2.Fields.create
      ~a:(fun acc field x _ -> acc ~a:(Fieldslib.Field.get field x))
      ~b:(fun acc field x _ -> acc ~b:(Fieldslib.Field.get field x))
      ~c:(fun acc _field _x _ -> acc)
  ;;

  let construct () : F1.t =
    F1.Fields.fold
      ~init:F1.Fields.create
      ~a:(fun f _ -> f ~a:8)
      ~b:(fun f _ -> f ~b:"foo")
      ~c:(fun f _ -> f ~c:true)
  ;;
end

(* We expect no unused value, unused type, unused module warnings, as only a part of the
   generated code is used in normal circumstances. *)
module Unused_warnings : sig end = struct
  [@@@ocaml.warning "+60"]

  type t =
    { a : int
    ; b : int
    }
  [@@deriving fields ~getters ~iterators:for_all ~direct_iterators:for_all]
end