compat.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. Copyright (C) 2012 Joost-Wim Boekesteijn <joost-wim@boekesteijn.nl>
  3. Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  12. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  13. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  14. ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  15. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  17. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  18. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  19. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  20. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. /*jslint node: true */
  23. /*global document: true, window:true, esprima: true, testReflect: true */
  24. var runTests;
  25. function getContext(esprima, reportCase, reportFailure) {
  26. 'use strict';
  27. var Reflect, Pattern;
  28. // Maps Mozilla Reflect object to our Esprima parser.
  29. Reflect = {
  30. parse: function (code) {
  31. var result;
  32. reportCase(code);
  33. try {
  34. result = esprima.parse(code);
  35. } catch (error) {
  36. result = error;
  37. }
  38. return result;
  39. }
  40. };
  41. // This is used by Reflect test suite to match a syntax tree.
  42. Pattern = function (obj) {
  43. var pattern;
  44. // Poor man's deep object cloning.
  45. pattern = JSON.parse(JSON.stringify(obj));
  46. // Special handling for regular expression literal since we need to
  47. // convert it to a string literal, otherwise it will be decoded
  48. // as object "{}" and the regular expression would be lost.
  49. if (obj.type && obj.type === 'Literal') {
  50. if (obj.value instanceof RegExp) {
  51. pattern = {
  52. type: obj.type,
  53. value: obj.value.toString()
  54. };
  55. }
  56. }
  57. // Special handling for branch statement because SpiderMonkey
  58. // prefers to put the 'alternate' property before 'consequent'.
  59. if (obj.type && obj.type === 'IfStatement') {
  60. pattern = {
  61. type: pattern.type,
  62. test: pattern.test,
  63. consequent: pattern.consequent,
  64. alternate: pattern.alternate
  65. };
  66. }
  67. // Special handling for do while statement because SpiderMonkey
  68. // prefers to put the 'test' property before 'body'.
  69. if (obj.type && obj.type === 'DoWhileStatement') {
  70. pattern = {
  71. type: pattern.type,
  72. body: pattern.body,
  73. test: pattern.test
  74. };
  75. }
  76. function adjustRegexLiteralAndRaw(key, value) {
  77. if (key === 'value' && value instanceof RegExp) {
  78. value = value.toString();
  79. } else if (key === 'raw' && typeof value === "string") {
  80. // Ignore Esprima-specific 'raw' property.
  81. return undefined;
  82. }
  83. return value;
  84. }
  85. if (obj.type && (obj.type === 'Program')) {
  86. pattern.assert = function (tree) {
  87. var actual, expected;
  88. actual = JSON.stringify(tree, adjustRegexLiteralAndRaw, 4);
  89. expected = JSON.stringify(obj, null, 4);
  90. if (expected !== actual) {
  91. reportFailure(expected, actual);
  92. }
  93. };
  94. }
  95. return pattern;
  96. };
  97. return {
  98. Reflect: Reflect,
  99. Pattern: Pattern
  100. };
  101. }
  102. if (typeof window !== 'undefined') {
  103. // Run all tests in a browser environment.
  104. runTests = function () {
  105. 'use strict';
  106. var total = 0,
  107. failures = 0;
  108. function setText(el, str) {
  109. if (typeof el.innerText === 'string') {
  110. el.innerText = str;
  111. } else {
  112. el.textContent = str;
  113. }
  114. }
  115. function reportCase(code) {
  116. var report, e;
  117. report = document.getElementById('report');
  118. e = document.createElement('pre');
  119. e.setAttribute('class', 'code');
  120. setText(e, code);
  121. report.appendChild(e);
  122. total += 1;
  123. }
  124. function reportFailure(expected, actual) {
  125. var report, e;
  126. failures += 1;
  127. report = document.getElementById('report');
  128. e = document.createElement('p');
  129. setText(e, 'Expected');
  130. report.appendChild(e);
  131. e = document.createElement('pre');
  132. e.setAttribute('class', 'expected');
  133. setText(e, expected);
  134. report.appendChild(e);
  135. e = document.createElement('p');
  136. setText(e, 'Actual');
  137. report.appendChild(e);
  138. e = document.createElement('pre');
  139. e.setAttribute('class', 'actual');
  140. setText(e, actual);
  141. report.appendChild(e);
  142. }
  143. setText(document.getElementById('version'), esprima.version);
  144. window.setTimeout(function () {
  145. var tick, context = getContext(esprima, reportCase, reportFailure);
  146. tick = new Date();
  147. testReflect(context.Reflect, context.Pattern);
  148. tick = (new Date()) - tick;
  149. if (failures > 0) {
  150. setText(document.getElementById('status'), total + ' tests. ' +
  151. 'Failures: ' + failures + '. ' + tick + ' ms');
  152. } else {
  153. setText(document.getElementById('status'), total + ' tests. ' +
  154. 'No failure. ' + tick + ' ms');
  155. }
  156. }, 513);
  157. };
  158. } else {
  159. (function (global) {
  160. 'use strict';
  161. var esprima = require('../esprima'),
  162. tick,
  163. total = 0,
  164. failures = [],
  165. header,
  166. current,
  167. context;
  168. function reportCase(code) {
  169. total += 1;
  170. current = code;
  171. }
  172. function reportFailure(expected, actual) {
  173. failures.push({
  174. source: current,
  175. expected: expected.toString(),
  176. actual: actual.toString()
  177. });
  178. }
  179. context = getContext(esprima, reportCase, reportFailure);
  180. tick = new Date();
  181. require('./reflect').testReflect(context.Reflect, context.Pattern);
  182. tick = (new Date()) - tick;
  183. header = total + ' tests. ' + failures.length + ' failures. ' +
  184. tick + ' ms';
  185. if (failures.length) {
  186. console.error(header);
  187. failures.forEach(function (failure) {
  188. console.error(failure.source + ': Expected\n ' +
  189. failure.expected.split('\n').join('\n ') +
  190. '\nto match\n ' + failure.actual);
  191. });
  192. } else {
  193. console.log(header);
  194. }
  195. process.exit(failures.length === 0 ? 0 : 1);
  196. }(this));
  197. }
  198. /* vim: set sw=4 ts=4 et tw=80 : */