Debian Codegraph @

closure-library/closure/bin/labs/code/generate_jsdoc_test.py

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