mockrandom_test.js 2.1 KB

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