react / wstein / node_modules / react / node_modules / envify / node_modules / jstransform / node_modules / esprima-fb / test / compat.js
80556 views/*1Copyright (C) 2012 Joost-Wim Boekesteijn <[email protected]>2Copyright (C) 2011 Ariya Hidayat <[email protected]>34Redistribution and use in source and binary forms, with or without5modification, are permitted provided that the following conditions are met:67* Redistributions of source code must retain the above copyright8notice, this list of conditions and the following disclaimer.9* Redistributions in binary form must reproduce the above copyright10notice, this list of conditions and the following disclaimer in the11documentation and/or other materials provided with the distribution.1213THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"14AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY17DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES18(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;19LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND20ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT21(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF22THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.23*/2425/*jslint node: true */26/*global document: true, window:true, esprima: true, testReflect: true */2728var runTests;2930function getContext(esprima, reportCase, reportFailure) {31'use strict';3233var Reflect, Pattern;3435// Maps Mozilla Reflect object to our Esprima parser.36Reflect = {37parse: function (code) {38var result;3940reportCase(code);4142try {43result = esprima.parse(code);44} catch (error) {45result = error;46}4748return result;49}50};5152// This is used by Reflect test suite to match a syntax tree.53Pattern = function (obj) {54var pattern;5556// Poor man's deep object cloning.57pattern = JSON.parse(JSON.stringify(obj));5859// Special handling for regular expression literal since we need to60// convert it to a string literal, otherwise it will be decoded61// as object "{}" and the regular expression would be lost.62if (obj.type && obj.type === 'Literal') {63if (obj.value instanceof RegExp) {64pattern = {65type: obj.type,66value: obj.value.toString()67};68}69}7071// Special handling for branch statement because SpiderMonkey72// prefers to put the 'alternate' property before 'consequent'.73if (obj.type && obj.type === 'IfStatement') {74pattern = {75type: pattern.type,76test: pattern.test,77consequent: pattern.consequent,78alternate: pattern.alternate79};80}8182// Special handling for do while statement because SpiderMonkey83// prefers to put the 'test' property before 'body'.84if (obj.type && obj.type === 'DoWhileStatement') {85pattern = {86type: pattern.type,87body: pattern.body,88test: pattern.test89};90}9192function adjustRegexLiteralAndRaw(key, value) {93if (key === 'value' && value instanceof RegExp) {94value = value.toString();95} else if (key === 'raw' && typeof value === "string") {96// Ignore Esprima-specific 'raw' property.97return undefined;98} else if (key === 'regex' && typeof value === "object") {99// Ignore Esprima-specific 'regex' property.100return undefined;101}102return value;103}104105if (obj.type && (obj.type === 'Program')) {106pattern.assert = function (tree) {107var actual, expected;108actual = JSON.stringify(tree, adjustRegexLiteralAndRaw, 4);109expected = JSON.stringify(obj, null, 4);110111if (expected !== actual) {112reportFailure(expected, actual);113}114};115}116117return pattern;118};119120return {121Reflect: Reflect,122Pattern: Pattern123};124}125126if (typeof window !== 'undefined') {127// Run all tests in a browser environment.128runTests = function () {129'use strict';130131var total = 0,132failures = 0;133134function setText(el, str) {135if (typeof el.innerText === 'string') {136el.innerText = str;137} else {138el.textContent = str;139}140}141142function reportCase(code) {143var report, e;144report = document.getElementById('report');145e = document.createElement('pre');146e.setAttribute('class', 'code');147setText(e, code);148report.appendChild(e);149total += 1;150}151152function reportFailure(expected, actual) {153var report, e;154155failures += 1;156157report = document.getElementById('report');158159e = document.createElement('p');160setText(e, 'Expected');161report.appendChild(e);162163e = document.createElement('pre');164e.setAttribute('class', 'expected');165setText(e, expected);166report.appendChild(e);167168e = document.createElement('p');169setText(e, 'Actual');170report.appendChild(e);171172e = document.createElement('pre');173e.setAttribute('class', 'actual');174setText(e, actual);175report.appendChild(e);176}177178setText(document.getElementById('version'), esprima.version);179180window.setTimeout(function () {181var tick, context = getContext(esprima, reportCase, reportFailure);182183tick = new Date();184testReflect(context.Reflect, context.Pattern);185tick = (new Date()) - tick;186187if (failures > 0) {188document.getElementById('status').className = 'alert-box alert';189setText(document.getElementById('status'), total + ' tests. ' +190'Failures: ' + failures + '. ' + tick + ' ms');191} else {192document.getElementById('status').className = 'alert-box success';193setText(document.getElementById('status'), total + ' tests. ' +194'No failure. ' + tick + ' ms');195}196}, 11);197};198} else {199(function (global) {200'use strict';201var esprima = require('../esprima'),202tick,203total = 0,204failures = [],205header,206current,207context;208209function reportCase(code) {210total += 1;211current = code;212}213214function reportFailure(expected, actual) {215failures.push({216source: current,217expected: expected.toString(),218actual: actual.toString()219});220}221222context = getContext(esprima, reportCase, reportFailure);223224tick = new Date();225require('./reflect').testReflect(context.Reflect, context.Pattern);226tick = (new Date()) - tick;227228header = total + ' tests. ' + failures.length + ' failures. ' +229tick + ' ms';230if (failures.length) {231console.error(header);232failures.forEach(function (failure) {233console.error(failure.source + ': Expected\n ' +234failure.expected.split('\n').join('\n ') +235'\nto match\n ' + failure.actual);236});237} else {238console.log(header);239}240process.exit(failures.length === 0 ? 0 : 1);241}(this));242}243/* vim: set sw=4 ts=4 et tw=80 : */244245246