mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-09-10 05:06:15 -05:00
37 lines
1008 B
TypeScript
37 lines
1008 B
TypeScript
import { expect } from "vitest";
|
|
import type {
|
|
PersistedDefinition,
|
|
PersistedMapDefinition,
|
|
} from "./persisted-state";
|
|
|
|
/** Asserts `decode(encode(x))` deep-equals `x` for every example, and that a missing value decodes to the default. */
|
|
export function assertRoundTrips<T>(
|
|
definition: PersistedDefinition<T> | PersistedMapDefinition<T>,
|
|
examples: NoInfer<T>[],
|
|
) {
|
|
expect
|
|
.soft(definition.decode(null), "missing → default")
|
|
.toEqual(definition.default);
|
|
|
|
for (const value of examples) {
|
|
expect
|
|
.soft(
|
|
definition.decode(definition.encode(value)),
|
|
`round trip of ${JSON.stringify(value)}`,
|
|
)
|
|
.toEqual(value);
|
|
}
|
|
}
|
|
|
|
/** Asserts that each given raw stored string decodes to the definition's default. */
|
|
export function assertDecodesToDefault<T>(
|
|
definition: PersistedDefinition<T> | PersistedMapDefinition<T>,
|
|
raws: string[],
|
|
) {
|
|
for (const raw of raws) {
|
|
expect
|
|
.soft(definition.decode(raw), `${JSON.stringify(raw)} → default`)
|
|
.toEqual(definition.default);
|
|
}
|
|
}
|