Skip to content

Commit 91f8b64

Browse files
committed
chore: add more tests for decoders
1 parent 648a549 commit 91f8b64

File tree

1 file changed

+124
-24
lines changed

1 file changed

+124
-24
lines changed

tests/decode_test.ts

Lines changed: 124 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { assert } from "https://deno.land/[email protected]/_util/assert.ts";
21
import {
32
decodeBigint,
43
decodeBigintArray,
@@ -7,6 +6,7 @@ import {
76
decodeBox,
87
decodeCircle,
98
decodeDate,
9+
decodeDatetime,
1010
decodeFloat,
1111
decodeInt,
1212
decodeJson,
@@ -19,15 +19,15 @@ import {
1919
import { assertEquals, assertThrows } from "./test_deps.ts";
2020

2121
Deno.test("decodeBigint", function () {
22-
assertEquals(decodeBigint("18014398509481984"), 18014399223381984n);
22+
assertEquals(decodeBigint("18014398509481984"), 18014398509481984n);
2323
});
2424

2525
Deno.test("decodeBigintArray", function () {
2626
assertEquals(
2727
decodeBigintArray(
28-
"{17365398509481972,9007199254740992,-10414398509481984}",
28+
"{17365398509481972,9007199254740992,-10414398509481984}"
2929
),
30-
[17365398509481972n, 9007199254740992n, -10414398509481984n],
30+
[17365398509481972n, 9007199254740992n, -10414398509481984n]
3131
);
3232
});
3333

@@ -50,19 +50,37 @@ Deno.test("decodeBoolean", function () {
5050

5151
Deno.test("decodeBooleanArray", function () {
5252
assertEquals(decodeBooleanArray("{True,0,T}"), [true, false, true]);
53-
assertEquals(decodeBooleanArray("{no,Y,1}"), [false, false, false]);
53+
assertEquals(decodeBooleanArray("{no,Y,1}"), [false, true, true]);
5454
});
5555

5656
Deno.test("decodeBox", function () {
5757
assertEquals(decodeBox("(12.4,2),(33,4.33)"), {
5858
a: { x: "12.4", y: "2" },
5959
b: { x: "33", y: "4.33" },
6060
});
61-
61+
let testValue = "(12.4,2)";
62+
assertThrows(
63+
() => decodeBox(testValue),
64+
Error,
65+
`Invalid Box: "${testValue}". Box must have only 2 point, 1 given.`
66+
);
67+
testValue = "(12.4,2),(123,123,123),(9303,33)";
68+
assertThrows(
69+
() => decodeBox(testValue),
70+
Error,
71+
`Invalid Box: "${testValue}". Box must have only 2 point, 3 given.`
72+
);
73+
testValue = "(0,0),(123,123,123)";
74+
assertThrows(
75+
() => decodeBox(testValue),
76+
Error,
77+
`Invalid Box: "${testValue}" : Invalid Point: "(123,123,123)". Points must have only 2 coordinates, 3 given.`
78+
);
79+
testValue = "(0,0),(100,r100)";
6280
assertThrows(
63-
() => decodeBox("(12.4,2)"),
81+
() => decodeBox(testValue),
6482
Error,
65-
"Invalid Box value: `(12.4,2)`",
83+
`Invalid Box: "${testValue}" : Invalid Point: "(100,r100)". Coordinate "r100" must be a valid number.`
6684
);
6785
});
6886

@@ -71,16 +89,32 @@ Deno.test("decodeCircle", function () {
7189
point: { x: "12.4", y: "2" },
7290
radius: "3.5",
7391
});
92+
let testValue = "<(c21 23,2),3.5>";
93+
assertThrows(
94+
() => decodeCircle(testValue),
95+
Error,
96+
`Invalid Circle: "${testValue}" : Invalid Point: "(c21 23,2)". Coordinate "c21 23" must be a valid number.`
97+
);
98+
testValue = "<(33,2),mn23 3.5>";
99+
assertThrows(
100+
() => decodeCircle(testValue),
101+
Error,
102+
`Invalid Circle: "${testValue}". Circle radius "mn23 3.5" must be a valid number.`
103+
);
74104
});
75105

76106
Deno.test("decodeDate", function () {
77-
assertEquals(decodeDate("2021-08-01"), new Date("2021-08-01"));
107+
assertEquals(decodeDate("2021-08-01"), new Date("2021-08-01 00:00:00-00"));
78108
});
79109

80110
Deno.test("decodeDatetime", function () {
81111
assertEquals(
82-
decodeDate("1997-12-17 07:37:16-08"),
83-
new Date("1997-12-17 07:37:16-08"),
112+
decodeDatetime("2021-08-01"),
113+
new Date("2021-08-01 00:00:00-00")
114+
);
115+
assertEquals(
116+
decodeDatetime("1997-12-17 07:37:16-08"),
117+
new Date("1997-12-17 07:37:16-08")
84118
);
85119
});
86120

@@ -97,31 +131,63 @@ Deno.test("decodeInt", function () {
97131
Deno.test("decodeJson", function () {
98132
assertEquals(
99133
decodeJson(
100-
'{"key_1": "MY VALUE", "key_2": null, "key_3": 10, "key_4": {"subkey_1": true}}',
134+
'{"key_1": "MY VALUE", "key_2": null, "key_3": 10, "key_4": {"subkey_1": true, "subkey_2": ["1",2]}}'
101135
),
102136
{
103137
key_1: "MY VALUE",
104138
key_2: null,
105139
key_3: 10,
106-
key_4: { subkey_1: true },
107-
},
140+
key_4: { subkey_1: true, subkey_2: ["1", 2] },
141+
}
108142
);
109-
assertEquals(decodeJson("{ 'eqw' ; ddd}"), null);
143+
assertThrows(() => decodeJson("{ 'eqw' ; ddd}"));
110144
});
111145

112146
Deno.test("decodeLine", function () {
113-
assertEquals(decodeLine("{100,50,350}"), { a: "100", b: "50", c: "350" });
147+
assertEquals(decodeLine("{100,50,0}"), { a: "100", b: "50", c: "0" });
148+
let testValue = "{100,50,0,100}";
149+
assertThrows(
150+
() => decodeLine("{100,50,0,100}"),
151+
Error,
152+
`Invalid Line: "${testValue}". Line in linear equation format must have 3 constants, 4 given.`
153+
);
154+
testValue = "{100,d3km,0}";
155+
assertThrows(
156+
() => decodeLine(testValue),
157+
Error,
158+
`Invalid Line: "${testValue}". Line constant "d3km" must be a valid number.`
159+
);
114160
});
115161

116162
Deno.test("decodeLineSegment", function () {
117163
assertEquals(decodeLineSegment("((100,50),(350,350))"), {
118164
a: { x: "100", y: "50" },
119165
b: { x: "350", y: "350" },
120166
});
167+
let testValue = "((100,50),(r344,350))";
121168
assertThrows(
122-
() => decodeLineSegment("((100,50))"),
169+
() => decodeLineSegment(testValue),
123170
Error,
124-
"Invalid LineSegment value: `((100,50))`",
171+
`Invalid Line Segment: "${testValue}" : Invalid Point: "(r344,350)". Coordinate "r344" must be a valid number.`
172+
173+
);
174+
testValue = "((100),(r344,350))";
175+
assertThrows(
176+
() => decodeLineSegment(testValue),
177+
Error,
178+
`Invalid Line Segment: "${testValue}" : Invalid Point: "(100)". Points must have only 2 coordinates, 1 given.`
179+
);
180+
testValue = "((100,50))";
181+
assertThrows(
182+
() => decodeLineSegment(testValue),
183+
Error,
184+
`Invalid Line Segment: "${testValue}". Line segments must have only 2 point, 1 given.`
185+
);
186+
testValue = "((100,50),(350,350),(100,100))";
187+
assertThrows(
188+
() => decodeLineSegment(testValue),
189+
Error,
190+
`Invalid Line Segment: "${testValue}". Line segments must have only 2 point, 3 given.`
125191
);
126192
});
127193

@@ -130,22 +196,56 @@ Deno.test("decodePath", function () {
130196
{ x: "100", y: "50" },
131197
{ x: "350", y: "350" },
132198
]);
199+
assertEquals(decodePath("[(1,10),(2,20),(3,30)]"), [
200+
{ x: "1", y: "10" },
201+
{ x: "2", y: "20" },
202+
{ x: "3", y: "30" },
203+
]);
204+
let testValue = "((100,50),(350,kjf334))";
133205
assertThrows(
134-
() => decodePath("((100,50))"),
206+
() => decodePath(testValue),
135207
Error,
136-
"Invalid Path value: `((100,50))`",
208+
`Invalid Path: "${testValue}" : Invalid Point: "(350,kjf334)". Coordinate "kjf334" must be a valid number.`
209+
);
210+
testValue = "((100,50,9949))";
211+
assertThrows(
212+
() => decodePath(testValue),
213+
Error,
214+
`Invalid Path: "${testValue}" : Invalid Point: "(100,50,9949)". Points must have only 2 coordinates, 3 given.`
137215
);
138216
});
139217

140218
Deno.test("decodePoint", function () {
141-
assertEquals(decodePoint("(10.5,50.8)"), { x: "10.5", y: "50.8" });
219+
assertEquals(decodePoint("(10.555,50.8)"), { x: "10.555", y: "50.8" });
220+
let testValue = "(1000)";
221+
assertThrows(
222+
() => decodePoint(testValue),
223+
Error,
224+
`Invalid Point: "${testValue}". Points must have only 2 coordinates, 1 given.`
225+
);
226+
testValue = "(100.100,50,350)";
142227
assertThrows(
143-
() => decodePoint("(100.100,50,350)"),
228+
() => decodePoint(testValue),
144229
Error,
145-
"Invalid Point value: `(100,50,350)`",
230+
`Invalid Point: "${testValue}". Points must have only 2 coordinates, 3 given.`
231+
);
232+
testValue = "(1,r344)";
233+
assertThrows(
234+
() => decodePoint(testValue),
235+
Error,
236+
`Invalid Point: "${testValue}". Coordinate "r344" must be a valid number.`
237+
);
238+
testValue = "(cd 213ee,100)";
239+
assertThrows(
240+
() => decodePoint(testValue),
241+
Error,
242+
`Invalid Point: "${testValue}". Coordinate "cd 213ee" must be a valid number.`
146243
);
147244
});
148245

149246
Deno.test("decodeTid", function () {
150-
assertEquals(decodeTid("(19714398509481984,5n)"), [19714398509481984n, 5n]);
247+
assertEquals(decodeTid("(19714398509481984,29383838509481984)"), [
248+
19714398509481984n,
249+
29383838509481984n,
250+
]);
151251
});

0 commit comments

Comments
 (0)