UI for Zipcoin Blue

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. source: null
  76. };
  77. if (typeof syntax.tokens !== 'undefined') {
  78. if (syntax.tokens.length > 0) {
  79. options.range = (typeof syntax.tokens[0].range !== 'undefined');
  80. options.loc = (typeof syntax.tokens[0].loc !== 'undefined');
  81. }
  82. }
  83. if (typeof syntax.comments !== 'undefined') {
  84. if (syntax.comments.length > 0) {
  85. options.range = (typeof syntax.comments[0].range !== 'undefined');
  86. options.loc = (typeof syntax.comments[0].loc !== 'undefined');
  87. }
  88. }
  89. if (options.loc) {
  90. options.source = syntax.loc.source;
  91. }
  92. expected = JSON.stringify(syntax, null, 4);
  93. try {
  94. tree = esprima.parse(code, options);
  95. tree = (options.comment || options.tokens || options.tolerant) ? tree : tree.body[0];
  96. if (options.tolerant) {
  97. for (i = 0, len = tree.errors.length; i < len; i += 1) {
  98. tree.errors[i] = errorToObject(tree.errors[i]);
  99. }
  100. }
  101. actual = JSON.stringify(tree, adjustRegexLiteral, 4);
  102. // Only to ensure that there is no error when using string object.
  103. esprima.parse(new StringObject(code), options);
  104. } catch (e) {
  105. throw new NotMatchingError(expected, e.toString());
  106. }
  107. if (expected !== actual) {
  108. throw new NotMatchingError(expected, actual);
  109. }
  110. function filter(key, value) {
  111. if (key === 'value' && value instanceof RegExp) {
  112. value = value.toString();
  113. }
  114. return (key === 'loc' || key === 'range') ? undefined : value;
  115. }
  116. if (options.tolerant) {
  117. return;
  118. }
  119. // Check again without any location info.
  120. options.range = false;
  121. options.loc = false;
  122. expected = JSON.stringify(syntax, filter, 4);
  123. try {
  124. tree = esprima.parse(code, options);
  125. tree = (options.comment || options.tokens) ? tree : tree.body[0];
  126. if (options.tolerant) {
  127. for (i = 0, len = tree.errors.length; i < len; i += 1) {
  128. tree.errors[i] = errorToObject(tree.errors[i]);
  129. }
  130. }
  131. actual = JSON.stringify(tree, filter, 4);
  132. } catch (e) {
  133. throw new NotMatchingError(expected, e.toString());
  134. }
  135. if (expected !== actual) {
  136. throw new NotMatchingError(expected, actual);
  137. }
  138. }
  139. function testTokenize(esprima, code, tokens) {
  140. 'use strict';
  141. var options, expected, actual, tree;
  142. options = {
  143. comment: true,
  144. tolerant: true,
  145. loc: true,
  146. range: true
  147. };
  148. expected = JSON.stringify(tokens, null, 4);
  149. try {
  150. tree = esprima.tokenize(code, options);
  151. actual = JSON.stringify(tree, null, 4);
  152. } catch (e) {
  153. throw new NotMatchingError(expected, e.toString());
  154. }
  155. if (expected !== actual) {
  156. throw new NotMatchingError(expected, actual);
  157. }
  158. }
  159. function testError(esprima, code, exception) {
  160. 'use strict';
  161. var i, options, expected, actual, err, handleInvalidRegexFlag, tokenize;
  162. // Different parsing options should give the same error.
  163. options = [
  164. {},
  165. { comment: true },
  166. { raw: true },
  167. { raw: true, comment: true }
  168. ];
  169. // If handleInvalidRegexFlag is true, an invalid flag in a regular expression
  170. // will throw an exception. In some old version V8, this is not the case
  171. // and hence handleInvalidRegexFlag is false.
  172. handleInvalidRegexFlag = false;
  173. try {
  174. 'test'.match(new RegExp('[a-z]', 'x'));
  175. } catch (e) {
  176. handleInvalidRegexFlag = true;
  177. }
  178. exception.description = exception.message.replace(/Error: Line [0-9]+: /, '');
  179. if (exception.tokenize) {
  180. tokenize = true;
  181. exception.tokenize = undefined;
  182. }
  183. expected = JSON.stringify(exception);
  184. for (i = 0; i < options.length; i += 1) {
  185. try {
  186. if (tokenize) {
  187. esprima.tokenize(code, options[i])
  188. } else {
  189. esprima.parse(code, options[i]);
  190. }
  191. } catch (e) {
  192. err = errorToObject(e);
  193. err.description = e.description;
  194. actual = JSON.stringify(err);
  195. }
  196. if (expected !== actual) {
  197. // Compensate for old V8 which does not handle invalid flag.
  198. if (exception.message.indexOf('Invalid regular expression') > 0) {
  199. if (typeof actual === 'undefined' && !handleInvalidRegexFlag) {
  200. return;
  201. }
  202. }
  203. throw new NotMatchingError(expected, actual);
  204. }
  205. }
  206. }
  207. function testAPI(esprima, code, result) {
  208. 'use strict';
  209. var expected, res, actual;
  210. expected = JSON.stringify(result.result, null, 4);
  211. try {
  212. if (typeof result.property !== 'undefined') {
  213. res = esprima[result.property];
  214. } else {
  215. res = esprima[result.call].apply(esprima, result.args);
  216. }
  217. actual = JSON.stringify(res, adjustRegexLiteral, 4);
  218. } catch (e) {
  219. throw new NotMatchingError(expected, e.toString());
  220. }
  221. if (expected !== actual) {
  222. throw new NotMatchingError(expected, actual);
  223. }
  224. }
  225. function runTest(esprima, code, result) {
  226. 'use strict';
  227. if (result.hasOwnProperty('lineNumber')) {
  228. testError(esprima, code, result);
  229. } else if (result.hasOwnProperty('result')) {
  230. testAPI(esprima, code, result);
  231. } else if (result instanceof Array) {
  232. testTokenize(esprima, code, result);
  233. } else {
  234. testParse(esprima, code, result);
  235. }
  236. }
  237. if (typeof window !== 'undefined') {
  238. // Run all tests in a browser environment.
  239. runTests = function () {
  240. 'use strict';
  241. var total = 0,
  242. failures = 0,
  243. category,
  244. fixture,
  245. source,
  246. tick,
  247. expected,
  248. index,
  249. len;
  250. function setText(el, str) {
  251. if (typeof el.innerText === 'string') {
  252. el.innerText = str;
  253. } else {
  254. el.textContent = str;
  255. }
  256. }
  257. function startCategory(category) {
  258. var report, e;
  259. report = document.getElementById('report');
  260. e = document.createElement('h4');
  261. setText(e, category);
  262. report.appendChild(e);
  263. }
  264. function reportSuccess(code) {
  265. var report, e;
  266. report = document.getElementById('report');
  267. e = document.createElement('pre');
  268. e.setAttribute('class', 'code');
  269. setText(e, code);
  270. report.appendChild(e);
  271. }
  272. function reportFailure(code, expected, actual) {
  273. var report, e;
  274. report = document.getElementById('report');
  275. e = document.createElement('p');
  276. setText(e, 'Code:');
  277. report.appendChild(e);
  278. e = document.createElement('pre');
  279. e.setAttribute('class', 'code');
  280. setText(e, code);
  281. report.appendChild(e);
  282. e = document.createElement('p');
  283. setText(e, 'Expected');
  284. report.appendChild(e);
  285. e = document.createElement('pre');
  286. e.setAttribute('class', 'expected');
  287. setText(e, expected);
  288. report.appendChild(e);
  289. e = document.createElement('p');
  290. setText(e, 'Actual');
  291. report.appendChild(e);
  292. e = document.createElement('pre');
  293. e.setAttribute('class', 'actual');
  294. setText(e, actual);
  295. report.appendChild(e);
  296. }
  297. setText(document.getElementById('version'), esprima.version);
  298. tick = new Date();
  299. for (category in testFixture) {
  300. if (testFixture.hasOwnProperty(category)) {
  301. startCategory(category);
  302. fixture = testFixture[category];
  303. for (source in fixture) {
  304. if (fixture.hasOwnProperty(source)) {
  305. expected = fixture[source];
  306. total += 1;
  307. try {
  308. runTest(esprima, source, expected);
  309. reportSuccess(source, JSON.stringify(expected, null, 4));
  310. } catch (e) {
  311. failures += 1;
  312. reportFailure(source, e.expected, e.actual);
  313. }
  314. }
  315. }
  316. }
  317. }
  318. tick = (new Date()) - tick;
  319. if (failures > 0) {
  320. document.getElementById('status').className = 'alert-box alert';
  321. setText(document.getElementById('status'), total + ' tests. ' +
  322. 'Failures: ' + failures + '. ' + tick + ' ms.');
  323. } else {
  324. document.getElementById('status').className = 'alert-box success';
  325. setText(document.getElementById('status'), total + ' tests. ' +
  326. 'No failure. ' + tick + ' ms.');
  327. }
  328. };
  329. } else {
  330. (function () {
  331. 'use strict';
  332. var esprima = require('../esprima'),
  333. vm = require('vm'),
  334. fs = require('fs'),
  335. diff = require('json-diff').diffString,
  336. total = 0,
  337. failures = [],
  338. tick = new Date(),
  339. expected,
  340. header;
  341. vm.runInThisContext(fs.readFileSync(__dirname + '/test.js', 'utf-8'));
  342. vm.runInThisContext(fs.readFileSync(__dirname + '/harmonytest.js', 'utf-8'));
  343. vm.runInThisContext(fs.readFileSync(__dirname + '/fbtest.js', 'utf-8'));
  344. Object.keys(testFixture).forEach(function (category) {
  345. Object.keys(testFixture[category]).forEach(function (source) {
  346. total += 1;
  347. expected = testFixture[category][source];
  348. try {
  349. runTest(esprima, source, expected);
  350. } catch (e) {
  351. e.source = source;
  352. failures.push(e);
  353. }
  354. });
  355. });
  356. tick = (new Date()) - tick;
  357. header = total + ' tests. ' + failures.length + ' failures. ' +
  358. tick + ' ms';
  359. if (failures.length) {
  360. console.error(header);
  361. failures.forEach(function (failure) {
  362. try {
  363. var expectedObject = JSON.parse(failure.expected);
  364. var actualObject = JSON.parse(failure.actual);
  365. console.error(failure.source + ': Expected\n ' +
  366. failure.expected.split('\n').join('\n ') +
  367. '\nto match\n ' + failure.actual + '\nDiff:\n' +
  368. diff(expectedObject, actualObject));
  369. } catch (ex) {
  370. console.error(failure.source + ': Expected\n ' +
  371. failure.expected.split('\n').join('\n ') +
  372. '\nto match\n ' + failure.actual);
  373. }
  374. });
  375. } else {
  376. console.log(header);
  377. }
  378. process.exit(failures.length === 0 ? 0 : 1);
  379. }());
  380. }