OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import idl_schema | 6 import idl_schema |
7 import json_parse | 7 import json_parse |
8 from js_externs_generator import JsExternsGenerator | 8 from js_externs_generator import JsExternsGenerator |
9 from datetime import datetime | 9 from datetime import datetime |
10 import model | 10 import model |
11 import sys | 11 import sys |
12 import unittest | 12 import unittest |
13 | 13 |
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) | |
18 | |
19 INFO = """// This file was generated by: | |
20 // %s. | |
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. | |
25 """ | |
26 | |
27 # The contents of a fake idl file. | 14 # The contents of a fake idl file. |
28 fake_idl = """ | 15 fake_idl = """ |
29 // Copyright 2014 The Chromium Authors. All rights reserved. | 16 // Copyright 2014 The Chromium Authors. All rights reserved. |
30 // Use of this source code is governed by a BSD-style license that can be | 17 // Use of this source code is governed by a BSD-style license that can be |
31 // found in the LICENSE file. | 18 // found in the LICENSE file. |
32 | 19 |
33 // A totally fake API. | 20 // A totally fake API. |
34 namespace fakeApi { | 21 namespace fakeApi { |
35 enum Greek { | 22 enum Greek { |
36 ALPHA, | 23 ALPHA, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
78 }; | 65 }; |
79 | 66 |
80 interface Events { | 67 interface Events { |
81 // Fired when we realize it's a trap! | 68 // Fired when we realize it's a trap! |
82 static void onTrapDetected(Baz baz); | 69 static void onTrapDetected(Baz baz); |
83 }; | 70 }; |
84 }; | 71 }; |
85 """ | 72 """ |
86 | 73 |
87 # The output we expect from our fake idl file. | 74 # The output we expect from our fake idl file. |
88 expected_output = COPYRIGHT + "\n" + (INFO % (sys.argv[0], "fakeApi")) + """ | 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. | |
78 | |
79 // This file was generated by: | |
80 // %s. | |
81 // NOTE: The format of types has changed. 'FooType' is now | |
82 // 'chrome.fakeApi.FooType'. | |
83 // Please run the closure compiler before committing changes. | |
84 // See https://code.google.com/p/chromium/wiki/ClosureCompilation. | |
85 | |
89 /** @fileoverview Externs generated from namespace: fakeApi */ | 86 /** @fileoverview Externs generated from namespace: fakeApi */ |
90 | 87 |
91 /** | 88 /** |
92 * @const | 89 * @const |
93 */ | 90 */ |
94 chrome.fakeApi = {}; | 91 chrome.fakeApi = {}; |
95 | 92 |
96 /** | 93 /** |
97 * @enum {string} | 94 * @enum {string} |
98 * @see https://developer.chrome.com/extensions/fakeApi#type-Greek | 95 * @see https://developer.chrome.com/extensions/fakeApi#type-Greek |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
154 * @deprecated Use a new method. | 151 * @deprecated Use a new method. |
155 * @see https://developer.chrome.com/extensions/fakeApi#method-returnString | 152 * @see https://developer.chrome.com/extensions/fakeApi#method-returnString |
156 */ | 153 */ |
157 chrome.fakeApi.returnString = function() {}; | 154 chrome.fakeApi.returnString = function() {}; |
158 | 155 |
159 /** | 156 /** |
160 * Fired when we realize it's a trap! | 157 * Fired when we realize it's a trap! |
161 * @type {!ChromeEvent} | 158 * @type {!ChromeEvent} |
162 * @see https://developer.chrome.com/extensions/fakeApi#event-onTrapDetected | 159 * @see https://developer.chrome.com/extensions/fakeApi#event-onTrapDetected |
163 */ | 160 */ |
164 chrome.fakeApi.onTrapDetected; | 161 chrome.fakeApi.onTrapDetected;""" % (datetime.now().year, sys.argv[0])) |
165 """ | |
166 | 162 |
167 | 163 |
168 fake_json = """// Copyright 2014 The Chromium Authors. All rights reserved. | 164 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 | 165 // Use of this source code is governed by a BSD-style license that can be |
170 // found in the LICENSE file. | 166 // found in the LICENSE file. |
171 | 167 |
172 [ | 168 [ |
173 { | 169 { |
174 "namespace": "fakeJson", | 170 "namespace": "fakeJson", |
175 "description": "Fake JSON API Stuff", | 171 "description": "Fake JSON API Stuff", |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
228 "type": "object", | 224 "type": "object", |
229 "properties": { | 225 "properties": { |
230 "str": { "type": "string" }, | 226 "str": { "type": "string" }, |
231 "int": { "type": "number" } | 227 "int": { "type": "number" } |
232 } | 228 } |
233 } | 229 } |
234 } ] | 230 } ] |
235 } | 231 } |
236 ]""" | 232 ]""" |
237 | 233 |
238 json_expected = COPYRIGHT + "\n" + (INFO % (sys.argv[0], "fakeJson")) + """ | 234 json_expected = ("""// Copyright %s The Chromium Authors. All rights reserved. |
235 // Use of this source code is governed by a BSD-style license that can be | |
236 // found in the LICENSE file. | |
237 | |
238 // This file was generated by: | |
239 // %s. | |
240 // NOTE: The format of types has changed. 'FooType' is now | |
241 // 'chrome.fakeJson.FooType'. | |
242 // Please run the closure compiler before committing changes. | |
243 // See https://code.google.com/p/chromium/wiki/ClosureCompilation. | |
244 | |
239 /** @fileoverview Externs generated from namespace: fakeJson */ | 245 /** @fileoverview Externs generated from namespace: fakeJson */ |
240 | 246 |
241 /** | 247 /** |
242 * @const | 248 * @const |
243 */ | 249 */ |
244 chrome.fakeJson = {}; | 250 chrome.fakeJson = {}; |
245 | 251 |
246 /** | 252 /** |
247 * @enum {string} | 253 * @enum {string} |
248 * @see https://developer.chrome.com/extensions/fakeJson#type-CrazyEnum | 254 * @see https://developer.chrome.com/extensions/fakeJson#type-CrazyEnum |
(...skipping 16 matching lines...) Expand all Loading... | |
265 * description that causes problems! | 271 * description that causes problems! |
266 * @param {function({ | 272 * @param {function({ |
267 * str: string | 273 * str: string |
268 * }):void} callback The callback to this heinous method | 274 * }):void} callback The callback to this heinous method |
269 * @return {{ | 275 * @return {{ |
270 * str: string, | 276 * str: string, |
271 * int: number | 277 * int: number |
272 * }} | 278 * }} |
273 * @see https://developer.chrome.com/extensions/fakeJson#method-funcWithInlineOb j | 279 * @see https://developer.chrome.com/extensions/fakeJson#method-funcWithInlineOb j |
274 */ | 280 */ |
275 chrome.fakeJson.funcWithInlineObj = function(inlineObj, callback) {}; | 281 chrome.fakeJson.funcWithInlineObj = function(inlineObj, callback) {};""" % |
276 """ | 282 (datetime.now().year, sys.argv[0])) |
Dan Beam
2015/12/10 22:17:33
not sure i understand this indent
stevenjb
2015/12/10 22:29:04
Hmm, me neither. I blame my editor. If (when) we n
| |
277 | 283 |
278 | 284 |
279 class JsExternGeneratorTest(unittest.TestCase): | 285 class JsExternGeneratorTest(unittest.TestCase): |
280 def _GetNamespace(self, fake_content, filename, is_idl): | 286 def _GetNamespace(self, fake_content, filename, is_idl): |
281 """Returns a namespace object for the given content""" | 287 """Returns a namespace object for the given content""" |
282 api_def = (idl_schema.Process(fake_content, filename) if is_idl | 288 api_def = (idl_schema.Process(fake_content, filename) if is_idl |
283 else json_parse.Parse(fake_content)) | 289 else json_parse.Parse(fake_content)) |
284 m = model.Model() | 290 m = model.Model() |
285 return m.AddNamespace(api_def[0], filename) | 291 return m.AddNamespace(api_def[0], filename) |
286 | 292 |
287 def setUp(self): | 293 def setUp(self): |
288 self.maxDiff = None # Lets us see the full diff when inequal. | 294 self.maxDiff = None # Lets us see the full diff when inequal. |
289 | 295 |
290 def testBasic(self): | 296 def testBasic(self): |
291 namespace = self._GetNamespace(fake_idl, 'fake_api.idl', True) | 297 namespace = self._GetNamespace(fake_idl, 'fake_api.idl', True) |
292 self.assertMultiLineEqual(expected_output, | 298 self.assertMultiLineEqual(expected_output, |
293 JsExternsGenerator().Generate(namespace).Render()) | 299 JsExternsGenerator().Generate(namespace).Render()) |
294 | 300 |
295 def testJsonWithInlineObjects(self): | 301 def testJsonWithInlineObjects(self): |
296 namespace = self._GetNamespace(fake_json, 'fake_api.json', False) | 302 namespace = self._GetNamespace(fake_json, 'fake_api.json', False) |
297 self.assertMultiLineEqual(json_expected, | 303 self.assertMultiLineEqual(json_expected, |
298 JsExternsGenerator().Generate(namespace).Render()) | 304 JsExternsGenerator().Generate(namespace).Render()) |
299 | 305 |
300 | 306 |
301 if __name__ == '__main__': | 307 if __name__ == '__main__': |
302 unittest.main() | 308 unittest.main() |
OLD | NEW |