diff options
author | Alex Yatskov <alex@foosoft.net> | 2020-06-27 19:04:19 -0700 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2020-06-27 19:04:19 -0700 |
commit | 88af95d20bfdbeb59d44bf0f0d46e772a329f839 (patch) | |
tree | d1dfa7268f274fed32061221c0f030e3647f9ae2 /test | |
parent | 19197a9a5d6a1f54a179d894577dfac513b97401 (diff) | |
parent | 0a6c08d0f53090a4ad48663bc5846ddae5723d52 (diff) |
Merge branch 'master' into testing
Diffstat (limited to 'test')
-rw-r--r-- | test/test-database.js | 76 | ||||
-rw-r--r-- | test/test-document.js | 14 |
2 files changed, 24 insertions, 66 deletions
diff --git a/test/test-database.js b/test/test-database.js index e8a4a343..03b2bd3b 100644 --- a/test/test-database.js +++ b/test/test-database.js @@ -38,60 +38,6 @@ const chrome = { } }; -class XMLHttpRequest { - constructor() { - this._eventCallbacks = new Map(); - this._url = ''; - this._responseText = null; - } - - overrideMimeType() { - // NOP - } - - addEventListener(eventName, callback) { - let callbacks = this._eventCallbacks.get(eventName); - if (typeof callbacks === 'undefined') { - callbacks = []; - this._eventCallbacks.set(eventName, callbacks); - } - callbacks.push(callback); - } - - open(action, url2) { - this._url = url2; - } - - send() { - const filePath = url.fileURLToPath(this._url); - Promise.resolve() - .then(() => { - let source; - try { - source = fs.readFileSync(filePath, {encoding: 'utf8'}); - } catch (e) { - this._trigger('error'); - return; - } - this._responseText = source; - this._trigger('load'); - }); - } - - get responseText() { - return this._responseText; - } - - _trigger(eventName, ...args) { - const callbacks = this._eventCallbacks.get(eventName); - if (typeof callbacks === 'undefined') { return; } - - for (let i = 0, ii = callbacks.length; i < ii; ++i) { - callbacks[i](...args); - } - } -} - class Image { constructor() { this._src = ''; @@ -138,11 +84,21 @@ class Image { } } +async function fetch(url2) { + const filePath = url.fileURLToPath(url2); + await Promise.resolve(); + const content = fs.readFileSync(filePath, {encoding: null}); + return { + text: async () => Promise.resolve(content.toString('utf8')), + json: async () => Promise.resolve(JSON.parse(content.toString('utf8'))) + }; +} + const vm = new VM({ chrome, Image, - XMLHttpRequest, + fetch, indexedDB: global.indexedDB, IDBKeyRange: global.IDBKeyRange, JSZip: yomichanTest.JSZip, @@ -159,6 +115,7 @@ vm.execute([ 'bg/js/media-utility.js', 'bg/js/request.js', 'bg/js/dictionary-importer.js', + 'bg/js/generic-database.js', 'bg/js/database.js' ]); const DictionaryImporter = vm.get('DictionaryImporter'); @@ -286,8 +243,8 @@ async function testDatabase1() { true ); vm.assert.deepStrictEqual(counts, { - counts: [{kanji: 2, kanjiMeta: 2, terms: 33, termMeta: 12, tagMeta: 14}], - total: {kanji: 2, kanjiMeta: 2, terms: 33, termMeta: 12, tagMeta: 14} + counts: [{kanji: 2, kanjiMeta: 2, terms: 33, termMeta: 12, tagMeta: 14, media: 1}], + total: {kanji: 2, kanjiMeta: 2, terms: 33, termMeta: 12, tagMeta: 14, media: 1} }); // Test find* functions @@ -313,7 +270,7 @@ async function testDatabaseEmpty1(database) { const counts = await database.getDictionaryCounts([], true); vm.assert.deepStrictEqual(counts, { counts: [], - total: {kanji: 0, kanjiMeta: 0, terms: 0, termMeta: 0, tagMeta: 0} + total: {kanji: 0, kanjiMeta: 0, terms: 0, termMeta: 0, tagMeta: 0, media: 0} }); } @@ -907,8 +864,7 @@ async function testDatabase2() { const database = new Database(); // Error: not prepared - await assert.rejects(async () => await database.purge()); - await assert.rejects(async () => await database.deleteDictionary(title, {}, () => {})); + await assert.rejects(async () => await database.deleteDictionary(title, {rate: 1000}, () => {})); await assert.rejects(async () => await database.findTermsBulk(['?'], titles, null)); await assert.rejects(async () => await database.findTermsExactBulk(['?'], ['?'], titles)); await assert.rejects(async () => await database.findTermsBySequenceBulk([1], title)); diff --git a/test/test-document.js b/test/test-document.js index 0d9026db..ba7acc49 100644 --- a/test/test-document.js +++ b/test/test-document.js @@ -94,10 +94,12 @@ async function testDocument1() { const vm = new VM({document, window, Range, Node}); vm.execute([ 'mixed/js/dom.js', + 'fg/js/dom-text-scanner.js', 'fg/js/source.js', 'fg/js/document.js' ]); - const [TextSourceRange, TextSourceElement, docRangeFromPoint, docSentenceExtract] = vm.get([ + const [DOMTextScanner, TextSourceRange, TextSourceElement, docRangeFromPoint, docSentenceExtract] = vm.get([ + 'DOMTextScanner', 'TextSourceRange', 'TextSourceElement', 'docRangeFromPoint', @@ -106,7 +108,7 @@ async function testDocument1() { try { await testDocumentTextScanningFunctions(dom, {docRangeFromPoint, docSentenceExtract, TextSourceRange, TextSourceElement}); - await testTextSourceRangeSeekFunctions(dom, {TextSourceRange}); + await testTextSourceRangeSeekFunctions(dom, {DOMTextScanner}); } finally { window.close(); } @@ -179,7 +181,7 @@ async function testDocumentTextScanningFunctions(dom, {docRangeFromPoint, docSen if (source === null) { continue; } // Test docSentenceExtract - const sentenceActual = docSentenceExtract(source, sentenceExtent).text; + const sentenceActual = docSentenceExtract(source, sentenceExtent, false).text; assert.strictEqual(sentenceActual, sentence); // Clean @@ -187,7 +189,7 @@ async function testDocumentTextScanningFunctions(dom, {docRangeFromPoint, docSen } } -async function testTextSourceRangeSeekFunctions(dom, {TextSourceRange}) { +async function testTextSourceRangeSeekFunctions(dom, {DOMTextScanner}) { const document = dom.window.document; for (const testElement of document.querySelectorAll('.test[data-test-type=text-source-range-seek]')) { @@ -220,8 +222,8 @@ async function testTextSourceRangeSeekFunctions(dom, {TextSourceRange}) { const {node, offset, content} = ( seekDirection === 'forward' ? - TextSourceRange.seekForward(seekNode, seekOffset, seekLength) : - TextSourceRange.seekBackward(seekNode, seekOffset, seekLength) + new DOMTextScanner(seekNode, seekOffset, true, false).seek(seekLength) : + new DOMTextScanner(seekNode, seekOffset, true, false).seek(-seekLength) ); assert.strictEqual(node, expectedResultNode); |