generate_jsdoc_test.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2013 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 generate_jsdoc."""
  17. __author__ = 'nnaze@google.com (Nathan Naze)'
  18. import re
  19. import unittest
  20. import generate_jsdoc
  21. class InsertJsDocTestCase(unittest.TestCase):
  22. """Unit test for source. Tests the parser on a known source input."""
  23. def testMatchFirstFunction(self):
  24. match = generate_jsdoc._MatchFirstFunction(_TEST_SOURCE)
  25. self.assertNotEqual(None, match)
  26. self.assertEqual('aaa, bbb, ccc', match.group('arguments'))
  27. match = generate_jsdoc._MatchFirstFunction(_INDENTED_SOURCE)
  28. self.assertNotEqual(None, match)
  29. self.assertEqual('', match.group('arguments'))
  30. match = generate_jsdoc._MatchFirstFunction(_ODD_NEWLINES_SOURCE)
  31. self.assertEquals('goog.\nfoo.\nbar\n.baz.\nqux',
  32. match.group('identifier'))
  33. def testParseArgString(self):
  34. self.assertEquals(
  35. ['foo', 'bar', 'baz'],
  36. list(generate_jsdoc._ParseArgString('foo, bar, baz')))
  37. def testExtractFunctionBody(self):
  38. self.assertEquals(
  39. '\n // Function comments.\n return;\n',
  40. generate_jsdoc._ExtractFunctionBody(_TEST_SOURCE))
  41. self.assertEquals(
  42. '\n var bar = 3;\n return true;\n',
  43. generate_jsdoc._ExtractFunctionBody(_INDENTED_SOURCE, 2))
  44. def testContainsValueReturn(self):
  45. self.assertTrue(generate_jsdoc._ContainsReturnValue(_INDENTED_SOURCE))
  46. self.assertFalse(generate_jsdoc._ContainsReturnValue(_TEST_SOURCE))
  47. def testInsertString(self):
  48. self.assertEquals(
  49. 'abc123def',
  50. generate_jsdoc._InsertString('abcdef', '123', 3))
  51. def testInsertJsDoc(self):
  52. self.assertEquals(
  53. _EXPECTED_INDENTED_SOURCE,
  54. generate_jsdoc.InsertJsDoc(_INDENTED_SOURCE))
  55. self.assertEquals(
  56. _EXPECTED_TEST_SOURCE,
  57. generate_jsdoc.InsertJsDoc(_TEST_SOURCE))
  58. self.assertEquals(
  59. _EXPECTED_ODD_NEWLINES_SOURCE,
  60. generate_jsdoc.InsertJsDoc(_ODD_NEWLINES_SOURCE))
  61. _INDENTED_SOURCE = """\
  62. boo.foo.woo = function() {
  63. var bar = 3;
  64. return true;
  65. };
  66. """
  67. _EXPECTED_INDENTED_SOURCE = """\
  68. /**
  69. * @return
  70. */
  71. boo.foo.woo = function() {
  72. var bar = 3;
  73. return true;
  74. };
  75. """
  76. _TEST_SOURCE = """\
  77. // Random comment.
  78. goog.foo.bar = function (aaa, bbb, ccc) {
  79. // Function comments.
  80. return;
  81. };
  82. """
  83. _EXPECTED_TEST_SOURCE = """\
  84. // Random comment.
  85. /**
  86. * @param {} aaa
  87. * @param {} bbb
  88. * @param {} ccc
  89. */
  90. goog.foo.bar = function (aaa, bbb, ccc) {
  91. // Function comments.
  92. return;
  93. };
  94. """
  95. _ODD_NEWLINES_SOURCE = """\
  96. goog.
  97. foo.
  98. bar
  99. .baz.
  100. qux
  101. =
  102. function
  103. (aaa,
  104. bbb, ccc) {
  105. // Function comments.
  106. return;
  107. };
  108. """
  109. _EXPECTED_ODD_NEWLINES_SOURCE = """\
  110. /**
  111. * @param {} aaa
  112. * @param {} bbb
  113. * @param {} ccc
  114. */
  115. goog.
  116. foo.
  117. bar
  118. .baz.
  119. qux
  120. =
  121. function
  122. (aaa,
  123. bbb, ccc) {
  124. // Function comments.
  125. return;
  126. };
  127. """
  128. if __name__ == '__main__':
  129. unittest.main()