|
@@ -5,7 +5,6 @@ import {
|
|
|
} from '@/features/source-editor/extensions/spelling/spellchecker'
|
|
} from '@/features/source-editor/extensions/spelling/spellchecker'
|
|
|
import { expect } from 'chai'
|
|
import { expect } from 'chai'
|
|
|
import { EditorView } from '@codemirror/view'
|
|
import { EditorView } from '@codemirror/view'
|
|
|
-import { IgnoredWords } from '@/features/dictionary/ignored-words'
|
|
|
|
|
import { LaTeXLanguage } from '@/features/source-editor/languages/latex/latex-language'
|
|
import { LaTeXLanguage } from '@/features/source-editor/languages/latex/latex-language'
|
|
|
import { LanguageSupport } from '@codemirror/language'
|
|
import { LanguageSupport } from '@codemirror/language'
|
|
|
|
|
|
|
@@ -13,11 +12,10 @@ const extensions = [new LanguageSupport(LaTeXLanguage)]
|
|
|
|
|
|
|
|
describe('SpellChecker', function () {
|
|
describe('SpellChecker', function () {
|
|
|
describe('getWordsFromLine', function () {
|
|
describe('getWordsFromLine', function () {
|
|
|
- let lang: string, ignoredWords: IgnoredWords
|
|
|
|
|
|
|
+ let lang: string
|
|
|
beforeEach(function () {
|
|
beforeEach(function () {
|
|
|
/* Note: ignore the word 'test' */
|
|
/* Note: ignore the word 'test' */
|
|
|
lang = 'en'
|
|
lang = 'en'
|
|
|
- ignoredWords = new Set([]) as unknown as IgnoredWords
|
|
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('should get words from a line', function () {
|
|
it('should get words from a line', function () {
|
|
@@ -26,7 +24,7 @@ describe('SpellChecker', function () {
|
|
|
extensions,
|
|
extensions,
|
|
|
})
|
|
})
|
|
|
const line = view.state.doc.line(1)
|
|
const line = view.state.doc.line(1)
|
|
|
- const words = Array.from(getWordsFromLine(view, line, ignoredWords, lang))
|
|
|
|
|
|
|
+ const words = Array.from(getWordsFromLine(view, line, lang))
|
|
|
expect(words).to.deep.equal([
|
|
expect(words).to.deep.equal([
|
|
|
{ text: 'Hello', from: 0, to: 5, lineNumber: 1, lang: 'en' },
|
|
{ text: 'Hello', from: 0, to: 5, lineNumber: 1, lang: 'en' },
|
|
|
{ text: 'test', from: 6, to: 10, lineNumber: 1, lang: 'en' },
|
|
{ text: 'test', from: 6, to: 10, lineNumber: 1, lang: 'en' },
|
|
@@ -35,28 +33,13 @@ describe('SpellChecker', function () {
|
|
|
])
|
|
])
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- it('should ignore words in ignoredWords', function () {
|
|
|
|
|
- ignoredWords = new Set(['test']) as unknown as IgnoredWords
|
|
|
|
|
- const view = new EditorView({
|
|
|
|
|
- doc: 'Hello test one two',
|
|
|
|
|
- extensions,
|
|
|
|
|
- })
|
|
|
|
|
- const line = view.state.doc.line(1)
|
|
|
|
|
- const words = Array.from(getWordsFromLine(view, line, ignoredWords, lang))
|
|
|
|
|
- expect(words).to.deep.equal([
|
|
|
|
|
- { text: 'Hello', from: 0, to: 5, lineNumber: 1, lang: 'en' },
|
|
|
|
|
- { text: 'one', from: 11, to: 14, lineNumber: 1, lang: 'en' },
|
|
|
|
|
- { text: 'two', from: 15, to: 18, lineNumber: 1, lang: 'en' },
|
|
|
|
|
- ])
|
|
|
|
|
- })
|
|
|
|
|
-
|
|
|
|
|
it('should get no words from an empty line', function () {
|
|
it('should get no words from an empty line', function () {
|
|
|
const view = new EditorView({
|
|
const view = new EditorView({
|
|
|
doc: ' ',
|
|
doc: ' ',
|
|
|
extensions,
|
|
extensions,
|
|
|
})
|
|
})
|
|
|
const line = view.state.doc.line(1)
|
|
const line = view.state.doc.line(1)
|
|
|
- const words = Array.from(getWordsFromLine(view, line, ignoredWords, lang))
|
|
|
|
|
|
|
+ const words = Array.from(getWordsFromLine(view, line, lang))
|
|
|
expect(words).to.deep.equal([])
|
|
expect(words).to.deep.equal([])
|
|
|
})
|
|
})
|
|
|
|
|
|
|
@@ -66,7 +49,7 @@ describe('SpellChecker', function () {
|
|
|
extensions,
|
|
extensions,
|
|
|
})
|
|
})
|
|
|
const line = view.state.doc.line(1)
|
|
const line = view.state.doc.line(1)
|
|
|
- const words = Array.from(getWordsFromLine(view, line, ignoredWords, lang))
|
|
|
|
|
|
|
+ const words = Array.from(getWordsFromLine(view, line, lang))
|
|
|
expect(words).to.deep.equal([
|
|
expect(words).to.deep.equal([
|
|
|
{ text: 'seven', from: 24, to: 29, lineNumber: 1, lang: 'en' },
|
|
{ text: 'seven', from: 24, to: 29, lineNumber: 1, lang: 'en' },
|
|
|
{ text: 'eight', from: 30, to: 35, lineNumber: 1, lang: 'en' },
|
|
{ text: 'eight', from: 30, to: 35, lineNumber: 1, lang: 'en' },
|
|
@@ -79,7 +62,7 @@ describe('SpellChecker', function () {
|
|
|
extensions,
|
|
extensions,
|
|
|
})
|
|
})
|
|
|
const line = view.state.doc.line(1)
|
|
const line = view.state.doc.line(1)
|
|
|
- const words = Array.from(getWordsFromLine(view, line, ignoredWords, lang))
|
|
|
|
|
|
|
+ const words = Array.from(getWordsFromLine(view, line, lang))
|
|
|
expect(words).to.deep.equal([
|
|
expect(words).to.deep.equal([
|
|
|
{ text: 'nine', from: 5, to: 9, lineNumber: 1, lang: 'en' },
|
|
{ text: 'nine', from: 5, to: 9, lineNumber: 1, lang: 'en' },
|
|
|
{ text: 'ten', from: 15, to: 18, lineNumber: 1, lang: 'en' },
|
|
{ text: 'ten', from: 15, to: 18, lineNumber: 1, lang: 'en' },
|
|
@@ -91,7 +74,7 @@ describe('SpellChecker', function () {
|
|
|
it('should build an empty result', function () {
|
|
it('should build an empty result', function () {
|
|
|
const knownMisspelledWords: Word[] = []
|
|
const knownMisspelledWords: Word[] = []
|
|
|
const unknownWords: Word[] = []
|
|
const unknownWords: Word[] = []
|
|
|
- const misspellings: { index: number; suggestions: string[] }[] = []
|
|
|
|
|
|
|
+ const misspellings: { index: number }[] = []
|
|
|
const result = buildSpellCheckResult(
|
|
const result = buildSpellCheckResult(
|
|
|
knownMisspelledWords,
|
|
knownMisspelledWords,
|
|
|
unknownWords,
|
|
unknownWords,
|
|
@@ -103,21 +86,17 @@ describe('SpellChecker', function () {
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
it('should build a realistic result', function () {
|
|
it('should build a realistic result', function () {
|
|
|
- const _makeWord = (text: string, suggestions?: string[]) => {
|
|
|
|
|
- const word = new Word({
|
|
|
|
|
|
|
+ const _makeWord = (text: string) => {
|
|
|
|
|
+ return new Word({
|
|
|
text,
|
|
text,
|
|
|
from: 0,
|
|
from: 0,
|
|
|
to: 0,
|
|
to: 0,
|
|
|
lineNumber: 0,
|
|
lineNumber: 0,
|
|
|
lang: 'xx',
|
|
lang: 'xx',
|
|
|
})
|
|
})
|
|
|
- if (suggestions != null) {
|
|
|
|
|
- word.suggestions = suggestions
|
|
|
|
|
- }
|
|
|
|
|
- return word
|
|
|
|
|
}
|
|
}
|
|
|
// We know this word is misspelled
|
|
// We know this word is misspelled
|
|
|
- const knownMisspelledWords = [_makeWord('fff', ['food', 'fleece'])]
|
|
|
|
|
|
|
+ const knownMisspelledWords = [_makeWord('fff')]
|
|
|
// These words we didn't know
|
|
// These words we didn't know
|
|
|
const unknownWords = [
|
|
const unknownWords = [
|
|
|
_makeWord('aaa'),
|
|
_makeWord('aaa'),
|
|
@@ -126,10 +105,7 @@ describe('SpellChecker', function () {
|
|
|
_makeWord('ddd'),
|
|
_makeWord('ddd'),
|
|
|
]
|
|
]
|
|
|
// These are the suggestions we got back from the backend
|
|
// These are the suggestions we got back from the backend
|
|
|
- const misspellings = [
|
|
|
|
|
- { index: 1, suggestions: ['box', 'bass'] },
|
|
|
|
|
- { index: 3, suggestions: ['docs', 'dance'] },
|
|
|
|
|
- ]
|
|
|
|
|
|
|
+ const misspellings = [{ index: 1 }, { index: 3 }]
|
|
|
// Build the result structure
|
|
// Build the result structure
|
|
|
const result = buildSpellCheckResult(
|
|
const result = buildSpellCheckResult(
|
|
|
knownMisspelledWords,
|
|
knownMisspelledWords,
|
|
@@ -140,21 +116,19 @@ describe('SpellChecker', function () {
|
|
|
// Check cache additions
|
|
// Check cache additions
|
|
|
expect(result.cacheAdditions.map(([k, v]) => [k.text, v])).to.deep.equal([
|
|
expect(result.cacheAdditions.map(([k, v]) => [k.text, v])).to.deep.equal([
|
|
|
// Put these in cache as known misspellings
|
|
// Put these in cache as known misspellings
|
|
|
- ['bbb', ['box', 'bass']],
|
|
|
|
|
- ['ddd', ['docs', 'dance']],
|
|
|
|
|
|
|
+ ['bbb', false],
|
|
|
|
|
+ ['ddd', false],
|
|
|
// Put these in cache as known-correct
|
|
// Put these in cache as known-correct
|
|
|
['aaa', true],
|
|
['aaa', true],
|
|
|
['ccc', true],
|
|
['ccc', true],
|
|
|
])
|
|
])
|
|
|
// Check misspellings
|
|
// Check misspellings
|
|
|
- expect(
|
|
|
|
|
- result.misspelledWords.map(w => [w.text, w.suggestions])
|
|
|
|
|
- ).to.deep.equal([
|
|
|
|
|
|
|
+ expect(result.misspelledWords.map(w => w.text)).to.deep.equal([
|
|
|
// Words in the payload that we now know were misspelled
|
|
// Words in the payload that we now know were misspelled
|
|
|
- ['bbb', ['box', 'bass']],
|
|
|
|
|
- ['ddd', ['docs', 'dance']],
|
|
|
|
|
|
|
+ 'bbb',
|
|
|
|
|
+ 'ddd',
|
|
|
// Word we already knew was misspelled, preserved here
|
|
// Word we already knew was misspelled, preserved here
|
|
|
- ['fff', ['food', 'fleece']],
|
|
|
|
|
|
|
+ 'fff',
|
|
|
])
|
|
])
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|