def testFunctionParse(self): functions, _ = testutil.ParseFunctionsAndComments(_FUNCTION_SCRIPT) self.assertEquals(2, len(functions)) function = functions[0] self.assertEquals(['aaa', 'bbb', 'ccc'], function.parameters) start_token = function.start_token end_token = function.end_token # First function self.assertEquals( javascripttokens.JavaScriptTokenType.FUNCTION_DECLARATION, function.start_token.type) self.assertEquals('function', start_token.string) self.assertEquals(3, start_token.line_number) self.assertEquals(0, start_token.start_index) self.assertEquals('}', end_token.string) self.assertEquals(5, end_token.line_number) self.assertEquals(0, end_token.start_index) self.assertEquals('foo', function.name) self.assertIsNone(function.doc) function = functions[1] self.assertEquals(['ddd', 'eee', 'fff'], function.parameters) start_token = function.start_token end_token = function.end_token # Second function self.assertEquals( javascripttokens.JavaScriptTokenType.FUNCTION_DECLARATION, function.start_token.type) self.assertEquals('function', start_token.string) self.assertEquals(11, start_token.line_number) self.assertEquals(10, start_token.start_index) self.assertEquals('}', end_token.string) self.assertEquals(13, end_token.line_number) self.assertEquals(0, end_token.start_index) self.assertEquals('bar', function.name) self.assertIsNotNone(function.doc) # Check function JSDoc doc = function.doc doc_tokens = tokenutil.GetTokenRange(doc.start_token, doc.end_token) comment_type = javascripttokens.JavaScriptTokenType.COMMENT comment_tokens = filter(lambda t: t.type is comment_type, doc_tokens) self.assertEquals('JSDoc comment.', tokenutil.TokensToString(comment_tokens).strip())
def _ParseComment(self, script): """Parse a script that contains one comment and return it.""" accumulator = erroraccumulator.ErrorAccumulator() _, comments = testutil.ParseFunctionsAndComments(script, accumulator) if accumulator.GetErrors(): raise TypeErrorException(accumulator.GetErrors()) self.assertEquals(1, len(comments)) return comments[0]
def DoTestFixJsDocPipeNull(self, expected, original): _, comments = testutil.ParseFunctionsAndComments( '/** @param {%s} */' % original) jstype = comments[0].GetDocFlags()[0].jstype self.error_fixer.HandleFile('unittest', None) self.error_fixer._FixJsDocPipeNull(jstype) self.assertEquals(expected, repr(jstype)) result = tokenutil.TokensToString(jstype.FirstToken()).strip('} */') self.assertEquals(expected, result)
def _ParseComment(self, script): """Parse a script that contains one comment and return it.""" _, comments = testutil.ParseFunctionsAndComments(script) self.assertEquals(1, len(comments)) return comments[0]