Skip to content

Commit

Permalink
Add generator identity test
Browse files Browse the repository at this point in the history
  • Loading branch information
Constellation committed Mar 1, 2012
1 parent 13f251f commit 2cccf9d
Showing 1 changed file with 80 additions and 2 deletions.
82 changes: 80 additions & 2 deletions test/test.js
Expand Up @@ -20420,6 +20420,34 @@ function testParse(code, syntax) {
} }
} }


function testIdentity(code, syntax) {
'use strict';
var expected, tree, actual, options, StringObject;

// alias, so that JSLint does not complain.
StringObject = String;

options = {
comment: false,
range: false,
loc: false,
tokens: false,
raw: false
};

try {
tree = esprima.parse(code, options);
expected = JSON.stringify(tree, adjustRegexLiteral, 4);
tree = esprima.parse(esprima.generate(tree), options);
actual = JSON.stringify(tree, adjustRegexLiteral, 4);
} catch (e) {
throw new NotMatchingError(expected, e.toString());
}
if (expected !== actual) {
throw new NotMatchingError(expected, actual);
}
}

function testError(code, exception) { function testError(code, exception) {
'use strict'; 'use strict';
var expected, msg, actual; var expected, msg, actual;
Expand Down Expand Up @@ -20532,6 +20560,14 @@ function testAPI(code, result) {
} }
} }


function isGeneratorIdentityFixture(result) {
'use strict';
return !result.hasOwnProperty('lineNumber') &&
!result.hasOwnProperty('modifiers') &&
!result.hasOwnProperty('from') &&
!result.hasOwnProperty('result');
}

function runTest(code, result) { function runTest(code, result) {
'use strict'; 'use strict';
if (result.hasOwnProperty('lineNumber')) { if (result.hasOwnProperty('lineNumber')) {
Expand All @@ -20557,7 +20593,11 @@ if (typeof window !== 'undefined') {
fixture, fixture,
source, source,
tick, tick,
expected; expected,
fixtures,
fixture,
index,
len;


function setText(el, str) { function setText(el, str) {
if (typeof el.innerText === 'string') { if (typeof el.innerText === 'string') {
Expand Down Expand Up @@ -20620,6 +20660,7 @@ if (typeof window !== 'undefined') {
setText(document.getElementById('version'), esprima.version); setText(document.getElementById('version'), esprima.version);


tick = new Date(); tick = new Date();
fixtures = [];
for (category in data) { for (category in data) {
if (data.hasOwnProperty(category)) { if (data.hasOwnProperty(category)) {
startCategory(category); startCategory(category);
Expand All @@ -20628,6 +20669,12 @@ if (typeof window !== 'undefined') {
if (fixture.hasOwnProperty(source)) { if (fixture.hasOwnProperty(source)) {
expected = fixture[source]; expected = fixture[source];
total += 1; total += 1;
if (isGeneratorIdentityFixture(expected)) {
fixtures.push({
source: source,
expected: expected
});
}
try { try {
runTest(source, expected); runTest(source, expected);
reportSuccess(source, JSON.stringify(expected, null, 4)); reportSuccess(source, JSON.stringify(expected, null, 4));
Expand All @@ -20639,6 +20686,20 @@ if (typeof window !== 'undefined') {
} }
} }
} }

startCategory('Generateor Identity');
for (index = 0, len = fixtures.length; index < len; index += 1) {
total += 1;
try {
fixture = fixtures[index];
testIdentity(fixture.source, fixture.expected);
reportSuccess(fixture.source, JSON.stringify(fixture.expected, null, 4));
} catch (e) {
failures += 1;
reportFailure(fixture.source, e.expected, e.actual);
}
}

tick = (new Date()) - tick; tick = (new Date()) - tick;


if (failures > 0) { if (failures > 0) {
Expand All @@ -20657,12 +20718,20 @@ if (typeof window !== 'undefined') {
failures = [], failures = [],
tick = new Date(), tick = new Date(),
expected, expected,
header; header,
fixtures;


fixtures = [];
Object.keys(data).forEach(function (category) { Object.keys(data).forEach(function (category) {
Object.keys(data[category]).forEach(function (source) { Object.keys(data[category]).forEach(function (source) {
total += 1; total += 1;
expected = data[category][source]; expected = data[category][source];
if (isGeneratorIdentityFixture(expected)) {
fixtures.push({
source: source,
expected: expected
});
}
try { try {
runTest(source, expected); runTest(source, expected);
} catch (e) { } catch (e) {
Expand All @@ -20671,6 +20740,15 @@ if (typeof window !== 'undefined') {
} }
}); });
}); });
fixtures.forEach(function (fixture) {
total += 1;
try {
testIdentity(fixture.source, fixture.expected);
} catch (e) {
e.source = fixture.source;
failures.push(e);
}
});
tick = (new Date()) - tick; tick = (new Date()) - tick;


header = total + ' tests. ' + failures.length + ' failures. ' + header = total + ' tests. ' + failures.length + ' failures. ' +
Expand Down

0 comments on commit 2cccf9d

Please sign in to comment.