Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/wikitext. Event-stream based wikitext parser
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319/** * Dedicated tests for text_source.ts. * * Covers the TextSource interface (string conformance, custom * implementations), the slice() helper, and edge cases for Unicode, * astral characters, empty strings, and boundary offsets. */// deno-lint-ignore-file no-import-prefix no-unversioned-importimport { describe, it } from 'jsr:@std/testing/bdd';import { expect } from 'jsr:@std/expect';import * as fc from 'npm:fast-check';
import type { TextSource } from './text_source.ts';import { slice } from './text_source.ts';
// ---------------------------------------------------------------------------// TextSource — string conformance// ---------------------------------------------------------------------------
describe('TextSource — string conformance', () => { it('plain string satisfies TextSource', () => { const src: TextSource = 'hello'; expect(src.length).toBe(5); expect(src.charCodeAt(0)).toBe(0x68); // 'h' expect(src.slice(0, 5)).toBe('hello'); });
it('empty string satisfies TextSource', () => { const src: TextSource = ''; expect(src.length).toBe(0); expect(src.slice(0, 0)).toBe(''); });
it('single character string', () => { const src: TextSource = 'x'; expect(src.length).toBe(1); expect(src.charCodeAt(0)).toBe(0x78); expect(src.slice(0, 1)).toBe('x'); });
it('reports NaN for out-of-range charCodeAt index', () => { const src: TextSource = 'abc'; expect(src.charCodeAt(-1)).toBeNaN(); expect(src.charCodeAt(3)).toBeNaN(); expect(src.charCodeAt(100)).toBeNaN(); });});
// ---------------------------------------------------------------------------// TextSource — Unicode and astral characters// ---------------------------------------------------------------------------
describe('TextSource — Unicode', () => { it('handles CJK characters', () => { const src: TextSource = '日本語'; expect(src.length).toBe(3); expect(src.charCodeAt(0)).toBe(0x65E5); // 日 expect(src.slice(0, 1)).toBe('日'); expect(src.slice(1, 3)).toBe('本語'); });
it('handles RTL text (Arabic)', () => { const src: TextSource = 'مرحبا'; expect(src.length).toBe(5); expect(src.slice(0, 5)).toBe('مرحبا'); });
it('handles astral Unicode (emoji) as surrogate pairs', () => { // 🌍 is U+1F30D, encoded as two UTF-16 code units. const src: TextSource = '🌍'; expect(src.length).toBe(2); // First surrogate expect(src.charCodeAt(0)).toBe(0xD83C); // Second surrogate expect(src.charCodeAt(1)).toBe(0xDF0D); expect(src.slice(0, 2)).toBe('🌍'); });
it('handles mixed ASCII and astral characters', () => { const src: TextSource = 'Hi 🌍!'; // 'H' 'i' ' ' 0xD83C 0xDF0D '!' expect(src.length).toBe(6); expect(src.slice(0, 2)).toBe('Hi'); expect(src.slice(3, 5)).toBe('🌍'); expect(src.slice(5, 6)).toBe('!'); });
it('handles null byte', () => { const src: TextSource = 'a\0b'; expect(src.length).toBe(3); expect(src.charCodeAt(1)).toBe(0); expect(src.slice(0, 3)).toBe('a\0b'); });});
// ---------------------------------------------------------------------------// TextSource — line endings// ---------------------------------------------------------------------------
describe('TextSource — line endings', () => { it('handles LF', () => { const src: TextSource = 'a\nb'; expect(src.length).toBe(3); expect(src.charCodeAt(1)).toBe(0x0A); expect(src.slice(0, 1)).toBe('a'); expect(src.slice(2, 3)).toBe('b'); });
it('handles CRLF', () => { const src: TextSource = 'a\r\nb'; expect(src.length).toBe(4); expect(src.charCodeAt(1)).toBe(0x0D); expect(src.charCodeAt(2)).toBe(0x0A); });
it('handles bare CR', () => { const src: TextSource = 'a\rb'; expect(src.length).toBe(3); expect(src.charCodeAt(1)).toBe(0x0D); });});
// ---------------------------------------------------------------------------// slice() helper// ---------------------------------------------------------------------------
describe('slice()', () => { it('resolves a mid-string range', () => { expect(slice('== Heading ==', 3, 10)).toBe('Heading'); });
it('resolves full string', () => { expect(slice('hello', 0, 5)).toBe('hello'); });
it('resolves zero-length range', () => { expect(slice('hello', 2, 2)).toBe(''); });
it('resolves at start', () => { expect(slice('abcdef', 0, 3)).toBe('abc'); });
it('resolves at end', () => { expect(slice('abcdef', 3, 6)).toBe('def'); });
it('resolves single character', () => { expect(slice('abcdef', 2, 3)).toBe('c'); });
it('resolves from empty string', () => { expect(slice('', 0, 0)).toBe(''); });
it('works with CJK ranges', () => { expect(slice('日本語テスト', 2, 5)).toBe('語テス'); });
it('works with astral character boundaries', () => { // '🌍' takes offsets 0 and 1 (surrogate pair). expect(slice('🌍abc', 0, 2)).toBe('🌍'); expect(slice('🌍abc', 2, 5)).toBe('abc'); });});
// ---------------------------------------------------------------------------// TextSource — custom implementation// ---------------------------------------------------------------------------
describe('TextSource — custom implementation', () => { /** A minimal TextSource backed by an array of code points. */ class ArraySource implements TextSource { readonly length: number; private readonly _str: string;
constructor(str: string) { this._str = str; this.length = str.length; }
// These methods intentionally delegate straight to the backing string so // the tests stay focused on interface conformance, not on inventing a new // text model with subtly different semantics. charCodeAt(index: number): number { return this._str.charCodeAt(index); }
slice(start: number, end: number): string { return this._str.slice(start, end); } }
it('custom TextSource works with slice() helper', () => { const src = new ArraySource('abcdef'); expect(slice(src, 1, 4)).toBe('bcd'); });
it('custom TextSource has correct length', () => { const src = new ArraySource('test'); expect(src.length).toBe(4); });
it('custom TextSource charCodeAt matches string behavior', () => { const str = 'hello'; const src = new ArraySource(str); for (let i = 0; i < str.length; i++) { expect(src.charCodeAt(i)).toBe(str.charCodeAt(i)); } });});
// ---------------------------------------------------------------------------// TextSource with iterSlices (optional method)// ---------------------------------------------------------------------------
describe('TextSource — iterSlices', () => { /** A TextSource that exposes iterSlices for chunked access. */ class ChunkedSource implements TextSource { readonly length: number; private readonly _chunks: string[]; private readonly _str: string;
constructor(chunks: string[]) { this._chunks = chunks; this._str = chunks.join(''); this.length = this._str.length; }
charCodeAt(index: number): number { return this._str.charCodeAt(index); }
slice(start: number, end: number): string { return this._str.slice(start, end); }
*iterSlices(start: number, end: number): Iterable<string> { // Simple implementation: just yield the slice. yield this._str.slice(start, end); } }
it('iterSlices produces the same content as slice', () => { const src = new ChunkedSource(['Hello', ' ', 'World']); const sliced = src.slice(0, 11); const iterated = [...src.iterSlices!(0, 11)].join(''); expect(iterated).toBe(sliced); });
it('iterSlices works for sub-ranges', () => { const src = new ChunkedSource(['abc', 'def']); const iterated = [...src.iterSlices!(2, 5)].join(''); expect(iterated).toBe('cde'); });});
// ---------------------------------------------------------------------------// Property-based tests// ---------------------------------------------------------------------------
describe('TextSource — property-based', () => { it('slice(str, 0, str.length) === str for any string', () => { fc.assert( fc.property(fc.string(), (s) => { expect(slice(s, 0, s.length)).toBe(s); }), { numRuns: 500 }, ); });
it('slice(str, i, i) is always empty for valid i', () => { fc.assert( fc.property(fc.string(), (s) => { const i = Math.min(Math.floor(s.length / 2), s.length); expect(slice(s, i, i)).toBe(''); }), { numRuns: 300 }, ); });
it('slice(str, a, b) matches String.prototype.slice(a, b)', () => { fc.assert( fc.property( fc.string({ minLength: 0, maxLength: 200 }), (s) => { if (s.length < 2) return; const a = Math.floor(s.length / 3); const b = Math.floor((2 * s.length) / 3); expect(slice(s, a, b)).toBe(s.slice(a, b)); }, ), { numRuns: 300 }, ); });
it('string length equals TextSource.length', () => { fc.assert( fc.property(fc.string(), (s) => { const src: TextSource = s; expect(src.length).toBe(s.length); }), { numRuns: 300 }, ); });
it('charCodeAt matches for all valid indices', () => { fc.assert( fc.property(fc.string({ minLength: 1, maxLength: 50 }), (s) => { const src: TextSource = s; for (let i = 0; i < s.length; i++) { expect(src.charCodeAt(i)).toBe(s.charCodeAt(i)); } }), { numRuns: 200 }, ); });});