base_module_test.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2006 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 Unit tests for Closure's base.js's goog.module support.
  16. */
  17. goog.module('goog.baseModuleTest');
  18. goog.setTestOnly('goog.baseModuleTest');
  19. // Used to test dynamic loading works, see testRequire*
  20. var Timer = goog.require('goog.Timer');
  21. var Replacer = goog.require('goog.testing.PropertyReplacer');
  22. var jsunit = goog.require('goog.testing.jsunit');
  23. var testSuite = goog.require('goog.testing.testSuite');
  24. var testModule = goog.require('goog.test_module');
  25. var stubs = new Replacer();
  26. function assertProvideFails(namespace) {
  27. assertThrows(
  28. 'goog.provide(' + namespace + ') should have failed',
  29. goog.partial(goog.provide, namespace));
  30. }
  31. function assertModuleFails(namespace) {
  32. assertThrows(
  33. 'goog.module(' + namespace + ') should have failed',
  34. goog.partial(goog.module, namespace));
  35. }
  36. function assertLoadModule(msg, moduleDef) {
  37. assertNotThrows(msg, goog.partial(goog.loadModule, moduleDef));
  38. }
  39. testSuite({
  40. teardown: function() { stubs.reset(); },
  41. testModuleDecl: function() {
  42. // assert that goog.module doesn't modify the global namespace
  43. assertUndefined(
  44. 'module failed to protect global namespace: ' +
  45. 'goog.baseModuleTest',
  46. goog.baseModuleTest);
  47. },
  48. testModuleScoping: function() {
  49. // assert test functions are not exported to the global namespace
  50. assertNotUndefined('module failed: testModule', testModule);
  51. assertFalse(
  52. 'module failed: testModule',
  53. goog.isFunction(goog.global.testModuleScoping));
  54. },
  55. testProvideStrictness1: function() {
  56. assertModuleFails('goog.xy'); // not in goog.loadModule
  57. assertProvideFails('goog.baseModuleTest'); // this file.
  58. },
  59. testProvideStrictness2: function() {
  60. // goog.module "provides" a namespace
  61. assertTrue(goog.isProvided_('goog.baseModuleTest'));
  62. },
  63. testExportSymbol: function() {
  64. // Assert that export symbol works from within a goog.module.
  65. var date = new Date();
  66. assertTrue(typeof nodots == 'undefined');
  67. goog.exportSymbol('nodots', date);
  68. assertEquals(date, nodots); // globals are accessible from within a module.
  69. nodots = undefined;
  70. },
  71. testLoadModule: function() {
  72. assertLoadModule(
  73. 'Loading a module that exports a typedef should succeed',
  74. 'goog.module(\'goog.test_module_typedef\');' +
  75. 'var typedef;' +
  76. 'exports = typedef;');
  77. },
  78. //=== tests for Require logic ===
  79. testLegacyRequire: function() {
  80. // goog.Timer is a legacy module loaded above
  81. assertNotUndefined('goog.Timer should be available', goog.Timer);
  82. // Verify that a legacy module can be aliases with goog.require
  83. assertTrue(
  84. 'Timer should be the goog.Timer namespace object',
  85. goog.Timer === Timer);
  86. // and its dependencies
  87. assertNotUndefined(
  88. 'goog.events.EventTarget should be available',
  89. /** @suppress {missingRequire} */ goog.events.EventTarget);
  90. },
  91. testRequireModule: function() {
  92. assertEquals(
  93. 'module failed to export legacy namespace: ' +
  94. 'goog.test_module',
  95. testModule, goog.test_module);
  96. assertUndefined(
  97. 'module failed to protect global namespace: ' +
  98. 'goog.test_module_dep',
  99. goog.test_module_dep);
  100. // The test module is available under its alias
  101. assertNotUndefined('testModule is loaded', testModule);
  102. assertTrue('module failed: testModule', goog.isFunction(testModule));
  103. // Test that any escaping of </script> in test files is correct. Escape the
  104. // / in </script> here so that any such code does not affect it here.
  105. assertEquals('<\/script>', testModule.CLOSING_SCRIPT_TAG);
  106. },
  107. testThisInModule: goog.bind(
  108. function() {
  109. // IE9 and below don't support "strict" mode and "undefined" gets
  110. // coersed to "window".
  111. if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher('10')) {
  112. assertEquals(this, undefined);
  113. } else {
  114. assertEquals(this, goog.global);
  115. }
  116. },
  117. this)
  118. });