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;
48 callback VoidCallback = void();
50 callback BazGreekCallback = void(Baz baz, Greek greek);
53 // Does something exciting! And what's more, this is a multiline function
54 // comment! It goes onto multiple lines!
55 // |baz| : The baz to use.
56 static void doSomething(Baz baz, VoidCallback callback);
58 // |callback| : The callback which will most assuredly in all cases be
59 // called; that is, of course, iff such a callback was provided and is
61 static void bazGreek(optional BazGreekCallback callback);
63 [deprecated="Use a new method."] static DOMString returnString();
67 // Fired when we realize it's a trap!
68 static void onTrapDetected(Baz baz);
73 # The output we expect from our fake idl file.
74 expected_output
= """// Copyright %s The Chromium Authors. All rights reserved.
75 // Use of this source code is governed by a BSD-style license that can be
76 // found in the LICENSE file.
78 /** @fileoverview Externs generated from namespace: fakeApi */
87 * @see https://developer.chrome.com/extensions/fakeApi#type-Greek
89 chrome.fakeApi.Greek = {
100 * @see https://developer.chrome.com/extensions/fakeApi#type-Bar
109 * letter: !chrome.fakeApi.Greek,
110 * optionalLetter: (!chrome.fakeApi.Greek|undefined),
111 * arr: !Array<number>,
112 * optionalObjArr: (!Array<Bar>|undefined),
113 * enumArr: !Array<!chrome.fakeApi.Greek>,
114 * anythingGoes: !Array<*>,
116 * maybe: (number|undefined),
117 * choice: (string|!chrome.fakeApi.Greek|!Array<number>)
119 * @see https://developer.chrome.com/extensions/fakeApi#type-Baz
124 * Does something exciting! And what's more, this is a multiline function
125 * comment! It goes onto multiple lines!
126 * @param {Baz} baz The baz to use.
127 * @param {function():void} callback
128 * @see https://developer.chrome.com/extensions/fakeApi#method-doSomething
130 chrome.fakeApi.doSomething = function(baz, callback) {};
133 * @param {function(Baz, !chrome.fakeApi.Greek):void=} callback The callback
134 * which will most assuredly in all cases be called; that is, of course, iff
135 * such a callback was provided and is not at all null.
136 * @see https://developer.chrome.com/extensions/fakeApi#method-bazGreek
138 chrome.fakeApi.bazGreek = function(callback) {};
142 * @deprecated Use a new method.
143 * @see https://developer.chrome.com/extensions/fakeApi#method-returnString
145 chrome.fakeApi.returnString = function() {};
148 * Fired when we realize it's a trap!
149 * @type {!ChromeEvent}
150 * @see https://developer.chrome.com/extensions/fakeApi#event-onTrapDetected
152 chrome.fakeApi.onTrapDetected;
153 """ % datetime
.now().year
156 fake_json
= """// Copyright 2014 The Chromium Authors. All rights reserved.
157 // Use of this source code is governed by a BSD-style license that can be
158 // found in the LICENSE file.
162 "namespace": "fakeJson",
163 "description": "Fake JSON API Stuff",
165 "name": "funcWithInlineObj",
171 "description": "Evil inline object! With a super duper duper long\
172 string description that causes problems!",
177 "description": "The foo."
181 "description": "The bar."
185 "description": "Inception object.",
202 "str": { "type": "string"}
206 "description": "The callback to this heinous method"
212 "str": { "type": "string" },
213 "int": { "type": "number" }
220 json_expected
= """// Copyright %s The Chromium Authors. All rights reserved.
221 // Use of this source code is governed by a BSD-style license that can be
222 // found in the LICENSE file.
224 /** @fileoverview Externs generated from namespace: fakeJson */
229 chrome.fakeJson = {};
233 * foo: (boolean|undefined),
238 * }} inlineObj Evil inline object! With a super duper duper long string
239 * description that causes problems!
242 * }):void} callback The callback to this heinous method
247 * @see https://developer.chrome.com/extensions/fakeJson#method-funcWithInlineObj
249 chrome.fakeJson.funcWithInlineObj = function(inlineObj, callback) {};
250 """ % datetime
.now().year
253 class JsExternGeneratorTest(unittest
.TestCase
):
254 def _GetNamespace(self
, fake_content
, filename
, is_idl
):
255 """Returns a namespace object for the given content"""
256 api_def
= (idl_schema
.Process(fake_content
, filename
) if is_idl
257 else json_parse
.Parse(fake_content
))
259 return m
.AddNamespace(api_def
[0], filename
)
262 self
.maxDiff
= None # Lets us see the full diff when inequal.
265 namespace
= self
._GetNamespace
(fake_idl
, 'fake_api.idl', True)
266 self
.assertMultiLineEqual(expected_output
,
267 JsExternsGenerator().Generate(namespace
).Render())
269 def testJsonWithInlineObjects(self
):
270 namespace
= self
._GetNamespace
(fake_json
, 'fake_api.json', False)
271 self
.assertMultiLineEqual(json_expected
,
272 JsExternsGenerator().Generate(namespace
).Render())
275 if __name__
== '__main__':