-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out and test some serial-handling functions
- Loading branch information
Showing
4 changed files
with
140 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { expect, test } from 'vitest'; | ||
import { parse } from '../modes/parse'; | ||
import { assertRose, findSubtree } from '../tree'; | ||
import { describeSerial, segmentSerial } from './serial'; | ||
|
||
function parseAndSegmentSerial(text: string): string[][] { | ||
const tree = parse(text)[0]; | ||
const serial = findSubtree(tree, '*Serial')!; | ||
assertRose(serial); | ||
const segments = segmentSerial(serial.children); | ||
return segments.map(s => s.map(w => w.source)); | ||
} | ||
|
||
test('it segments serials', () => { | ||
expect(parseAndSegmentSerial('rua')).toEqual([['rua']]); | ||
expect(parseAndSegmentSerial('du rua')).toEqual([['du', 'rua']]); | ||
expect(parseAndSegmentSerial('rua de')).toEqual([['rua'], ['de']]); | ||
expect(parseAndSegmentSerial('rua jaq de')).toEqual([['rua'], ['jaq', 'de']]); | ||
expect(parseAndSegmentSerial('du rua jaq de')).toEqual([ | ||
['du', 'rua'], | ||
['jaq', 'de'], | ||
]); | ||
expect(parseAndSegmentSerial('du sho jaq de')).toEqual([ | ||
['du', 'sho', 'jaq', 'de'], | ||
]); | ||
expect(parseAndSegmentSerial('du jaq kịde')).toEqual([ | ||
['du', 'jaq'], | ||
['kı-', 'de'], | ||
]); | ||
expect(parseAndSegmentSerial('du kịjaq de')).toEqual([ | ||
['du'], | ||
['kı-', 'jaq', 'de'], | ||
]); | ||
}); | ||
|
||
function parseAndDescribeSerial(text: string): string { | ||
const tree = parse(text)[0]; | ||
const serial = findSubtree(tree, '*Serial')!; | ||
assertRose(serial); | ||
const children = serial.children; | ||
const description = describeSerial(children); | ||
if (!description) return 'bizarre'; | ||
return description | ||
.map( | ||
({ verbIndex, slotIndex }) => | ||
`${children[verbIndex].source}${slotIndex + 1}`, | ||
) | ||
.join(' '); | ||
} | ||
|
||
test('it describes serials', () => { | ||
expect(parseAndDescribeSerial('do')).toEqual('do1 do2 do3'); | ||
expect(parseAndDescribeSerial('jaq de')).toEqual('de1'); | ||
expect(parseAndDescribeSerial('dua de')).toEqual('dua1 de1'); | ||
expect(parseAndDescribeSerial('jaq cho')).toEqual('cho1 cho2'); | ||
expect(parseAndDescribeSerial('dua cho')).toEqual('dua1 cho1 cho2'); | ||
expect(parseAndDescribeSerial('rua jaq de')).toEqual('rua1'); | ||
expect(parseAndDescribeSerial('leo baı')).toEqual('leo1 baı2'); | ||
expect(parseAndDescribeSerial('taq cho')).toEqual('taq1'); | ||
expect(parseAndDescribeSerial('chı do')).toEqual('chı1 do1 do2 do3'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters