Skip to content

Commit

Permalink
chore: add more tests for decoders
Browse files Browse the repository at this point in the history
  • Loading branch information
bombillazo committed Feb 4, 2024
1 parent 648a549 commit 91f8b64
Showing 1 changed file with 124 additions and 24 deletions.
148 changes: 124 additions & 24 deletions tests/decode_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { assert } from "https://deno.land/[email protected]/_util/assert.ts";
import {
decodeBigint,
decodeBigintArray,
Expand All @@ -7,6 +6,7 @@ import {
decodeBox,
decodeCircle,
decodeDate,
decodeDatetime,
decodeFloat,
decodeInt,
decodeJson,
Expand All @@ -19,15 +19,15 @@ import {
import { assertEquals, assertThrows } from "./test_deps.ts";

Deno.test("decodeBigint", function () {
assertEquals(decodeBigint("18014398509481984"), 18014399223381984n);
assertEquals(decodeBigint("18014398509481984"), 18014398509481984n);
});

Deno.test("decodeBigintArray", function () {
assertEquals(
decodeBigintArray(
"{17365398509481972,9007199254740992,-10414398509481984}",
"{17365398509481972,9007199254740992,-10414398509481984}"
),
[17365398509481972n, 9007199254740992n, -10414398509481984n],
[17365398509481972n, 9007199254740992n, -10414398509481984n]
);
});

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

Deno.test("decodeBooleanArray", function () {
assertEquals(decodeBooleanArray("{True,0,T}"), [true, false, true]);
assertEquals(decodeBooleanArray("{no,Y,1}"), [false, false, false]);
assertEquals(decodeBooleanArray("{no,Y,1}"), [false, true, true]);
});

Deno.test("decodeBox", function () {
assertEquals(decodeBox("(12.4,2),(33,4.33)"), {
a: { x: "12.4", y: "2" },
b: { x: "33", y: "4.33" },
});

let testValue = "(12.4,2)";
assertThrows(
() => decodeBox(testValue),
Error,
`Invalid Box: "${testValue}". Box must have only 2 point, 1 given.`
);
testValue = "(12.4,2),(123,123,123),(9303,33)";
assertThrows(
() => decodeBox(testValue),
Error,
`Invalid Box: "${testValue}". Box must have only 2 point, 3 given.`
);
testValue = "(0,0),(123,123,123)";
assertThrows(
() => decodeBox(testValue),
Error,
`Invalid Box: "${testValue}" : Invalid Point: "(123,123,123)". Points must have only 2 coordinates, 3 given.`
);
testValue = "(0,0),(100,r100)";
assertThrows(
() => decodeBox("(12.4,2)"),
() => decodeBox(testValue),
Error,
"Invalid Box value: `(12.4,2)`",
`Invalid Box: "${testValue}" : Invalid Point: "(100,r100)". Coordinate "r100" must be a valid number.`
);
});

Expand All @@ -71,16 +89,32 @@ Deno.test("decodeCircle", function () {
point: { x: "12.4", y: "2" },
radius: "3.5",
});
let testValue = "<(c21 23,2),3.5>";
assertThrows(
() => decodeCircle(testValue),
Error,
`Invalid Circle: "${testValue}" : Invalid Point: "(c21 23,2)". Coordinate "c21 23" must be a valid number.`
);
testValue = "<(33,2),mn23 3.5>";
assertThrows(
() => decodeCircle(testValue),
Error,
`Invalid Circle: "${testValue}". Circle radius "mn23 3.5" must be a valid number.`
);
});

Deno.test("decodeDate", function () {
assertEquals(decodeDate("2021-08-01"), new Date("2021-08-01"));
assertEquals(decodeDate("2021-08-01"), new Date("2021-08-01 00:00:00-00"));
});

Deno.test("decodeDatetime", function () {
assertEquals(
decodeDate("1997-12-17 07:37:16-08"),
new Date("1997-12-17 07:37:16-08"),
decodeDatetime("2021-08-01"),
new Date("2021-08-01 00:00:00-00")
);
assertEquals(
decodeDatetime("1997-12-17 07:37:16-08"),
new Date("1997-12-17 07:37:16-08")
);
});

Expand All @@ -97,31 +131,63 @@ Deno.test("decodeInt", function () {
Deno.test("decodeJson", function () {
assertEquals(
decodeJson(
'{"key_1": "MY VALUE", "key_2": null, "key_3": 10, "key_4": {"subkey_1": true}}',
'{"key_1": "MY VALUE", "key_2": null, "key_3": 10, "key_4": {"subkey_1": true, "subkey_2": ["1",2]}}'
),
{
key_1: "MY VALUE",
key_2: null,
key_3: 10,
key_4: { subkey_1: true },
},
key_4: { subkey_1: true, subkey_2: ["1", 2] },
}
);
assertEquals(decodeJson("{ 'eqw' ; ddd}"), null);
assertThrows(() => decodeJson("{ 'eqw' ; ddd}"));
});

Deno.test("decodeLine", function () {
assertEquals(decodeLine("{100,50,350}"), { a: "100", b: "50", c: "350" });
assertEquals(decodeLine("{100,50,0}"), { a: "100", b: "50", c: "0" });
let testValue = "{100,50,0,100}";
assertThrows(
() => decodeLine("{100,50,0,100}"),
Error,
`Invalid Line: "${testValue}". Line in linear equation format must have 3 constants, 4 given.`
);
testValue = "{100,d3km,0}";
assertThrows(
() => decodeLine(testValue),
Error,
`Invalid Line: "${testValue}". Line constant "d3km" must be a valid number.`
);
});

Deno.test("decodeLineSegment", function () {
assertEquals(decodeLineSegment("((100,50),(350,350))"), {
a: { x: "100", y: "50" },
b: { x: "350", y: "350" },
});
let testValue = "((100,50),(r344,350))";
assertThrows(
() => decodeLineSegment("((100,50))"),
() => decodeLineSegment(testValue),
Error,
"Invalid LineSegment value: `((100,50))`",
`Invalid Line Segment: "${testValue}" : Invalid Point: "(r344,350)". Coordinate "r344" must be a valid number.`

);
testValue = "((100),(r344,350))";
assertThrows(
() => decodeLineSegment(testValue),
Error,
`Invalid Line Segment: "${testValue}" : Invalid Point: "(100)". Points must have only 2 coordinates, 1 given.`
);
testValue = "((100,50))";
assertThrows(
() => decodeLineSegment(testValue),
Error,
`Invalid Line Segment: "${testValue}". Line segments must have only 2 point, 1 given.`
);
testValue = "((100,50),(350,350),(100,100))";
assertThrows(
() => decodeLineSegment(testValue),
Error,
`Invalid Line Segment: "${testValue}". Line segments must have only 2 point, 3 given.`
);
});

Expand All @@ -130,22 +196,56 @@ Deno.test("decodePath", function () {
{ x: "100", y: "50" },
{ x: "350", y: "350" },
]);
assertEquals(decodePath("[(1,10),(2,20),(3,30)]"), [
{ x: "1", y: "10" },
{ x: "2", y: "20" },
{ x: "3", y: "30" },
]);
let testValue = "((100,50),(350,kjf334))";
assertThrows(
() => decodePath("((100,50))"),
() => decodePath(testValue),
Error,
"Invalid Path value: `((100,50))`",
`Invalid Path: "${testValue}" : Invalid Point: "(350,kjf334)". Coordinate "kjf334" must be a valid number.`
);
testValue = "((100,50,9949))";
assertThrows(
() => decodePath(testValue),
Error,
`Invalid Path: "${testValue}" : Invalid Point: "(100,50,9949)". Points must have only 2 coordinates, 3 given.`
);
});

Deno.test("decodePoint", function () {
assertEquals(decodePoint("(10.5,50.8)"), { x: "10.5", y: "50.8" });
assertEquals(decodePoint("(10.555,50.8)"), { x: "10.555", y: "50.8" });
let testValue = "(1000)";
assertThrows(
() => decodePoint(testValue),
Error,
`Invalid Point: "${testValue}". Points must have only 2 coordinates, 1 given.`
);
testValue = "(100.100,50,350)";
assertThrows(
() => decodePoint("(100.100,50,350)"),
() => decodePoint(testValue),
Error,
"Invalid Point value: `(100,50,350)`",
`Invalid Point: "${testValue}". Points must have only 2 coordinates, 3 given.`
);
testValue = "(1,r344)";
assertThrows(
() => decodePoint(testValue),
Error,
`Invalid Point: "${testValue}". Coordinate "r344" must be a valid number.`
);
testValue = "(cd 213ee,100)";
assertThrows(
() => decodePoint(testValue),
Error,
`Invalid Point: "${testValue}". Coordinate "cd 213ee" must be a valid number.`
);
});

Deno.test("decodeTid", function () {
assertEquals(decodeTid("(19714398509481984,5n)"), [19714398509481984n, 5n]);
assertEquals(decodeTid("(19714398509481984,29383838509481984)"), [
19714398509481984n,
29383838509481984n,
]);
});

0 comments on commit 91f8b64

Please sign in to comment.