2 Copyright (C) 2012 Joost-Wim Boekesteijn <joost-wim@boekesteijn.nl>
3 Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
18 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 /*jslint node: true */
27 /*global document: true, window:true, esprima: true, testReflect: true */
31 function getContext(esprima, reportCase, reportFailure) {
36 // Maps Mozilla Reflect object to our Esprima parser.
38 parse: function (code) {
44 result = esprima.parse(code);
53 // This is used by Reflect test suite to match a syntax tree.
54 Pattern = function (obj) {
57 // Poor man's deep object cloning.
58 pattern = JSON.parse(JSON.stringify(obj));
60 // Special handling for regular expression literal since we need to
61 // convert it to a string literal, otherwise it will be decoded
62 // as object "{}" and the regular expression would be lost.
63 if (obj.type && obj.type === 'Literal') {
64 if (obj.value instanceof RegExp) {
67 value: obj.value.toString()
72 // Special handling for branch statement because SpiderMonkey
73 // prefers to put the 'alternate' property before 'consequent'.
74 if (obj.type && obj.type === 'IfStatement') {
78 consequent: pattern.consequent,
79 alternate: pattern.alternate
83 // Special handling for do while statement because SpiderMonkey
84 // prefers to put the 'test' property before 'body'.
85 if (obj.type && obj.type === 'DoWhileStatement') {
93 function adjustRegexLiteralAndRaw(key, value) {
94 if (key === 'value' && value instanceof RegExp) {
95 value = value.toString();
96 } else if (key === 'raw' && typeof value === "string") {
97 // Ignore Esprima-specific 'raw' property.
103 if (obj.type && (obj.type === 'Program')) {
104 pattern.assert = function (tree) {
105 var actual, expected;
106 actual = JSON.stringify(tree, adjustRegexLiteralAndRaw, 4);
107 expected = JSON.stringify(obj, null, 4);
109 if (expected !== actual) {
110 reportFailure(expected, actual);
124 if (typeof window !== 'undefined') {
125 // Run all tests in a browser environment.
126 runTests = function () {
132 function setText(el, str) {
133 if (typeof el.innerText === 'string') {
136 el.textContent = str;
140 function reportCase(code) {
142 report = document.getElementById('report');
143 e = document.createElement('pre');
144 e.setAttribute('class', 'code');
146 report.appendChild(e);
150 function reportFailure(expected, actual) {
155 report = document.getElementById('report');
157 e = document.createElement('p');
158 setText(e, 'Expected');
159 report.appendChild(e);
161 e = document.createElement('pre');
162 e.setAttribute('class', 'expected');
163 setText(e, expected);
164 report.appendChild(e);
166 e = document.createElement('p');
167 setText(e, 'Actual');
168 report.appendChild(e);
170 e = document.createElement('pre');
171 e.setAttribute('class', 'actual');
173 report.appendChild(e);
176 setText(document.getElementById('version'), esprima.version);
178 window.setTimeout(function () {
179 var tick, context = getContext(esprima, reportCase, reportFailure);
182 testReflect(context.Reflect, context.Pattern);
183 tick = (new Date()) - tick;
186 document.getElementById('status').className = 'alert-box alert';
187 setText(document.getElementById('status'), total + ' tests. ' +
188 'Failures: ' + failures + '. ' + tick + ' ms');
190 document.getElementById('status').className = 'alert-box success';
191 setText(document.getElementById('status'), total + ' tests. ' +
192 'No failure. ' + tick + ' ms');
199 var esprima = require('../esprima'),
207 function reportCase(code) {
212 function reportFailure(expected, actual) {
215 expected: expected.toString(),
216 actual: actual.toString()
220 context = getContext(esprima, reportCase, reportFailure);
223 require('./reflect').testReflect(context.Reflect, context.Pattern);
224 tick = (new Date()) - tick;
226 header = total + ' tests. ' + failures.length + ' failures. ' +
228 if (failures.length) {
229 console.error(header);
230 failures.forEach(function (failure) {
231 console.error(failure.source + ': Expected\n ' +
232 failure.expected.split('\n').join('\n ') +
233 '\nto match\n ' + failure.actual);
238 process.exit(failures.length === 0 ? 0 : 1);
241 /* vim: set sw=4 ts=4 et tw=80 : */