mirror of
https://github.com/voson-wang/toon.git
synced 2026-01-29 23:34:10 +08:00
54 lines
1.9 KiB
TypeScript
54 lines
1.9 KiB
TypeScript
import type { ResolvedEncodeOptions } from '../src/types'
|
|
import type { Fixtures, TestCase } from './types'
|
|
import arraysNested from '@toon-format/spec/tests/fixtures/encode/arrays-nested.json'
|
|
import arraysObjects from '@toon-format/spec/tests/fixtures/encode/arrays-objects.json'
|
|
import arraysPrimitive from '@toon-format/spec/tests/fixtures/encode/arrays-primitive.json'
|
|
import arraysTabular from '@toon-format/spec/tests/fixtures/encode/arrays-tabular.json'
|
|
import delimiters from '@toon-format/spec/tests/fixtures/encode/delimiters.json'
|
|
import keyFolding from '@toon-format/spec/tests/fixtures/encode/key-folding.json'
|
|
import objects from '@toon-format/spec/tests/fixtures/encode/objects.json'
|
|
import primitives from '@toon-format/spec/tests/fixtures/encode/primitives.json'
|
|
import whitespace from '@toon-format/spec/tests/fixtures/encode/whitespace.json'
|
|
import { describe, expect, it } from 'vitest'
|
|
import { DEFAULT_DELIMITER, encode } from '../src/index'
|
|
|
|
const fixtureFiles = [
|
|
primitives,
|
|
objects,
|
|
arraysPrimitive,
|
|
arraysTabular,
|
|
arraysNested,
|
|
arraysObjects,
|
|
keyFolding,
|
|
delimiters,
|
|
whitespace,
|
|
] as Fixtures[]
|
|
|
|
for (const fixtures of fixtureFiles) {
|
|
describe(fixtures.description, () => {
|
|
for (const test of fixtures.tests) {
|
|
it(test.name, () => {
|
|
const resolvedOptions = resolveEncodeOptions(test.options)
|
|
|
|
if (test.shouldError) {
|
|
expect(() => encode(test.input, resolvedOptions))
|
|
.toThrow()
|
|
}
|
|
else {
|
|
const result = encode(test.input, resolvedOptions)
|
|
expect(result).toBe(test.expected)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
function resolveEncodeOptions(options?: TestCase['options']): ResolvedEncodeOptions {
|
|
return {
|
|
indent: options?.indent ?? 2,
|
|
delimiter: options?.delimiter ?? DEFAULT_DELIMITER,
|
|
keyFolding: options?.keyFolding ?? 'off',
|
|
flattenDepth: options?.flattenDepth ?? Number.POSITIVE_INFINITY,
|
|
}
|
|
}
|