1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | var runTests;
|
30 |
|
31 | function getContext(esprima, reportCase, reportFailure) {
|
32 | 'use strict';
|
33 |
|
34 | var Reflect, Pattern;
|
35 |
|
36 |
|
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 |
|
54 | Pattern = function (obj) {
|
55 | var pattern;
|
56 |
|
57 |
|
58 | pattern = JSON.parse(JSON.stringify(obj));
|
59 |
|
60 |
|
61 |
|
62 |
|
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 |
|
73 |
|
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 |
|
84 |
|
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 |
|
98 | return undefined;
|
99 | }
|
100 | return value;
|
101 | }
|
102 |
|
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);
|
108 |
|
109 | if (expected !== actual) {
|
110 | reportFailure(expected, actual);
|
111 | }
|
112 | };
|
113 | }
|
114 |
|
115 | return pattern;
|
116 | };
|
117 |
|
118 | return {
|
119 | Reflect: Reflect,
|
120 | Pattern: Pattern
|
121 | };
|
122 | }
|
123 |
|
124 | if (typeof window !== 'undefined') {
|
125 |
|
126 | runTests = function () {
|
127 | 'use strict';
|
128 |
|
129 | var total = 0,
|
130 | failures = 0;
|
131 |
|
132 | function setText(el, str) {
|
133 | if (typeof el.innerText === 'string') {
|
134 | el.innerText = str;
|
135 | } else {
|
136 | el.textContent = str;
|
137 | }
|
138 | }
|
139 |
|
140 | function reportCase(code) {
|
141 | var report, e;
|
142 | report = document.getElementById('report');
|
143 | e = document.createElement('pre');
|
144 | e.setAttribute('class', 'code');
|
145 | setText(e, code);
|
146 | report.appendChild(e);
|
147 | total += 1;
|
148 | }
|
149 |
|
150 | function reportFailure(expected, actual) {
|
151 | var report, e;
|
152 |
|
153 | failures += 1;
|
154 |
|
155 | report = document.getElementById('report');
|
156 |
|
157 | e = document.createElement('p');
|
158 | setText(e, 'Expected');
|
159 | report.appendChild(e);
|
160 |
|
161 | e = document.createElement('pre');
|
162 | e.setAttribute('class', 'expected');
|
163 | setText(e, expected);
|
164 | report.appendChild(e);
|
165 |
|
166 | e = document.createElement('p');
|
167 | setText(e, 'Actual');
|
168 | report.appendChild(e);
|
169 |
|
170 | e = document.createElement('pre');
|
171 | e.setAttribute('class', 'actual');
|
172 | setText(e, actual);
|
173 | report.appendChild(e);
|
174 | }
|
175 |
|
176 | setText(document.getElementById('version'), esprima.version);
|
177 |
|
178 | window.setTimeout(function () {
|
179 | var tick, context = getContext(esprima, reportCase, reportFailure);
|
180 |
|
181 | tick = new Date();
|
182 | testReflect(context.Reflect, context.Pattern);
|
183 | tick = (new Date()) - tick;
|
184 |
|
185 | if (failures > 0) {
|
186 | document.getElementById('status').className = 'alert-box alert';
|
187 | setText(document.getElementById('status'), total + ' tests. ' +
|
188 | 'Failures: ' + failures + '. ' + tick + ' ms');
|
189 | } else {
|
190 | document.getElementById('status').className = 'alert-box success';
|
191 | setText(document.getElementById('status'), total + ' tests. ' +
|
192 | 'No failure. ' + tick + ' ms');
|
193 | }
|
194 | }, 11);
|
195 | };
|
196 | } else {
|
197 | (function (global) {
|
198 | 'use strict';
|
199 | var esprima = require('../esprima'),
|
200 | tick,
|
201 | total = 0,
|
202 | failures = [],
|
203 | header,
|
204 | current,
|
205 | context;
|
206 |
|
207 | function reportCase(code) {
|
208 | total += 1;
|
209 | current = code;
|
210 | }
|
211 |
|
212 | function reportFailure(expected, actual) {
|
213 | failures.push({
|
214 | source: current,
|
215 | expected: expected.toString(),
|
216 | actual: actual.toString()
|
217 | });
|
218 | }
|
219 |
|
220 | context = getContext(esprima, reportCase, reportFailure);
|
221 |
|
222 | tick = new Date();
|
223 | require('./reflect').testReflect(context.Reflect, context.Pattern);
|
224 | tick = (new Date()) - tick;
|
225 |
|
226 | header = total + ' tests. ' + failures.length + ' failures. ' +
|
227 | tick + ' ms';
|
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);
|
234 | });
|
235 | } else {
|
236 | console.log(header);
|
237 | }
|
238 | process.exit(failures.length === 0 ? 0 : 1);
|
239 | }(this));
|
240 | }
|
241 |
|