Added gcdPrivate.getDeviceInfo function
[chromium-blink-merge.git] / tools / json_schema_compiler / js_externs_generator_test.py
blobb74260daf86e5fcc0932bdd79b6501d651356fe6
1 #!/usr/bin/env python
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.
6 import idl_schema
7 import json_parse
8 from js_externs_generator import JsExternsGenerator
9 from datetime import datetime
10 import model
11 import unittest
14 # The contents of a fake idl file.
15 fake_idl = """
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.
21 namespace fakeApi {
22 enum Greek {
23 ALPHA,
24 BETA,
25 GAMMA,
26 DELTA
29 dictionary Bar {
30 long num;
33 dictionary Baz {
34 DOMString str;
35 long num;
36 boolean b;
37 Greek letter;
38 Greek? optionalLetter;
39 long[] arr;
40 Bar[]? optionalObjArr;
41 Greek[] enumArr;
42 any[] anythingGoes;
43 Bar obj;
44 long? maybe;
45 (DOMString or Greek or long[]) choice;
46 object plainObj;
49 callback VoidCallback = void();
51 callback BazGreekCallback = void(Baz baz, Greek greek);
53 interface Functions {
54 // Does something exciting! And what's more, this is a multiline function
55 // comment! It goes onto multiple lines!
56 // |baz| : The baz to use.
57 static void doSomething(Baz baz, VoidCallback callback);
59 // |callback| : The callback which will most assuredly in all cases be
60 // called; that is, of course, iff such a callback was provided and is
61 // not at all null.
62 static void bazGreek(optional BazGreekCallback callback);
64 [deprecated="Use a new method."] static DOMString returnString();
67 interface Events {
68 // Fired when we realize it's a trap!
69 static void onTrapDetected(Baz baz);
72 """
74 # The output we expect from our fake idl file.
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.
79 /** @fileoverview Externs generated from namespace: fakeApi */
81 /**
82 * @const
84 chrome.fakeApi = {};
86 /**
87 * @enum {string}
88 * @see https://developer.chrome.com/extensions/fakeApi#type-Greek
90 chrome.fakeApi.Greek = {
91 ALPHA: 'ALPHA',
92 BETA: 'BETA',
93 GAMMA: 'GAMMA',
94 DELTA: 'DELTA',
97 /**
98 * @typedef {{
99 * num: number
100 * }}
101 * @see https://developer.chrome.com/extensions/fakeApi#type-Bar
103 var Bar;
106 * @typedef {{
107 * str: string,
108 * num: number,
109 * b: boolean,
110 * letter: !chrome.fakeApi.Greek,
111 * optionalLetter: (!chrome.fakeApi.Greek|undefined),
112 * arr: !Array<number>,
113 * optionalObjArr: (!Array<Bar>|undefined),
114 * enumArr: !Array<!chrome.fakeApi.Greek>,
115 * anythingGoes: !Array<*>,
116 * obj: Bar,
117 * maybe: (number|undefined),
118 * choice: (string|!chrome.fakeApi.Greek|!Array<number>),
119 * plainObj: Object
120 * }}
121 * @see https://developer.chrome.com/extensions/fakeApi#type-Baz
123 var Baz;
126 * Does something exciting! And what's more, this is a multiline function
127 * comment! It goes onto multiple lines!
128 * @param {Baz} baz The baz to use.
129 * @param {function():void} callback
130 * @see https://developer.chrome.com/extensions/fakeApi#method-doSomething
132 chrome.fakeApi.doSomething = function(baz, callback) {};
135 * @param {function(Baz, !chrome.fakeApi.Greek):void=} callback The callback
136 * which will most assuredly in all cases be called; that is, of course, iff
137 * such a callback was provided and is not at all null.
138 * @see https://developer.chrome.com/extensions/fakeApi#method-bazGreek
140 chrome.fakeApi.bazGreek = function(callback) {};
143 * @return {string}
144 * @deprecated Use a new method.
145 * @see https://developer.chrome.com/extensions/fakeApi#method-returnString
147 chrome.fakeApi.returnString = function() {};
150 * Fired when we realize it's a trap!
151 * @type {!ChromeEvent}
152 * @see https://developer.chrome.com/extensions/fakeApi#event-onTrapDetected
154 chrome.fakeApi.onTrapDetected;
155 """ % datetime.now().year
158 fake_json = """// Copyright 2014 The Chromium Authors. All rights reserved.
159 // Use of this source code is governed by a BSD-style license that can be
160 // found in the LICENSE file.
164 "namespace": "fakeJson",
165 "description": "Fake JSON API Stuff",
166 "functions": [ {
167 "name": "funcWithInlineObj",
168 "type": "function",
169 "parameters": [
171 "type": "object",
172 "name": "inlineObj",
173 "description": "Evil inline object! With a super duper duper long\
174 string description that causes problems!",
175 "properties": {
176 "foo": {
177 "type": "boolean",
178 "optional": "true",
179 "description": "The foo."
181 "bar": {
182 "type": "integer",
183 "description": "The bar."
185 "baz": {
186 "type": "object",
187 "description": "Inception object.",
188 "properties": {
189 "depth": {
190 "type": "integer"
197 "name": "callback",
198 "type": "function",
199 "parameters": [
201 "type": "object",
202 "name": "returnObj",
203 "properties": {
204 "str": { "type": "string"}
208 "description": "The callback to this heinous method"
211 "returns": {
212 "type": "object",
213 "properties": {
214 "str": { "type": "string" },
215 "int": { "type": "number" }
220 ]"""
222 json_expected = """// Copyright %s The Chromium Authors. All rights reserved.
223 // Use of this source code is governed by a BSD-style license that can be
224 // found in the LICENSE file.
226 /** @fileoverview Externs generated from namespace: fakeJson */
229 * @const
231 chrome.fakeJson = {};
234 * @param {{
235 * foo: (boolean|undefined),
236 * bar: number,
237 * baz: {
238 * depth: number
240 * }} inlineObj Evil inline object! With a super duper duper long string
241 * description that causes problems!
242 * @param {function({
243 * str: string
244 * }):void} callback The callback to this heinous method
245 * @return {{
246 * str: string,
247 * int: number
248 * }}
249 * @see https://developer.chrome.com/extensions/fakeJson#method-funcWithInlineObj
251 chrome.fakeJson.funcWithInlineObj = function(inlineObj, callback) {};
252 """ % datetime.now().year
255 class JsExternGeneratorTest(unittest.TestCase):
256 def _GetNamespace(self, fake_content, filename, is_idl):
257 """Returns a namespace object for the given content"""
258 api_def = (idl_schema.Process(fake_content, filename) if is_idl
259 else json_parse.Parse(fake_content))
260 m = model.Model()
261 return m.AddNamespace(api_def[0], filename)
263 def setUp(self):
264 self.maxDiff = None # Lets us see the full diff when inequal.
266 def testBasic(self):
267 namespace = self._GetNamespace(fake_idl, 'fake_api.idl', True)
268 self.assertMultiLineEqual(expected_output,
269 JsExternsGenerator().Generate(namespace).Render())
271 def testJsonWithInlineObjects(self):
272 namespace = self._GetNamespace(fake_json, 'fake_api.json', False)
273 self.assertMultiLineEqual(json_expected,
274 JsExternsGenerator().Generate(namespace).Render())
277 if __name__ == '__main__':
278 unittest.main()