rsvp-reader/spec/parseText.spec.js
2019-10-18 13:51:50 +09:00

48 lines
1.5 KiB
JavaScript

import { parseText, _privates } from '../src/textProcessing/parseText.js'
const { splitLongWord } = _privates
describe('parseText', function() {
it('returns an object with expected properties', function() {
let parsed = parseText('Hello World. Test Sentence.')
expect(parsed.segments).toEqual(['Hello', 'World.', 'Test', 'Sentence.'])
expect(parsed.words).toEqual([0, 1, 2, 3])
expect(parsed.sentences).toEqual([0, 2])
})
})
describe('splitLongWord', function() {
it('returns an array', function() {
expect(Array.isArray(splitLongWord('asdf'))).toBeTruthy()
})
it('returns the single word by default', function() {
expect(splitLongWord('asdf')).toEqual(['asdf'])
})
it('returns small words unmodified', function() {
expect(splitLongWord('asdf', 5)).toEqual(['asdf'])
expect(splitLongWord('asdf', 4)).toEqual(['asdf'])
})
it('splits long words', function() {
expect(splitLongWord('asdf', 3)).toEqual(['as', 'df'])
})
it('split into even parts', function() {
expect(splitLongWord('1234567890', 9)).toEqual(['12345', '67890'])
})
it('corner case: uneven length', function() {
expect(splitLongWord('123456789', 8)).toEqual(['1234', '56789'])
})
it('corner case: multiple uneven parts', function() {
let word = '1234567890123'
let segments = splitLongWord(word, 3)
expect(segments.reduce((x, y) => x + y, '')).toBe(word)
expect(Math.max(...segments.map(seg => seg.length))).toBe(3)
expect(Math.min(...segments.map(seg => seg.length)))
})
})