a zip code crypto-currency system good for red ONLY

jasmine-patch.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * @license
  3. * Copyright Google Inc. All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.io/license
  7. */
  8. (function (global, factory) {
  9. typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
  10. typeof define === 'function' && define.amd ? define(factory) :
  11. (factory());
  12. }(this, (function () { 'use strict';
  13. /**
  14. * @license
  15. * Copyright Google Inc. All Rights Reserved.
  16. *
  17. * Use of this source code is governed by an MIT-style license that can be
  18. * found in the LICENSE file at https://angular.io/license
  19. */
  20. (function () {
  21. var __extends = function (d, b) {
  22. for (var p in b)
  23. if (b.hasOwnProperty(p))
  24. d[p] = b[p];
  25. function __() {
  26. this.constructor = d;
  27. }
  28. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  29. };
  30. var _global = typeof window !== 'undefined' && window || typeof self !== 'undefined' && self || global;
  31. // Patch jasmine's describe/it/beforeEach/afterEach functions so test code always runs
  32. // in a testZone (ProxyZone). (See: angular/zone.js#91 & angular/angular#10503)
  33. if (!Zone)
  34. throw new Error('Missing: zone.js');
  35. if (typeof jasmine == 'undefined')
  36. throw new Error('Missing: jasmine.js');
  37. if (jasmine['__zone_patch__'])
  38. throw new Error("'jasmine' has already been patched with 'Zone'.");
  39. jasmine['__zone_patch__'] = true;
  40. var SyncTestZoneSpec = Zone['SyncTestZoneSpec'];
  41. var ProxyZoneSpec = Zone['ProxyZoneSpec'];
  42. if (!SyncTestZoneSpec)
  43. throw new Error('Missing: SyncTestZoneSpec');
  44. if (!ProxyZoneSpec)
  45. throw new Error('Missing: ProxyZoneSpec');
  46. var ambientZone = Zone.current;
  47. // Create a synchronous-only zone in which to run `describe` blocks in order to raise an
  48. // error if any asynchronous operations are attempted inside of a `describe` but outside of
  49. // a `beforeEach` or `it`.
  50. var syncZone = ambientZone.fork(new SyncTestZoneSpec('jasmine.describe'));
  51. var symbol = Zone.__symbol__;
  52. // whether patch jasmine clock when in fakeAsync
  53. var enableClockPatch = _global[symbol('fakeAsyncPatchLock')] === true;
  54. // Monkey patch all of the jasmine DSL so that each function runs in appropriate zone.
  55. var jasmineEnv = jasmine.getEnv();
  56. ['describe', 'xdescribe', 'fdescribe'].forEach(function (methodName) {
  57. var originalJasmineFn = jasmineEnv[methodName];
  58. jasmineEnv[methodName] = function (description, specDefinitions) {
  59. return originalJasmineFn.call(this, description, wrapDescribeInZone(specDefinitions));
  60. };
  61. });
  62. ['it', 'xit', 'fit'].forEach(function (methodName) {
  63. var originalJasmineFn = jasmineEnv[methodName];
  64. jasmineEnv[symbol(methodName)] = originalJasmineFn;
  65. jasmineEnv[methodName] = function (description, specDefinitions, timeout) {
  66. arguments[1] = wrapTestInZone(specDefinitions);
  67. return originalJasmineFn.apply(this, arguments);
  68. };
  69. });
  70. ['beforeEach', 'afterEach'].forEach(function (methodName) {
  71. var originalJasmineFn = jasmineEnv[methodName];
  72. jasmineEnv[symbol(methodName)] = originalJasmineFn;
  73. jasmineEnv[methodName] = function (specDefinitions, timeout) {
  74. arguments[0] = wrapTestInZone(specDefinitions);
  75. return originalJasmineFn.apply(this, arguments);
  76. };
  77. });
  78. // need to patch jasmine.clock().mockDate and jasmine.clock().tick() so
  79. // they can work properly in FakeAsyncTest
  80. var originalClockFn = (jasmine[symbol('clock')] = jasmine['clock']);
  81. jasmine['clock'] = function () {
  82. var clock = originalClockFn.apply(this, arguments);
  83. if (!clock[symbol('patched')]) {
  84. clock[symbol('patched')] = symbol('patched');
  85. var originalTick_1 = (clock[symbol('tick')] = clock.tick);
  86. clock.tick = function () {
  87. var fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
  88. if (fakeAsyncZoneSpec) {
  89. return fakeAsyncZoneSpec.tick.apply(fakeAsyncZoneSpec, arguments);
  90. }
  91. return originalTick_1.apply(this, arguments);
  92. };
  93. var originalMockDate_1 = (clock[symbol('mockDate')] = clock.mockDate);
  94. clock.mockDate = function () {
  95. var fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
  96. if (fakeAsyncZoneSpec) {
  97. var dateTime = arguments.length > 0 ? arguments[0] : new Date();
  98. return fakeAsyncZoneSpec.setCurrentRealTime.apply(fakeAsyncZoneSpec, dateTime && typeof dateTime.getTime === 'function' ? [dateTime.getTime()] :
  99. arguments);
  100. }
  101. return originalMockDate_1.apply(this, arguments);
  102. };
  103. // for auto go into fakeAsync feature, we need the flag to enable it
  104. if (enableClockPatch) {
  105. ['install', 'uninstall'].forEach(function (methodName) {
  106. var originalClockFn = (clock[symbol(methodName)] = clock[methodName]);
  107. clock[methodName] = function () {
  108. var FakeAsyncTestZoneSpec = Zone['FakeAsyncTestZoneSpec'];
  109. if (FakeAsyncTestZoneSpec) {
  110. jasmine[symbol('clockInstalled')] = 'install' === methodName;
  111. return;
  112. }
  113. return originalClockFn.apply(this, arguments);
  114. };
  115. });
  116. }
  117. }
  118. return clock;
  119. };
  120. /**
  121. * Gets a function wrapping the body of a Jasmine `describe` block to execute in a
  122. * synchronous-only zone.
  123. */
  124. function wrapDescribeInZone(describeBody) {
  125. return function () {
  126. return syncZone.run(describeBody, this, arguments);
  127. };
  128. }
  129. function runInTestZone(testBody, applyThis, queueRunner, done) {
  130. var isClockInstalled = !!jasmine[symbol('clockInstalled')];
  131. var testProxyZoneSpec = queueRunner.testProxyZoneSpec;
  132. var testProxyZone = queueRunner.testProxyZone;
  133. if (isClockInstalled && enableClockPatch) {
  134. // auto run a fakeAsync
  135. var fakeAsyncModule = Zone[Zone.__symbol__('fakeAsyncTest')];
  136. if (fakeAsyncModule && typeof fakeAsyncModule.fakeAsync === 'function') {
  137. testBody = fakeAsyncModule.fakeAsync(testBody);
  138. }
  139. }
  140. if (done) {
  141. return testProxyZone.run(testBody, applyThis, [done]);
  142. }
  143. else {
  144. return testProxyZone.run(testBody, applyThis);
  145. }
  146. }
  147. /**
  148. * Gets a function wrapping the body of a Jasmine `it/beforeEach/afterEach` block to
  149. * execute in a ProxyZone zone.
  150. * This will run in `testProxyZone`. The `testProxyZone` will be reset by the `ZoneQueueRunner`
  151. */
  152. function wrapTestInZone(testBody) {
  153. // The `done` callback is only passed through if the function expects at least one argument.
  154. // Note we have to make a function with correct number of arguments, otherwise jasmine will
  155. // think that all functions are sync or async.
  156. return (testBody && (testBody.length ? function (done) {
  157. return runInTestZone(testBody, this, this.queueRunner, done);
  158. } : function () {
  159. return runInTestZone(testBody, this, this.queueRunner);
  160. }));
  161. }
  162. var QueueRunner = jasmine.QueueRunner;
  163. jasmine.QueueRunner = (function (_super) {
  164. __extends(ZoneQueueRunner, _super);
  165. function ZoneQueueRunner(attrs) {
  166. var _this = this;
  167. attrs.onComplete = (function (fn) { return function () {
  168. // All functions are done, clear the test zone.
  169. _this.testProxyZone = null;
  170. _this.testProxyZoneSpec = null;
  171. ambientZone.scheduleMicroTask('jasmine.onComplete', fn);
  172. }; })(attrs.onComplete);
  173. var nativeSetTimeout = _global['__zone_symbol__setTimeout'];
  174. var nativeClearTimeout = _global['__zone_symbol__clearTimeout'];
  175. if (nativeSetTimeout) {
  176. // should run setTimeout inside jasmine outside of zone
  177. attrs.timeout = {
  178. setTimeout: nativeSetTimeout ? nativeSetTimeout : _global.setTimeout,
  179. clearTimeout: nativeClearTimeout ? nativeClearTimeout : _global.clearTimeout
  180. };
  181. }
  182. // create a userContext to hold the queueRunner itself
  183. // so we can access the testProxy in it/xit/beforeEach ...
  184. if (jasmine.UserContext) {
  185. if (!attrs.userContext) {
  186. attrs.userContext = new jasmine.UserContext();
  187. }
  188. attrs.userContext.queueRunner = this;
  189. }
  190. else {
  191. if (!attrs.userContext) {
  192. attrs.userContext = {};
  193. }
  194. attrs.userContext.queueRunner = this;
  195. }
  196. // patch attrs.onException
  197. var onException = attrs.onException;
  198. attrs.onException = function (error) {
  199. if (error &&
  200. error.message ===
  201. 'Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.') {
  202. // jasmine timeout, we can make the error message more
  203. // reasonable to tell what tasks are pending
  204. var proxyZoneSpec = this && this.testProxyZoneSpec;
  205. if (proxyZoneSpec) {
  206. var pendingTasksInfo = proxyZoneSpec.getAndClearPendingTasksInfo();
  207. error.message += pendingTasksInfo;
  208. }
  209. }
  210. if (onException) {
  211. onException.call(this, error);
  212. }
  213. };
  214. _super.call(this, attrs);
  215. }
  216. ZoneQueueRunner.prototype.execute = function () {
  217. var _this = this;
  218. var zone = Zone.current;
  219. var isChildOfAmbientZone = false;
  220. while (zone) {
  221. if (zone === ambientZone) {
  222. isChildOfAmbientZone = true;
  223. break;
  224. }
  225. zone = zone.parent;
  226. }
  227. if (!isChildOfAmbientZone)
  228. throw new Error('Unexpected Zone: ' + Zone.current.name);
  229. // This is the zone which will be used for running individual tests.
  230. // It will be a proxy zone, so that the tests function can retroactively install
  231. // different zones.
  232. // Example:
  233. // - In beforeEach() do childZone = Zone.current.fork(...);
  234. // - In it() try to do fakeAsync(). The issue is that because the beforeEach forked the
  235. // zone outside of fakeAsync it will be able to escape the fakeAsync rules.
  236. // - Because ProxyZone is parent fo `childZone` fakeAsync can retroactively add
  237. // fakeAsync behavior to the childZone.
  238. this.testProxyZoneSpec = new ProxyZoneSpec();
  239. this.testProxyZone = ambientZone.fork(this.testProxyZoneSpec);
  240. if (!Zone.currentTask) {
  241. // if we are not running in a task then if someone would register a
  242. // element.addEventListener and then calling element.click() the
  243. // addEventListener callback would think that it is the top most task and would
  244. // drain the microtask queue on element.click() which would be incorrect.
  245. // For this reason we always force a task when running jasmine tests.
  246. Zone.current.scheduleMicroTask('jasmine.execute().forceTask', function () { return QueueRunner.prototype.execute.call(_this); });
  247. }
  248. else {
  249. _super.prototype.execute.call(this);
  250. }
  251. };
  252. return ZoneQueueRunner;
  253. })(QueueRunner);
  254. })();
  255. })));