testqueue.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2007 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 Generic queue for writing unit tests.
  16. */
  17. goog.setTestOnly('goog.testing.TestQueue');
  18. goog.provide('goog.testing.TestQueue');
  19. /**
  20. * Generic queue for writing unit tests
  21. * @constructor
  22. */
  23. goog.testing.TestQueue = function() {
  24. /**
  25. * Events that have accumulated
  26. * @type {Array<Object>}
  27. * @private
  28. */
  29. this.events_ = [];
  30. };
  31. /**
  32. * Adds a new event onto the queue.
  33. * @param {Object} event The event to queue.
  34. */
  35. goog.testing.TestQueue.prototype.enqueue = function(event) {
  36. this.events_.push(event);
  37. };
  38. /**
  39. * Returns whether the queue is empty.
  40. * @return {boolean} Whether the queue is empty.
  41. */
  42. goog.testing.TestQueue.prototype.isEmpty = function() {
  43. return this.events_.length == 0;
  44. };
  45. /**
  46. * Gets the next event from the queue. Throws an exception if the queue is
  47. * empty.
  48. * @param {string=} opt_comment Comment if the queue is empty.
  49. * @return {Object} The next event from the queue.
  50. */
  51. goog.testing.TestQueue.prototype.dequeue = function(opt_comment) {
  52. if (this.isEmpty()) {
  53. throw Error('Handler is empty: ' + opt_comment);
  54. }
  55. return this.events_.shift();
  56. };