Skip to content

Commit

Permalink
extract runner_support.createIterableHelper to test-utils.testHelpers
Browse files Browse the repository at this point in the history
As the createIterableHelper export needs to be accessible on old
environments that do not support newer EcmaScript syntax

fix runner_support used combination of console.log and print

Where 'print' is something native to Rhino and console.log (not being
part of the EcmaScript spec) not available on all environments either

Now the `logCommand` to use can be passed into runner_support.runTests
via the options parameter
  • Loading branch information
p-bakker authored and ljharb committed Oct 11, 2023
1 parent a40a7a2 commit 0f26d4f
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 81 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ es*/compilers/*
.DS_Store
.ruby-version
_site/
.idea/

# Only apps should have lockfiles
npm-shrinkwrap.json
package-lock.json
yarn.lock
yarn.lock

// temp file created by runner_support
test.js
4 changes: 2 additions & 2 deletions node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var fs = require('fs');
var chalk = require('chalk');
var path = require('path');
var cheerio = require('cheerio');
var runner_support = require('./runner_support');
var createIterableHelper = require('./test-utils/testHelpers').createIterableHelper;

var page = fs.readFileSync(path.join(__dirname, String(process.argv[2] || 'es6').toLowerCase(), 'index.html')).toString().replace(/data-source="[^"]*"/g,'');
var $ = cheerio.load(page);
Expand All @@ -26,7 +26,7 @@ $('#body tbody tr').each(function (index) {
global.asyncPassed = function asyncPassed() {
results[index] = true;
};
eval(runner_support.createIterableHelper);
eval(createIterableHelper);

results[index] = null;

Expand Down
4 changes: 3 additions & 1 deletion rhino.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ function rhinoRunner(testFilename) {
}
}

runner_support.runTests(rhinoRunner, rhinoKey, 'Rhino');
runner_support.runTests(rhinoRunner, rhinoKey, 'Rhino', {
logCommand: 'print'
});
150 changes: 74 additions & 76 deletions runner_support.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
/* jshint esversion: 11 */
var fs = require('fs');

exports.createIterableHelper =
'function __createIterableObject(arr, methods) {\n' +
' methods = methods || {};\n' +
' if (typeof Symbol !== "function" || !Symbol.iterator)\n' +
' return {};\n' +
' arr.length++;\n' +
' var iterator = {\n' +
' next: function() {\n' +
' return { value: arr.shift(), done: arr.length <= 0 };\n' +
' },\n' +
' "return": methods["return"],\n' +
' "throw": methods["throw"]\n' +
' };\n' +
' var iterable = {};\n' +
' iterable[Symbol.iterator] = function(){ return iterator; };\n' +
' return iterable;\n' +
'}\n' +
'if (typeof global !== "undefined") {\n' +
' global.__createIterableObject = __createIterableObject;\n' +
'}\n';
var createIterableHelper = require('./test-utils/testHelpers').createIterableHelper;

function getKeys(key, family, environments) {
var res = [];
for (var k in environments) {
var env = environments[k];
if (env.family !== family) {
continue;
}
res.push(k);
if (k === key) {
// Include versions up to 'key' but not newer.
break;
}
}
return res;
}

function removeIndent(str) {
var indentation = /^[\t ]+/m.exec(str);
return str.replace(new RegExp('^' + indentation[0], 'gm'), '');
}

function testCode(exec) {
if (typeof exec === 'function') {
var src = exec.toString();
var functionBody = /^function\s*\w*\s*\(.*?\)\s*\{\s*\/\*([\s\S]*?)\*\/\s*\}$/m.exec(src);
if (functionBody) {
return '(function () { ' + removeIndent(functionBody[1]) + ' })()';
} else {
return '(' + src + ')()';
}
} else if (Array.isArray(exec)) {
return exec.map(function (e) { return testCode(e.script); }).join("; ");
} else {
return undefined;
}
}

exports.runTests = function runTests(runner, key, family, options) {
options = options === undefined ? {} : options;
Expand All @@ -37,6 +55,7 @@ exports.runTests = function runTests(runner, key, family, options) {
}
var testName = options.testName;
var bail = options.bail;
var logCommand = options.logCommand || 'console.log';

var testCount = 0;
var testSuccess = 0;
Expand All @@ -48,7 +67,7 @@ exports.runTests = function runTests(runner, key, family, options) {

var asyncTestHelperHead =
'function asyncTestPassed() {\n' +
' print("[SUCCESS]");\n' +
' ' + logCommand + '("[SUCCESS]");\n' +
'}\n' +
'\n' +
'var jobQueue = [];\n' +
Expand All @@ -63,7 +82,7 @@ exports.runTests = function runTests(runner, key, family, options) {
' });\n' +
'}\n';
var asyncTestHelperTail =
'function flushQueue() {\n' +
'function flushQueue() {\n' +
' var curTime = Date.now();\n' +
' var empty = true;\n' +
' for (var runTime in jobQueue) {\n' +
Expand All @@ -84,44 +103,9 @@ exports.runTests = function runTests(runner, key, family, options) {
'Promise.resolve().then(flushQueue);\n';

// List of keys for inheriting results from previous versions.
var keyList = (function () {
var res = [];
for (var k in environments) {
var env = environments[k];
if (env.family !== family) {
continue;
}
res.push(k);
if (k === key) {
// Include versions up to 'key' but not newer.
break;
}
}
return res;
})();
var keyList = getKeys(key, family, environments);
console.log(family + ' key list for inheriting results is:', keyList);

function testCode(exec) {
if (typeof exec === 'function') {
var src = exec.toString();
var functionBody = /^function\s*\w*\s*\(.*?\)\s*\{\s*\/\*([\s\S]*?)\*\/\s*\}$/m.exec(src);
if (functionBody) {
return '(function () { ' + removeIndent(functionBody[1]) + ' })()';
} else {
return '(' + src + ')()';
}
} else if (Array.isArray(exec)) {
return exec.map(function (e) { return testCode(e.script); }).join("; ");
} else {
return undefined;
}
}

function removeIndent(str) {
var indentation = /^[\t ]+/m.exec(str);
return str.replace(new RegExp('^' + indentation[0], 'gm'), '');
}

// Run test / subtests, recursively. Report results, indicate data files
// which are out of date.
function runTest(parents, test) {
Expand Down Expand Up @@ -157,27 +141,41 @@ exports.runTests = function runTests(runner, key, family, options) {
'}\n';
}
if (/\b__createIterableObject\b/.test(evalcode)) {
script += exports.createIterableHelper;
script += createIterableHelper;
}

script += `var testCode = ${JSON.stringify(evalcode)};`;

if (/\basyncTestPassed\b/.test(evalcode)) {
script += asyncTestHelperHead + '\n' + evalcode + '\n\n' + asyncTestHelperTail;
script += `
try {
${asyncTestHelperHead}
eval(testCode);
${asyncTestHelperTail}
} catch (e) {};`;
} else if (/\bglobal\.test\b/.test(evalcode)) {
script += 'global.test = function (expression) {\n' +
' if (expression) {\n' +
' console.log("[SUCCESS]");\n' +
' }\n' +
'}\n' +
evalcode;
script += `
global.test = function (expression) {
if (expression) {
${logCommand}("[SUCCESS]");
}
}
try {
eval(testCode);
} catch (e) {}
`;
} else {
script += 'var result = ' + evalcode + ';\n' +
'if (result) {\n' +
' print("[SUCCESS]");\n' +
'}\n';
script += `
try {
if (eval(testCode)) {
${logCommand}("[SUCCESS]");
}
} catch (e) {}
`;
}

fs.writeFileSync(testFilename, script);

actual = runner(testFilename, parents[0]);
} else {
actual = 'skip';
Expand All @@ -190,7 +188,7 @@ exports.runTests = function runTests(runner, key, family, options) {
if (actual) {
testSuccess++;
}

if (test.res) {
// Take expected result from newest engine version not newer
// than current version.
Expand Down Expand Up @@ -222,8 +220,8 @@ exports.runTests = function runTests(runner, key, family, options) {
}

if (test.subtests) {
var newParents = parents.slice(0);
newParents.push(test.name);
var newParents = parents.concat(test.name);

test.subtests.forEach(function (subtest) {
runTest(newParents, subtest);
});
Expand Down Expand Up @@ -253,4 +251,4 @@ exports.runTests = function runTests(runner, key, family, options) {

console.log(testCount + ' tests executed: ' + testSuccess + ' success, ' + (testCount - testSuccess) + ' fail');
console.log(testOutOfDate + ' tests are out of date (data-*.js file .res)');
};
};
22 changes: 22 additions & 0 deletions test-utils/testHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// this file must only contain js code written in ES3 as it's used in tests

exports.createIterableHelper =
'function __createIterableObject(arr, methods) {\n' +
' methods = methods || {};\n' +
' if (typeof Symbol !== "function" || !Symbol.iterator)\n' +
' return {};\n' +
' arr.length++;\n' +
' var iterator = {\n' +
' next: function() {\n' +
' return { value: arr.shift(), done: arr.length <= 0 };\n' +
' },\n' +
' "return": methods["return"],\n' +
' "throw": methods["throw"]\n' +
' };\n' +
' var iterable = {};\n' +
' iterable[Symbol.iterator] = function(){ return iterator; };\n' +
' return iterable;\n' +
'}\n' +
'if (typeof global !== "undefined") {\n' +
' global.__createIterableObject = __createIterableObject;\n' +
'}\n';

0 comments on commit 0f26d4f

Please sign in to comment.