Debian Codegraph @

closure-library/closure/goog/testing/mockrandom.js

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