runner.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. Copyright (C) 2012 Ariya Hidayat <ariya.hidayat@gmail.com>
  3. Copyright (C) 2012 Joost-Wim Boekesteijn <joost-wim@boekesteijn.nl>
  4. Copyright (C) 2012 Yusuke Suzuki <utatane.tea@gmail.com>
  5. Copyright (C) 2012 Arpad Borsos <arpad.borsos@googlemail.com>
  6. Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
  7. Copyright (C) 2011 Yusuke Suzuki <utatane.tea@gmail.com>
  8. Copyright (C) 2011 Arpad Borsos <arpad.borsos@googlemail.com>
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  20. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /*jslint browser:true node:true */
  28. /*global esprima:true, testFixture:true */
  29. var runTests;
  30. // Special handling for regular expression literal since we need to
  31. // convert it to a string literal, otherwise it will be decoded
  32. // as object "{}" and the regular expression would be lost.
  33. function adjustRegexLiteral(key, value) {
  34. 'use strict';
  35. if (key === 'value' && value instanceof RegExp) {
  36. value = value.toString();
  37. }
  38. return value;
  39. }
  40. function NotMatchingError(expected, actual) {
  41. 'use strict';
  42. Error.call(this, 'Expected ');
  43. this.expected = expected;
  44. this.actual = actual;
  45. }
  46. NotMatchingError.prototype = new Error();
  47. function errorToObject(e) {
  48. 'use strict';
  49. var msg = e.toString();
  50. // Opera 9.64 produces an non-standard string in toString().
  51. if (msg.substr(0, 6) !== 'Error:') {
  52. if (typeof e.message === 'string') {
  53. msg = 'Error: ' + e.message;
  54. }
  55. }
  56. return {
  57. index: e.index,
  58. lineNumber: e.lineNumber,
  59. column: e.column,
  60. message: msg
  61. };
  62. }
  63. function testParse(esprima, code, syntax) {
  64. 'use strict';
  65. var expected, tree, actual, options, StringObject, i, len, err;
  66. // alias, so that JSLint does not complain.
  67. StringObject = String;
  68. options = {
  69. comment: (typeof syntax.comments !== 'undefined'),
  70. range: true,
  71. loc: true,
  72. tokens: (typeof syntax.tokens !== 'undefined'),
  73. raw: true,
  74. tolerant: (typeof syntax.errors !== 'undefined')
  75. };
  76. if (typeof syntax.tokens !== 'undefined') {
  77. if (syntax.tokens.length > 0) {
  78. options.range = (typeof syntax.tokens[0].range !== 'undefined');
  79. options.loc = (typeof syntax.tokens[0].loc !== 'undefined');
  80. }
  81. }
  82. if (typeof syntax.comments !== 'undefined') {
  83. if (syntax.comments.length > 0) {
  84. options.range = (typeof syntax.comments[0].range !== 'undefined');
  85. options.loc = (typeof syntax.comments[0].loc !== 'undefined');
  86. }
  87. }
  88. expected = JSON.stringify(syntax, null, 4);
  89. try {
  90. tree = esprima.parse(code, options);
  91. tree = (options.comment || options.tokens || options.tolerant) ? tree : tree.body[0];
  92. if (options.tolerant) {
  93. for (i = 0, len = tree.errors.length; i < len; i += 1) {
  94. tree.errors[i] = errorToObject(tree.errors[i]);
  95. }
  96. }
  97. actual = JSON.stringify(tree, adjustRegexLiteral, 4);
  98. // Only to ensure that there is no error when using string object.
  99. esprima.parse(new StringObject(code), options);
  100. } catch (e) {
  101. throw new NotMatchingError(expected, e.toString());
  102. }
  103. if (expected !== actual) {
  104. throw new NotMatchingError(expected, actual);
  105. }
  106. function filter(key, value) {
  107. if (key === 'value' && value instanceof RegExp) {
  108. value = value.toString();
  109. }
  110. return (key === 'loc' || key === 'range') ? undefined : value;
  111. }
  112. if (options.tolerant) {
  113. return;
  114. }
  115. // Check again without any location info.
  116. options.range = false;
  117. options.loc = false;
  118. expected = JSON.stringify(syntax, filter, 4);
  119. try {
  120. tree = esprima.parse(code, options);
  121. tree = (options.comment || options.tokens) ? tree : tree.body[0];
  122. if (options.tolerant) {
  123. for (i = 0, len = tree.errors.length; i < len; i += 1) {
  124. tree.errors[i] = errorToObject(tree.errors[i]);
  125. }
  126. }
  127. actual = JSON.stringify(tree, filter, 4);
  128. } catch (e) {
  129. throw new NotMatchingError(expected, e.toString());
  130. }
  131. if (expected !== actual) {
  132. throw new NotMatchingError(expected, actual);
  133. }
  134. }
  135. function testError(esprima, code, exception) {
  136. 'use strict';
  137. var i, options, expected, actual, handleInvalidRegexFlag;
  138. // Different parsing options should give the same error.
  139. options = [
  140. {},
  141. { comment: true },
  142. { raw: true },
  143. { raw: true, comment: true }
  144. ];
  145. // If handleInvalidRegexFlag is true, an invalid flag in a regular expression
  146. // will throw an exception. In some old version V8, this is not the case
  147. // and hence handleInvalidRegexFlag is false.
  148. handleInvalidRegexFlag = false;
  149. try {
  150. 'test'.match(new RegExp('[a-z]', 'x'));
  151. } catch (e) {
  152. handleInvalidRegexFlag = true;
  153. }
  154. expected = JSON.stringify(exception);
  155. for (i = 0; i < options.length; i += 1) {
  156. try {
  157. esprima.parse(code, options[i]);
  158. } catch (e) {
  159. actual = JSON.stringify(errorToObject(e));
  160. }
  161. if (expected !== actual) {
  162. // Compensate for old V8 which does not handle invalid flag.
  163. if (exception.message.indexOf('Invalid regular expression') > 0) {
  164. if (typeof actual === 'undefined' && !handleInvalidRegexFlag) {
  165. return;
  166. }
  167. }
  168. throw new NotMatchingError(expected, actual);
  169. }
  170. }
  171. }
  172. function testAPI(esprima, code, result) {
  173. 'use strict';
  174. var expected, res, actual;
  175. expected = JSON.stringify(result.result, null, 4);
  176. try {
  177. if (typeof result.property !== 'undefined') {
  178. res = esprima[result.property];
  179. } else {
  180. res = esprima[result.call].apply(esprima, result.args);
  181. }
  182. actual = JSON.stringify(res, adjustRegexLiteral, 4);
  183. } catch (e) {
  184. throw new NotMatchingError(expected, e.toString());
  185. }
  186. if (expected !== actual) {
  187. throw new NotMatchingError(expected, actual);
  188. }
  189. }
  190. function runTest(esprima, code, result) {
  191. 'use strict';
  192. if (result.hasOwnProperty('lineNumber')) {
  193. testError(esprima, code, result);
  194. } else if (result.hasOwnProperty('result')) {
  195. testAPI(esprima, code, result);
  196. } else {
  197. testParse(esprima, code, result);
  198. }
  199. }
  200. if (typeof window !== 'undefined') {
  201. // Run all tests in a browser environment.
  202. runTests = function () {
  203. 'use strict';
  204. var total = 0,
  205. failures = 0,
  206. category,
  207. fixture,
  208. source,
  209. tick,
  210. expected,
  211. index,
  212. len;
  213. function setText(el, str) {
  214. if (typeof el.innerText === 'string') {
  215. el.innerText = str;
  216. } else {
  217. el.textContent = str;
  218. }
  219. }
  220. function startCategory(category) {
  221. var report, e;
  222. report = document.getElementById('report');
  223. e = document.createElement('h4');
  224. setText(e, category);
  225. report.appendChild(e);
  226. }
  227. function reportSuccess(code) {
  228. var report, e;
  229. report = document.getElementById('report');
  230. e = document.createElement('pre');
  231. e.setAttribute('class', 'code');
  232. setText(e, code);
  233. report.appendChild(e);
  234. }
  235. function reportFailure(code, expected, actual) {
  236. var report, e;
  237. report = document.getElementById('report');
  238. e = document.createElement('p');
  239. setText(e, 'Code:');
  240. report.appendChild(e);
  241. e = document.createElement('pre');
  242. e.setAttribute('class', 'code');
  243. setText(e, code);
  244. report.appendChild(e);
  245. e = document.createElement('p');
  246. setText(e, 'Expected');
  247. report.appendChild(e);
  248. e = document.createElement('pre');
  249. e.setAttribute('class', 'expected');
  250. setText(e, expected);
  251. report.appendChild(e);
  252. e = document.createElement('p');
  253. setText(e, 'Actual');
  254. report.appendChild(e);
  255. e = document.createElement('pre');
  256. e.setAttribute('class', 'actual');
  257. setText(e, actual);
  258. report.appendChild(e);
  259. }
  260. setText(document.getElementById('version'), esprima.version);
  261. tick = new Date();
  262. for (category in testFixture) {
  263. if (testFixture.hasOwnProperty(category)) {
  264. startCategory(category);
  265. fixture = testFixture[category];
  266. for (source in fixture) {
  267. if (fixture.hasOwnProperty(source)) {
  268. expected = fixture[source];
  269. total += 1;
  270. try {
  271. runTest(esprima, source, expected);
  272. reportSuccess(source, JSON.stringify(expected, null, 4));
  273. } catch (e) {
  274. failures += 1;
  275. reportFailure(source, e.expected, e.actual);
  276. }
  277. }
  278. }
  279. }
  280. }
  281. tick = (new Date()) - tick;
  282. if (failures > 0) {
  283. setText(document.getElementById('status'), total + ' tests. ' +
  284. 'Failures: ' + failures + '. ' + tick + ' ms');
  285. } else {
  286. setText(document.getElementById('status'), total + ' tests. ' +
  287. 'No failure. ' + tick + ' ms');
  288. }
  289. };
  290. } else {
  291. (function () {
  292. 'use strict';
  293. var esprima = require('../esprima'),
  294. vm = require('vm'),
  295. fs = require('fs'),
  296. total = 0,
  297. failures = [],
  298. tick = new Date(),
  299. expected,
  300. header;
  301. vm.runInThisContext(fs.readFileSync(__dirname + '/test.js', 'utf-8'));
  302. Object.keys(testFixture).forEach(function (category) {
  303. Object.keys(testFixture[category]).forEach(function (source) {
  304. total += 1;
  305. expected = testFixture[category][source];
  306. try {
  307. runTest(esprima, source, expected);
  308. } catch (e) {
  309. e.source = source;
  310. failures.push(e);
  311. }
  312. });
  313. });
  314. tick = (new Date()) - tick;
  315. header = total + ' tests. ' + failures.length + ' failures. ' +
  316. tick + ' ms';
  317. if (failures.length) {
  318. console.error(header);
  319. failures.forEach(function (failure) {
  320. console.error(failure.source + ': Expected\n ' +
  321. failure.expected.split('\n').join('\n ') +
  322. '\nto match\n ' + failure.actual);
  323. });
  324. } else {
  325. console.log(header);
  326. }
  327. process.exit(failures.length === 0 ? 0 : 1);
  328. }());
  329. }