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 # The contents of a fake idl file.
16 // Copyright 2014 The Chromium Authors. All rights reserved.
17 // Use of this source code is governed by a BSD-style license that can be
18 // found in the LICENSE file.
20 // A totally fake API.
38 Greek? optionalLetter;
40 Bar[]? optionalObjArr;
45 (DOMString or Greek or long[]) choice;
49 callback VoidCallback = void();
51 callback BazGreekCallback = void(Baz baz, Greek greek);
54 // Does something exciting! And what's more, this is a multiline function
55 // comment! It goes onto multiple lines!
56 // |baz| : The baz to use.
57 static void doSomething(Baz baz, VoidCallback callback);
59 // |callback| : The callback which will most assuredly in all cases be
60 // called; that is, of course, iff such a callback was provided and is
62 static void bazGreek(optional BazGreekCallback callback);
64 [deprecated="Use a new method."] static DOMString returnString();
68 // Fired when we realize it's a trap!
69 static void onTrapDetected(Baz baz);
74 # The output we expect from our fake idl file.
75 expected_output
= """// Copyright %s The Chromium Authors. All rights reserved.
76 // Use of this source code is governed by a BSD-style license that can be
77 // found in the LICENSE file.
79 /** @fileoverview Externs generated from namespace: fakeApi */
88 * @see https://developer.chrome.com/extensions/fakeApi#type-Greek
90 chrome.fakeApi.Greek = {
101 * @see https://developer.chrome.com/extensions/fakeApi#type-Bar
110 * letter: !chrome.fakeApi.Greek,
111 * optionalLetter: (!chrome.fakeApi.Greek|undefined),
112 * arr: !Array<number>,
113 * optionalObjArr: (!Array<Bar>|undefined),
114 * enumArr: !Array<!chrome.fakeApi.Greek>,
115 * anythingGoes: !Array<*>,
117 * maybe: (number|undefined),
118 * choice: (string|!chrome.fakeApi.Greek|!Array<number>),
121 * @see https://developer.chrome.com/extensions/fakeApi#type-Baz
126 * Does something exciting! And what's more, this is a multiline function
127 * comment! It goes onto multiple lines!
128 * @param {Baz} baz The baz to use.
129 * @param {function():void} callback
130 * @see https://developer.chrome.com/extensions/fakeApi#method-doSomething
132 chrome.fakeApi.doSomething = function(baz, callback) {};
135 * @param {function(Baz, !chrome.fakeApi.Greek):void=} callback The callback
136 * which will most assuredly in all cases be called; that is, of course, iff
137 * such a callback was provided and is not at all null.
138 * @see https://developer.chrome.com/extensions/fakeApi#method-bazGreek
140 chrome.fakeApi.bazGreek = function(callback) {};
144 * @deprecated Use a new method.
145 * @see https://developer.chrome.com/extensions/fakeApi#method-returnString
147 chrome.fakeApi.returnString = function() {};
150 * Fired when we realize it's a trap!
151 * @type {!ChromeEvent}
152 * @see https://developer.chrome.com/extensions/fakeApi#event-onTrapDetected
154 chrome.fakeApi.onTrapDetected;
155 """ % datetime
.now().year
158 fake_json
= """// Copyright 2014 The Chromium Authors. All rights reserved.
159 // Use of this source code is governed by a BSD-style license that can be
160 // found in the LICENSE file.
164 "namespace": "fakeJson",
165 "description": "Fake JSON API Stuff",
169 "enum": ["camelCaseEnum", "Non-Characters", "5NumFirst", \
170 "3Just-plainOld_MEAN"]
173 "name": "funcWithInlineObj",
179 "description": "Evil inline object! With a super duper duper long\
180 string description that causes problems!",
185 "description": "The foo."
189 "description": "The bar."
193 "description": "Inception object.",
210 "str": { "type": "string"}
214 "description": "The callback to this heinous method"
220 "str": { "type": "string" },
221 "int": { "type": "number" }
228 json_expected
= """// Copyright %s The Chromium Authors. All rights reserved.
229 // Use of this source code is governed by a BSD-style license that can be
230 // found in the LICENSE file.
232 /** @fileoverview Externs generated from namespace: fakeJson */
237 chrome.fakeJson = {};
241 * @see https://developer.chrome.com/extensions/fakeJson#type-CrazyEnum
243 chrome.fakeJson.CrazyEnum = {
244 CAMEL_CASE_ENUM: 'camelCaseEnum',
245 NON_CHARACTERS: 'Non-Characters',
246 _5NUM_FIRST: '5NumFirst',
247 _3JUST_PLAIN_OLD_MEAN: '3Just-plainOld_MEAN',
252 * foo: (boolean|undefined),
257 * }} inlineObj Evil inline object! With a super duper duper long string
258 * description that causes problems!
261 * }):void} callback The callback to this heinous method
266 * @see https://developer.chrome.com/extensions/fakeJson#method-funcWithInlineObj
268 chrome.fakeJson.funcWithInlineObj = function(inlineObj, callback) {};
269 """ % datetime
.now().year
272 class JsExternGeneratorTest(unittest
.TestCase
):
273 def _GetNamespace(self
, fake_content
, filename
, is_idl
):
274 """Returns a namespace object for the given content"""
275 api_def
= (idl_schema
.Process(fake_content
, filename
) if is_idl
276 else json_parse
.Parse(fake_content
))
278 return m
.AddNamespace(api_def
[0], filename
)
281 self
.maxDiff
= None # Lets us see the full diff when inequal.
284 namespace
= self
._GetNamespace
(fake_idl
, 'fake_api.idl', True)
285 self
.assertMultiLineEqual(expected_output
,
286 JsExternsGenerator().Generate(namespace
).Render())
288 def testJsonWithInlineObjects(self
):
289 namespace
= self
._GetNamespace
(fake_json
, 'fake_api.json', False)
290 self
.assertMultiLineEqual(json_expected
,
291 JsExternsGenerator().Generate(namespace
).Render())
294 if __name__
== '__main__':