Debian Codegraph @

closure-library/closure/goog/testing/mockrandom_test.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 goog.provide('goog.testing.MockRandomTest');
16 goog.setTestOnly('goog.testing.MockRandomTest');
17
18 goog.require('goog.testing.MockRandom');
19 goog.require('goog.testing.jsunit');
20
21 function testMockRandomInstall() {
22 var random = new goog.testing.MockRandom([]);
23 var originalRandom = Math.random;
24
25 assertFalse(!!random.installed_);
26
27 random.install();
28 assertTrue(random.installed_);
29 assertNotEquals(Math.random, originalRandom);
30
31 random.uninstall();
32 assertFalse(random.installed_);
33 assertEquals(originalRandom, Math.random);
34 }
35
36 function testMockRandomRandom() {
37 var random = new goog.testing.MockRandom([], true);
38
39 assertFalse(random.hasMoreValues());
40
41 random.inject(2);
42 assertTrue(random.hasMoreValues());
43 assertEquals(2, Math.random());
44
45 random.inject([1, 2, 3]);
46 assertTrue(random.hasMoreValues());
47 assertEquals(1, Math.random());
48 assertEquals(2, Math.random());
49 assertEquals(3, Math.random());
50 assertFalse(random.hasMoreValues());
51 assertNotUndefined(Math.random());
52 }
53
54 function testRandomStrictlyFromSequence() {
55 var random = new goog.testing.MockRandom([], /* install */ true);
56 random.setStrictlyFromSequence(true);
57 assertFalse(random.hasMoreValues());
58 assertThrows(function() { Math.random(); });
59
60 random.inject(3);
61 assertTrue(random.hasMoreValues());
62 assertNotThrows(function() { Math.random(); });
63
64 random.setStrictlyFromSequence(false);
65 assertFalse(random.hasMoreValues());
66 assertNotThrows(function() { Math.random(); });
67 }