aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/testing/mockrandom.js
blob: a29df1b1fe0bc809084ce0eab8cd946c0a425a22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview MockRandom provides a mechanism for specifying a stream of
 * numbers to expect from calls to Math.random().
 *
 */

goog.provide('goog.testing.MockRandom');

goog.require('goog.Disposable');



/**
 * Class for unit testing code that uses Math.random.
 *
 * @param {Array.<number>} sequence The sequence of numbers to return.
 * @param {boolean=} opt_install Whether to install the MockRandom at
 *     construction time.
 * @extends {goog.Disposable}
 * @constructor
 */
goog.testing.MockRandom = function(sequence, opt_install) {
  goog.Disposable.call(this);

  /**
   * The sequence of numbers to be returned by calls to random()
   * @type {Array.<number>}
   * @private
   */
  this.sequence_ = sequence || [];

  /**
   * The original Math.random function.
   * @type {function(): number}
   * @private
   */
  this.mathRandom_ = Math.random;

  if (opt_install) {
    this.install();
  }
};
goog.inherits(goog.testing.MockRandom, goog.Disposable);


/**
 * Whether this MockRandom has been installed.
 * @type {boolean}
 * @private
 */
goog.testing.MockRandom.prototype.installed_;


/**
 * Installs this MockRandom as the system number generator.
 */
goog.testing.MockRandom.prototype.install = function() {
  if (!this.installed_) {
    Math.random = goog.bind(this.random, this);
    this.installed_ = true;
  }
};


/**
 * @return {number} The next number in the sequence. If there are no more values
 *     left, this will return a random number.
 */
goog.testing.MockRandom.prototype.random = function() {
  return this.hasMoreValues() ? this.sequence_.shift() : this.mathRandom_();
};


/**
 * @return {boolean} Whether there are more numbers left in the sequence.
 */
goog.testing.MockRandom.prototype.hasMoreValues = function() {
  return this.sequence_.length > 0;
};


/**
 * Injects new numbers into the beginning of the sequence.
 * @param {Array.<number>|number} values Number or array of numbers to inject.
 */
goog.testing.MockRandom.prototype.inject = function(values) {
  if (goog.isArray(values)) {
    this.sequence_ = values.concat(this.sequence_);
  } else {
    this.sequence_.splice(0, 0, values);
  }
};


/**
 * Uninstalls the MockRandom.
 */
goog.testing.MockRandom.prototype.uninstall = function() {
  if (this.installed_) {
    Math.random = this.mathRandom_;
    this.installed_ = false;
  }
};


/** @override */
goog.testing.MockRandom.prototype.disposeInternal = function() {
  this.uninstall();
  delete this.sequence_;
  delete this.mathRandom_;
  goog.testing.MockRandom.superClass_.disposeInternal.call(this);
};