This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
generated from JoshuaKGoldberg/create-typescript-app
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
342 additions
and
324 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { expect, test } from "vitest"; | ||
import { createTupleson } from "../tson.js"; | ||
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); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { expect, test } from "vitest"; | ||
import { createTupleson } from "../tson.js"; | ||
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { expect, test } from "vitest"; | ||
import { createTupleson } from "../tson.js"; | ||
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 + ""); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
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); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.