From 923489d3175a8bd818e10639cfc45c67142604e9 Mon Sep 17 00:00:00 2001 From: Sebastien Mondet Date: Fri, 3 Jan 2025 16:16:31 -0500 Subject: [PATCH] Add tests for `-j-gen-modules` --- atdgen/test/dune | 10 +++--- atdgen/test/test_abstract_j.expected.ml | 10 ++++++ atdgen/test/test_abstract_j.expected.mli | 26 +++++++++++++++ .../test/test_ambiguous_record_j.expected.ml | 17 ++++++++++ .../test/test_ambiguous_variant_j.expected.ml | 17 ++++++++++ atdgen/test/test_atdgen_main.ml | 33 +++++++++++++++++++ .../test/test_polymorphic_wrap_j.expected.ml | 10 ++++++ 7 files changed, 118 insertions(+), 5 deletions(-) diff --git a/atdgen/test/dune b/atdgen/test/dune index 87417b0c..1b71db5f 100644 --- a/atdgen/test/dune +++ b/atdgen/test/dune @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/atdgen/test/test_abstract_j.expected.ml b/atdgen/test/test_abstract_j.expected.ml index e1d5a62e..c7f24af8 100644 --- a/atdgen/test/test_abstract_j.expected.ml +++ b/atdgen/test/test_abstract_j.expected.ml @@ -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 diff --git a/atdgen/test/test_abstract_j.expected.mli b/atdgen/test/test_abstract_j.expected.mli index 30a36cfc..416d7c99 100644 --- a/atdgen/test/test_abstract_j.expected.mli +++ b/atdgen/test/test_abstract_j.expected.mli @@ -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 diff --git a/atdgen/test/test_ambiguous_record_j.expected.ml b/atdgen/test/test_ambiguous_record_j.expected.ml index 29884668..02238e65 100644 --- a/atdgen/test/test_ambiguous_record_j.expected.ml +++ b/atdgen/test/test_ambiguous_record_j.expected.ml @@ -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 diff --git a/atdgen/test/test_ambiguous_variant_j.expected.ml b/atdgen/test/test_ambiguous_variant_j.expected.ml index 2192fe4b..e5432e30 100644 --- a/atdgen/test/test_ambiguous_variant_j.expected.ml +++ b/atdgen/test/test_ambiguous_variant_j.expected.ml @@ -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 diff --git a/atdgen/test/test_atdgen_main.ml b/atdgen/test/test_atdgen_main.ml index c6636730..620dded4 100644 --- a/atdgen/test/test_atdgen_main.ml +++ b/atdgen/test/test_atdgen_main.ml @@ -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 @@ -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 () = diff --git a/atdgen/test/test_polymorphic_wrap_j.expected.ml b/atdgen/test/test_polymorphic_wrap_j.expected.ml index 2c0e1356..58ae5275 100644 --- a/atdgen/test/test_polymorphic_wrap_j.expected.ml +++ b/atdgen/test/test_polymorphic_wrap_j.expected.ml @@ -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