Skip to content
This repository has been archived by the owner on May 19, 2024. It is now read-only.

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andreypopp committed Apr 3, 2024
1 parent f026bee commit 5fb4451
Show file tree
Hide file tree
Showing 3 changed files with 271 additions and 2 deletions.
4 changes: 2 additions & 2 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
update-test: ppx_deriving_json_native.t ppx_deriving_json_browser.t

ppx_deriving_json_browser.t: example.ml
bash ./gen_test.sh ../native/ppx_deriving_json_native_test.exe > $@
bash ./gen_test.sh ../browser/ppx_deriving_json_browser_test.exe > $@

ppx_deriving_json_native.t: example.ml
bash ./gen_test.sh ../browser/ppx_deriving_json_browser_test.exe > $@
bash ./gen_test.sh ../native/ppx_deriving_json_native_test.exe > $@
135 changes: 135 additions & 0 deletions test/ppx_deriving_json_browser.t
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,69 @@
let _ = record_to_json
end [@@ocaml.doc "@inline"] [@@merlin.hide]

$ cat <<"EOF" | run
> type record_aliased = { name : string; [@json.key "my_name"] age : int; [@json.key "my_age"] [@json.default 100] } [@@deriving json]
> EOF
type record_aliased = {
name : string; [@json.key "my_name"]
age : int; [@json.key "my_age"] [@json.default 100]
}
[@@deriving json]

include struct
let _ = fun (_ : record_aliased) -> ()

[@@@ocaml.warning "-39-11-27"]

let rec record_aliased_of_json =
(fun x ->
if
not
(Js.typeof x = "object"
&& (not (Js.Array.isArray x))
&& not ((Obj.magic x : 'a Js.null) == Js.null))
then
Ppx_deriving_json_runtime.of_json_error "expected a JSON object";
let fs =
(Obj.magic x
: < my_name : Js.Json.t Js.undefined
; my_age : Js.Json.t Js.undefined >
Js.t)
in
{
name =
(match Js.Undefined.toOption fs##my_name with
| Stdlib.Option.Some v -> string_of_json v
| Stdlib.Option.None ->
Ppx_deriving_json_runtime.of_json_error
"missing field \"my_name\"");
age =
(match Js.Undefined.toOption fs##my_age with
| Stdlib.Option.Some v -> int_of_json v
| Stdlib.Option.None -> 100);
}
: Js.Json.t -> record_aliased)
let _ = record_aliased_of_json
[@@@ocaml.warning "-39-11-27"]
let rec record_aliased_to_json =
(fun x ->
match x with
| { name = x_name; age = x_age } ->
(Obj.magic
[%mel.obj
{
my_name = string_to_json x_name;
my_age = int_to_json x_age;
}]
: Js.Json.t)
: record_aliased -> Js.Json.t)
let _ = record_aliased_to_json
end [@@ocaml.doc "@inline"] [@@merlin.hide]
$ cat <<"EOF" | run
> type sum = A | B of int | C of { name : string } [@@deriving json]
> EOF
Expand Down Expand Up @@ -536,3 +599,75 @@
let _ = polyrecur_to_json
end [@@ocaml.doc "@inline"] [@@merlin.hide]

$ cat <<"EOF" | run
> type evar = A | B [@json.as "b_aliased"] [@@deriving json]
> EOF
type evar = A | B [@json.as "b_aliased"] [@@deriving json]

include struct
let _ = fun (_ : evar) -> ()

[@@@ocaml.warning "-39-11-27"]

let rec evar_of_json =
(fun x ->
let tag = Ppx_deriving_json_runtime.Primitives.string_of_json x in
if tag = "A" then A
else if tag = "b_aliased" then B
else Ppx_deriving_json_runtime.of_json_error "invalid JSON"
: Js.Json.t -> evar)

let _ = evar_of_json

[@@@ocaml.warning "-39-11-27"]

let rec evar_to_json =
(fun x ->
match x with
| A -> (Obj.magic (string_to_json "A") : Js.Json.t)
| B -> (Obj.magic (string_to_json "b_aliased") : Js.Json.t)
: evar -> Js.Json.t)

let _ = evar_to_json
end [@@ocaml.doc "@inline"] [@@merlin.hide]

$ cat <<"EOF" | run
> type epoly = [ `a [@json.as "A_aliased"] | `b ] [@@deriving json]
> EOF
type epoly = [ `a [@json.as "A_aliased"] | `b ] [@@deriving json]

include struct
let _ = fun (_ : epoly) -> ()

[@@@ocaml.warning "-39-11-27"]

let rec epoly_of_json_poly =
(fun x ->
let tag = Ppx_deriving_json_runtime.Primitives.string_of_json x in
if tag = "A_aliased" then Some `a
else if tag = "b" then Some `b
else None
: Js.Json.t -> epoly option)

and epoly_of_json =
(fun x ->
match epoly_of_json_poly x with
| Some x -> x
| None -> Ppx_deriving_json_runtime.of_json_error "invalid JSON"
: Js.Json.t -> epoly)

let _ = epoly_of_json_poly
and _ = epoly_of_json

[@@@ocaml.warning "-39-11-27"]

let rec epoly_to_json =
(fun x ->
match x with
| `a -> (Obj.magic (string_to_json "A_aliased") : Js.Json.t)
| `b -> (Obj.magic (string_to_json "b") : Js.Json.t)
: epoly -> Js.Json.t)

let _ = epoly_to_json
end [@@ocaml.doc "@inline"] [@@merlin.hide]

134 changes: 134 additions & 0 deletions test/ppx_deriving_json_native.t
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,74 @@
let _ = record_to_json
end [@@ocaml.doc "@inline"] [@@merlin.hide]

$ cat <<"EOF" | run
> type record_aliased = { name : string; [@json.key "my_name"] age : int; [@json.key "my_age"] [@json.default 100] } [@@deriving json]
> EOF
type record_aliased = {
name : string; [@json.key "my_name"]
age : int; [@json.key "my_age"] [@json.default 100]
}
[@@deriving json]

include struct
let _ = fun (_ : record_aliased) -> ()

[@@@ocaml.warning "-39-11-27"]

let rec record_aliased_of_json =
(fun x ->
match x with
| `Assoc fs ->
let x_name = ref Stdlib.Option.None in
let x_age = ref (Stdlib.Option.Some 100) in
let rec iter = function
| [] -> ()
| (n', v) :: fs ->
(match n' with
| "my_name" ->
x_name := Stdlib.Option.Some (string_of_json v)
| "my_age" -> x_age := Stdlib.Option.Some (int_of_json v)
| name ->
Ppx_deriving_json_runtime.of_json_error
(Stdlib.Printf.sprintf "unknown field: %s" name));
iter fs
in
iter fs;
{
name =
(match Stdlib.( ! ) x_name with
| Stdlib.Option.Some v -> v
| Stdlib.Option.None ->
Ppx_deriving_json_runtime.of_json_error
"missing field \"my_name\"");
age =
(match Stdlib.( ! ) x_age with
| Stdlib.Option.Some v -> v
| Stdlib.Option.None -> 100);
}
| _ ->
Ppx_deriving_json_runtime.of_json_error
"expected a JSON object"
: Yojson.Basic.t -> record_aliased)

let _ = record_aliased_of_json

[@@@ocaml.warning "-39-11-27"]

let rec record_aliased_to_json =
(fun x ->
match x with
| { name = x_name; age = x_age } ->
`Assoc
[
"my_name", string_to_json x_name;
"my_age", int_to_json x_age;
]
: record_aliased -> Yojson.Basic.t)

let _ = record_aliased_to_json
end [@@ocaml.doc "@inline"] [@@merlin.hide]

$ cat <<"EOF" | run
> type sum = A | B of int | C of { name : string } [@@deriving json]
> EOF
Expand Down Expand Up @@ -396,3 +464,69 @@
let _ = polyrecur_to_json
end [@@ocaml.doc "@inline"] [@@merlin.hide]

$ cat <<"EOF" | run
> type evar = A | B [@json.as "b_aliased"] [@@deriving json]
> EOF
type evar = A | B [@json.as "b_aliased"] [@@deriving json]

include struct
let _ = fun (_ : evar) -> ()

[@@@ocaml.warning "-39-11-27"]

let rec evar_of_json =
(fun x ->
match x with
| `String "A" -> A
| `String "b_aliased" -> B
| _ -> Ppx_deriving_json_runtime.of_json_error "invalid JSON"
: Yojson.Basic.t -> evar)

let _ = evar_of_json

[@@@ocaml.warning "-39-11-27"]

let rec evar_to_json =
(fun x -> match x with A -> `String "A" | B -> `String "b_aliased"
: evar -> Yojson.Basic.t)

let _ = evar_to_json
end [@@ocaml.doc "@inline"] [@@merlin.hide]

$ cat <<"EOF" | run
> type epoly = [ `a [@json.as "A_aliased"] | `b ] [@@deriving json]
> EOF
type epoly = [ `a [@json.as "A_aliased"] | `b ] [@@deriving json]

include struct
let _ = fun (_ : epoly) -> ()

[@@@ocaml.warning "-39-11-27"]

let rec epoly_of_json_poly =
(fun x ->
match x with
| `String "A_aliased" -> Some `a
| `String "b" -> Some `b
| x -> None
: Yojson.Basic.t -> epoly option)

and epoly_of_json =
(fun x ->
match epoly_of_json_poly x with
| Some x -> x
| None -> Ppx_deriving_json_runtime.of_json_error "invalid JSON"
: Yojson.Basic.t -> epoly)

let _ = epoly_of_json_poly
and _ = epoly_of_json

[@@@ocaml.warning "-39-11-27"]

let rec epoly_to_json =
(fun x -> match x with `a -> `String "A_aliased" | `b -> `String "b"
: epoly -> Yojson.Basic.t)

let _ = epoly_to_json
end [@@ocaml.doc "@inline"] [@@merlin.hide]

0 comments on commit 5fb4451

Please sign in to comment.