-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathDecode_AsResult_OfStringNel_test.re
133 lines (115 loc) · 3.64 KB
/
Decode_AsResult_OfStringNel_test.re
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
open Jest;
open Expect;
open Relude.Globals;
[@ocaml.warning "-3"]
module Decode = Decode.AsResult.OfStringNel;
module Sample = Decode_TestSampleData;
module Nel = Decode.NonEmptyList;
let makeErr = (msg, json) =>
Result.error(Nel.pure(msg ++ " " ++ Js.Json.stringify(json)));
describe("Simple decode errors", () => {
test("boolean", () =>
expect(Decode.boolean(Sample.jsonNull))
|> toEqual(makeErr("Expected boolean but found", Sample.jsonNull))
);
test("string", () =>
expect(Decode.string(Sample.jsonNull))
|> toEqual(makeErr("Expected string but found", Sample.jsonNull))
);
test("floatFromNumber", () =>
expect(Decode.floatFromNumber(Sample.jsonNull))
|> toEqual(makeErr("Expected number but found", Sample.jsonNull))
);
test("intFromNumber (non-number)", () =>
expect(Decode.intFromNumber(Sample.jsonNull))
|> toEqual(makeErr("Expected number but found", Sample.jsonNull))
);
test("intFromNumber (float)", () =>
expect(Decode.intFromNumber(Sample.jsonFloat))
|> toEqual(makeErr("Expected int but found", Sample.jsonFloat))
);
test("date", () =>
expect(Decode.date(Sample.jsonString))
|> toEqual(makeErr("Expected a valid date but found", Sample.jsonString))
);
test("variant", () =>
expect(Decode.variantFromString(Sample.colorFromJs, Sample.jsonString))
|> toEqual(
makeErr("Expected a valid option but found", Sample.jsonString),
)
);
test("array", () =>
expect(Decode.array(Decode.string, Sample.jsonNull))
|> toEqual(makeErr("Expected array but found", Sample.jsonNull))
);
test("tuple", () =>
expect(Decode.(tuple(string, boolean, Sample.jsonArrayEmpty)))
|> toEqual(
makeErr("Expected tuple of size 2 but found", Sample.jsonArrayEmpty),
)
);
test("object", () =>
expect(Decode.field("x", Decode.string, Sample.jsonArrayEmpty))
|> toEqual(makeErr("Expected object but found", Sample.jsonArrayEmpty))
);
});
describe("Inner decoders", () => {
test("array inner value (success)", () =>
expect(Decode.(array(string, Sample.jsonArrayString)))
|> toEqual(Result.ok(Sample.valArrayString))
);
test("array inner value (failure)", () =>
expect(Decode.(array(boolean, Sample.jsonArrayString)))
|> toEqual(
Result.error(
NonEmpty.List.make(
"While decoding array, at position 0: Expected boolean but found \"A\"",
[
"While decoding array, at position 1: Expected boolean but found \"B\"",
"While decoding array, at position 2: Expected boolean but found \"C\"",
],
),
),
)
);
test("field (missing)", () =>
expect(Decode.field("x", Decode.string, Sample.jsonDictEmpty))
|> toEqual(
Result.error(NonEmpty.List.pure("Object field \"x\" was missing")),
)
);
test("field (multiple, failure on second)", () =>
expect(
Decode.(
map4(
Sample.makeJob,
field("title", string),
field("manager", string),
field("startDate", date),
pure(None),
Sample.jsonJobCeo,
)
),
)
|> toEqual(
makeErr(
"While decoding object, for field \"manager\": Expected string but found",
Js.Json.null,
),
)
);
test("oneOf", () => {
let decodeUnion =
Decode.(
oneOf(
map(Sample.unionS, string),
[
map(Sample.unionN, optional(floatFromNumber)),
map(Sample.unionB, boolean),
],
)
);
expect(decodeUnion(Sample.jsonTrue))
|> toEqual(Result.ok(Sample.(B(valBool))));
});
});