Skip to content

Commit

Permalink
Add more tests for inline-records (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
smondet committed Jan 3, 2025
1 parent 720638c commit 429dc0b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
4 changes: 3 additions & 1 deletion atdgen/test/dune
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@
(rule
(targets test_classic_inline_record_j.ml test_classic_inline_record_j.mli)
(deps test_classic_inline_record.atd)
(action (run %{bin:atdgen} -j %{deps})))
(action (run %{bin:atdgen} -j-std -j %{deps})))

(rule
(alias runtest)
Expand Down Expand Up @@ -416,6 +416,8 @@
test_ppx_t
test_abstract_t
test_abstract_j
test_classic_inline_record_t
test_classic_inline_record_j
))

(rule
Expand Down
15 changes: 15 additions & 0 deletions atdgen/test/test_atdgen_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,20 @@ let test_untyped_json () =
let encoded = Test_abstract_j.string_of_any_items decoded in
check (encoded = input)

let test_inline_records () =
let open Test_classic_inline_record_t in
let foo = Foo { x = 42; y = 5.1 } in
Alcotest.(check string)
"test_inline_records"
(Test_classic_inline_record_j.string_of_foo foo)
{|["Foo",{"x":42,"y":5.1}]|};
Alcotest.(check bool)
"test_inline_records-involution"
(foo = (Test_classic_inline_record_j.string_of_foo foo
|> Test_classic_inline_record_j.foo_of_string))
true;
()

let all_tests : (string * (unit -> unit)) list = [
"ocaml internals", test_ocaml_internals;
"biniou missing record fields", test_biniou_missing_field;
Expand Down Expand Up @@ -722,6 +736,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;
"inline-records", test_inline_records;
]

let () =
Expand Down
8 changes: 3 additions & 5 deletions atdgen/test/test_classic_inline_record_j.expected.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ let write_foo : _ -> foo -> _ = (
fun ob (x : foo) ->
match x with
| Foo x ->
Buffer.add_string ob "<\"Foo\":";
Buffer.add_string ob "{";
Buffer.add_string ob "[\"Foo\",";
begin
Buffer.add_char ob '{';
let is_first = ref true in
Expand All @@ -27,13 +26,12 @@ let write_foo : _ -> foo -> _ = (
Buffer.add_char ob ',';
Buffer.add_string ob "\"y\":";
(
Yojson.Safe.write_float
Yojson.Safe.write_std_float
)
ob x.y;
Buffer.add_char ob '}';
end (* ob x *);
Buffer.add_string ob "}";
Buffer.add_char ob '>'
Buffer.add_char ob ']'
)
let string_of_foo ?(len = 1024) x =
let ob = Buffer.create len in
Expand Down

0 comments on commit 429dc0b

Please sign in to comment.