react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / jstransform / node_modules / esprima-fb / test / runner.js
81159 views/*1Copyright (C) 2012 Ariya Hidayat <[email protected]>2Copyright (C) 2012 Joost-Wim Boekesteijn <[email protected]>3Copyright (C) 2012 Yusuke Suzuki <[email protected]>4Copyright (C) 2012 Arpad Borsos <[email protected]>5Copyright (C) 2011 Ariya Hidayat <[email protected]>6Copyright (C) 2011 Yusuke Suzuki <[email protected]>7Copyright (C) 2011 Arpad Borsos <[email protected]>89Redistribution and use in source and binary forms, with or without10modification, are permitted provided that the following conditions are met:1112* Redistributions of source code must retain the above copyright13notice, this list of conditions and the following disclaimer.14* Redistributions in binary form must reproduce the above copyright15notice, this list of conditions and the following disclaimer in the16documentation and/or other materials provided with the distribution.1718THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY22DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES23(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;24LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND25ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF27THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.28*/2930/*jslint browser:true node:true */31/*global esprima:true, testFixture:true */3233var runTests;3435// Special handling for regular expression literals: remove their `value`36// property since it may be `null` if it represents a regular expression37// that is not supported in the current environment. The `regex` property38// will be compared instead.39function adjustRegexLiteral(key, value) {40'use strict';41if (key === 'value' && value instanceof RegExp) {42value = value.toString();43}44return value;45}4647function NotMatchingError(expected, actual) {48'use strict';49Error.call(this, 'Expected ');50this.expected = expected;51this.actual = actual;52}53NotMatchingError.prototype = new Error();5455function errorToObject(e) {56'use strict';57var msg = e.toString();5859// Opera 9.64 produces an non-standard string in toString().60if (msg.substr(0, 6) !== 'Error:') {61if (typeof e.message === 'string') {62msg = 'Error: ' + e.message;63}64}6566return {67index: e.index,68lineNumber: e.lineNumber,69column: e.column,70message: msg71};72}7374function needLoc(syntax) {75var need = true;76if (typeof syntax.tokens !== 'undefined' && syntax.tokens.length > 0) {77need = (typeof syntax.tokens[0].loc !== 'undefined');78}79if (typeof syntax.comments !== 'undefined' && syntax.comments.length > 0) {80need = (typeof syntax.comments[0].loc !== 'undefined');81}82return need;83}8485function needRange(syntax) {86var need = true;87if (typeof syntax.tokens !== 'undefined' && syntax.tokens.length > 0) {88need = (typeof syntax.tokens[0].range !== 'undefined');89}90if (typeof syntax.comments !== 'undefined' && syntax.comments.length > 0) {91need = (typeof syntax.comments[0].range !== 'undefined');92}93return need;94}9596function hasAttachedComment(syntax) {97var key;98for (key in syntax) {99if (key === 'leadingComments' || key === 'trailingComments') {100return true;101}102if (syntax[key] && typeof syntax[key] === 'object') {103if (hasAttachedComment(syntax[key])) {104return true;105}106}107}108return false;109}110111function testParse(esprima, code, syntax) {112'use strict';113var expected, tree, actual, options, StringObject, i, len, err;114115// alias, so that JSLint does not complain.116StringObject = String;117118options = {119comment: (typeof syntax.comments !== 'undefined'),120range: needRange(syntax),121loc: needLoc(syntax),122tokens: (typeof syntax.tokens !== 'undefined'),123raw: true,124tolerant: (typeof syntax.errors !== 'undefined'),125source: null126};127128if (options.comment) {129options.attachComment = hasAttachedComment(syntax);130}131132if (options.loc) {133options.source = syntax.loc.source;134}135136expected = JSON.stringify(syntax, adjustRegexLiteral, 4);137try {138tree = esprima.parse(code, options);139tree = (options.comment || options.tokens || options.tolerant) ? tree : tree.body[0];140141if (options.tolerant) {142for (i = 0, len = tree.errors.length; i < len; i += 1) {143tree.errors[i] = errorToObject(tree.errors[i]);144}145}146147actual = JSON.stringify(tree, adjustRegexLiteral, 4);148149// Only to ensure that there is no error when using string object.150esprima.parse(new StringObject(code), options);151152} catch (e) {153throw new NotMatchingError(expected, e.toString());154}155if (expected !== actual) {156throw new NotMatchingError(expected, actual);157}158159function filter(key, value) {160if (key === 'value' && value instanceof RegExp) {161value = value.toString();162}163return (key === 'loc' || key === 'range') ? undefined : value;164}165166if (options.tolerant) {167return;168}169170171// Check again without any location info.172options.range = false;173options.loc = false;174expected = JSON.stringify(syntax, filter, 4);175try {176tree = esprima.parse(code, options);177tree = (options.comment || options.tokens) ? tree : tree.body[0];178179if (options.tolerant) {180for (i = 0, len = tree.errors.length; i < len; i += 1) {181tree.errors[i] = errorToObject(tree.errors[i]);182}183}184185actual = JSON.stringify(tree, filter, 4);186} catch (e) {187throw new NotMatchingError(expected, e.toString());188}189if (expected !== actual) {190throw new NotMatchingError(expected, actual);191}192}193194function mustHaveLocRange(testName, node, needLoc, needRange, stack) {195var error;196if (node.hasOwnProperty('type')) {197if (needLoc && !node.loc) {198error = "doesn't have 'loc' property";199}200if (needRange && !node.range) {201error = "doesn't have 'range' property";202}203if (error) {204stack = stack.length ? ' at [' + stack.join('][') + ']' : '';205throw new Error("Test '" + testName + "'" + stack + " (type = " + node.type + ") " + error);206}207}208for (i in node) {209if (node.hasOwnProperty(i) && node[i] !== null && typeof node[i] === 'object') {210stack.push(i);211mustHaveLocRange(testName, node[i], needLoc, needRange, stack);212stack.pop();213}214}215}216217function testTokenize(esprima, code, tokens) {218'use strict';219var options, expected, actual, tree;220221options = {222comment: true,223tolerant: true,224loc: true,225range: true226};227228expected = JSON.stringify(tokens, null, 4);229230try {231tree = esprima.tokenize(code, options);232actual = JSON.stringify(tree, null, 4);233} catch (e) {234throw new NotMatchingError(expected, e.toString());235}236if (expected !== actual) {237throw new NotMatchingError(expected, actual);238}239}240241function testError(esprima, code, exception) {242'use strict';243var i, options, expected, actual, err, handleInvalidRegexFlag, tokenize;244245// Different parsing options should give the same error.246options = [247{},248{ comment: true },249{ raw: true },250{ raw: true, comment: true }251];252253// If handleInvalidRegexFlag is true, an invalid flag in a regular expression254// will throw an exception. In some old version of V8, this is not the case255// and hence handleInvalidRegexFlag is false.256handleInvalidRegexFlag = false;257try {258'test'.match(new RegExp('[a-z]', 'x'));259} catch (e) {260handleInvalidRegexFlag = true;261}262263exception.description = exception.message.replace(/Error: Line [0-9]+: /, '');264265if (exception.tokenize) {266tokenize = true;267exception.tokenize = undefined;268}269expected = JSON.stringify(exception);270271for (i = 0; i < options.length; i += 1) {272273try {274if (tokenize) {275esprima.tokenize(code, options[i])276} else {277esprima.parse(code, options[i]);278}279} catch (e) {280err = errorToObject(e);281err.description = e.description;282actual = JSON.stringify(err);283}284285if (expected !== actual) {286287// Compensate for old V8 which does not handle invalid flag.288if (exception.message.indexOf('Invalid regular expression') > 0) {289if (typeof actual === 'undefined' && !handleInvalidRegexFlag) {290return;291}292}293294throw new NotMatchingError(expected, actual);295}296297}298}299300function testAPI(esprima, code, result) {301'use strict';302var expected, res, actual;303304expected = JSON.stringify(result.result, null, 4);305try {306if (typeof result.property !== 'undefined') {307res = esprima[result.property];308} else {309res = esprima[result.call].apply(esprima, result.args);310}311actual = JSON.stringify(res, adjustRegexLiteral, 4);312} catch (e) {313throw new NotMatchingError(expected, e.toString());314}315if (expected !== actual) {316throw new NotMatchingError(expected, actual);317}318}319320function runTest(esprima, code, result) {321'use strict';322if (result.hasOwnProperty('lineNumber')) {323testError(esprima, code, result);324} else if (result.hasOwnProperty('result')) {325testAPI(esprima, code, result);326} else if (result instanceof Array) {327testTokenize(esprima, code, result);328} else {329testParse(esprima, code, result);330}331}332333if (typeof window !== 'undefined') {334// Run all tests in a browser environment.335runTests = function () {336'use strict';337var total = 0,338failures = 0,339category,340fixture,341source,342tick,343expected,344index,345len;346347function setText(el, str) {348if (typeof el.innerText === 'string') {349el.innerText = str;350} else {351el.textContent = str;352}353}354355function startCategory(category) {356var report, e;357report = document.getElementById('report');358e = document.createElement('h4');359setText(e, category);360report.appendChild(e);361}362363function reportSuccess(code) {364var report, e;365report = document.getElementById('report');366e = document.createElement('pre');367e.setAttribute('class', 'code');368setText(e, code);369report.appendChild(e);370}371372function reportFailure(code, expected, actual) {373var report, e;374375report = document.getElementById('report');376377e = document.createElement('p');378setText(e, 'Code:');379report.appendChild(e);380381e = document.createElement('pre');382e.setAttribute('class', 'code');383setText(e, code);384report.appendChild(e);385386e = document.createElement('p');387setText(e, 'Expected');388report.appendChild(e);389390e = document.createElement('pre');391e.setAttribute('class', 'expected');392setText(e, expected);393report.appendChild(e);394395e = document.createElement('p');396setText(e, 'Actual');397report.appendChild(e);398399e = document.createElement('pre');400e.setAttribute('class', 'actual');401setText(e, actual);402report.appendChild(e);403}404405setText(document.getElementById('version'), esprima.version);406407tick = new Date();408for (category in testFixture) {409if (testFixture.hasOwnProperty(category)) {410startCategory(category);411fixture = testFixture[category];412for (source in fixture) {413if (fixture.hasOwnProperty(source)) {414expected = fixture[source];415total += 1;416try {417runTest(esprima, source, expected);418reportSuccess(source, JSON.stringify(expected, null, 4));419} catch (e) {420failures += 1;421reportFailure(source, e.expected, e.actual);422}423}424}425}426}427tick = (new Date()) - tick;428429if (failures > 0) {430document.getElementById('status').className = 'alert-box alert';431setText(document.getElementById('status'), total + ' tests. ' +432'Failures: ' + failures + '. ' + tick + ' ms.');433} else {434document.getElementById('status').className = 'alert-box success';435setText(document.getElementById('status'), total + ' tests. ' +436'No failure. ' + tick + ' ms.');437}438};439} else {440(function () {441'use strict';442443var esprima = require('../esprima'),444vm = require('vm'),445fs = require('fs'),446diff = require('json-diff').diffString,447total = 0,448failures = [],449tick = new Date(),450expected,451header;452453vm.runInThisContext(fs.readFileSync(__dirname + '/test.js', 'utf-8'));454vm.runInThisContext(fs.readFileSync(__dirname + '/harmonytest.js', 'utf-8'));455vm.runInThisContext(fs.readFileSync(__dirname + '/fbtest.rec.js', 'utf-8'));456vm.runInThisContext(fs.readFileSync(__dirname + '/harmonymodulestest.js', 'utf-8'));457458Object.keys(testFixture).forEach(function (category) {459Object.keys(testFixture[category]).forEach(function (source) {460total += 1;461expected = testFixture[category][source];462if (!expected.hasOwnProperty('lineNumber') && !expected.hasOwnProperty('result')) {463mustHaveLocRange(source, expected, needLoc(expected), needRange(expected), []);464}465try {466runTest(esprima, source, expected);467} catch (e) {468e.source = source;469failures.push(e);470}471});472});473tick = (new Date()) - tick;474475header = total + ' tests. ' + failures.length + ' failures. ' +476tick + ' ms';477if (failures.length) {478console.error(header);479failures.forEach(function (failure) {480try {481var expectedObject = JSON.parse(failure.expected);482var actualObject = JSON.parse(failure.actual);483484console.error(failure.source + ': Expected\n ' +485failure.expected.split('\n').join('\n ') +486'\nto match\n ' + failure.actual + '\nDiff:\n' +487diff(expectedObject, actualObject));488} catch (ex) {489console.error(failure.source + ': Expected\n ' +490failure.expected.split('\n').join('\n ') +491'\nto match\n ' + failure.actual);492}493});494} else {495console.log(header);496}497process.exit(failures.length === 0 ? 0 : 1);498}());499}500501502