Skip to content

Commit

Permalink
test: tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
Khady committed Oct 4, 2024
1 parent 3349ce1 commit a59ba5a
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 77 deletions.
50 changes: 23 additions & 27 deletions ppx/test/ppx_deriving_json_js.e2e.t
Original file line number Diff line number Diff line change
Expand Up @@ -61,30 +61,26 @@
JSON DATA: ["Fix",["Fix",["Fix",["A"]]]]
JSON REPRINT: ["Fix",["Fix",["Fix",["A"]]]]
JSON DATA: "A"
JSON REPRINT: "A"
JSON DATA: "b_aliased"
JSON REPRINT: "b_aliased"
JSON DATA: "b"
JSON REPRINT: "b"
JSON DATA: "A_aliased"
JSON REPRINT: "A_aliased"
JSON DATA: {"my_name":"N","my_age":1}
JSON REPRINT: {"my_name":"N","my_age":1}
JSON DATA: {"my_name":"N"}
JSON REPRINT: {"my_name":"N","my_age":100}
JSON DATA: {}
JSON REPRINT: {"k":null}
JSON DATA: {"k":42}
JSON REPRINT: {"k":42}
JSON DATA: ["A",1]
JSON REPRINT: ["A",1]
JSON DATA: ["B","ok"]
JSON REPRINT: ["B","ok"]
JSON DATA: {"a":1,"b":2}
JSON REPRINT: {"a":1}
JSON DATA: ["A",{"a":1,"b":2}]
JSON REPRINT: ["A",{"a":1}]
JSON DATA: {"a":1}
JSON REPRINT: {"a":1}
JSON DATA: {"a":1,"b_opt":2}
JSON REPRINT: {"a":1,"b_opt":2}
$TESTCASE_ROOT/_build/default/output/node_modules/melange-json.ppx-runtime/ppx_deriving_json_runtime.js:29
throw new Caml_js_exceptions.MelangeError(Json__Json_decode.DecodeError, {
^

Error [MelangeError]: Json__Json_decode.DecodeError/6
at new MelangeError ($TESTCASE_ROOT/_build/default/output/node_modules/melange.js/caml_js_exceptions.js:28:21)
at Object.of_json_error ($TESTCASE_ROOT/_build/default/output/node_modules/melange-json.ppx-runtime/ppx_deriving_json_runtime.js:29:9)
at evar_of_json ($TESTCASE_ROOT/_build/default/output/example.js:429:38)
at Object._1 ($TESTCASE_ROOT/_build/default/output/node_modules/melange.js/curry.js:31:12)
at run$p ($TESTCASE_ROOT/_build/default/output/example.js:978:21)
at $TESTCASE_ROOT/_build/default/output/example.js:1007:18
at Object._1 ($TESTCASE_ROOT/_build/default/output/node_modules/melange.js/curry.js:31:12)
at Object.iter ($TESTCASE_ROOT/_build/default/output/node_modules/melange/list.js:343:11)
at Object.run ($TESTCASE_ROOT/_build/default/output/example.js:1006:16)
at Object.<anonymous> ($TESTCASE_ROOT/_build/default/output/main.js:6:20) {
[cause]: {
MEL_EXN_ID: 'Json__Json_decode.DecodeError/6',
_1: 'expected a non empty JSON array'
}
}

Node.js v18.20.4
[1]
100 changes: 85 additions & 15 deletions ppx/test/ppx_deriving_json_js.t
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,29 @@
let rec other_of_json_poly =
(fun x ->
let tag = Ppx_deriving_json_runtime.Primitives.string_of_json x in
if Stdlib.( = ) tag "C" then Some `C else None
if Js.Array.isArray x then
let array = (Obj.magic x : Js.Json.t array) in
let len = Js.Array.length array in
if Stdlib.( > ) len 0 then
let tag = Js.Array.unsafe_get array 0 in
if Stdlib.( = ) (Js.typeof tag) "string" then
let tag = (Obj.magic tag : string) in
if Stdlib.( = ) tag "C" then (
if Stdlib.( <> ) len 1 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 1";
Some `C)
else None
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array with element being a \
string"
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array"
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array"
: Js.Json.t -> other option)
and other_of_json =
Expand All @@ -405,7 +426,8 @@
let rec other_to_json =
(fun x ->
match x with `C -> (Obj.magic (string_to_json "C") : Js.Json.t)
match x with
| `C -> (Obj.magic [| string_to_json "C" |] : Js.Json.t)
: other -> Js.Json.t)
let _ = other_to_json
Expand Down Expand Up @@ -670,10 +692,34 @@
let rec evar_of_json =
(fun x ->
let tag = Ppx_deriving_json_runtime.Primitives.string_of_json x in
if Stdlib.( = ) tag "A" then A
else if Stdlib.( = ) tag "b_aliased" then B
else Ppx_deriving_json_runtime.of_json_error "invalid JSON"
if Js.Array.isArray x then
let array = (Obj.magic x : Js.Json.t array) in
let len = Js.Array.length array in
if Stdlib.( > ) len 0 then
let tag = Js.Array.unsafe_get array 0 in
if Stdlib.( = ) (Js.typeof tag) "string" then
let tag = (Obj.magic tag : string) in
if Stdlib.( = ) tag "A" then (
if Stdlib.( <> ) len 1 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 1";
A)
else if Stdlib.( = ) tag "b_aliased" then (
if Stdlib.( <> ) len 1 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 1";
B)
else Ppx_deriving_json_runtime.of_json_error "invalid JSON"
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array with element being a \
string"
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array"
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array"
: Js.Json.t -> evar)
let _ = evar_of_json
Expand All @@ -683,8 +729,8 @@
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)
| 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
Expand All @@ -702,10 +748,34 @@
let rec epoly_of_json_poly =
(fun x ->
let tag = Ppx_deriving_json_runtime.Primitives.string_of_json x in
if Stdlib.( = ) tag "A_aliased" then Some `a
else if Stdlib.( = ) tag "b" then Some `b
else None
if Js.Array.isArray x then
let array = (Obj.magic x : Js.Json.t array) in
let len = Js.Array.length array in
if Stdlib.( > ) len 0 then
let tag = Js.Array.unsafe_get array 0 in
if Stdlib.( = ) (Js.typeof tag) "string" then
let tag = (Obj.magic tag : string) in
if Stdlib.( = ) tag "A_aliased" then (
if Stdlib.( <> ) len 1 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 1";
Some `a)
else if Stdlib.( = ) tag "b" then (
if Stdlib.( <> ) len 1 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 1";
Some `b)
else None
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array with element being a \
string"
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array"
else
Ppx_deriving_json_runtime.of_json_error
"expected a non empty JSON array"
: Js.Json.t -> epoly option)
and epoly_of_json =
Expand All @@ -723,8 +793,8 @@
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)
| `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
Expand Down
29 changes: 2 additions & 27 deletions ppx/test/ppx_deriving_json_native.e2e.t
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,5 @@
JSON DATA: ["Fix",["Fix",["Fix",["A"]]]]
JSON REPRINT: ["Fix",["Fix",["Fix",["A"]]]]
JSON DATA: "A"
JSON REPRINT: "A"
JSON DATA: "b_aliased"
JSON REPRINT: "b_aliased"
JSON DATA: "b"
JSON REPRINT: "b"
JSON DATA: "A_aliased"
JSON REPRINT: "A_aliased"
JSON DATA: {"my_name":"N","my_age":1}
JSON REPRINT: {"my_name":"N","my_age":1}
JSON DATA: {"my_name":"N"}
JSON REPRINT: {"my_name":"N","my_age":100}
JSON DATA: {}
JSON REPRINT: {"k":null}
JSON DATA: {"k":42}
JSON REPRINT: {"k":42}
JSON DATA: ["A",1]
JSON REPRINT: ["A",1]
JSON DATA: ["B","ok"]
JSON REPRINT: ["B","ok"]
JSON DATA: {"a":1,"b":2}
JSON REPRINT: {"a":1}
JSON DATA: ["A",{"a":1,"b":2}]
JSON REPRINT: ["A",{"a":1}]
JSON DATA: {"a":1}
JSON REPRINT: {"a":1}
JSON DATA: {"a":1,"b_opt":2}
JSON REPRINT: {"a":1,"b_opt":2}
Fatal error: exception Ppx_deriving_json_runtime.Of_json_error("invalid JSON")
[2]
24 changes: 16 additions & 8 deletions ppx/test/ppx_deriving_json_native.t
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,8 @@
[@@@ocaml.warning "-39-11-27"]

let rec other_of_json_poly =
(fun x -> match x with `String "C" -> Some `C | x -> None
(fun x ->
match x with `List (`String "C" :: []) -> Some `C | x -> None
: Yojson.Basic.t -> other option)

and other_of_json =
Expand All @@ -388,7 +389,8 @@
[@@@ocaml.warning "-39-11-27"]

let rec other_to_json =
(fun x -> match x with `C -> `String "C" : other -> Yojson.Basic.t)
(fun x -> match x with `C -> `List [ `String "C" ]
: other -> Yojson.Basic.t)

let _ = other_to_json
end [@@ocaml.doc "@inline"] [@@merlin.hide]
Expand Down Expand Up @@ -553,8 +555,8 @@
let rec evar_of_json =
(fun x ->
match x with
| `String "A" -> A
| `String "b_aliased" -> B
| `List (`String "A" :: []) -> A
| `List (`String "b_aliased" :: []) -> B
| _ -> Ppx_deriving_json_runtime.of_json_error "invalid JSON"
: Yojson.Basic.t -> evar)

Expand All @@ -563,7 +565,10 @@
[@@@ocaml.warning "-39-11-27"]

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

let _ = evar_to_json
Expand All @@ -582,8 +587,8 @@
let rec epoly_of_json_poly =
(fun x ->
match x with
| `String "A_aliased" -> Some `a
| `String "b" -> Some `b
| `List (`String "A_aliased" :: []) -> Some `a
| `List (`String "b" :: []) -> Some `b
| x -> None
: Yojson.Basic.t -> epoly option)

Expand All @@ -600,7 +605,10 @@
[@@@ocaml.warning "-39-11-27"]

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

let _ = epoly_to_json
Expand Down

0 comments on commit a59ba5a

Please sign in to comment.