Skip to content

Commit

Permalink
Add tests for -j-gen-modules
Browse files Browse the repository at this point in the history
  • Loading branch information
smondet committed Jan 3, 2025
1 parent ec12e76 commit 923489d
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 5 deletions.
10 changes: 5 additions & 5 deletions atdgen/test/dune
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
(rule
(targets test_int_with_string_repr_j.ml test_int_with_string_repr_j.mli)
(deps test_int_with_string_repr.atd)
(action (run %{bin:atdgen} -j %{deps})))
(action (run %{bin:atdgen} -j -j-gen-modules %{deps})))

(rule
(alias runtest)
Expand Down Expand Up @@ -94,7 +94,7 @@
(targets test_ambiguous_record_j.ml test_ambiguous_record_j.mli)
(deps test_ambiguous_record.atd)
(action
(run %{bin:atdgen} -json -std-json -o test_ambiguous_record_j -open Test_ambiguous_record_t -ntd %{deps})))
(run %{bin:atdgen} -json -std-json -j-gen-modules -o test_ambiguous_record_j -open Test_ambiguous_record_t -ntd %{deps})))

(rule
(targets test_ambiguous_variant_t.ml test_ambiguous_variant_t.mli)
Expand All @@ -106,7 +106,7 @@
(targets test_ambiguous_variant_j.ml test_ambiguous_variant_j.mli)
(deps test_ambiguous_variant.atd)
(action
(run %{bin:atdgen} -j -j-std %{deps})))
(run %{bin:atdgen} -j -j-std -j-gen-modules %{deps})))

(rule
(targets test_polymorphic_wrap_t.ml test_polymorphic_wrap_t.mli)
Expand All @@ -118,7 +118,7 @@
(targets test_polymorphic_wrap_j.ml test_polymorphic_wrap_j.mli)
(deps test_polymorphic_wrap.atd)
(action
(run %{bin:atdgen} -json -std-json -o test_polymorphic_wrap_j %{deps})))
(run %{bin:atdgen} -json -std-json -j-gen-modules -o test_polymorphic_wrap_j %{deps})))

(rule
(alias runtest)
Expand Down Expand Up @@ -343,7 +343,7 @@
(rule
(targets test_abstract_j.ml test_abstract_j.mli)
(deps test_abstract.atd)
(action (run %{bin:atdgen} -j %{deps})))
(action (run %{bin:atdgen} -j -j-gen-modules %{deps})))

(rule
(targets test_abstract_v.ml test_abstract_v.mli)
Expand Down
10 changes: 10 additions & 0 deletions atdgen/test/test_abstract_j.expected.ml
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,13 @@ let read_abs1 read__x = (
)
let abs1_of_string read__x s =
read_abs1 read__x (Yojson.Safe.init_lexer ()) (Lexing.from_string s)


(** {3 Generic Modules } *)
module Any_items = struct
type nonrec t = any_items
let write = write_any_items
let read = read_any_items
let to_string = string_of_any_items
let of_string = any_items_of_string
end
26 changes: 26 additions & 0 deletions atdgen/test/test_abstract_j.expected.mli
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,29 @@ val abs1_of_string :
string -> 'x abs1
(** Deserialize JSON data of type {!type:abs1}. *)



(** {3 Generic Modules } *)
module Any_items : sig
type nonrec t = any_items
val write :
Buffer.t -> any_items -> unit
(** Output a JSON value of type {!type:any_items}. *)

val to_string :
?len:int -> any_items -> string
(** Serialize a value of type {!type:any_items}
into a JSON string.
@param len specifies the initial length
of the buffer used internally.
Default: 1024. *)

val read :
Yojson.Safe.lexer_state -> Lexing.lexbuf -> any_items
(** Input JSON data of type {!type:any_items}. *)

val of_string :
string -> any_items
(** Deserialize JSON data of type {!type:any_items}. *)

end
17 changes: 17 additions & 0 deletions atdgen/test/test_ambiguous_record_j.expected.ml
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,20 @@ let create_ambiguous
ambiguous = ambiguous;
not_ambiguous1 = not_ambiguous1;
}


(** {3 Generic Modules } *)
module Ambiguous' = struct
type nonrec t = ambiguous'
let write = write_ambiguous'
let read = read_ambiguous'
let to_string = string_of_ambiguous'
let of_string = ambiguous'_of_string
end
module Ambiguous = struct
type nonrec t = ambiguous
let write = write_ambiguous
let read = read_ambiguous
let to_string = string_of_ambiguous
let of_string = ambiguous_of_string
end
17 changes: 17 additions & 0 deletions atdgen/test/test_ambiguous_variant_j.expected.ml
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,20 @@ let read_ambiguous = (
)
let ambiguous_of_string s =
read_ambiguous (Yojson.Safe.init_lexer ()) (Lexing.from_string s)


(** {3 Generic Modules } *)
module Ambiguous' = struct
type nonrec t = ambiguous'
let write = write_ambiguous'
let read = read_ambiguous'
let to_string = string_of_ambiguous'
let of_string = ambiguous'_of_string
end
module Ambiguous = struct
type nonrec t = ambiguous
let write = write_ambiguous
let read = read_ambiguous
let to_string = string_of_ambiguous
let of_string = ambiguous_of_string
end
33 changes: 33 additions & 0 deletions atdgen/test/test_atdgen_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,38 @@ let test_raw_json () =
let x' = Test3j_j.t_of_string s in
check (x = x')

let test_generic () =
let module Stringables = struct
module type Mono = sig
type t
val of_string: string -> t
val to_string: ?len: int -> t -> string
end
end in
let test_involution (module M : Stringables.Mono) name input =
let x = M.of_string input in
let s = M.to_string x in
Alcotest.(check string) (sprintf "involution-%s" name) input s;
in
test_involution (module Test_abstract_j.Any_items) "abstract_j" "[]";
test_involution (module Test_int_with_string_repr_j.Afloat) "test_int_with_string_repr_j.Afloat" {|"42"|};
let module Mono_poly_wrap =
struct
include Test_polymorphic_wrap_j.T
type nonrec t = Test_int_with_string_repr_j.int32 t
let of_string =
of_string Test_int_with_string_repr_j.read_int32
let to_string =
to_string Test_int_with_string_repr_j.write_int32
end
in
test_involution (module Mono_poly_wrap) "test_monopoly_wrap" {|["42","4000000"]|};
test_involution (module Test_ambiguous_record_j.Ambiguous) "test_ambiguous_record"
{|{"ambiguous":"Hello","not_ambiguous1":42}|};
test_involution (module Test_ambiguous_variant_j.Ambiguous') "test_ambiguous_variant"
{|["Int",42]|};
()

let test_abstract_types () =
let input = ["a", 1; "b", 2] in
let encoded = Test_abstract_j.string_of_int_assoc_list input in
Expand Down Expand Up @@ -722,6 +754,7 @@ let all_tests : (string * (unit -> unit)) list = [
"json encoding & decoding int with string representation", test_encoding_decoding_int_with_string_repr;
"abstract types", test_abstract_types;
"untyped json", test_untyped_json;
"generic", test_generic;
]

let () =
Expand Down
10 changes: 10 additions & 0 deletions atdgen/test/test_polymorphic_wrap_j.expected.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,13 @@ let read_t read__a = (
)
let t_of_string read__a s =
read_t read__a (Yojson.Safe.init_lexer ()) (Lexing.from_string s)


(** {3 Generic Modules } *)
module T = struct
type nonrec ('a) t = ('a) t
let write = write_t
let read = read_t
let to_string = string_of_t
let of_string = t_of_string
end

0 comments on commit 923489d

Please sign in to comment.