(** Tests for JSON decode/encode roundtrips through the bytesrw streaming I/O surface. *) let test_decode_primitive () = match Json.of_string Json.Codec.int "42" with | Error e -> Alcotest.failf "decode failed: %a" Json.Error.pp e | Ok n -> Alcotest.(check int) "42 round-trip" 42 n let test_encode_primitive () = let s = Json.to_string Json.Codec.int 7 in Alcotest.(check string) "7 encoded" "7" s let test_roundtrip_object () = let pair_codec = let open Json.Codec.Object in map ~kind:"pair" (fun a b -> (a, b)) |> member "a" Json.Codec.int ~enc:fst |> member "b" Json.Codec.string ~enc:snd |> seal in let input = {|{"a": 7, "b": "hi"}|} in match Json.of_string pair_codec input with | Error e -> Alcotest.failf "decode failed: %a" Json.Error.pp e | Ok (a, b) -> Alcotest.(check int) "a" 7 a; Alcotest.(check string) "b" "hi" b let test_decode_error () = match Json.of_string Json.Codec.int "not json" with | Ok _ -> Alcotest.fail "expected decode error" | Error _ -> () let suite = ( "json_bytesrw", [ Alcotest.test_case "decode int" `Quick test_decode_primitive; Alcotest.test_case "encode int" `Quick test_encode_primitive; Alcotest.test_case "roundtrip object" `Quick test_roundtrip_object; Alcotest.test_case "decode error" `Quick test_decode_error; ] )