2020-08-28 19:02:52 +10:00
|
|
|
import { replaceWord, addPositionToWords, wordAtPosition, splitByWhitespaceBoundary } from '../../../../../src/services/completion/completion.js'
|
2017-03-16 02:22:36 +11:00
|
|
|
|
|
|
|
describe('addPositiontoWords', () => {
|
|
|
|
it('adds the position to a word list', () => {
|
2020-08-28 19:02:52 +10:00
|
|
|
const words = ['hey', ' ', 'this', ' ', 'is', ' ', 'fun']
|
2017-03-16 02:22:36 +11:00
|
|
|
|
|
|
|
const expected = [
|
|
|
|
{
|
|
|
|
word: 'hey',
|
|
|
|
start: 0,
|
|
|
|
end: 3
|
|
|
|
},
|
|
|
|
{
|
2020-08-28 19:02:52 +10:00
|
|
|
word: ' ',
|
2017-03-16 02:22:36 +11:00
|
|
|
start: 3,
|
2020-08-28 19:02:52 +10:00
|
|
|
end: 4
|
2017-03-16 02:22:36 +11:00
|
|
|
},
|
|
|
|
{
|
2020-08-28 19:02:52 +10:00
|
|
|
word: 'this',
|
|
|
|
start: 4,
|
|
|
|
end: 8
|
|
|
|
},
|
|
|
|
{
|
|
|
|
word: ' ',
|
|
|
|
start: 8,
|
2017-03-16 02:22:36 +11:00
|
|
|
end: 9
|
|
|
|
},
|
|
|
|
{
|
2020-08-28 19:02:52 +10:00
|
|
|
word: 'is',
|
2017-03-16 02:22:36 +11:00
|
|
|
start: 9,
|
2020-08-28 19:02:52 +10:00
|
|
|
end: 11
|
|
|
|
},
|
|
|
|
{
|
|
|
|
word: ' ',
|
|
|
|
start: 11,
|
2017-03-16 02:22:36 +11:00
|
|
|
end: 12
|
2020-08-28 19:02:52 +10:00
|
|
|
},
|
|
|
|
{
|
|
|
|
word: 'fun',
|
|
|
|
start: 12,
|
|
|
|
end: 15
|
2017-03-16 02:22:36 +11:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const res = addPositionToWords(words)
|
|
|
|
|
|
|
|
expect(res).to.eql(expected)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-28 19:02:52 +10:00
|
|
|
describe('splitByWhitespaceBoundary', () => {
|
2017-03-16 02:22:36 +11:00
|
|
|
it('splits at whitespace boundaries', () => {
|
2020-08-28 19:02:52 +10:00
|
|
|
const str = 'This is a #nice @test for you, @idiot@idiot.com'
|
|
|
|
const expected = ['This', ' ', 'is', ' ', 'a', ' ', '#nice', ' ', '@test', ' ', 'for', ' ', 'you,', ' ', '@idiot@idiot.com']
|
|
|
|
const res = splitByWhitespaceBoundary(str)
|
2017-03-16 02:22:36 +11:00
|
|
|
|
|
|
|
expect(res).to.eql(expected)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('wordAtPosition', () => {
|
|
|
|
it('returns the word for a given string and postion, plus the start and end position of that word', () => {
|
|
|
|
const str = 'Hey this is fun'
|
|
|
|
|
|
|
|
const { word, start, end } = wordAtPosition(str, 4)
|
|
|
|
|
|
|
|
expect(word).to.eql('this')
|
|
|
|
expect(start).to.eql(4)
|
|
|
|
expect(end).to.eql(8)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('replaceWord', () => {
|
|
|
|
it('replaces a word (with start and end) with another word in a given string', () => {
|
2020-08-28 19:02:52 +10:00
|
|
|
const str = 'hey @take , how are you'
|
|
|
|
const wordsWithPosition = addPositionToWords(splitByWhitespaceBoundary(str))
|
2017-03-16 02:22:36 +11:00
|
|
|
const toReplace = wordsWithPosition[2]
|
|
|
|
|
|
|
|
expect(toReplace.word).to.eql('@take')
|
|
|
|
|
2020-08-28 19:02:52 +10:00
|
|
|
const expected = 'hey @takeshitakenji , how are you'
|
2017-03-16 02:22:36 +11:00
|
|
|
const res = replaceWord(str, toReplace, '@takeshitakenji')
|
|
|
|
expect(res).to.eql(expected)
|
|
|
|
})
|
|
|
|
})
|