abd623aa254cf442717ab3b5a8c1b630caf6b564
[platform/framework/web/crosswalk-tizen.git] /
1 /*
2   Copyright (C) 2012 Joost-Wim Boekesteijn <joost-wim@boekesteijn.nl>
3   Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions are met:
7
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.
13
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.
24 */
25
26 /*jslint node: true */
27 /*global document: true, window:true, espree: true, testReflect: true */
28
29 var runTests;
30
31 function getContext(espree, reportCase, reportFailure) {
32     'use strict';
33
34     var Reflect, Pattern;
35
36     // Maps Mozilla Reflect object to our espree parser.
37     Reflect = {
38         parse: function (code) {
39             var result;
40
41             reportCase(code);
42
43             try {
44                 result = espree.parse(code);
45             } catch (error) {
46                 result = error;
47             }
48
49             return result;
50         }
51     };
52
53     // This is used by Reflect test suite to match a syntax tree.
54     Pattern = function (obj) {
55         var pattern;
56
57         // Poor man's deep object cloning.
58         pattern = JSON.parse(JSON.stringify(obj));
59
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) {
65                 pattern = {
66                     type: obj.type,
67                     value: obj.value.toString()
68                 };
69             }
70         }
71
72         // Special handling for branch statement because SpiderMonkey
73         // prefers to put the 'alternate' property before 'consequent'.
74         if (obj.type && obj.type === 'IfStatement') {
75             pattern = {
76                 type: pattern.type,
77                 test: pattern.test,
78                 consequent: pattern.consequent,
79                 alternate: pattern.alternate
80             };
81         }
82
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') {
86             pattern = {
87                 type: pattern.type,
88                 body: pattern.body,
89                 test: pattern.test
90             };
91         }
92
93         // Remove special properties on Property node
94         if (obj.type && obj.type === 'Property') {
95             pattern = {
96                 type: pattern.type,
97                 key: pattern.key,
98                 value: pattern.value,
99                 kind: pattern.kind
100             };
101         }
102
103         function adjustRegexLiteralAndRaw(key, value) {
104             if (key === 'value' && value instanceof RegExp) {
105                 value = value.toString();
106             } else if (key === 'raw' && typeof value === "string") {
107                 // Ignore Espree-specific 'raw' property.
108                 return undefined;
109             } else if (key === 'regex' && typeof value === "object") {
110                 // Ignore Espree-specific 'regex' property.
111                 return undefined;
112             }
113             return value;
114         }
115
116
117         if (obj.type && (obj.type === 'Program')) {
118             pattern.assert = function (tree) {
119                 var actual, expected;
120                 actual = JSON.stringify(tree, adjustRegexLiteralAndRaw, 4);
121                 expected = JSON.stringify(obj, null, 4);
122
123                 if (expected !== actual) {
124                     reportFailure(expected, actual);
125                 }
126             };
127         }
128
129         return pattern;
130     };
131
132     return {
133         Reflect: Reflect,
134         Pattern: Pattern
135     };
136 }
137
138 if (typeof window !== 'undefined') {
139     // Run all tests in a browser environment.
140     runTests = function () {
141         'use strict';
142
143         var total = 0,
144             failures = 0;
145
146         function setText(el, str) {
147             if (typeof el.innerText === 'string') {
148                 el.innerText = str;
149             } else {
150                 el.textContent = str;
151             }
152         }
153
154         function reportCase(code) {
155             var report, e;
156             report = document.getElementById('report');
157             e = document.createElement('pre');
158             e.setAttribute('class', 'code');
159             setText(e, code);
160             report.appendChild(e);
161             total += 1;
162         }
163
164         function reportFailure(expected, actual) {
165             var report, e;
166
167             failures += 1;
168
169             report = document.getElementById('report');
170
171             e = document.createElement('p');
172             setText(e, 'Expected');
173             report.appendChild(e);
174
175             e = document.createElement('pre');
176             e.setAttribute('class', 'expected');
177             setText(e, expected);
178             report.appendChild(e);
179
180             e = document.createElement('p');
181             setText(e, 'Actual');
182             report.appendChild(e);
183
184             e = document.createElement('pre');
185             e.setAttribute('class', 'actual');
186             setText(e, actual);
187             report.appendChild(e);
188         }
189
190         setText(document.getElementById('version'), espree.version);
191
192         window.setTimeout(function () {
193             var tick, context = getContext(espree, reportCase, reportFailure);
194
195             tick = new Date();
196             testReflect(context.Reflect, context.Pattern);
197             tick = (new Date()) - tick;
198
199             if (failures > 0) {
200                 document.getElementById('status').className = 'alert-box alert';
201                 setText(document.getElementById('status'), total + ' tests. ' +
202                     'Failures: ' + failures + '. ' + tick + ' ms');
203             } else {
204                 document.getElementById('status').className = 'alert-box success';
205                 setText(document.getElementById('status'), total + ' tests. ' +
206                     'No failure. ' + tick + ' ms');
207             }
208         }, 11);
209     };
210 } else {
211     (function (global) {
212         'use strict';
213         var espree = require('../espree'),
214             tick,
215             total = 0,
216             failures = [],
217             header,
218             current,
219             context;
220
221         function reportCase(code) {
222             total += 1;
223             current = code;
224         }
225
226         function reportFailure(expected, actual) {
227             failures.push({
228                 source: current,
229                 expected: expected.toString(),
230                 actual: actual.toString()
231             });
232         }
233
234         context = getContext(espree, reportCase, reportFailure);
235
236         tick = new Date();
237         require('./reflect').testReflect(context.Reflect, context.Pattern);
238         tick = (new Date()) - tick;
239
240         header = total + ' tests. ' + failures.length + ' failures. ' +
241             tick + ' ms';
242         if (failures.length) {
243             console.error(header);
244             failures.forEach(function (failure) {
245                 console.error(failure.source + ': Expected\n    ' +
246                     failure.expected.split('\n').join('\n    ') +
247                     '\nto match\n    ' + failure.actual);
248             });
249         } else {
250             console.log(header);
251         }
252         process.exit(failures.length === 0 ? 0 : 1);
253     }(this));
254 }
255 /* vim: set sw=4 ts=4 et tw=80 : */