2 # Copyright 2015 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
8 from js_externs_generator
import JsExternsGenerator
9 from datetime
import datetime
14 COPYRIGHT
= ("""// Copyright %s The Chromium Authors. All rights reserved.
15 // Use of this source code is governed by a BSD-style license that can be
16 // found in the LICENSE file.
17 """ % datetime
.now().year
)
19 INFO
= """// This file was generated by:
21 // NOTE: The format of types has changed. 'FooType' is now
22 // 'chrome.%s.FooType'.
23 // Please run the closure compiler before committing changes.
24 // See https://code.google.com/p/chromium/wiki/ClosureCompilation.
27 # The contents of a fake idl file.
29 // Copyright 2014 The Chromium Authors. All rights reserved.
30 // Use of this source code is governed by a BSD-style license that can be
31 // found in the LICENSE file.
33 // A totally fake API.
51 Greek? optionalLetter;
53 Bar[]? optionalObjArr;
58 (DOMString or Greek or long[]) choice;
62 callback VoidCallback = void();
64 callback BazGreekCallback = void(Baz baz, Greek greek);
67 // Does something exciting! And what's more, this is a multiline function
68 // comment! It goes onto multiple lines!
69 // |baz| : The baz to use.
70 static void doSomething(Baz baz, VoidCallback callback);
72 // |callback| : The callback which will most assuredly in all cases be
73 // called; that is, of course, iff such a callback was provided and is
75 static void bazGreek(optional BazGreekCallback callback);
77 [deprecated="Use a new method."] static DOMString returnString();
81 // Fired when we realize it's a trap!
82 static void onTrapDetected(Baz baz);
87 # The output we expect from our fake idl file.
88 expected_output
= COPYRIGHT
+ "\n" + (INFO
% (sys
.argv
[0], "fakeApi")) + """
89 /** @fileoverview Externs generated from namespace: fakeApi */
98 * @see https://developer.chrome.com/extensions/fakeApi#type-Greek
100 chrome.fakeApi.Greek = {
111 * @see https://developer.chrome.com/extensions/fakeApi#type-Bar
120 * letter: !chrome.fakeApi.Greek,
121 * optionalLetter: (!chrome.fakeApi.Greek|undefined),
122 * arr: !Array<number>,
123 * optionalObjArr: (!Array<!chrome.fakeApi.Bar>|undefined),
124 * enumArr: !Array<!chrome.fakeApi.Greek>,
125 * anythingGoes: !Array<*>,
126 * obj: !chrome.fakeApi.Bar,
127 * maybe: (number|undefined),
128 * choice: (string|!chrome.fakeApi.Greek|!Array<number>),
131 * @see https://developer.chrome.com/extensions/fakeApi#type-Baz
136 * Does something exciting! And what's more, this is a multiline function
137 * comment! It goes onto multiple lines!
138 * @param {!chrome.fakeApi.Baz} baz The baz to use.
139 * @param {function():void} callback
140 * @see https://developer.chrome.com/extensions/fakeApi#method-doSomething
142 chrome.fakeApi.doSomething = function(baz, callback) {};
145 * @param {function(!chrome.fakeApi.Baz, !chrome.fakeApi.Greek):void=} callback
146 * The callback which will most assuredly in all cases be called; that is,
147 * of course, iff such a callback was provided and is not at all null.
148 * @see https://developer.chrome.com/extensions/fakeApi#method-bazGreek
150 chrome.fakeApi.bazGreek = function(callback) {};
154 * @deprecated Use a new method.
155 * @see https://developer.chrome.com/extensions/fakeApi#method-returnString
157 chrome.fakeApi.returnString = function() {};
160 * Fired when we realize it's a trap!
161 * @type {!ChromeEvent}
162 * @see https://developer.chrome.com/extensions/fakeApi#event-onTrapDetected
164 chrome.fakeApi.onTrapDetected;
168 fake_json
= """// Copyright 2014 The Chromium Authors. All rights reserved.
169 // Use of this source code is governed by a BSD-style license that can be
170 // found in the LICENSE file.
174 "namespace": "fakeJson",
175 "description": "Fake JSON API Stuff",
179 "enum": ["camelCaseEnum", "Non-Characters", "5NumFirst", \
180 "3Just-plainOld_MEAN"]
183 "name": "funcWithInlineObj",
189 "description": "Evil inline object! With a super duper duper long\
190 string description that causes problems!",
195 "description": "The foo."
199 "description": "The bar."
203 "description": "Inception object.",
220 "str": { "type": "string"}
224 "description": "The callback to this heinous method"
230 "str": { "type": "string" },
231 "int": { "type": "number" }
238 json_expected
= COPYRIGHT
+ "\n" + (INFO
% (sys
.argv
[0], "fakeJson")) + """
239 /** @fileoverview Externs generated from namespace: fakeJson */
244 chrome.fakeJson = {};
248 * @see https://developer.chrome.com/extensions/fakeJson#type-CrazyEnum
250 chrome.fakeJson.CrazyEnum = {
251 CAMEL_CASE_ENUM: 'camelCaseEnum',
252 NON_CHARACTERS: 'Non-Characters',
253 _5NUM_FIRST: '5NumFirst',
254 _3JUST_PLAIN_OLD_MEAN: '3Just-plainOld_MEAN',
259 * foo: (boolean|undefined),
264 * }} inlineObj Evil inline object! With a super duper duper long string
265 * description that causes problems!
268 * }):void} callback The callback to this heinous method
273 * @see https://developer.chrome.com/extensions/fakeJson#method-funcWithInlineObj
275 chrome.fakeJson.funcWithInlineObj = function(inlineObj, callback) {};
279 class JsExternGeneratorTest(unittest
.TestCase
):
280 def _GetNamespace(self
, fake_content
, filename
, is_idl
):
281 """Returns a namespace object for the given content"""
282 api_def
= (idl_schema
.Process(fake_content
, filename
) if is_idl
283 else json_parse
.Parse(fake_content
))
285 return m
.AddNamespace(api_def
[0], filename
)
288 self
.maxDiff
= None # Lets us see the full diff when inequal.
291 namespace
= self
._GetNamespace
(fake_idl
, 'fake_api.idl', True)
292 self
.assertMultiLineEqual(expected_output
,
293 JsExternsGenerator().Generate(namespace
).Render())
295 def testJsonWithInlineObjects(self
):
296 namespace
= self
._GetNamespace
(fake_json
, 'fake_api.json', False)
297 self
.assertMultiLineEqual(json_expected
,
298 JsExternsGenerator().Generate(namespace
).Render())
301 if __name__
== '__main__':