mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-19 22:55:44 +01:00
Count a stripping run only between its non-blank leaves, so the trim takes two passes over alternating code and emphasized blanks
This commit is contained in:
@@ -2,11 +2,13 @@ import assert from 'node:assert/strict'
|
||||
import fc from 'fast-check'
|
||||
import test from 'node:test'
|
||||
|
||||
import type { AdfNode } from '../../adf/document.ts'
|
||||
import type { AdfMark, AdfNode } from '../../adf/document.ts'
|
||||
import type { LineContainer } from '../line-container.ts'
|
||||
import type { PortableLineFallback } from '../emit/inline-line.ts'
|
||||
import type { RunGrouping, RunNode } from '../emit/inline-runs.ts'
|
||||
import { inlineRuns } from '../emit/inline-runs.ts'
|
||||
import { lineContainer, markedLine, propertyRuns } from '../../conformance/property-harness.ts'
|
||||
import { portableJudgement, portableLeaves, reduceInline, withoutFallback } from './inline-reduction.ts'
|
||||
import { portableJudgement, portableLeaves, reduceInline, trimmedEdges, trimmingRuns, trimPass, withoutFallback } from './inline-reduction.ts'
|
||||
import { success, type Result } from '../../result.ts'
|
||||
|
||||
test('reduces a line alike dropping every run a pass may or the first alone', () => {
|
||||
@@ -58,6 +60,88 @@ test('refuses an unspellable-run fallback that takes no mark', () => {
|
||||
assert.equal(refused.ok ? 'kept' : refused.error.code, 'unsupported-node-shape')
|
||||
})
|
||||
|
||||
const groupings: readonly RunGrouping[] = ['parted', 'spanned']
|
||||
const link: AdfMark = { attrs: { href: 'u' }, type: 'link' }
|
||||
const marked = (text: string, ...marks: AdfMark[]): AdfNode => ({ marks, text, type: 'text' })
|
||||
const trimMarks = fc.oneof(
|
||||
fc.uniqueArray(fc.constantFrom<AdfMark>({ type: 'backgroundColor' }, { type: 'em' }, link, { attrs: { href: 'v' }, type: 'link' }, { type: 'strike' }, { type: 'strong' }), { maxLength: 3, selector: (held) => held.type }),
|
||||
fc.constantFrom<AdfMark[]>([{ type: 'code' }], [link, { type: 'code' }]),
|
||||
)
|
||||
const trimLeaf = fc.oneof(
|
||||
{ arbitrary: fc.record({ marks: trimMarks, text: fc.string({ maxLength: 3, minLength: 1, unit: fc.constantFrom(' ', ' ', '\t', 'x') }) }).map((held): AdfNode => ({ ...held, type: 'text' })), weight: 6 },
|
||||
{ arbitrary: fc.constant<AdfNode>({ type: 'hardBreak' }), weight: 1 },
|
||||
)
|
||||
const trimLine = fc.oneof(
|
||||
fc.array(trimLeaf, { maxLength: 12, minLength: 1 }),
|
||||
fc.tuple(fc.array(trimLeaf, { maxLength: 2 }), fc.array(trimLeaf, { maxLength: 3, minLength: 1 }), fc.integer({ max: 8, min: 2 }), fc.array(trimLeaf, { maxLength: 2 })).map(([head, unit, repeats, tail]) => [...head, ...Array.from({ length: repeats }, () => unit).flat(), ...tail]),
|
||||
)
|
||||
|
||||
for (const grouping of groupings) {
|
||||
test(`trims a line ${grouping} as peeling each run's end leaf by leaf does, in no more passes`, () => {
|
||||
fc.assert(
|
||||
fc.property(trimLine, (line) => {
|
||||
const peeled = trimmedWith(line, (leaves) => runsSharedWithNext(leaves, grouping))
|
||||
const trimmed = trimmedWith(line, (leaves) => trimmingRuns(leaves, grouping))
|
||||
assert.deepEqual(trimmedEdges(line, grouping), peeled.line)
|
||||
assert.ok(trimmed.passes <= peeled.passes, `${trimmed.passes} passes against ${peeled.passes}`)
|
||||
}),
|
||||
propertyRuns(3000),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
test('counts a run crossing a code span to its next holder where the line is spanned, and none where it is parted', () => {
|
||||
const line = [marked('Run ', { type: 'strong' }), marked('npm ci', { type: 'code' }), marked(' first', { type: 'strong' })]
|
||||
assert.deepEqual(trimmingRuns(line, 'spanned'), [1, 1, 0])
|
||||
assert.deepEqual(trimmingRuns(line, 'parted'), [0, 0, 0])
|
||||
})
|
||||
|
||||
test('counts every nested run crossing a hard break, and none crossing a hard break holding an empty marks', () => {
|
||||
assert.deepEqual(trimmingRuns([marked('a', { type: 'em' }, { type: 'strong' }), { type: 'hardBreak' }, marked('b', { type: 'em' }, { type: 'strong' })], 'spanned'), [2, 2, 0])
|
||||
assert.deepEqual(trimmingRuns([marked('a', { type: 'em' }), { marks: [], type: 'hardBreak' }, marked('b', { type: 'em' })], 'spanned'), [0, 0, 0])
|
||||
})
|
||||
|
||||
test('counts an emphasis run from its first to its last leaf holding more than spaces and tabs, and a link run to its last leaf', () => {
|
||||
const line = [marked(' \t', link, { type: 'em' }), marked('x', link, { type: 'code' }), marked('a', link, { type: 'em' }), marked('x', link, { type: 'code' }), marked('b', link, { type: 'em' }), marked('x', link, { type: 'code' }), marked(' ', link, { type: 'em' })]
|
||||
assert.deepEqual(trimmingRuns(line, 'spanned'), [1, 1, 2, 2, 1, 1, 0])
|
||||
})
|
||||
|
||||
const alternatingLines: readonly [string, AdfNode, AdfNode][] = [
|
||||
['a code space beside an emphasized space', marked(' ', { type: 'code' }), marked(' ', { type: 'em' })],
|
||||
['code x beside an emphasized space', marked('x', { type: 'code' }), marked(' ', { type: 'em' })],
|
||||
['linked code x beside a linked emphasized space', marked('x', link, { type: 'code' }), marked(' ', link, { type: 'em' })],
|
||||
]
|
||||
for (const [name, code, emphasized] of alternatingLines) {
|
||||
test(`trims a line of ${name}, 1 000 leaves long, in the passes its 6-leaf form takes`, () => {
|
||||
const alternating = (pairs: number) => Array.from({ length: pairs }, () => [code, emphasized]).flat()
|
||||
assert.equal(trimmedWith(alternating(500), (leaves) => trimmingRuns(leaves, 'spanned')).passes, trimmedWith(alternating(3), (leaves) => trimmingRuns(leaves, 'spanned')).passes)
|
||||
})
|
||||
}
|
||||
|
||||
// The trim's passes and line where each pass judges every leaf against `shared`'s counts.
|
||||
function trimmedWith(leaves: readonly AdfNode[], shared: (leaves: readonly AdfNode[]) => readonly number[]): { line: AdfNode[]; passes: number } {
|
||||
for (let current = leaves, passes = 1; ; passes += 1) {
|
||||
const pass = trimPass(current, shared)
|
||||
if (pass.settled) return { line: pass.line, passes }
|
||||
current = pass.line
|
||||
}
|
||||
}
|
||||
|
||||
// The reference counts: every run reaches its last leaf holding its mark, so the trim peels a run's end leaves one pass each.
|
||||
function runsSharedWithNext(leaves: readonly AdfNode[], grouping: RunGrouping): number[] {
|
||||
const shared = leaves.map(() => 0)
|
||||
countSharedRuns(leaves.map((node) => ({ carried: false, node, outer: 0 })), 0, grouping, shared)
|
||||
return shared
|
||||
}
|
||||
|
||||
function countSharedRuns(nodes: readonly RunNode[], firstIndex: number, grouping: RunGrouping, shared: number[]): void {
|
||||
for (const run of inlineRuns(nodes, firstIndex, grouping)) {
|
||||
if (run.kind === 'plain') continue
|
||||
for (let index = run.index; index < run.index + run.nodes.length - 1; index += 1) shared[index] = (shared[index] ?? 0) + 1
|
||||
countSharedRuns(run.nodes, run.index, grouping, shared)
|
||||
}
|
||||
}
|
||||
|
||||
// Judges each pass spanned, else parted, dropping only the first run it finds, with every misflanked highlight, and takes no cap.
|
||||
function firstRunLine(nodes: readonly AdfNode[], container: LineContainer): Result<AdfNode[]> {
|
||||
const leaves = portableLeaves(nodes, container, { depth: 0, path: [], sources: new Map() })
|
||||
|
||||
Reference in New Issue
Block a user