Create yomichan-test script to reduce repeated code

This commit is contained in:
toasted-nutbread 2020-02-17 22:31:28 -05:00
parent 7cbebf6897
commit 06480751a9
4 changed files with 40 additions and 16 deletions

View File

@ -1,13 +1,9 @@
const fs = require('fs');
const path = require('path');
const yomichanTest = require('./yomichan-test');
process.noDeprecation = true; // Suppress a warning about JSZip
const JSZip = require(path.join(__dirname, '../ext/mixed/lib/jszip.min.js'));
process.noDeprecation = false;
const jsonSchemaFileName = path.join(__dirname, '../ext/bg/js/json-schema.js');
const jsonSchemaFileSource = fs.readFileSync(jsonSchemaFileName, {encoding: 'utf8'});
const JsonSchema = Function(`'use strict';${jsonSchemaFileSource};return JsonSchema;`)();
const JSZip = yomichanTest.JSZip;
const {JsonSchema} = yomichanTest.requireScript('ext/bg/js/json-schema.js', ['JsonSchema']);
function readSchema(relativeFileName) {

View File

@ -1,9 +1,7 @@
const fs = require('fs');
const path = require('path');
const yomichanTest = require('./yomichan-test');
const jsonSchemaFileName = path.join(__dirname, '../ext/bg/js/json-schema.js');
const jsonSchemaFileSource = fs.readFileSync(jsonSchemaFileName, {encoding: 'utf8'});
const JsonSchema = Function(`'use strict';${jsonSchemaFileSource};return JsonSchema;`)();
const {JsonSchema} = yomichanTest.requireScript('ext/bg/js/json-schema.js', ['JsonSchema']);
function main() {

View File

@ -1,10 +1,7 @@
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const yomichanTest = require('./yomichan-test');
const jsonSchemaFileName = path.join(__dirname, '../ext/bg/js/json-schema.js');
const jsonSchemaFileSource = fs.readFileSync(jsonSchemaFileName, {encoding: 'utf8'});
const JsonSchema = Function(`'use strict';${jsonSchemaFileSource};return JsonSchema;`)();
const {JsonSchema} = yomichanTest.requireScript('ext/bg/js/json-schema.js', ['JsonSchema']);
function testValidate1() {

33
test/yomichan-test.js Normal file
View File

@ -0,0 +1,33 @@
const fs = require('fs');
const path = require('path');
let JSZip = null;
function requireScript(fileName, exportNames, variables) {
const absoluteFileName = path.join(__dirname, '..', fileName);
const source = fs.readFileSync(absoluteFileName, {encoding: 'utf8'});
const exportNamesString = Array.isArray(exportNames) ? exportNames.join(',') : '';
const variablesArgumentName = '__variables__';
let variableString = '';
if (typeof variables === 'object' && variables !== null) {
variableString = Object.keys(variables).join(',');
variableString = `const {${variableString}} = ${variablesArgumentName};`;
}
return Function(variablesArgumentName, `'use strict';${variableString}${source}\n;return {${exportNamesString}};`)(variables);
}
function getJSZip() {
if (JSZip === null) {
process.noDeprecation = true; // Suppress a warning about JSZip
JSZip = require(path.join(__dirname, '../ext/mixed/lib/jszip.min.js'));
process.noDeprecation = false;
}
return JSZip;
}
module.exports = {
requireScript,
get JSZip() { return getJSZip(); }
};