Commit 68411116 authored by rdevlin.cronin's avatar rdevlin.cronin Committed by Commit bot

[Extension API Extern Generation] Fix a few bugs in extern generation

Properly handle the case of an "object" (one deliberately left undefined in the
idl/json), and fix an off-by-one error in the comment wrapping.
Add tests for each.

BUG=469920

Review URL: https://codereview.chromium.org/1062573004

Cr-Commit-Position: refs/heads/master@{#324063}
parent 155d7e5b
...@@ -132,7 +132,7 @@ class Code(object): ...@@ -132,7 +132,7 @@ class Code(object):
# First line has the full maximum length. # First line has the full maximum length.
if not new_line and self._code: if not new_line and self._code:
max_len = self._comment_length - len(self._code[-1].value) - 1 max_len = self._comment_length - len(self._code[-1].value)
else: else:
max_len = (self._comment_length - len(''.join(self._line_prefixes)) - max_len = (self._comment_length - len(''.join(self._line_prefixes)) -
len(comment_prefix)) len(comment_prefix))
......
...@@ -191,7 +191,7 @@ class CodeTest(unittest.TestCase): ...@@ -191,7 +191,7 @@ class CodeTest(unittest.TestCase):
' */', ' */',
output) output)
def testSameLineAppendAndConcat(self): def testSameLineAppendConcatComment(self):
c = Code() c = Code()
c.Append('This is a line.') c.Append('This is a line.')
c.Append('This too.', new_line=False) c.Append('This too.', new_line=False)
...@@ -199,6 +199,15 @@ class CodeTest(unittest.TestCase): ...@@ -199,6 +199,15 @@ class CodeTest(unittest.TestCase):
d.Append('And this.') d.Append('And this.')
c.Concat(d, new_line=False) c.Concat(d, new_line=False)
self.assertEquals('This is a line.This too.And this.', c.Render()) self.assertEquals('This is a line.This too.And this.', c.Render())
c = Code()
c.Append('This is a')
c.Comment(' spectacular 80-character line thingy ' +
'that fits wonderfully everywhere.',
comment_prefix='',
new_line=False)
self.assertEquals('This is a spectacular 80-character line thingy that ' +
'fits wonderfully everywhere.',
c.Render())
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
...@@ -126,7 +126,7 @@ class _Generator(object): ...@@ -126,7 +126,7 @@ class _Generator(object):
"""Given an OrderedDict of properties, returns a Code containing the """Given an OrderedDict of properties, returns a Code containing the
description of an object. description of an object.
""" """
if not properties: return '' if not properties: return Code()
c = Code() c = Code()
c.Sblock('{') c.Sblock('{')
...@@ -212,38 +212,39 @@ class _Generator(object): ...@@ -212,38 +212,39 @@ class _Generator(object):
def _TypeToJsType(self, js_type): def _TypeToJsType(self, js_type):
"""Converts a model.Type to a JS type (number, Array, etc.)""" """Converts a model.Type to a JS type (number, Array, etc.)"""
c = Code()
if js_type.property_type in (PropertyType.INTEGER, PropertyType.DOUBLE): if js_type.property_type in (PropertyType.INTEGER, PropertyType.DOUBLE):
c.Append('number') return Code().Append('number')
elif js_type.property_type is PropertyType.OBJECT: if js_type.property_type is PropertyType.OBJECT:
c = self._GenerateObjectDefinition(js_type.properties) if js_type.properties:
elif js_type.property_type is PropertyType.ARRAY: return self._GenerateObjectDefinition(js_type.properties)
(c.Append('!Array<'). return Code().Append('Object')
if js_type.property_type is PropertyType.ARRAY:
return (Code().Append('!Array<').
Concat(self._TypeToJsType(js_type.item_type), new_line=False). Concat(self._TypeToJsType(js_type.item_type), new_line=False).
Append('>', new_line=False)) Append('>', new_line=False))
elif js_type.property_type is PropertyType.REF: if js_type.property_type is PropertyType.REF:
ref_type = js_type.ref_type ref_type = js_type.ref_type
# Enums are defined as chrome.fooAPI.MyEnum, but types are defined simply # Enums are defined as chrome.fooAPI.MyEnum, but types are defined simply
# as MyType. # as MyType.
if self._namespace.types[ref_type].property_type is PropertyType.ENUM: if self._namespace.types[ref_type].property_type is PropertyType.ENUM:
ref_type = '!chrome.%s.%s' % (self._namespace.name, ref_type) ref_type = '!chrome.%s.%s' % (self._namespace.name, ref_type)
c.Append(ref_type) return Code().Append(ref_type)
elif js_type.property_type is PropertyType.CHOICES: if js_type.property_type is PropertyType.CHOICES:
c = Code()
c.Append('(') c.Append('(')
for i, choice in enumerate(js_type.choices): for i, choice in enumerate(js_type.choices):
c.Concat(self._TypeToJsType(choice), new_line=False) c.Concat(self._TypeToJsType(choice), new_line=False)
if i is not len(js_type.choices) - 1: if i is not len(js_type.choices) - 1:
c.Append('|', new_line=False) c.Append('|', new_line=False)
c.Append(')', new_line=False) c.Append(')', new_line=False)
elif js_type.property_type is PropertyType.FUNCTION: return c
c = self._FunctionToJsFunction(js_type.function) if js_type.property_type is PropertyType.FUNCTION:
elif js_type.property_type is PropertyType.ANY: return self._FunctionToJsFunction(js_type.function)
c.Append('*') if js_type.property_type is PropertyType.ANY:
elif js_type.property_type.is_fundamental: return Code().Append('*')
c.Append(js_type.property_type.name) if js_type.property_type.is_fundamental:
else: return Code().Append(js_type.property_type.name)
c.Append('?') # TODO(tbreisacher): Make this more specific. return Code().Append('?') # TODO(tbreisacher): Make this more specific.
return c
def _GenerateFunction(self, function): def _GenerateFunction(self, function):
"""Generates the code representing a function, including its documentation. """Generates the code representing a function, including its documentation.
......
...@@ -43,6 +43,7 @@ namespace fakeApi { ...@@ -43,6 +43,7 @@ namespace fakeApi {
Bar obj; Bar obj;
long? maybe; long? maybe;
(DOMString or Greek or long[]) choice; (DOMString or Greek or long[]) choice;
object plainObj;
}; };
callback VoidCallback = void(); callback VoidCallback = void();
...@@ -114,7 +115,8 @@ var Bar; ...@@ -114,7 +115,8 @@ var Bar;
* anythingGoes: !Array<*>, * anythingGoes: !Array<*>,
* obj: Bar, * obj: Bar,
* maybe: (number|undefined), * maybe: (number|undefined),
* choice: (string|!chrome.fakeApi.Greek|!Array<number>) * choice: (string|!chrome.fakeApi.Greek|!Array<number>),
* plainObj: Object
* }} * }}
* @see https://developer.chrome.com/extensions/fakeApi#type-Baz * @see https://developer.chrome.com/extensions/fakeApi#type-Baz
*/ */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment