Skip to content

Commit

Permalink
unify: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jchavarri committed Dec 14, 2024
1 parent 610e753 commit 01db80f
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 58 deletions.
1 change: 1 addition & 0 deletions ppx/browser/ppx_deriving_json_runtime.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[@@@alert "-deprecated"]
type t = Js.Json.t

let to_json t = t
Expand Down
7 changes: 7 additions & 0 deletions src/Json.ml
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,13 @@ end

module Decode = struct
type 'a decoder = 'a of_json
type error = Json_error of string | Unexpected_variant of string

let error_to_string = function
| Json_error msg -> msg
| Unexpected_variant tag -> "unexpected variant: " ^ tag

exception DecodeError of error

let id json = json
let bool = Of_json.bool
Expand Down
6 changes: 6 additions & 0 deletions src/Json.mli
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,12 @@ module Decode : sig
type 'a decoder = 'a of_json [@@deprecated "Use `of_json` instead"]
(** The type of a decoder combinator *)

type error = Json_error of string | Unexpected_variant of string

val error_to_string : error -> string

exception DecodeError of error

val id : t of_json [@@deprecated "Use `of_json` instead"]
val bool : bool of_json [@@deprecated "Use `Of_json.bool` instead"]
val float : float of_json [@@deprecated "Use `Of_json.float` instead"]
Expand Down
109 changes: 51 additions & 58 deletions src/__tests__/Json_decode_test.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[@@@alert "-deprecated"]
open Jest
open Expect

Expand Down Expand Up @@ -31,7 +32,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 () =
Expand Down Expand Up @@ -76,7 +77,7 @@ let () =
let (_ : int) = int (Encode.int inf) in
fail "should throw"
with
| Decode.DecodeError (Json_error "Expected integer, got null")
| Json.Of_json_error (Json_error "expected an integer")
->
pass);

Expand Down Expand Up @@ -116,8 +117,8 @@ let () =
let (_ : char) = char (Encode.string "") in
fail "should throw"
with
| Decode.DecodeError
(Json_error "Expected single-character string, got \"\"")
| Json.Of_json_error
(Json_error "expected a single-character string")
->
pass);

Expand All @@ -126,8 +127,8 @@ let () =
let (_ : char) = char (Encode.string "abc") in
fail "should throw"
with
| Decode.DecodeError
(Json_error "Expected single-character string, got \"abc\"")
| Json.Of_json_error
(Json_error "expected a single-character string")
->
pass);

Expand Down Expand Up @@ -206,8 +207,8 @@ let () =
in
fail "should throw"
with
| DecodeError
(Json_error "Expected boolean, got 1\n\tin array at index 0")
| Json.Of_json_error
(Json_error "expected a boolean")
->
pass);
test "non-DecodeError exceptions in decoder should pass through"
Expand Down Expand Up @@ -253,8 +254,8 @@ let () =
in
fail "should throw"
with
| DecodeError
(Json_error "Expected boolean, got 1\n\tin array at index 0")
| Json.Of_json_error
(Json_error "expected a boolean")
->
pass);
test "non-DecodeError exceptions in decoder should pass through"
Expand Down Expand Up @@ -285,7 +286,7 @@ let () =
in
fail "should throw"
with
| DecodeError
| Json.Of_json_error
(Json_error
"Expected array of length 2, got array of length 1")
->
Expand All @@ -297,7 +298,7 @@ let () =
in
fail "should throw"
with
| DecodeError
| Json.Of_json_error
(Json_error
"Expected array of length 2, got array of length 3")
->
Expand All @@ -309,8 +310,8 @@ let () =
in
fail "should throw"
with
| DecodeError
(Json_error "Expected number, got \"3\"\n\tin pair/tuple2")
| Json.Of_json_error
(Json_error "expected an integer\n\tin pair/tuple2")
->
pass);
test "bad type b" (fun () ->
Expand All @@ -320,15 +321,15 @@ let () =
in
fail "should throw"
with
| DecodeError
(Json_error "Expected string, got 4\n\tin pair/tuple2")
| Json.Of_json_error
(Json_error "expected a string\n\tin pair/tuple2")
->
pass);
test "not array" (fun () ->
try
let (_ : int * int) = (pair int int) (parseOrRaise {| 4 |}) in
fail "should throw"
with DecodeError (Json_error "Expected array, got 4") -> pass);
with Of_json_error (Json_error "Expected array, got 4") -> pass);
test "non-DecodeError exceptions in decoder should pass through"
(fun () ->
try
Expand All @@ -351,7 +352,7 @@ let () =
in
fail "should throw"
with
| DecodeError
| Json.Of_json_error
(Json_error
"Expected array of length 2, got array of length 1")
->
Expand All @@ -363,7 +364,7 @@ let () =
in
fail "should throw"
with
| DecodeError
| Json.Of_json_error
(Json_error
"Expected array of length 2, got array of length 3")
->
Expand All @@ -375,8 +376,8 @@ let () =
in
fail "should throw"
with
| DecodeError
(Json_error "Expected number, got \"3\"\n\tin pair/tuple2")
| Json.Of_json_error
(Json_error "expected an integer\n\tin pair/tuple2")
->
pass);
test "bad type b" (fun () ->
Expand All @@ -386,8 +387,8 @@ let () =
in
fail "should throw"
with
| DecodeError
(Json_error "Expected string, got 4\n\tin pair/tuple2")
| Json.Of_json_error
(Json_error "expected a string\n\tin pair/tuple2")
->
pass);
test "not array" (fun () ->
Expand All @@ -396,7 +397,7 @@ let () =
(tuple2 int int) (parseOrRaise {| 4 |})
in
fail "should throw"
with DecodeError (Json_error "Expected array, got 4") -> pass);
with Of_json_error (Json_error "Expected array, got 4") -> pass);
test "non-DecodeError exceptions in decoder should pass through"
(fun () ->
try
Expand All @@ -421,7 +422,7 @@ let () =
in
fail "should throw"
with
| DecodeError
| Json.Of_json_error
(Json_error
"Expected array of length 3, got array of length 1")
->
Expand All @@ -433,7 +434,7 @@ let () =
in
fail "should throw"
with
| DecodeError
| Json.Of_json_error
(Json_error
"Expected array of length 3, got array of length 5")
->
Expand All @@ -445,8 +446,8 @@ let () =
in
fail "should throw"
with
| DecodeError
(Json_error "Expected number, got \"3\"\n\tin tuple3")
| Json.Of_json_error
(Json_error "expected an integer\n\tin tuple3")
->
pass);
test "bad type b" (fun () ->
Expand All @@ -457,7 +458,7 @@ let () =
in
fail "should throw"
with
| DecodeError (Json_error "Expected string, got 4\n\tin tuple3")
| Json.Of_json_error (Json_error "expected a string\n\tin tuple3")
->
pass);
test "not array" (fun () ->
Expand All @@ -466,7 +467,7 @@ let () =
(tuple3 int int int) (parseOrRaise {| 4 |})
in
fail "should throw"
with DecodeError (Json_error "Expected array, got 4") -> pass);
with Of_json_error (Json_error "Expected array, got 4") -> pass);
test "non-DecodeError exceptions in decoder should pass through"
(fun () ->
try
Expand All @@ -492,7 +493,7 @@ let () =
in
fail "should throw"
with
| DecodeError
| Json.Of_json_error
(Json_error
"Expected array of length 4, got array of length 1")
->
Expand All @@ -505,7 +506,7 @@ let () =
in
fail "should throw"
with
| DecodeError
| Json.Of_json_error
(Json_error
"Expected array of length 4, got array of length 6")
->
Expand All @@ -517,8 +518,8 @@ let () =
in
fail "should throw"
with
| DecodeError
(Json_error "Expected number, got \"3\"\n\tin tuple4")
| Json.Of_json_error
(Json_error "expected an integer\n\tin tuple4")
->
pass);
test "bad type b" (fun () ->
Expand All @@ -529,7 +530,7 @@ let () =
in
fail "should throw"
with
| DecodeError (Json_error "Expected string, got 4\n\tin tuple4")
| Json.Of_json_error (Json_error "expected a string\n\tin tuple4")
->
pass);
test "not array" (fun () ->
Expand All @@ -538,7 +539,7 @@ let () =
(tuple4 int int int int) (parseOrRaise {| 4 |})
in
fail "should throw"
with DecodeError (Json_error "Expected array, got 4") -> pass);
with Of_json_error (Json_error "Expected array, got 4") -> pass);
test "non-DecodeError exceptions in decoder should pass through"
(fun () ->
try
Expand Down Expand Up @@ -582,8 +583,8 @@ let () =
in
fail "should throw"
with
| DecodeError
(Json_error "Expected string, got null\n\tin dict")
| Json.Of_json_error
(Json_error "expected a string\n\tin dict")
->
pass);
test "non-DecodeError exceptions in decoder should pass through"
Expand Down Expand Up @@ -628,7 +629,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) =
Expand All @@ -637,8 +638,8 @@ let () =
in
fail "should throw"
with
| DecodeError
(Json_error "Expected string, got null\n\tat field 'b'")
| Of_json_error
(Json_error "expected a string\n\tat field 'b'")
->
pass);

Expand Down Expand Up @@ -689,7 +690,7 @@ let () =
in
fail "should throw"
with
| DecodeError (Json_error "Expected field 'y'\n\tat field 'a'")
| Json.Of_json_error (Json_error "Expected field 'y'\n\tat field 'a'")
->
pass);
test "decoder error" (fun () ->
Expand All @@ -704,12 +705,9 @@ let () =
in
fail "should throw"
with
| DecodeError
| Json.Of_json_error
(Json_error
"Expected null, got \"foo\"\n\
\tat field 'y'\n\
\tat field 'x'\n\
\tat field 'a'")
"Expected null\n\tat field 'y'\n\tat field 'x'\n\tat field 'a'")
->
pass);
test "empty list of keys should raise Invalid_argument" (fun () ->
Expand Down Expand Up @@ -785,7 +783,7 @@ 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"
(fun () ->
Expand Down Expand Up @@ -921,12 +919,9 @@ let () =
in
fail "should throw"
with
| DecodeError
| Json.Of_json_error
(Json_error
"Expected number, got true\n\
\tin array at index 0\n\
\tin array at index 1\n\
\tin dict")
"expected an integer\n\tin dict")
->
pass);
test "dict array array int - heterogenous structure 2" (fun () ->
Expand All @@ -938,11 +933,9 @@ let () =
in
fail "should throw"
with
| DecodeError
(Json_error
"Expected array, got \"foo\"\n\
\tin array at index 1\n\
\tin dict")
| Json.Of_json_error
(Json.Json_error
"expected a JSON array\n\tin dict")
->
pass);
test "field" (fun () ->
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/Json_encode_test.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[@@@alert "-deprecated"]
open Jest
open Expect
open! Json.Encode
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/Json_test.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[@@@alert "-deprecated"]
open Jest
open Expect
open Json
Expand Down

0 comments on commit 01db80f

Please sign in to comment.