mockrandom.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2008 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 MockRandom provides a mechanism for specifying a stream of
  16. * numbers to expect from calls to Math.random().
  17. *
  18. */
  19. goog.setTestOnly('goog.testing.MockRandom');
  20. goog.provide('goog.testing.MockRandom');
  21. goog.require('goog.Disposable');
  22. /**
  23. * Class for unit testing code that uses Math.random.
  24. *
  25. * @param {Array<number>} sequence The sequence of numbers to return.
  26. * @param {boolean=} opt_install Whether to install the MockRandom at
  27. * construction time.
  28. * @extends {goog.Disposable}
  29. * @constructor
  30. * @final
  31. */
  32. goog.testing.MockRandom = function(sequence, opt_install) {
  33. goog.Disposable.call(this);
  34. /**
  35. * The sequence of numbers to be returned by calls to random()
  36. * @type {!Array<number>}
  37. * @private
  38. */
  39. this.sequence_ = sequence || [];
  40. /**
  41. * The original Math.random function.
  42. * @type {function(): number}
  43. * @private
  44. */
  45. this.mathRandom_ = Math.random;
  46. /**
  47. * Whether to throw an exception when Math.random() is called when there is
  48. * nothing left in the sequence.
  49. * @type {boolean}
  50. * @private
  51. */
  52. this.strictlyFromSequence_ = false;
  53. if (opt_install) {
  54. this.install();
  55. }
  56. };
  57. goog.inherits(goog.testing.MockRandom, goog.Disposable);
  58. /**
  59. * Whether this MockRandom has been installed.
  60. * @type {boolean}
  61. * @private
  62. */
  63. goog.testing.MockRandom.prototype.installed_;
  64. /**
  65. * Installs this MockRandom as the system number generator.
  66. */
  67. goog.testing.MockRandom.prototype.install = function() {
  68. if (!this.installed_) {
  69. Math.random = goog.bind(this.random, this);
  70. this.installed_ = true;
  71. }
  72. };
  73. /**
  74. * @return {number} The next number in the sequence. If there are no more values
  75. * left, this will return a random number, unless
  76. * {@code this.strictlyFromSequence_} is true, in which case an error will
  77. * be thrown.
  78. */
  79. goog.testing.MockRandom.prototype.random = function() {
  80. if (this.hasMoreValues()) {
  81. return this.sequence_.shift();
  82. }
  83. if (this.strictlyFromSequence_) {
  84. throw new Error('No numbers left in sequence.');
  85. }
  86. return this.mathRandom_();
  87. };
  88. /**
  89. * @return {boolean} Whether there are more numbers left in the sequence.
  90. */
  91. goog.testing.MockRandom.prototype.hasMoreValues = function() {
  92. return this.sequence_.length > 0;
  93. };
  94. /**
  95. * Injects new numbers into the beginning of the sequence.
  96. * @param {!Array<number>|number} values Number or array of numbers to inject.
  97. */
  98. goog.testing.MockRandom.prototype.inject = function(values) {
  99. if (goog.isArray(values)) {
  100. this.sequence_ = values.concat(this.sequence_);
  101. } else {
  102. this.sequence_.splice(0, 0, values);
  103. }
  104. };
  105. /**
  106. * Uninstalls the MockRandom.
  107. */
  108. goog.testing.MockRandom.prototype.uninstall = function() {
  109. if (this.installed_) {
  110. Math.random = this.mathRandom_;
  111. this.installed_ = false;
  112. }
  113. };
  114. /** @override */
  115. goog.testing.MockRandom.prototype.disposeInternal = function() {
  116. this.uninstall();
  117. delete this.sequence_;
  118. delete this.mathRandom_;
  119. goog.testing.MockRandom.superClass_.disposeInternal.call(this);
  120. };
  121. /**
  122. * @param {boolean} strictlyFromSequence Whether to throw an exception when
  123. * Math.random() is called when there is nothing left in the sequence.
  124. */
  125. goog.testing.MockRandom.prototype.setStrictlyFromSequence = function(
  126. strictlyFromSequence) {
  127. this.strictlyFromSequence_ = strictlyFromSequence;
  128. };