Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: tools/json_schema_compiler/js_externs_generator_test.py

Issue 1033223007: [Extension API Extern Generation] Support inline object definitions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Dan's Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 stri ng description that causes problems!",
154 "properties": {
155 "foo": {
156 "type": "boolean",
157 "optional": "true",
158 "description": "The foo."
159 },
160 "bar": {
161 "type": "integer",
162 "description": "The bar."
163 },
164 "baz": {
165 "type": "object",
166 "description": "Inception object.",
167 "properties": {
168 "depth": {
169 "type": "integer"
170 }
171 }
172 }
173 }
174 },
175 {
176 "name": "callback",
177 "type": "function",
178 "parameters": [
179 {
180 "type": "object",
181 "name": "returnObj",
182 "properties": {
183 "str": { "type": "string"}
184 }
185 }
186 ],
187 "description": "The callback to this heinous method"
188 }
189 ],
190 "returns": {
191 "type": "object",
192 "properties": {
193 "str": { "type": "string" },
194 "int": { "type": "number" }
195 }
196 }
197 } ]
198 }
199 ]"""
200
201 json_expected = """// Copyright %s The Chromium Authors. All rights reserved.
202 // Use of this source code is governed by a BSD-style license that can be
203 // found in the LICENSE file.
204
205 /** @fileoverview Externs generated from namespace: fakeJson */
206
207 /**
208 * @const
209 */
210 chrome.fakeJson = {};
211
212 /**
213 * @param {{
214 * foo: (boolean|undefined),
215 * bar: number,
216 * baz: {
217 * depth: number
218 * }
219 * }} inlineObj Evil inline object! With a super duper duper long string
220 * description that causes problems!
221 * @param {function({
222 * str: string
223 * }):void} callback The callback to this heinous method
224 * @return {{
225 * str: string,
226 * int: number
227 * }}
228 */
229 chrome.fakeJson.funcWithInlineObj = function(inlineObj, callback) {};
230 """ % datetime.now().year
231
232
137 class JsExternGeneratorTest(unittest.TestCase): 233 class JsExternGeneratorTest(unittest.TestCase):
234 def _GetNamespace(self, fake_content, filename, is_idl):
235 """Returns a namespace object for the given content"""
236 api_def = (idl_schema.Process(fake_content, filename) if is_idl
237 else json_parse.Parse(fake_content))
238 m = model.Model()
239 return m.AddNamespace(api_def[0], filename)
240
138 def testBasic(self): 241 def testBasic(self):
139 self.maxDiff = None # Lets us see the full diff when inequal. 242 self.maxDiff = None # Lets us see the full diff when inequal.
140 filename = 'fake_api.idl' 243 namespace = self._GetNamespace(fake_idl, 'fake_api.idl', True)
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, 244 self.assertMultiLineEqual(expected_output,
145 JsExternsGenerator().Generate(namespace).Render()) 245 JsExternsGenerator().Generate(namespace).Render())
146 246
247 def testJsonWithInlineObjects(self):
248 self.maxDiff = None # Lets us see the full diff when inequal.
Tyler Breisacher (Chromium) 2015/03/27 23:22:32 If you're going to do this for every test (and I t
Devlin 2015/03/27 23:39:46 Done.
249 namespace = self._GetNamespace(fake_json, 'fake_api.json', False)
250 self.assertMultiLineEqual(json_expected,
251 JsExternsGenerator().Generate(namespace).Render())
252
147 253
148 if __name__ == '__main__': 254 if __name__ == '__main__':
149 unittest.main() 255 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698