mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-19 06:35:44 +01:00
Assert outcomes in the entity, JSON and error-code tests, so none reads source text
This commit is contained in:
@@ -1,21 +1,24 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { dirname, join } from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import test from 'node:test'
|
||||
|
||||
import { readEntityReference } from './entity-references.ts'
|
||||
|
||||
const source = readFileSync(join(dirname(fileURLToPath(import.meta.url)), 'entity-references.ts'), 'utf8')
|
||||
import { namedReferences, readEntityReference } from './entity-references.ts'
|
||||
|
||||
// A value holding a space or a tilde mis-splits into a wrong name and a lost one, silently, at load.
|
||||
test('every packed reference parts into one name and one value the table decodes', () => {
|
||||
const packed = /const packedReferences =\n {2}'([\s\S]*?)'\n/.exec(source)?.[1]
|
||||
assert.ok(packed !== undefined)
|
||||
const references = packed.split(/(?:\\\n)? +/)
|
||||
assert.equal(references.length, 2125)
|
||||
for (const reference of references) {
|
||||
assert.match(reference, /^[A-Za-z][A-Za-z0-9]*~[^ ~]+$/)
|
||||
assert.ok((readEntityReference(`&${reference.slice(0, reference.indexOf('~'))};`, 0)?.text ?? '') !== '')
|
||||
test('every named reference parts into one name and one value', () => {
|
||||
assert.equal(namedReferences.size, 2125)
|
||||
for (const [name, value] of namedReferences) {
|
||||
assert.match(name, /^[A-Za-z][A-Za-z0-9]*$/, name)
|
||||
assert.match(value, /^[^ ~]+$/u, name)
|
||||
}
|
||||
})
|
||||
|
||||
test('decodes a reference to the characters HTML5 gives its name', () => {
|
||||
assert.deepEqual(readEntityReference('⁡', 0), { length: 15, text: '' })
|
||||
assert.deepEqual(readEntityReference('
', 0), { length: 9, text: '\n' })
|
||||
assert.deepEqual(readEntityReference('	', 0), { length: 5, text: '\t' })
|
||||
assert.deepEqual(readEntityReference('  ', 0), { length: 12, text: ' ' })
|
||||
assert.deepEqual(readEntityReference('⊂⃒', 0), { length: 11, text: '⊂⃒' })
|
||||
assert.deepEqual(readEntityReference(' ', 0), { length: 6, text: ' ' })
|
||||
assert.deepEqual(readEntityReference('‌', 0), { length: 6, text: '' })
|
||||
assert.equal(readEntityReference('¬areference;', 0), undefined)
|
||||
})
|
||||
|
||||
@@ -200,7 +200,7 @@ const packedReferences =
|
||||
xsqcup~⨆ xuplus~⨄ xutri~△ xvee~⋁ xwedge~⋀ yacute~ý yacy~я ycirc~ŷ ycy~ы yen~¥ yfr~𝔶 yicy~ї yopf~𝕪 yscr~𝓎 yucy~ю\
|
||||
yuml~ÿ zacute~ź zcaron~ž zcy~з zdot~ż zeetrf~ℨ zeta~ζ zfr~𝔷 zhcy~ж zigrarr~⇝ zopf~𝕫 zscr~𝓏 zwj~ zwnj~'
|
||||
|
||||
const namedReferences = new Map(packedReferences.split(/ +/).map(namedReference))
|
||||
export const namedReferences: ReadonlyMap<string, string> = new Map(packedReferences.split(/ +/).map(namedReference))
|
||||
|
||||
export function holdsEntityReference(text: string): boolean {
|
||||
for (let index = text.indexOf('&'); index !== -1; index = text.indexOf('&', index + 1)) {
|
||||
|
||||
Reference in New Issue
Block a user