source_test.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2010 The Closure Library Authors. All Rights Reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS-IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Unit test for source."""
  17. __author__ = 'nnaze@google.com (Nathan Naze)'
  18. import unittest
  19. import source
  20. class SourceTestCase(unittest.TestCase):
  21. """Unit test for source. Tests the parser on a known source input."""
  22. def testSourceScan(self):
  23. test_source = source.Source(_TEST_SOURCE)
  24. self.assertEqual(set(['foo', 'foo.test']),
  25. test_source.provides)
  26. self.assertEqual(set(['goog.dom', 'goog.events.EventType']),
  27. test_source.requires)
  28. self.assertFalse(test_source.is_goog_module)
  29. def testSourceScanBase(self):
  30. test_source = source.Source(_TEST_BASE_SOURCE)
  31. self.assertEqual(set(['goog']),
  32. test_source.provides)
  33. self.assertEqual(test_source.requires, set())
  34. self.assertFalse(test_source.is_goog_module)
  35. def testSourceScanBadBase(self):
  36. def MakeSource():
  37. source.Source(_TEST_BAD_BASE_SOURCE)
  38. self.assertRaises(Exception, MakeSource)
  39. def testSourceScanGoogModule(self):
  40. test_source = source.Source(_TEST_MODULE_SOURCE)
  41. self.assertEqual(set(['foo']),
  42. test_source.provides)
  43. self.assertEqual(set(['bar']),
  44. test_source.requires)
  45. self.assertTrue(test_source.is_goog_module)
  46. def testSourceScanModuleAlias(self):
  47. test_source = source.Source(_TEST_MODULE_ALIAS_SOURCE)
  48. self.assertEqual(set(['goog.dom', 'goog.events']), test_source.requires)
  49. self.assertTrue(test_source.is_goog_module)
  50. def testStripComments(self):
  51. self.assertEquals(
  52. '\nvar foo = function() {}',
  53. source.Source._StripComments((
  54. '/* This is\n'
  55. ' a comment split\n'
  56. ' over multiple lines\n'
  57. '*/\n'
  58. 'var foo = function() {}')))
  59. def testGoogStatementsInComments(self):
  60. test_source = source.Source(_TEST_COMMENT_SOURCE)
  61. self.assertEqual(set(['foo']),
  62. test_source.provides)
  63. self.assertEqual(set(['goog.events.EventType']),
  64. test_source.requires)
  65. self.assertFalse(test_source.is_goog_module)
  66. def testHasProvideGoog(self):
  67. self.assertTrue(source.Source._HasProvideGoogFlag(_TEST_BASE_SOURCE))
  68. self.assertTrue(source.Source._HasProvideGoogFlag(_TEST_BAD_BASE_SOURCE))
  69. self.assertFalse(source.Source._HasProvideGoogFlag(_TEST_COMMENT_SOURCE))
  70. _TEST_MODULE_ALIAS_SOURCE = """
  71. goog.module('foo');
  72. const {createDom: domCreator} = goog.require('goog.dom');
  73. const {listen} = goog.require('goog.events');
  74. """
  75. _TEST_MODULE_SOURCE = """
  76. goog.module('foo');
  77. var b = goog.require('bar');
  78. """
  79. _TEST_SOURCE = """// Fake copyright notice
  80. /** Very important comment. */
  81. goog.provide('foo');
  82. goog.provide('foo.test');
  83. goog.require('goog.dom');
  84. goog.require('goog.events.EventType');
  85. function foo() {
  86. // Set bar to seventeen to increase performance.
  87. this.bar = 17;
  88. }
  89. """
  90. _TEST_COMMENT_SOURCE = """// Fake copyright notice
  91. goog.provide('foo');
  92. /*
  93. goog.provide('foo.test');
  94. */
  95. /*
  96. goog.require('goog.dom');
  97. */
  98. // goog.require('goog.dom');
  99. goog.require('goog.events.EventType');
  100. function bar() {
  101. this.baz = 55;
  102. }
  103. """
  104. _TEST_BASE_SOURCE = """
  105. /**
  106. * @fileoverview The base file.
  107. * @provideGoog
  108. */
  109. var goog = goog || {};
  110. """
  111. _TEST_BAD_BASE_SOURCE = """
  112. /**
  113. * @fileoverview The base file.
  114. * @provideGoog
  115. */
  116. goog.provide('goog');
  117. """
  118. if __name__ == '__main__':
  119. unittest.main()