diff --git a/src/Decode.re b/src/Decode.re index 105eb9c..6f265d8 100644 --- a/src/Decode.re +++ b/src/Decode.re @@ -5,9 +5,6 @@ module Make = Base.Make; module ParseError = Decode_ParseError; -[@deprecated "Use Decode.ParseError instead"] -module AsOption = Decode_AsOption; - module AsResult = { module OfParseError = Decode_AsResult_OfParseError; }; diff --git a/test/Decode_AsOption_test.re b/test/Decode_AsOption_test.re deleted file mode 100644 index 9dacf73..0000000 --- a/test/Decode_AsOption_test.re +++ /dev/null @@ -1,69 +0,0 @@ -open Jest; -open Expect; -open Relude.Globals; - -[@ocaml.warning "-3"] -module Decode = Decode.AsOption; -module Sample = Decode_TestSampleData; - -describe("Nested decoders", () => { - test("at (succeeds on nested field)", () => - expect( - Decode.( - at(["job", "manager", "job", "title"], string, Sample.jsonPersonBill) - ), - ) - |> toEqual(Some("CEO")) - ); - - test("at (succeeds on inner decode with no fields)", () => - expect(Decode.(at([], string, Sample.jsonString))) - |> toEqual(Some(Sample.valString)) - ); - - test("at (fails on missing field)", () => - expect(Decode.(at(["manager", "name"], string, Sample.jsonJobCeo))) - |> toEqual(None) - ); - - test("at (fails on invalid inner data)", () => - expect(Decode.(at(["age"], string, Sample.jsonPersonBill))) - |> toEqual(None) - ); - - test("field (succeeds)", () => - expect(Decode.(field("title", string, Sample.jsonJobCeo))) - |> toEqual(Some("CEO")) - ); - - test("field (fails on missing)", () => - expect(Decode.(field("x", string, Sample.jsonDictEmpty))) - |> toEqual(None) - ); -}); - -describe("Decode records", () => { - let ((<$>), (<*>)) = Decode.(map, apply); - let decodeJobInfix = - Sample.makeJob - <$> Decode.(field("title", string)) - <*> Decode.(field("companyName", string)) - <*> Decode.(field("startDate", date)) - <*> Decode.pure(None); - - test("lazy infix", () => - expect(decodeJobInfix(Sample.jsonJobCeo)) - |> toEqual(Some(Sample.jobCeo)) - ); -}); - -// here we import a gigantic json file (as raw json, to avoid slowing down the -// compiler) -[@bs.module] external bigjson: Js.Json.t = "./utils/BigJson.json"; - -describe("Big JSON array", () => - test("is stack-safe", () => - expect(Decode.array(Option.pure, bigjson) |> Option.isSome) - |> toEqual(true) - ) -); diff --git a/test/Decode_AsResult_OfParseError_test.re b/test/Decode_AsResult_OfParseError_test.re index a944588..81a963d 100644 --- a/test/Decode_AsResult_OfParseError_test.re +++ b/test/Decode_AsResult_OfParseError_test.re @@ -395,6 +395,45 @@ describe("Dictionaries, records", () => { ) ); + test("at (succeeds on nested field)", () => + Sample.jsonPersonBill + |> at(["job", "manager", "job", "title"], string) + |> expect + |> toEqual(Ok("CEO")) + ); + + test("at (succeeds on inner decode with no fields)", () => + Sample.jsonString + |> at([], string) + |> expect + |> toEqual(Ok(Sample.valString)) + ); + + test("at (fails on missing field)", () => + Sample.jsonJobCeo + |> at(["manager", "name"], string) + |> expect + |> toEqual( + invalidFieldErr("manager", Val(`ExpectedObject, Js.Json.null)), + ) + ); + + test("at (fails on invalid inner data)", () => + Sample.jsonPersonBill + |> at(["age"], string) + |> expect + |> toEqual( + invalidFieldErr("age", Val(`ExpectedString, Js.Json.number(27.0))), + ) + ); + + test("field (succeeds)", () => + Sample.jsonJobCeo + |> field("title", string) + |> expect + |> toEqual(Ok("CEO")) + ); + test("field (failure, non-object)", () => Sample.jsonArrayEmpty |> field("x", string) @@ -704,7 +743,19 @@ describe("Decode utils", () => { }); describe("Letops, infix", () => { - test("field (failure, second field has wrong type)", () => { + test("letops map, apply (success)", () => { + let decodeJob = { + let+ title = field("title", string) + and+ companyName = field("companyName", string) + and+ startDate = field("startDate", date) + and+ manager = pure(None); + Sample.{title, companyName, startDate, manager}; + }; + + Sample.jsonJobCeo |> decodeJob |> expect |> toEqual(Ok(Sample.jobCeo)); + }); + + test("letops map, apply (failure, second field has wrong type)", () => { // companyName is intentionally wrong let decodeJob = { let+ title = field("title", string) @@ -720,7 +771,22 @@ describe("Letops, infix", () => { |> toEqual( invalidFieldErr("manager", Val(`ExpectedString, Sample.jsonNull)), ); - }) + }); + + test("infix map, apply (success)", () => { + let ((<$>), (<*>)) = (map, apply); + let decodeJobInfix = + Sample.makeJob + <$> field("title", string) + <*> field("companyName", string) + <*> field("startDate", date) + <*> pure(None); + + Sample.jsonJobCeo + |> decodeJobInfix + |> expect + |> toEqual(Ok(Sample.jobCeo)); + }); }); describe("ParseError", () => { @@ -955,3 +1021,13 @@ describe("Deprecated decoders", () => { |> toEqual(objErr(("x", MissingField), [("y", MissingField)])) ); }); + +// here we import a gigantic json file (as raw json, to avoid slowing down the +// compiler) +[@bs.module] external bigjson: Js.Json.t = "./utils/BigJson.json"; + +describe("Big JSON", () => { + test("is stack-safe", () => + bigjson |> array(okJson) |> Result.isOk |> expect |> toEqual(true) + ) +});