Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
split
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT committed Sep 30, 2023
1 parent 6830366 commit 3268be9
Show file tree
Hide file tree
Showing 12 changed files with 342 additions and 324 deletions.
45 changes: 45 additions & 0 deletions src/handlers/tsonBigint.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { expect, test } from "vitest";
import { createTupleson } from "../tson.js";

Check failure on line 2 in src/handlers/tsonBigint.test.ts

View workflow job for this annotation

GitHub Actions / lint

Missed spacing between "vitest" and "../tson.js" imports
import { tsonBigint } from "./tsonBigint.js";
import { tsonMap } from "./tsonMap.js";
import { tsonSet } from "./tsonSet.js";

test("bigint", () => {
const t = createTupleson({
types: {
Map: tsonMap,
Set: tsonSet,
bigint: tsonBigint,
},
});

{
// bigint
const expected = 1n;

const stringified = t.stringify(expected);
const deserialized = t.parse(stringified);

expect(deserialized).toEqual(expected);

{
// set of BigInt
const expected = new Set([1n]);

const stringified = t.stringify(expected);
const deserialized = t.parse(stringified);

expect(deserialized).toEqual(expected);
}

{
// set of a map of bigint
const expected = new Set([new Map([["a", 1n]])]);

const stringified = t.stringify(expected);
const deserialized = t.parse(stringified);

expect(deserialized).toEqual(expected);
}
}
});
17 changes: 17 additions & 0 deletions src/handlers/tsonDate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect, test } from "vitest";
import { createTupleson } from "../tson.js";

Check failure on line 2 in src/handlers/tsonDate.test.ts

View workflow job for this annotation

GitHub Actions / lint

Missed spacing between "vitest" and "../tson.js" imports
import { tsonDate } from "./tsonDate.js";

test("Date", () => {
const ctx = createTupleson({
types: {
Date: tsonDate,
},
});

const date = new Date();

const stringified = ctx.stringify(date);
const deserialized = ctx.parse(stringified);
expect(deserialized).toEqual(date);
});
18 changes: 18 additions & 0 deletions src/handlers/tsonMap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect, test } from "vitest";

import { createTupleson } from "../tson.js";
import { tsonMap } from "./tsonMap.js";

test("Map", () => {
const t = createTupleson({
types: {
Map: tsonMap,
},
});

const expected = new Map([["a", "b"]]);

const stringified = t.stringify(expected);
const deserialized = t.parse(stringified);
expect(deserialized).toEqual(expected);
});
41 changes: 41 additions & 0 deletions src/handlers/tsonNumber.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { expect, test } from "vitest";

import { expectError } from "../testUtils.js";
import { createTupleson } from "../tson.js";
import { tsonNumber } from "./tsonNumber.js";

test("number", () => {
const t = createTupleson({
types: {
number: tsonNumber,
},
});

const bad = [
//
NaN,
Infinity,
-Infinity,
];
const good = [1, 0, -1, 1.1, -1.1];

const errors: unknown[] = [];

for (const n of bad) {
const err = expectError(() => t.parse(t.stringify(n)));
errors.push(err);
}

expect(errors).toMatchInlineSnapshot(`
[
[Error: Encountered NaN],
[Error: Encountered Infinity],
[Error: Encountered Infinity],
]
`);

for (const n of good) {
const deserialized = t.parse(t.stringify(n));
expect(deserialized).toEqual(n);
}
});
34 changes: 34 additions & 0 deletions src/handlers/tsonRegExp.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { expect, test } from "vitest";
import { createTupleson } from "../tson.js";

Check failure on line 2 in src/handlers/tsonRegExp.test.ts

View workflow job for this annotation

GitHub Actions / lint

Missed spacing between "vitest" and "../tson.js" imports
import { tsonRegExp } from "./index.js";

test("regex", () => {
const t = createTupleson({
types: {
RegExp: tsonRegExp,
},
});

const expected = /foo/g;

const stringified = t.stringify(expected, 2);

expect(stringified).toMatchInlineSnapshot(
`
"{
\\"json\\": [
\\"RegExp\\",
\\"/foo/g\\",
\\"__tson\\"
],
\\"nonce\\": \\"__tson\\"
}"
`,
);

const deserialized = t.parse(stringified);

expect(deserialized).toBeInstanceOf(RegExp);
expect(deserialized).toMatchInlineSnapshot("/foo/g");
expect(deserialized + "").toEqual(expected + "");
});
18 changes: 18 additions & 0 deletions src/handlers/tsonSet.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect, test } from "vitest";

import { createTupleson } from "../tson.js";
import { tsonSet } from "./tsonSet.js";

test("Set", () => {
const t = createTupleson({
types: {
Set: tsonSet,
},
});

const expected = new Set(["a", "b"]);

const stringified = t.stringify(expected);
const deserialized = t.parse(stringified);
expect(deserialized).toEqual(expected);
});
20 changes: 20 additions & 0 deletions src/handlers/tsonUndefined.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect, test } from "vitest";

import { createTupleson } from "../tson.js";
import { tsonUndefined } from "./tsonUndefined.js";

test("undefined", () => {
const ctx = createTupleson({
types: {
undefined: tsonUndefined,
},
});

const expected = {
foo: [1, undefined, 2],
} as const;
const stringified = ctx.stringify(expected);
const deserialized = ctx.parse(stringified);

expect(deserialized).toEqual(expected);
});
47 changes: 47 additions & 0 deletions src/handlers/tsonUnknown.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { assert, expect, test } from "vitest";
import { expectError } from "../testUtils.js";

Check failure on line 2 in src/handlers/tsonUnknown.test.ts

View workflow job for this annotation

GitHub Actions / lint

Missed spacing between "vitest" and "../testUtils.js" imports
import { createTupleson } from "../tson.js";
import { tsonSet } from "./tsonSet.js";
import { UnknownObjectGuardError, tsonUnknown } from "./tsonUnknown.js";

test("guard unwanted objects", () => {
// Sets are okay, but not Maps
const t = createTupleson({
types: {
Set: tsonSet,
// defined last so it runs last
unknownObjectGuard: tsonUnknown,
},
});

{
// sets are okay
const expected = new Set([1]);

const stringified = t.stringify(expected);
const deserialized = t.parse(stringified);

expect(deserialized).toEqual(expected);
}

{
// plain objects are okay
const expected = { a: 1 };
const stringified = t.stringify(expected);
const deserialized = t.parse(stringified);
expect(deserialized).toEqual(expected);
}

{
// maps are not okay
const expected = new Map([["a", 1]]);

const err = expectError(() => t.parse(t.stringify(expected)));
assert(err instanceof UnknownObjectGuardError);

expect(err).toMatchInlineSnapshot(
"[UnknownObjectGuardError: Unknown object found]",
);
expect(err.value).toEqual(expected);
}
});
73 changes: 73 additions & 0 deletions src/stringify.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { expect, test } from "vitest";

import { tsonBigint } from "./handlers/tsonBigint.js";
import { tsonMap } from "./handlers/tsonMap.js";
import { tsonSet } from "./handlers/tsonSet.js";
import { tsonUndefined } from "./handlers/tsonUndefined.js";
import { createTupleson } from "./tson.js";

test("lets have a look at the stringified output", () => {
const t = createTupleson({
types: {
Map: tsonMap,
Set: tsonSet,
bigint: tsonBigint,
undefined: tsonUndefined,
},
});

const expected = new Set([
//
1,
"string",
undefined,
null,
true,
false,
1n,
new Map([["foo", "bar"]]),
]);

const stringified = t.stringify(expected, 2);

expect(stringified).toMatchInlineSnapshot(`
"{
\\"json\\": [
\\"Set\\",
[
1,
\\"string\\",
[
\\"undefined\\",
0,
\\"__tson\\"
],
null,
true,
false,
[
\\"bigint\\",
\\"1\\",
\\"__tson\\"
],
[
\\"Map\\",
[
[
\\"foo\\",
\\"bar\\"
]
],
\\"__tson\\"
]
],
\\"__tson\\"
],
\\"nonce\\": \\"__tson\\"
}"
`);

const deserialized = t.parse(stringified);

expect(deserialized).toEqual(expected);
});
8 changes: 0 additions & 8 deletions src/setup.ts → src/testUtils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import { expect } from "vitest";

import {
tsonDeserializer,
tsonParser,
tsonSerializer,
tsonStringifier,
} from "./tson.js";
import { TsonOptions } from "./types.js";

export const expectError = (fn: () => unknown) => {
let err: unknown;
try {
Expand Down
Loading

0 comments on commit 3268be9

Please sign in to comment.