| 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 = JsExternsGenerator.GetHeader(sys.argv[0], 'fakeApi') + """ |
| 89 /** @fileoverview Externs generated from namespace: fakeApi */ | |
| 90 | 76 |
| 91 /** | 77 /** |
| 92 * @const | 78 * @const |
| 93 */ | 79 */ |
| 94 chrome.fakeApi = {}; | 80 chrome.fakeApi = {}; |
| 95 | 81 |
| 96 /** | 82 /** |
| 97 * @enum {string} | 83 * @enum {string} |
| 98 * @see https://developer.chrome.com/extensions/fakeApi#type-Greek | 84 * @see https://developer.chrome.com/extensions/fakeApi#type-Greek |
| 99 */ | 85 */ |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 * @deprecated Use a new method. | 140 * @deprecated Use a new method. |
| 155 * @see https://developer.chrome.com/extensions/fakeApi#method-returnString | 141 * @see https://developer.chrome.com/extensions/fakeApi#method-returnString |
| 156 */ | 142 */ |
| 157 chrome.fakeApi.returnString = function() {}; | 143 chrome.fakeApi.returnString = function() {}; |
| 158 | 144 |
| 159 /** | 145 /** |
| 160 * Fired when we realize it's a trap! | 146 * Fired when we realize it's a trap! |
| 161 * @type {!ChromeEvent} | 147 * @type {!ChromeEvent} |
| 162 * @see https://developer.chrome.com/extensions/fakeApi#event-onTrapDetected | 148 * @see https://developer.chrome.com/extensions/fakeApi#event-onTrapDetected |
| 163 */ | 149 */ |
| 164 chrome.fakeApi.onTrapDetected; | 150 chrome.fakeApi.onTrapDetected;""" |
| 165 """ | |
| 166 | 151 |
| 167 | 152 |
| 168 fake_json = """// Copyright 2014 The Chromium Authors. All rights reserved. | 153 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 | 154 // Use of this source code is governed by a BSD-style license that can be |
| 170 // found in the LICENSE file. | 155 // found in the LICENSE file. |
| 171 | 156 |
| 172 [ | 157 [ |
| 173 { | 158 { |
| 174 "namespace": "fakeJson", | 159 "namespace": "fakeJson", |
| 175 "description": "Fake JSON API Stuff", | 160 "description": "Fake JSON API Stuff", |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 "type": "object", | 213 "type": "object", |
| 229 "properties": { | 214 "properties": { |
| 230 "str": { "type": "string" }, | 215 "str": { "type": "string" }, |
| 231 "int": { "type": "number" } | 216 "int": { "type": "number" } |
| 232 } | 217 } |
| 233 } | 218 } |
| 234 } ] | 219 } ] |
| 235 } | 220 } |
| 236 ]""" | 221 ]""" |
| 237 | 222 |
| 238 json_expected = COPYRIGHT + "\n" + (INFO % (sys.argv[0], "fakeJson")) + """ | 223 json_expected = JsExternsGenerator.GetHeader(sys.argv[0], 'fakeJson') + """ |
| 239 /** @fileoverview Externs generated from namespace: fakeJson */ | |
| 240 | 224 |
| 241 /** | 225 /** |
| 242 * @const | 226 * @const |
| 243 */ | 227 */ |
| 244 chrome.fakeJson = {}; | 228 chrome.fakeJson = {}; |
| 245 | 229 |
| 246 /** | 230 /** |
| 247 * @enum {string} | 231 * @enum {string} |
| 248 * @see https://developer.chrome.com/extensions/fakeJson#type-CrazyEnum | 232 * @see https://developer.chrome.com/extensions/fakeJson#type-CrazyEnum |
| 249 */ | 233 */ |
| (...skipping 15 matching lines...) Expand all Loading... |
| 265 * description that causes problems! | 249 * description that causes problems! |
| 266 * @param {function({ | 250 * @param {function({ |
| 267 * str: string | 251 * str: string |
| 268 * }):void} callback The callback to this heinous method | 252 * }):void} callback The callback to this heinous method |
| 269 * @return {{ | 253 * @return {{ |
| 270 * str: string, | 254 * str: string, |
| 271 * int: number | 255 * int: number |
| 272 * }} | 256 * }} |
| 273 * @see https://developer.chrome.com/extensions/fakeJson#method-funcWithInlineOb
j | 257 * @see https://developer.chrome.com/extensions/fakeJson#method-funcWithInlineOb
j |
| 274 */ | 258 */ |
| 275 chrome.fakeJson.funcWithInlineObj = function(inlineObj, callback) {}; | 259 chrome.fakeJson.funcWithInlineObj = function(inlineObj, callback) {};""" |
| 276 """ | |
| 277 | 260 |
| 278 | 261 |
| 279 class JsExternGeneratorTest(unittest.TestCase): | 262 class JsExternGeneratorTest(unittest.TestCase): |
| 280 def _GetNamespace(self, fake_content, filename, is_idl): | 263 def _GetNamespace(self, fake_content, filename, is_idl): |
| 281 """Returns a namespace object for the given content""" | 264 """Returns a namespace object for the given content""" |
| 282 api_def = (idl_schema.Process(fake_content, filename) if is_idl | 265 api_def = (idl_schema.Process(fake_content, filename) if is_idl |
| 283 else json_parse.Parse(fake_content)) | 266 else json_parse.Parse(fake_content)) |
| 284 m = model.Model() | 267 m = model.Model() |
| 285 return m.AddNamespace(api_def[0], filename) | 268 return m.AddNamespace(api_def[0], filename) |
| 286 | 269 |
| 287 def setUp(self): | 270 def setUp(self): |
| 288 self.maxDiff = None # Lets us see the full diff when inequal. | 271 self.maxDiff = None # Lets us see the full diff when inequal. |
| 289 | 272 |
| 290 def testBasic(self): | 273 def testBasic(self): |
| 291 namespace = self._GetNamespace(fake_idl, 'fake_api.idl', True) | 274 namespace = self._GetNamespace(fake_idl, 'fake_api.idl', True) |
| 292 self.assertMultiLineEqual(expected_output, | 275 self.assertMultiLineEqual(expected_output, |
| 293 JsExternsGenerator().Generate(namespace).Render()) | 276 JsExternsGenerator().Generate(namespace).Render()) |
| 294 | 277 |
| 295 def testJsonWithInlineObjects(self): | 278 def testJsonWithInlineObjects(self): |
| 296 namespace = self._GetNamespace(fake_json, 'fake_api.json', False) | 279 namespace = self._GetNamespace(fake_json, 'fake_api.json', False) |
| 297 self.assertMultiLineEqual(json_expected, | 280 self.assertMultiLineEqual(json_expected, |
| 298 JsExternsGenerator().Generate(namespace).Render()) | 281 JsExternsGenerator().Generate(namespace).Render()) |
| 299 | 282 |
| 300 | 283 |
| 301 if __name__ == '__main__': | 284 if __name__ == '__main__': |
| 302 unittest.main() | 285 unittest.main() |
| OLD | NEW |