Add tests

This commit is contained in:
toasted-nutbread 2020-04-11 14:24:03 -04:00
parent ac603d54a3
commit 5f49f0fed2
4 changed files with 62 additions and 8 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 B

View File

@ -30,5 +30,6 @@
["打ち込む", "ぶちこむ", "tag1 tag2", "v5", 29, ["definition1a (打ち込む, ぶちこむ)", "definition1b (打ち込む, ぶちこむ)"], 6, "tag3 tag4 tag5"], ["打ち込む", "ぶちこむ", "tag1 tag2", "v5", 29, ["definition1a (打ち込む, ぶちこむ)", "definition1b (打ち込む, ぶちこむ)"], 6, "tag3 tag4 tag5"],
["打ち込む", "ぶちこむ", "tag1 tag2", "v5", 30, ["definition2a (打ち込む, ぶちこむ)", "definition2b (打ち込む, ぶちこむ)"], 6, "tag3 tag4 tag5"], ["打ち込む", "ぶちこむ", "tag1 tag2", "v5", 30, ["definition2a (打ち込む, ぶちこむ)", "definition2b (打ち込む, ぶちこむ)"], 6, "tag3 tag4 tag5"],
["打ち込む", "ぶちこむ", "tag1 tag2", "v5", 31, ["definition3a (打ち込む, ぶちこむ)", "definition3b (打ち込む, ぶちこむ)"], 6, "tag3 tag4 tag5"], ["打ち込む", "ぶちこむ", "tag1 tag2", "v5", 31, ["definition3a (打ち込む, ぶちこむ)", "definition3b (打ち込む, ぶちこむ)"], 6, "tag3 tag4 tag5"],
["打ち込む", "ぶちこむ", "tag1 tag2", "v5", 32, ["definition4a (打ち込む, ぶちこむ)", "definition4b (打ち込む, ぶちこむ)"], 6, "tag3 tag4 tag5"] ["打ち込む", "ぶちこむ", "tag1 tag2", "v5", 32, ["definition4a (打ち込む, ぶちこむ)", "definition4b (打ち込む, ぶちこむ)"], 6, "tag3 tag4 tag5"],
["画像", "がぞう", "tag1 tag2", "", 33, ["definition1a (画像, がぞう)", {"type": "image", "path": "image.gif", "width": 350, "height": 350, "description": "An image", "pixelated": true}], 7, "tag3 tag4 tag5"]
] ]

View File

@ -92,9 +92,56 @@ class XMLHttpRequest {
} }
} }
class Image {
constructor() {
this._src = '';
this._loadCallbacks = [];
}
get src() {
return this._src;
}
set src(value) {
this._src = value;
this._delayTriggerLoad();
}
get naturalWidth() {
return 100;
}
get naturalHeight() {
return 100;
}
addEventListener(eventName, callback) {
if (eventName === 'load') {
this._loadCallbacks.push(callback);
}
}
removeEventListener(eventName, callback) {
if (eventName === 'load') {
const index = this._loadCallbacks.indexOf(callback);
if (index >= 0) {
this._loadCallbacks.splice(index, 1);
}
}
}
async _delayTriggerLoad() {
await Promise.resolve();
for (const callback of this._loadCallbacks) {
callback();
}
}
}
const vm = new VM({ const vm = new VM({
chrome, chrome,
Image,
XMLHttpRequest, XMLHttpRequest,
indexedDB: global.indexedDB, indexedDB: global.indexedDB,
IDBKeyRange: global.IDBKeyRange, IDBKeyRange: global.IDBKeyRange,
@ -106,6 +153,7 @@ vm.execute([
'bg/js/json-schema.js', 'bg/js/json-schema.js',
'bg/js/dictionary.js', 'bg/js/dictionary.js',
'mixed/js/core.js', 'mixed/js/core.js',
'bg/js/media-utility.js',
'bg/js/request.js', 'bg/js/request.js',
'bg/js/dictionary-importer.js', 'bg/js/dictionary-importer.js',
'bg/js/database.js' 'bg/js/database.js'
@ -235,8 +283,8 @@ async function testDatabase1() {
true true
); );
vm.assert.deepStrictEqual(counts, { vm.assert.deepStrictEqual(counts, {
counts: [{kanji: 2, kanjiMeta: 2, terms: 32, termMeta: 12, tagMeta: 14}], counts: [{kanji: 2, kanjiMeta: 2, terms: 33, termMeta: 12, tagMeta: 14}],
total: {kanji: 2, kanjiMeta: 2, terms: 32, termMeta: 12, tagMeta: 14} total: {kanji: 2, kanjiMeta: 2, terms: 33, termMeta: 12, tagMeta: 14}
}); });
// Test find* functions // Test find* functions

View File

@ -38,12 +38,17 @@ function createTestDictionaryArchive(dictionary, dictionaryName) {
const archive = new (getJSZip())(); const archive = new (getJSZip())();
for (const fileName of fileNames) { for (const fileName of fileNames) {
if (/\.json$/.test(fileName)) {
const source = fs.readFileSync(path.join(dictionaryDirectory, fileName), {encoding: 'utf8'}); const source = fs.readFileSync(path.join(dictionaryDirectory, fileName), {encoding: 'utf8'});
const json = JSON.parse(source); const json = JSON.parse(source);
if (fileName === 'index.json' && typeof dictionaryName === 'string') { if (fileName === 'index.json' && typeof dictionaryName === 'string') {
json.title = dictionaryName; json.title = dictionaryName;
} }
archive.file(fileName, JSON.stringify(json, null, 0)); archive.file(fileName, JSON.stringify(json, null, 0));
} else {
const source = fs.readFileSync(path.join(dictionaryDirectory, fileName), {encoding: null});
archive.file(fileName, source);
}
} }
return archive; return archive;