11// tests/extract/tei.test.ts
22import { describe , expect , test } from "bun:test" ;
3- import { XMLParser , XMLValidator } from "fast-xml-parser" ;
43import { buildTei } from "../../scripts/export-tei.ts" ;
54import { extractVolume } from "../../lib/extract/extract.ts" ;
65
6+ function assertWellFormedXml ( xml : string ) : void {
7+ const stack : string [ ] = [ ] ;
8+ const tags = xml . matchAll ( / < [ ^ ! ? ] [ ^ > ] * \/ > | < ( [ ^ ! ? / ] [ ^ > \s / ] * ) (?: \s [ ^ > ] * ) ? > | < \/ ( [ ^ > \s ] + ) > / g) ;
9+
10+ for ( const match of tags ) {
11+ const [ , open , close ] = match ;
12+ if ( open ) {
13+ stack . push ( open ) ;
14+ continue ;
15+ }
16+
17+ if ( close ) {
18+ expect ( stack . pop ( ) ) . toBe ( close ) ;
19+ }
20+ }
21+
22+ expect ( stack ) . toEqual ( [ ] ) ;
23+ }
24+
725describe ( "TEI export" , ( ) => {
826 const xml = buildTei ( [ extractVolume ( process . cwd ( ) , "01" ) . sinograms [ 0 ] ! ] ) ;
927 test ( "well-formed" , ( ) => {
10- expect ( XMLValidator . validate ( xml ) ) . toBe ( true ) ;
28+ assertWellFormedXml ( xml ) ;
1129 } ) ;
1230 test ( "八 readings serialize with usg" , ( ) => {
13- const doc = new XMLParser ( { ignoreAttributes : false } ) . parse ( xml ) ;
1431 expect ( xml ) . toContain ( '<etym type="fanqie">布拔切,黠韻</etym>' ) ;
1532 expect ( xml ) . toContain ( '<usg type="geographic">漳</usg>' ) ;
16- expect ( doc ) . toBeTruthy ( ) ;
1733 } ) ;
1834} ) ;
0 commit comments