testsuiteadapter.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2013 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Test adapter for testing Closure Promises against the
  16. * Promises/A+ Compliance Test Suite, which is implemented as a Node.js module.
  17. *
  18. * This test suite adapter may not be run in Node.js directly, but must first be
  19. * compiled with the Closure Compiler to pull in the required dependencies.
  20. *
  21. * @see https://npmjs.org/package/promises-aplus-tests
  22. */
  23. goog.provide('goog.promise.testSuiteAdapter');
  24. goog.require('goog.Promise');
  25. goog.setTestOnly('goog.promise.testSuiteAdapter');
  26. var promisesAplusTests = /** @type {function(!Object, function(*))} */ (
  27. require('promises_aplus_tests'));
  28. /**
  29. * Adapter for specifying Promise-creating functions to the Promises test suite.
  30. * @const
  31. */
  32. goog.promise.testSuiteAdapter = {
  33. /** @type {function(*): !goog.Promise} */
  34. 'resolved': goog.Promise.resolve,
  35. /** @type {function(*): !goog.Promise} */
  36. 'rejected': goog.Promise.reject,
  37. /** @return {!Object} */
  38. 'deferred': function() {
  39. var promiseObj = {};
  40. promiseObj['promise'] = new goog.Promise(function(resolve, reject) {
  41. promiseObj['resolve'] = resolve;
  42. promiseObj['reject'] = reject;
  43. });
  44. return promiseObj;
  45. }
  46. };
  47. // Node.js defines setTimeout globally, but Closure relies on finding it
  48. // defined on goog.global.
  49. goog.exportSymbol('setTimeout', setTimeout);
  50. // Rethrowing an error to the global scope kills Node immediately. Suppress
  51. // error rethrowing for running this test suite.
  52. goog.Promise.setUnhandledRejectionHandler(goog.nullFunction);
  53. // Run the tests, exiting with a failure code if any of the tests fail.
  54. promisesAplusTests(goog.promise.testSuiteAdapter, function(err) {
  55. if (err) {
  56. process.exit(1);
  57. }
  58. });