From b1456882ce7aede7bd1970fff6fc66983736a429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Ch=C3=A1varri?= Date: Mon, 16 Dec 2024 07:33:37 +0100 Subject: [PATCH] unify: fix tests (#46) --- README.md | 2 +- examples/complex.ml | 1 + examples/decode.ml | 6 +- examples/dynamicDict_Ocaml.ml | 2 + examples/dynamicDict_Reason.re | 2 + examples/encode.ml | 1 + examples/parse.ml | 2 + examples/tree.ml | 1 + ppx/browser/ppx_deriving_json_runtime.ml | 95 ++++------------ ppx/native/ppx_deriving_json_runtime.ml | 3 +- src/Json.ml | 107 ++++++++++++------ src/Json.mli | 4 +- src/__tests__/Json_decode_test.ml | 137 +++++++++++++---------- src/__tests__/Json_encode_test.ml | 1 + src/__tests__/Json_test.ml | 1 + 15 files changed, 194 insertions(+), 171 deletions(-) diff --git a/README.md b/README.md index ae146ff..6c781b0 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Based on [@glennsl/bs-json](https://github.com/glennsl/bs-json). The Decode module in particular provides a basic set of decoder functions to be composed into more complex decoders. A decoder is a function that takes a `Js.Json.t` and either returns a value of the desired type if successful or -raises a `DecodeError` exception if not. Other functions accept a decoder and +raises an `Of_json_error` exception if not. Other functions accept a decoder and produce another decoder. Like `array`, which when given a decoder for type `t` will return a decoder that tries to produce a value of type `t array`. So to decode an `int array` you combine `Json.Decode.int` with `Json.Decode.array` diff --git a/examples/complex.ml b/examples/complex.ml index 2b7a2fb..5f7452e 100644 --- a/examples/complex.ml +++ b/examples/complex.ml @@ -1,3 +1,4 @@ +[@@@alert "-deprecated"] type line = { start : point; end_ : point; thickness : int option } and point = { x : int; y : int } diff --git a/examples/decode.ml b/examples/decode.ml index 250c9b0..2c13eb5 100644 --- a/examples/decode.ml +++ b/examples/decode.ml @@ -1,3 +1,5 @@ +[@@@alert "-deprecated"] + (* Decoding a fixed JSON data structure using Json.Decode *) let mapJsonObjectString f decoder (encoder : int -> Js.Json.t) str = let json = Json.parseOrRaise str in @@ -26,5 +28,5 @@ let _ = let json = {|{ "y": 42 } |} |> Json.parseOrRaise in match Json.Decode.(field "x" int json) with | x -> Js.log x - | exception Json.Decode.DecodeError err -> - Js.log ("Error:" ^ Json.Decode.error_to_string err) + | exception Json.Of_json_error err -> + Js.log ("Error:" ^ Json.of_json_error_to_string err) diff --git a/examples/dynamicDict_Ocaml.ml b/examples/dynamicDict_Ocaml.ml index a80050d..10a2f7c 100644 --- a/examples/dynamicDict_Ocaml.ml +++ b/examples/dynamicDict_Ocaml.ml @@ -1,3 +1,5 @@ +[@@@alert "-deprecated"] + (* Handling an object with dynamic keys for sub-objects. example: diff --git a/examples/dynamicDict_Reason.re b/examples/dynamicDict_Reason.re index be3e76c..b341ea7 100644 --- a/examples/dynamicDict_Reason.re +++ b/examples/dynamicDict_Reason.re @@ -1,3 +1,5 @@ +[@alert "-deprecated"]; + /* Handling an object with dynamic keys for sub-objects. example: diff --git a/examples/encode.ml b/examples/encode.ml index 7ef86b8..f8ebd3a 100644 --- a/examples/encode.ml +++ b/examples/encode.ml @@ -1,3 +1,4 @@ +[@@@alert "-deprecated"] (* Encoding a JSON data structure using Json.Encode *) (* prints ["foo", "bar"] *) diff --git a/examples/parse.ml b/examples/parse.ml index 8582c14..0eb6d8e 100644 --- a/examples/parse.ml +++ b/examples/parse.ml @@ -1,3 +1,5 @@ +[@@@alert "-deprecated"] + (* Parsing a JSON string using Json.parseOrRaise *) let arrayOfInts str = diff --git a/examples/tree.ml b/examples/tree.ml index e46b19e..5a6d054 100644 --- a/examples/tree.ml +++ b/examples/tree.ml @@ -1,3 +1,4 @@ +[@@@alert "-deprecated"] (* Decode a JSON tree structure *) type 'a tree = Node of 'a * 'a tree list | Leaf of 'a diff --git a/ppx/browser/ppx_deriving_json_runtime.ml b/ppx/browser/ppx_deriving_json_runtime.ml index 56241ce..0d5ece2 100644 --- a/ppx/browser/ppx_deriving_json_runtime.ml +++ b/ppx/browser/ppx_deriving_json_runtime.ml @@ -1,11 +1,11 @@ +[@@@alert "-deprecated"] + type t = Js.Json.t let to_json t = t let of_json t = t let to_string t = Js.Json.stringify t -exception Of_string_error of string - let of_string s = try Js.Json.parseExn s with exn -> @@ -18,13 +18,13 @@ let of_string s = (* msg really cannot be None in browser or any sane JS runtime *) Option.value msg ~default:"JSON error" in - raise (Of_string_error msg) + raise (Json.Of_string_error msg) -type error = Json.Decode.error = +type error = Json.of_json_error = | Json_error of string | Unexpected_variant of string -exception Of_json_error = Json.Decode.DecodeError +exception Of_json_error = Json.Of_json_error let of_json_error msg = raise (Of_json_error (Json_error msg)) @@ -60,75 +60,16 @@ module To_json = struct end module Of_json = struct - let string_of_json (json : t) : string = - if Js.typeof json = "string" then (Obj.magic json : string) - else of_json_error "expected a string" - - let bool_of_json (json : t) : bool = - if Js.typeof json = "boolean" then (Obj.magic json : bool) - else of_json_error "expected a boolean" - - let is_int value = - Js.Float.isFinite value && Js.Math.floor_float value == value - - let int_of_json (json : t) : int = - if Js.typeof json = "number" then - let v = (Obj.magic json : float) in - if is_int v then (Obj.magic v : int) - else of_json_error "expected an integer" - else of_json_error "expected an integer" - - let int64_of_json (json : t) : int64 = - if Js.typeof json = "string" then - let v = (Obj.magic json : string) in - match Int64.of_string_opt v with - | Some v -> v - | None -> of_json_error "expected int64 as string" - else of_json_error "expected int64 as string" - - let float_of_json (json : t) : float = - if Js.typeof json = "number" then (Obj.magic json : float) - else of_json_error "expected a float" - - let unit_of_json (json : t) = - if (Obj.magic json : 'a Js.null) == Js.null then () - else of_json_error "expected null" - - let array_of_json v_of_json (json : t) = - if Js.Array.isArray json then - let json = (Obj.magic json : Js.Json.t array) in - Js.Array.map ~f:v_of_json json - else of_json_error "expected a JSON array" - - let list_of_json v_of_json (json : t) = - array_of_json v_of_json json |> Array.to_list - - let option_of_json v_of_json (json : t) = - if (Obj.magic json : 'a Js.null) == Js.null then None - else Some (v_of_json json) - - let result_of_json ok_of_json err_of_json (json : t) = - if Js.Array.isArray json then - let array = (Obj.magic json : 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 "Ok" then ( - if Stdlib.( <> ) len 2 then - of_json_error "expected a JSON array of length 2"; - Ok (ok_of_json (Js.Array.unsafe_get array 1))) - else if Stdlib.( = ) tag "Error" then ( - if Stdlib.( <> ) len 2 then - of_json_error "expected a JSON array of length 2"; - Error (err_of_json (Js.Array.unsafe_get array 1))) - else of_json_error "invalid JSON" - else - of_json_error - "expected a non empty JSON array with element being a string" - else of_json_error "expected a non empty JSON array" - else of_json_error "expected a non empty JSON array" + let string_of_json = Json.Of_json.string + let bool_of_json = Json.Of_json.bool + let int_of_json = Json.Of_json.int + let int64_of_json = Json.Of_json.int64 + let float_of_json = Json.Of_json.float + let unit_of_json = Json.Of_json.unit + let array_of_json = Json.Of_json.array + let list_of_json = Json.Of_json.list + let option_of_json = Json.Of_json.option + let result_of_json = Json.Of_json.result end module Primitives = struct @@ -137,6 +78,10 @@ module Primitives = struct end module Classify = struct + (* This function is also defined in `Json` module, but not exposed on its mli *) + let is_int value = + Js.Float.isFinite value && Js.Math.floor_float value == value + let classify : t -> [ `Null @@ -153,7 +98,7 @@ module Classify = struct | "string" -> `String (Obj.magic json : string) | "number" -> let v = (Obj.magic json : float) in - if Of_json.is_int v then `Int (Obj.magic v : int) else `Float v + if is_int v then `Int (Obj.magic v : int) else `Float v | "boolean" -> `Bool (Obj.magic json : bool) | "object" -> if Js.Array.isArray json then diff --git a/ppx/native/ppx_deriving_json_runtime.ml b/ppx/native/ppx_deriving_json_runtime.ml index f043e9b..1d3b146 100644 --- a/ppx/native/ppx_deriving_json_runtime.ml +++ b/ppx/native/ppx_deriving_json_runtime.ml @@ -26,8 +26,7 @@ let show_json_type = function | `String _ -> "string" let of_json_error_type_mismatch json expected = - of_json_error - ("expected " ^ expected ^ " but got " ^ show_json_type json) + of_json_error ("Expected " ^ expected ^ ", got " ^ show_json_type json) module To_json = struct let string_to_json v = `String v diff --git a/src/Json.ml b/src/Json.ml index a1599b8..b156a86 100644 --- a/src/Json.ml +++ b/src/Json.ml @@ -19,6 +19,9 @@ let to_string t = Js.Json.stringify t exception Of_string_error of string +external _unsafeCreateUninitializedArray : int -> 'a array = "Array" +[@@mel.new] + let of_string s = try Js.Json.parseExn s with exn -> @@ -34,22 +37,23 @@ let of_string s = raise (Of_string_error msg) module Of_json = struct - external _stringify : Js.Json.t -> string = "JSON.stringify" - let string (json : t) : string = if Js.typeof json = "string" then (Obj.magic json : string) - else of_json_error "expected a string" + else of_json_error ("Expected string, got " ^ Js.Json.stringify json) let char (json : t) = if Js.typeof json = "string" then let s = (Obj.magic json : string) in if String.length s = 1 then String.get s 0 - else of_json_error "expected a single-character string" - else of_json_error "expected a string" + else + of_json_error + ("Expected single-character string, got " + ^ Js.Json.stringify json) + else of_json_error ("Expected string, got " ^ Js.Json.stringify json) let bool (json : t) : bool = if Js.typeof json = "boolean" then (Obj.magic json : bool) - else of_json_error "expected a boolean" + else of_json_error ("Expected boolean, got " ^ Js.Json.stringify json) let is_int value = Js.Float.isFinite value && Js.Math.floor_float value == value @@ -58,30 +62,48 @@ module Of_json = struct if Js.typeof json = "number" then let v = (Obj.magic json : float) in if is_int v then (Obj.magic v : int) - else of_json_error "expected an integer" - else of_json_error "expected an integer" + else + of_json_error ("Expected integer, got " ^ Js.Json.stringify json) + else of_json_error ("Expected number, got " ^ Js.Json.stringify json) let int64 (json : t) : int64 = if Js.typeof json = "string" then let v = (Obj.magic json : string) in match Int64.of_string_opt v with | Some v -> v - | None -> of_json_error "expected int64 as string" - else of_json_error "expected int64 as string" + | None -> + of_json_error + ("Expected int64 as string, got " ^ Js.Json.stringify json) + else + of_json_error + ("Expected int64 as string, got " ^ Js.Json.stringify json) let float (json : t) : float = if Js.typeof json = "number" then (Obj.magic json : float) - else of_json_error "expected a float" + else of_json_error ("Expected number, got " ^ Js.Json.stringify json) let unit (json : t) : unit = if (Obj.magic json : 'a Js.null) == Js.null then () - else of_json_error "expected null" + else + of_json_error + ("Expected null as unit, got " ^ Js.Json.stringify json) - let array v_of_json (json : t) : _ array = - if Js.Array.isArray json then - let json = (Obj.magic json : Js.Json.t array) in - Js.Array.map ~f:v_of_json json - else of_json_error "expected a JSON array" + let array v_of_json (json : t) = + if Js.Array.isArray json then ( + let source = (Obj.magic (json : Js.Json.t) : Js.Json.t array) in + let length = Js.Array.length source in + let target = _unsafeCreateUninitializedArray length in + for i = 0 to length - 1 do + let value = + try v_of_json (Array.unsafe_get source i) + with Of_json_error (Json_error err) -> + of_json_error + (err ^ "\n\tin array at index " ^ string_of_int i) + in + Array.unsafe_set target i value + done; + target) + else of_json_error ("Expected array, got " ^ Js.Json.stringify json) let list v_of_json (json : t) : _ list = array v_of_json json |> Array.to_list @@ -110,8 +132,10 @@ module Of_json = struct else let length = Js.String.make length in of_json_error - {j|Expected array of length 2, got array of length $length|j} - else of_json_error ("Expected array, got " ^ _stringify json) + {j|Expected array of length 2 as tuple, got array of length $length|j} + else + of_json_error + ("Expected array as tuple, got " ^ Js.Json.stringify json) let tuple3 decodeA decodeB decodeC json : _ * _ * _ = if Js.Array.isArray json then @@ -127,8 +151,10 @@ module Of_json = struct else let length = Js.String.make length in of_json_error - {j|Expected array of length 3, got array of length $length|j} - else of_json_error ("Expected array, got " ^ _stringify json) + {j|Expected array of length 3 as tuple, got array of length $length|j} + else + of_json_error + ("Expected array as tuple, got " ^ Js.Json.stringify json) let tuple4 decodeA decodeB decodeC decodeD json : _ * _ * _ * _ = if Js.Array.isArray json then @@ -145,8 +171,10 @@ module Of_json = struct else let length = Js.String.make length in of_json_error - {j|Expected array of length 4, got array of length $length|j} - else of_json_error ("Expected array, got " ^ _stringify json) + {j|Expected array of length 4 as tuple, got array of length $length|j} + else + of_json_error + ("Expected array as tuple, got " ^ Js.Json.stringify json) let js_dict decode json : _ Js.Dict.t = if @@ -163,12 +191,18 @@ module Of_json = struct let value = try decode (Js.Dict.unsafeGet source key) with Of_json_error err -> - of_json_error (of_json_error_to_string err ^ "\n\tin dict") + of_json_error + (of_json_error_to_string err + ^ "\n\tin object at key '" + ^ key + ^ "'") in Js.Dict.set target key value done; target) - else of_json_error ("Expected object, got " ^ _stringify json) + else + of_json_error + ("Expected object as dict, got " ^ Js.Json.stringify json) let result ok_of_json err_of_json (json : t) : (_, _) result = if Js.Array.isArray json then @@ -180,18 +214,27 @@ module Of_json = struct let tag = (Obj.magic tag : string) in if Stdlib.( = ) tag "Ok" then ( if Stdlib.( <> ) len 2 then - of_json_error "expected a JSON array of length 2"; + of_json_error + ("Expected array of length 2 as result 'Ok', got " + ^ Js.Json.stringify json); Ok (ok_of_json (Js.Array.unsafe_get array 1))) else if Stdlib.( = ) tag "Error" then ( if Stdlib.( <> ) len 2 then - of_json_error "expected a JSON array of length 2"; + of_json_error + ("Expected array of length 2 as result 'Error', got " + ^ Js.Json.stringify json); Error (err_of_json (Js.Array.unsafe_get array 1))) else of_json_error "invalid JSON" else of_json_error - "expected a non empty JSON array with element being a string" - else of_json_error "expected a non empty JSON array" - else of_json_error "expected a non empty JSON array" + ("Expected non-empty array with element being a string, got " + ^ Js.Json.stringify json) + else + of_json_error + ("Expected non-empty array, got " ^ Js.Json.stringify json) + else + of_json_error + ("Expected a non-empty array, got " ^ Js.Json.stringify json) let at' key decode json = if @@ -208,7 +251,7 @@ module Of_json = struct (of_json_error_to_string err ^ "\n\tat field '" ^ key ^ "'") ) | None -> of_json_error {j|Expected field '$(key)'|j} - else of_json_error ("Expected object, got " ^ _stringify json) + else of_json_error ("Expected object, got " ^ Js.Json.stringify json) let rec at key_path decoder = match key_path with @@ -229,7 +272,7 @@ module Of_json = struct in of_json_error ({j|All decoders given to oneOf failed. Here are all the errors: $formattedErrors\nAnd the JSON being decoded: |j} - ^ _stringify json) + ^ Js.Json.stringify json) | decode :: rest -> ( try decode json with Of_json_error e -> inner rest (e :: errors)) diff --git a/src/Json.mli b/src/Json.mli index b77fc9f..a0c013b 100644 --- a/src/Json.mli +++ b/src/Json.mli @@ -126,6 +126,8 @@ val to_json : json to_json (** The type of a error which occurs during decoding JSON values. *) type of_json_error = Json_error of string | Unexpected_variant of string +val of_json_error_to_string : of_json_error -> string + type exn += | Of_json_error of of_json_error (** The exception raised when a decoding error occurs *) @@ -139,7 +141,7 @@ val of_json : json of_json module Of_json : sig (** Provides a set of low level combinator primitives to decode Js.Json.t data structures A decoder combinator will return the - decoded value if successful, or raise a [DecodeError of string] if + decoded value if successful, or raise a [Of_json_error] exception if unsuccessful, where the string argument contains the error message. Decoders are designed to be combined to produce more complex decoders that can decode arbitrary data structures, though the diff --git a/src/__tests__/Json_decode_test.ml b/src/__tests__/Json_decode_test.ml index 19bccda..268ddb4 100644 --- a/src/__tests__/Json_decode_test.ml +++ b/src/__tests__/Json_decode_test.ml @@ -1,3 +1,5 @@ +[@@@alert "-deprecated"] + open Jest open Expect @@ -31,7 +33,7 @@ module Test = struct try let _ = decoder value in fail "should throw" - with Json.Decode.DecodeError _ -> pass) + with Json.Of_json_error _ -> pass) end let () = @@ -76,8 +78,7 @@ let () = let (_ : int) = int (Encode.int inf) in fail "should throw" with - | Decode.DecodeError (Json_error "Expected integer, got null") - -> + | Of_json_error (Json_error "Expected integer, got null") -> pass); Test.throws int [ Bool; Float; String; Null; Array; Object; Char ]); @@ -116,7 +117,7 @@ let () = let (_ : char) = char (Encode.string "") in fail "should throw" with - | Decode.DecodeError + | Of_json_error (Json_error "Expected single-character string, got \"\"") -> pass); @@ -126,7 +127,7 @@ let () = let (_ : char) = char (Encode.string "abc") in fail "should throw" with - | Decode.DecodeError + | Of_json_error (Json_error "Expected single-character string, got \"abc\"") -> pass); @@ -206,11 +207,11 @@ let () = in fail "should throw" with - | DecodeError + | Of_json_error (Json_error "Expected boolean, got 1\n\tin array at index 0") -> pass); - test "non-DecodeError exceptions in decoder should pass through" + test "non-Of_json_error exceptions in decoder should pass through" (fun () -> try let (_ : _ array) = @@ -253,11 +254,11 @@ let () = in fail "should throw" with - | DecodeError + | Of_json_error (Json_error "Expected boolean, got 1\n\tin array at index 0") -> pass); - test "non-DecodeError exceptions in decoder should pass through" + test "non-Of_json_error exceptions in decoder should pass through" (fun () -> try let (_ : 'a list) = @@ -285,9 +286,10 @@ let () = in fail "should throw" with - | DecodeError + | Of_json_error (Json_error - "Expected array of length 2, got array of length 1") + "Expected array of length 2 as tuple, got array of \ + length 1") -> pass); test "too large" (fun () -> @@ -297,9 +299,10 @@ let () = in fail "should throw" with - | DecodeError + | Of_json_error (Json_error - "Expected array of length 2, got array of length 3") + "Expected array of length 2 as tuple, got array of \ + length 3") -> pass); test "bad type a" (fun () -> @@ -309,7 +312,7 @@ let () = in fail "should throw" with - | DecodeError + | Of_json_error (Json_error "Expected number, got \"3\"\n\tin pair/tuple2") -> pass); @@ -320,7 +323,7 @@ let () = in fail "should throw" with - | DecodeError + | Of_json_error (Json_error "Expected string, got 4\n\tin pair/tuple2") -> pass); @@ -328,8 +331,10 @@ let () = try let (_ : int * int) = (pair int int) (parseOrRaise {| 4 |}) in fail "should throw" - with DecodeError (Json_error "Expected array, got 4") -> pass); - test "non-DecodeError exceptions in decoder should pass through" + with + | Of_json_error (Json_error "Expected array as tuple, got 4") -> + pass); + test "non-Of_json_error exceptions in decoder should pass through" (fun () -> try let (_ : int * int) = @@ -351,9 +356,10 @@ let () = in fail "should throw" with - | DecodeError + | Of_json_error (Json_error - "Expected array of length 2, got array of length 1") + "Expected array of length 2 as tuple, got array of \ + length 1") -> pass); test "too large" (fun () -> @@ -363,9 +369,10 @@ let () = in fail "should throw" with - | DecodeError + | Of_json_error (Json_error - "Expected array of length 2, got array of length 3") + "Expected array of length 2 as tuple, got array of \ + length 3") -> pass); test "bad type a" (fun () -> @@ -375,7 +382,7 @@ let () = in fail "should throw" with - | DecodeError + | Of_json_error (Json_error "Expected number, got \"3\"\n\tin pair/tuple2") -> pass); @@ -386,7 +393,7 @@ let () = in fail "should throw" with - | DecodeError + | Of_json_error (Json_error "Expected string, got 4\n\tin pair/tuple2") -> pass); @@ -396,8 +403,10 @@ let () = (tuple2 int int) (parseOrRaise {| 4 |}) in fail "should throw" - with DecodeError (Json_error "Expected array, got 4") -> pass); - test "non-DecodeError exceptions in decoder should pass through" + with + | Of_json_error (Json_error "Expected array as tuple, got 4") -> + pass); + test "non-Of_json_error exceptions in decoder should pass through" (fun () -> try let (_ : 'a * int) = @@ -421,9 +430,10 @@ let () = in fail "should throw" with - | DecodeError + | Of_json_error (Json_error - "Expected array of length 3, got array of length 1") + "Expected array of length 3 as tuple, got array of \ + length 1") -> pass); test "too large" (fun () -> @@ -433,9 +443,10 @@ let () = in fail "should throw" with - | DecodeError + | Of_json_error (Json_error - "Expected array of length 3, got array of length 5") + "Expected array of length 3 as tuple, got array of \ + length 5") -> pass); test "bad type a" (fun () -> @@ -445,7 +456,7 @@ let () = in fail "should throw" with - | DecodeError + | Of_json_error (Json_error "Expected number, got \"3\"\n\tin tuple3") -> pass); @@ -457,7 +468,8 @@ let () = in fail "should throw" with - | DecodeError (Json_error "Expected string, got 4\n\tin tuple3") + | Of_json_error + (Json_error "Expected string, got 4\n\tin tuple3") -> pass); test "not array" (fun () -> @@ -466,8 +478,10 @@ let () = (tuple3 int int int) (parseOrRaise {| 4 |}) in fail "should throw" - with DecodeError (Json_error "Expected array, got 4") -> pass); - test "non-DecodeError exceptions in decoder should pass through" + with + | Of_json_error (Json_error "Expected array as tuple, got 4") -> + pass); + test "non-Of_json_error exceptions in decoder should pass through" (fun () -> try let (_ : int * int * int) = @@ -492,9 +506,10 @@ let () = in fail "should throw" with - | DecodeError + | Of_json_error (Json_error - "Expected array of length 4, got array of length 1") + "Expected array of length 4 as tuple, got array of \ + length 1") -> pass); test "too large" (fun () -> @@ -505,9 +520,10 @@ let () = in fail "should throw" with - | DecodeError + | Of_json_error (Json_error - "Expected array of length 4, got array of length 6") + "Expected array of length 4 as tuple, got array of \ + length 6") -> pass); test "bad type a" (fun () -> @@ -517,7 +533,7 @@ let () = in fail "should throw" with - | DecodeError + | Of_json_error (Json_error "Expected number, got \"3\"\n\tin tuple4") -> pass); @@ -529,7 +545,8 @@ let () = in fail "should throw" with - | DecodeError (Json_error "Expected string, got 4\n\tin tuple4") + | Of_json_error + (Json_error "Expected string, got 4\n\tin tuple4") -> pass); test "not array" (fun () -> @@ -538,8 +555,10 @@ let () = (tuple4 int int int int) (parseOrRaise {| 4 |}) in fail "should throw" - with DecodeError (Json_error "Expected array, got 4") -> pass); - test "non-DecodeError exceptions in decoder should pass through" + with + | Of_json_error (Json_error "Expected array as tuple, got 4") -> + pass); + test "non-Of_json_error exceptions in decoder should pass through" (fun () -> try let (_ : int * int * int * int) = @@ -582,11 +601,12 @@ let () = in fail "should throw" with - | DecodeError - (Json_error "Expected string, got null\n\tin dict") + | Of_json_error + (Json_error + "Expected string, got null\n\tin object at key 'a'") -> pass); - test "non-DecodeError exceptions in decoder should pass through" + test "non-Of_json_error exceptions in decoder should pass through" (fun () -> try let (_ : _ Js.Dict.t) = @@ -628,7 +648,7 @@ let () = (parseOrRaise {| { "a": null, "b": null } |}) in fail "should throw" - with DecodeError (Json_error "Expected field 'c'") -> pass); + with Of_json_error (Json_error "Expected field 'c'") -> pass); test "decoder error" (fun () -> try let (_ : string) = @@ -637,12 +657,12 @@ let () = in fail "should throw" with - | DecodeError + | Of_json_error (Json_error "Expected string, got null\n\tat field 'b'") -> pass); - test "non-DecodeError exceptions in decoder should pass through" + test "non-Of_json_error exceptions in decoder should pass through" (fun () -> try let _ = @@ -689,7 +709,8 @@ let () = in fail "should throw" with - | DecodeError (Json_error "Expected field 'y'\n\tat field 'a'") + | Of_json_error + (Json_error "Expected field 'y'\n\tat field 'a'") -> pass); test "decoder error" (fun () -> @@ -704,9 +725,9 @@ let () = in fail "should throw" with - | DecodeError + | Of_json_error (Json_error - "Expected null, got \"foo\"\n\ + "Expected null\n\ \tat field 'y'\n\ \tat field 'x'\n\ \tat field 'a'") @@ -785,9 +806,9 @@ let () = (field "y" (optional int)) (parseOrRaise {| { "x": 2} |}) in fail "should throw" - with DecodeError (Json_error "Expected field 'y'") -> pass); + with Of_json_error (Json_error "Expected field 'y'") -> pass); - test "non-DecodeError exceptions in decoder should pass through" + test "non-Of_json_error exceptions in decoder should pass through" (fun () -> try let (_ : 'a option) = @@ -807,7 +828,7 @@ let () = expect @@ (oneOf [ int; field "x" int ]) (Encode.int 23) |> toEqual 23); - test "non-DecodeError exceptions in decoder should pass through" + test "non-Of_json_error exceptions in decoder should pass through" (fun () -> try let _ = (oneOf [ (fun _ -> raise Foo) ]) Encode.null in @@ -853,7 +874,7 @@ let () = test "object" (fun () -> expect @@ (withDefault 0 int) (Encode.object_ []) |> toEqual 0); - test "non-DecodeError exceptions in decoder should pass through" + test "non-Of_json_error exceptions in decoder should pass through" (fun () -> try let (_ : int) = @@ -921,12 +942,12 @@ let () = in fail "should throw" with - | DecodeError + | Of_json_error (Json_error "Expected number, got true\n\ \tin array at index 0\n\ \tin array at index 1\n\ - \tin dict") + \tin object at key 'a'") -> pass); test "dict array array int - heterogenous structure 2" (fun () -> @@ -938,11 +959,11 @@ let () = in fail "should throw" with - | DecodeError + | Of_json_error (Json_error "Expected array, got \"foo\"\n\ \tin array at index 1\n\ - \tin dict") + \tin object at key 'a'") -> pass); test "field" (fun () -> diff --git a/src/__tests__/Json_encode_test.ml b/src/__tests__/Json_encode_test.ml index 0eed75f..0db7b6a 100644 --- a/src/__tests__/Json_encode_test.ml +++ b/src/__tests__/Json_encode_test.ml @@ -1,3 +1,4 @@ +[@@@alert "-deprecated"] open Jest open Expect open! Json.Encode diff --git a/src/__tests__/Json_test.ml b/src/__tests__/Json_test.ml index 10199ce..852903f 100644 --- a/src/__tests__/Json_test.ml +++ b/src/__tests__/Json_test.ml @@ -1,3 +1,4 @@ +[@@@alert "-deprecated"] open Jest open Expect open Json