a zip code crypto-currency system good for red ONLY

jasmine.ts 11KB

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