1f67307ecbd1aff1b7fb958c418e9e71e87181c9
[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, esprima: true, testReflect: true */
28
29 var runTests;
30
31 function getContext(esprima, reportCase, reportFailure) {
32     'use strict';
33
34     var Reflect, Pattern;
35
36     // Maps Mozilla Reflect object to our Esprima parser.
37     Reflect = {
38         parse: function (code) {
39             var result;
40
41             reportCase(code);
42
43             try {
44                 result = esprima.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         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.
98                 return undefined;
99             } else if (key === 'regex' && typeof value === "object") {
100                 // Ignore Esprima-specific 'regex' property.
101                 return undefined;
102             }
103             return value;
104         }
105
106         if (obj.type && (obj.type === 'Program')) {
107             pattern.assert = function (tree) {
108                 var actual, expected;
109                 actual = JSON.stringify(tree, adjustRegexLiteralAndRaw, 4);
110                 expected = JSON.stringify(obj, null, 4);
111
112                 if (expected !== actual) {
113                     reportFailure(expected, actual);
114                 }
115             };
116         }
117
118         return pattern;
119     };
120
121     return {
122         Reflect: Reflect,
123         Pattern: Pattern
124     };
125 }
126
127 if (typeof window !== 'undefined') {
128     // Run all tests in a browser environment.
129     runTests = function () {
130         'use strict';
131
132         var total = 0,
133             failures = 0;
134
135         function setText(el, str) {
136             if (typeof el.innerText === 'string') {
137                 el.innerText = str;
138             } else {
139                 el.textContent = str;
140             }
141         }
142
143         function reportCase(code) {
144             var report, e;
145             report = document.getElementById('report');
146             e = document.createElement('pre');
147             e.setAttribute('class', 'code');
148             setText(e, code);
149             report.appendChild(e);
150             total += 1;
151         }
152
153         function reportFailure(expected, actual) {
154             var report, e;
155
156             failures += 1;
157
158             report = document.getElementById('report');
159
160             e = document.createElement('p');
161             setText(e, 'Expected');
162             report.appendChild(e);
163
164             e = document.createElement('pre');
165             e.setAttribute('class', 'expected');
166             setText(e, expected);
167             report.appendChild(e);
168
169             e = document.createElement('p');
170             setText(e, 'Actual');
171             report.appendChild(e);
172
173             e = document.createElement('pre');
174             e.setAttribute('class', 'actual');
175             setText(e, actual);
176             report.appendChild(e);
177         }
178
179         setText(document.getElementById('version'), esprima.version);
180
181         window.setTimeout(function () {
182             var tick, context = getContext(esprima, reportCase, reportFailure);
183
184             tick = new Date();
185             testReflect(context.Reflect, context.Pattern);
186             tick = (new Date()) - tick;
187
188             if (failures > 0) {
189                 document.getElementById('status').className = 'alert-box alert';
190                 setText(document.getElementById('status'), total + ' tests. ' +
191                     'Failures: ' + failures + '. ' + tick + ' ms');
192             } else {
193                 document.getElementById('status').className = 'alert-box success';
194                 setText(document.getElementById('status'), total + ' tests. ' +
195                     'No failure. ' + tick + ' ms');
196             }
197         }, 11);
198     };
199 } else {
200     (function (global) {
201         'use strict';
202         var esprima = require('../esprima'),
203             tick,
204             total = 0,
205             failures = [],
206             header,
207             current,
208             context;
209
210         function reportCase(code) {
211             total += 1;
212             current = code;
213         }
214
215         function reportFailure(expected, actual) {
216             failures.push({
217                 source: current,
218                 expected: expected.toString(),
219                 actual: actual.toString()
220             });
221         }
222
223         context = getContext(esprima, reportCase, reportFailure);
224
225         tick = new Date();
226         require('./reflect').testReflect(context.Reflect, context.Pattern);
227         tick = (new Date()) - tick;
228
229         header = total + ' tests. ' + failures.length + ' failures. ' +
230             tick + ' ms';
231         if (failures.length) {
232             console.error(header);
233             failures.forEach(function (failure) {
234                 console.error(failure.source + ': Expected\n    ' +
235                     failure.expected.split('\n').join('\n    ') +
236                     '\nto match\n    ' + failure.actual);
237             });
238         } else {
239             console.log(header);
240         }
241         process.exit(failures.length === 0 ? 0 : 1);
242     }(this));
243 }
244 /* vim: set sw=4 ts=4 et tw=80 : */