singleton.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2009 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 This module simplifies testing code which uses stateful
  16. * singletons. {@code goog.testing.singleton.reset} resets all instances, so
  17. * next time when {@code getInstance} is called, a new instance is created.
  18. * It's recommended to reset the singletons in {@code tearDown} to prevent
  19. * interference between subsequent tests.
  20. *
  21. * The {@code goog.testing.singleton} functions expect that the goog.DEBUG flag
  22. * is enabled, and the tests are either uncompiled or compiled without renaming.
  23. *
  24. */
  25. goog.setTestOnly('goog.testing.singleton');
  26. goog.provide('goog.testing.singleton');
  27. /**
  28. * Deletes all singleton instances, so {@code getInstance} will return a new
  29. * instance on next call.
  30. */
  31. goog.testing.singleton.reset = function() {
  32. var singletons = goog.getObjectByName('goog.instantiatedSingletons_');
  33. var ctor;
  34. while (ctor = singletons.pop()) {
  35. delete ctor.instance_;
  36. }
  37. };
  38. /**
  39. * @deprecated Please use {@code goog.addSingletonGetter}.
  40. */
  41. goog.testing.singleton.addSingletonGetter = goog.addSingletonGetter;