| 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 from js_externs_generator import JsExternsGenerator | 8 from js_externs_generator import JsExternsGenerator |
| 8 from datetime import datetime | 9 from datetime import datetime |
| 9 import model | 10 import model |
| 10 import unittest | 11 import unittest |
| 11 | 12 |
| 12 | 13 |
| 13 # The contents of a fake idl file. | 14 # The contents of a fake idl file. |
| 14 fake_idl = """ | 15 fake_idl = """ |
| 15 // Copyright %s The Chromium Authors. All rights reserved. | 16 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 16 // 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 |
| 17 // found in the LICENSE file. | 18 // found in the LICENSE file. |
| 18 | 19 |
| 19 // A totally fake API. | 20 // A totally fake API. |
| 20 namespace fakeApi { | 21 namespace fakeApi { |
| 21 enum Greek { | 22 enum Greek { |
| 22 ALPHA, | 23 ALPHA, |
| 23 BETA, | 24 BETA, |
| 24 GAMMA, | 25 GAMMA, |
| 25 DELTA | 26 DELTA |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 chrome.fakeApi.bazGreek = function(callback) {}; | 128 chrome.fakeApi.bazGreek = function(callback) {}; |
| 128 | 129 |
| 129 /** | 130 /** |
| 130 * @return {string} | 131 * @return {string} |
| 131 * @deprecated Use a new method. | 132 * @deprecated Use a new method. |
| 132 */ | 133 */ |
| 133 chrome.fakeApi.returnString = function() {}; | 134 chrome.fakeApi.returnString = function() {}; |
| 134 """ % datetime.now().year | 135 """ % datetime.now().year |
| 135 | 136 |
| 136 | 137 |
| 138 fake_json = """// Copyright 2014 The Chromium Authors. All rights reserved. |
| 139 // Use of this source code is governed by a BSD-style license that can be |
| 140 // found in the LICENSE file. |
| 141 |
| 142 [ |
| 143 { |
| 144 "namespace": "fakeJson", |
| 145 "description": "Fake JSON API Stuff", |
| 146 "functions": [ { |
| 147 "name": "funcWithInlineObj", |
| 148 "type": "function", |
| 149 "parameters": [ |
| 150 { |
| 151 "type": "object", |
| 152 "name": "inlineObj", |
| 153 "description": "Evil inline object! With a super duper duper long\ |
| 154 string description that causes problems!", |
| 155 "properties": { |
| 156 "foo": { |
| 157 "type": "boolean", |
| 158 "optional": "true", |
| 159 "description": "The foo." |
| 160 }, |
| 161 "bar": { |
| 162 "type": "integer", |
| 163 "description": "The bar." |
| 164 }, |
| 165 "baz": { |
| 166 "type": "object", |
| 167 "description": "Inception object.", |
| 168 "properties": { |
| 169 "depth": { |
| 170 "type": "integer" |
| 171 } |
| 172 } |
| 173 } |
| 174 } |
| 175 }, |
| 176 { |
| 177 "name": "callback", |
| 178 "type": "function", |
| 179 "parameters": [ |
| 180 { |
| 181 "type": "object", |
| 182 "name": "returnObj", |
| 183 "properties": { |
| 184 "str": { "type": "string"} |
| 185 } |
| 186 } |
| 187 ], |
| 188 "description": "The callback to this heinous method" |
| 189 } |
| 190 ], |
| 191 "returns": { |
| 192 "type": "object", |
| 193 "properties": { |
| 194 "str": { "type": "string" }, |
| 195 "int": { "type": "number" } |
| 196 } |
| 197 } |
| 198 } ] |
| 199 } |
| 200 ]""" |
| 201 |
| 202 json_expected = """// Copyright %s The Chromium Authors. All rights reserved. |
| 203 // Use of this source code is governed by a BSD-style license that can be |
| 204 // found in the LICENSE file. |
| 205 |
| 206 /** @fileoverview Externs generated from namespace: fakeJson */ |
| 207 |
| 208 /** |
| 209 * @const |
| 210 */ |
| 211 chrome.fakeJson = {}; |
| 212 |
| 213 /** |
| 214 * @param {{ |
| 215 * foo: (boolean|undefined), |
| 216 * bar: number, |
| 217 * baz: { |
| 218 * depth: number |
| 219 * } |
| 220 * }} inlineObj Evil inline object! With a super duper duper long string |
| 221 * description that causes problems! |
| 222 * @param {function({ |
| 223 * str: string |
| 224 * }):void} callback The callback to this heinous method |
| 225 * @return {{ |
| 226 * str: string, |
| 227 * int: number |
| 228 * }} |
| 229 */ |
| 230 chrome.fakeJson.funcWithInlineObj = function(inlineObj, callback) {}; |
| 231 """ % datetime.now().year |
| 232 |
| 233 |
| 137 class JsExternGeneratorTest(unittest.TestCase): | 234 class JsExternGeneratorTest(unittest.TestCase): |
| 235 def _GetNamespace(self, fake_content, filename, is_idl): |
| 236 """Returns a namespace object for the given content""" |
| 237 api_def = (idl_schema.Process(fake_content, filename) if is_idl |
| 238 else json_parse.Parse(fake_content)) |
| 239 m = model.Model() |
| 240 return m.AddNamespace(api_def[0], filename) |
| 241 |
| 242 def setUp(self): |
| 243 self.maxDiff = None # Lets us see the full diff when inequal. |
| 244 |
| 138 def testBasic(self): | 245 def testBasic(self): |
| 139 self.maxDiff = None # Lets us see the full diff when inequal. | 246 namespace = self._GetNamespace(fake_idl, 'fake_api.idl', True) |
| 140 filename = 'fake_api.idl' | |
| 141 api_def = idl_schema.Process(fake_idl, filename) | |
| 142 m = model.Model() | |
| 143 namespace = m.AddNamespace(api_def[0], filename) | |
| 144 self.assertMultiLineEqual(expected_output, | 247 self.assertMultiLineEqual(expected_output, |
| 145 JsExternsGenerator().Generate(namespace).Render()) | 248 JsExternsGenerator().Generate(namespace).Render()) |
| 146 | 249 |
| 250 def testJsonWithInlineObjects(self): |
| 251 namespace = self._GetNamespace(fake_json, 'fake_api.json', False) |
| 252 self.assertMultiLineEqual(json_expected, |
| 253 JsExternsGenerator().Generate(namespace).Render()) |
| 254 |
| 147 | 255 |
| 148 if __name__ == '__main__': | 256 if __name__ == '__main__': |
| 149 unittest.main() | 257 unittest.main() |
| OLD | NEW |