You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
2.2 KiB
45 lines
2.2 KiB
import { describe, expect, test } from '@jest/globals'
|
|
import { renderText, TextSource } from './renderableText'
|
|
import { forbiddenRNG } from './random'
|
|
|
|
const source: TextSource = {
|
|
empty: '',
|
|
emptyList: [],
|
|
example: 'basic string value',
|
|
singletonList: ['singleton value'],
|
|
singleWeightedItem: [{ wt: 5, v: 'weighted item' }],
|
|
nestedItem: [[[['layered value']]]],
|
|
nestedWeightedItem: [[{ wt: 5, v: [[['innermost text']]] }]],
|
|
}
|
|
|
|
describe('renderText', () => {
|
|
test.each<[string, string, string]>([
|
|
['returns blank string for blank string', '', ''],
|
|
['returns constant string for no-substitution string', 'candy', 'candy'],
|
|
['does not replace empty brackets', '{{}}', '{{}}'],
|
|
['does not replace escaped brackets', '\\{{empty}}', '{{empty}}'],
|
|
['does not replace half started brackets', '{{ empty', '{{ empty'],
|
|
['does not replace brackets without only word characters inside', '{{ ??? }}', '{{ ??? }}'],
|
|
['escapes backslash and replaces brackets after', '\\\\{{empty}}', '\\'],
|
|
['replaces empty reference with empty value', '{{empty}}', ''],
|
|
['replaces empty list reference with empty value', '{{emptyList}}', ''],
|
|
['replaces string reference with value', '{{example}}', 'basic string value'],
|
|
['replaces singleton list reference with value', '{{singletonList}}', 'singleton value'],
|
|
['replaces singleton list reference with weighted value', '{{singleWeightedItem}}', 'weighted item'],
|
|
['replaces multilevel singleton list reference with inner value', '{{nestedItem}}', 'layered value'],
|
|
[
|
|
'replaces multilevel singleton list reference through weighted item with inner value',
|
|
'{{nestedWeightedItem}}',
|
|
'innermost text',
|
|
],
|
|
])(' %s (%j -> %j)', (caseName, input, result) => {
|
|
expect(renderText(input, source, forbiddenRNG)).toEqual(result)
|
|
})
|
|
|
|
test.each<[string, string]>([
|
|
['forbids \\ not followed by { or \\', '\\c'],
|
|
['forbids reference to nonexistent value', '{{ nonexistentKey }}'],
|
|
])(' %s (%j -> throws)', (caseName, input) => {
|
|
expect(() => renderText(input, source, forbiddenRNG)).toThrow()
|
|
})
|
|
})
|
|
|