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.
51 lines
2.4 KiB
51 lines
2.4 KiB
import { describe, expect, test } from '@jest/globals'
|
|
import { fulfillText, TextSource } from './weightedText'
|
|
import { forbidRandomness } from '../types/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('fulfillText', () => {
|
|
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 (%p -> %p)', (caseName, input, result) => {
|
|
expect(fulfillText(input, source, forbidRandomness)).toEqual(result)
|
|
})
|
|
|
|
test.each<[string, string]>([
|
|
[' forbids \\ not followed by { or \\', '\\c'],
|
|
[' forbids reference to nonexistent value', '{{ nonexistentKey }}'],
|
|
])('%s (%p -> throws)', (caseName, input) => {
|
|
expect(() => fulfillText(input, source, forbidRandomness)).toThrow()
|
|
})
|
|
})
|
|
|
|
describe('selectAndFulfillWeightedText', () => {
|
|
test(' needs tests', () => {
|
|
expect('tests').toBe('written')
|
|
})
|
|
})
|
|
|