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",
167 "name": "funcWithInlineObj",
173 "description": "Evil inline object! With a super duper duper long\
174 string description that causes problems!",
179 "description": "The foo."
183 "description": "The bar."
187 "description": "Inception object.",
204 "str": { "type": "string"}
208 "description": "The callback to this heinous method"
214 "str": { "type": "string" },
215 "int": { "type": "number" }
222 json_expected
= """// Copyright %s The Chromium Authors. All rights reserved.
223 // Use of this source code is governed by a BSD-style license that can be
224 // found in the LICENSE file.
226 /** @fileoverview Externs generated from namespace: fakeJson */
231 chrome.fakeJson = {};
235 * foo: (boolean|undefined),
240 * }} inlineObj Evil inline object! With a super duper duper long string
241 * description that causes problems!
244 * }):void} callback The callback to this heinous method
249 * @see https://developer.chrome.com/extensions/fakeJson#method-funcWithInlineObj
251 chrome.fakeJson.funcWithInlineObj = function(inlineObj, callback) {};
252 """ % datetime
.now().year
255 class JsExternGeneratorTest(unittest
.TestCase
):
256 def _GetNamespace(self
, fake_content
, filename
, is_idl
):
257 """Returns a namespace object for the given content"""
258 api_def
= (idl_schema
.Process(fake_content
, filename
) if is_idl
259 else json_parse
.Parse(fake_content
))
261 return m
.AddNamespace(api_def
[0], filename
)
264 self
.maxDiff
= None # Lets us see the full diff when inequal.
267 namespace
= self
._GetNamespace
(fake_idl
, 'fake_api.idl', True)
268 self
.assertMultiLineEqual(expected_output
,
269 JsExternsGenerator().Generate(namespace
).Render())
271 def testJsonWithInlineObjects(self
):
272 namespace
= self
._GetNamespace
(fake_json
, 'fake_api.json', False)
273 self
.assertMultiLineEqual(json_expected
,
274 JsExternsGenerator().Generate(namespace
).Render())
277 if __name__
== '__main__':