Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / tools / json_schema_compiler / js_externs_generator_test.py
blobcdf49301c7b7372a99a401d2212f6e22d3e39356
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 sys
12 import unittest
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)
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 """
27 # The contents of a fake idl file.
28 fake_idl = """
29 // Copyright 2014 The Chromium Authors. All rights reserved.
30 // Use of this source code is governed by a BSD-style license that can be
31 // found in the LICENSE file.
33 // A totally fake API.
34 namespace fakeApi {
35 enum Greek {
36 ALPHA,
37 BETA,
38 GAMMA,
39 DELTA
42 dictionary Bar {
43 long num;
46 dictionary Baz {
47 DOMString str;
48 long num;
49 boolean b;
50 Greek letter;
51 Greek? optionalLetter;
52 long[] arr;
53 Bar[]? optionalObjArr;
54 Greek[] enumArr;
55 any[] anythingGoes;
56 Bar obj;
57 long? maybe;
58 (DOMString or Greek or long[]) choice;
59 object plainObj;
62 callback VoidCallback = void();
64 callback BazGreekCallback = void(Baz baz, Greek greek);
66 interface Functions {
67 // Does something exciting! And what's more, this is a multiline function
68 // comment! It goes onto multiple lines!
69 // |baz| : The baz to use.
70 static void doSomething(Baz baz, VoidCallback callback);
72 // |callback| : The callback which will most assuredly in all cases be
73 // called; that is, of course, iff such a callback was provided and is
74 // not at all null.
75 static void bazGreek(optional BazGreekCallback callback);
77 [deprecated="Use a new method."] static DOMString returnString();
80 interface Events {
81 // Fired when we realize it's a trap!
82 static void onTrapDetected(Baz baz);
85 """
87 # The output we expect from our fake idl file.
88 expected_output = COPYRIGHT + "\n" + (INFO % (sys.argv[0], "fakeApi")) + """
89 /** @fileoverview Externs generated from namespace: fakeApi */
91 /**
92 * @const
94 chrome.fakeApi = {};
96 /**
97 * @enum {string}
98 * @see https://developer.chrome.com/extensions/fakeApi#type-Greek
100 chrome.fakeApi.Greek = {
101 ALPHA: 'ALPHA',
102 BETA: 'BETA',
103 GAMMA: 'GAMMA',
104 DELTA: 'DELTA',
108 * @typedef {{
109 * num: number
110 * }}
111 * @see https://developer.chrome.com/extensions/fakeApi#type-Bar
113 chrome.fakeApi.Bar;
116 * @typedef {{
117 * str: string,
118 * num: number,
119 * b: boolean,
120 * letter: !chrome.fakeApi.Greek,
121 * optionalLetter: (!chrome.fakeApi.Greek|undefined),
122 * arr: !Array<number>,
123 * optionalObjArr: (!Array<!chrome.fakeApi.Bar>|undefined),
124 * enumArr: !Array<!chrome.fakeApi.Greek>,
125 * anythingGoes: !Array<*>,
126 * obj: !chrome.fakeApi.Bar,
127 * maybe: (number|undefined),
128 * choice: (string|!chrome.fakeApi.Greek|!Array<number>),
129 * plainObj: Object
130 * }}
131 * @see https://developer.chrome.com/extensions/fakeApi#type-Baz
133 chrome.fakeApi.Baz;
136 * Does something exciting! And what's more, this is a multiline function
137 * comment! It goes onto multiple lines!
138 * @param {!chrome.fakeApi.Baz} baz The baz to use.
139 * @param {function():void} callback
140 * @see https://developer.chrome.com/extensions/fakeApi#method-doSomething
142 chrome.fakeApi.doSomething = function(baz, callback) {};
145 * @param {function(!chrome.fakeApi.Baz, !chrome.fakeApi.Greek):void=} callback
146 * The callback which will most assuredly in all cases be called; that is,
147 * of course, iff such a callback was provided and is not at all null.
148 * @see https://developer.chrome.com/extensions/fakeApi#method-bazGreek
150 chrome.fakeApi.bazGreek = function(callback) {};
153 * @return {string}
154 * @deprecated Use a new method.
155 * @see https://developer.chrome.com/extensions/fakeApi#method-returnString
157 chrome.fakeApi.returnString = function() {};
160 * Fired when we realize it's a trap!
161 * @type {!ChromeEvent}
162 * @see https://developer.chrome.com/extensions/fakeApi#event-onTrapDetected
164 chrome.fakeApi.onTrapDetected;
168 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
170 // found in the LICENSE file.
174 "namespace": "fakeJson",
175 "description": "Fake JSON API Stuff",
176 "types": [ {
177 "id": "CrazyEnum",
178 "type": "string",
179 "enum": ["camelCaseEnum", "Non-Characters", "5NumFirst", \
180 "3Just-plainOld_MEAN"]
181 } ],
182 "functions": [ {
183 "name": "funcWithInlineObj",
184 "type": "function",
185 "parameters": [
187 "type": "object",
188 "name": "inlineObj",
189 "description": "Evil inline object! With a super duper duper long\
190 string description that causes problems!",
191 "properties": {
192 "foo": {
193 "type": "boolean",
194 "optional": "true",
195 "description": "The foo."
197 "bar": {
198 "type": "integer",
199 "description": "The bar."
201 "baz": {
202 "type": "object",
203 "description": "Inception object.",
204 "properties": {
205 "depth": {
206 "type": "integer"
213 "name": "callback",
214 "type": "function",
215 "parameters": [
217 "type": "object",
218 "name": "returnObj",
219 "properties": {
220 "str": { "type": "string"}
224 "description": "The callback to this heinous method"
227 "returns": {
228 "type": "object",
229 "properties": {
230 "str": { "type": "string" },
231 "int": { "type": "number" }
236 ]"""
238 json_expected = COPYRIGHT + "\n" + (INFO % (sys.argv[0], "fakeJson")) + """
239 /** @fileoverview Externs generated from namespace: fakeJson */
242 * @const
244 chrome.fakeJson = {};
247 * @enum {string}
248 * @see https://developer.chrome.com/extensions/fakeJson#type-CrazyEnum
250 chrome.fakeJson.CrazyEnum = {
251 CAMEL_CASE_ENUM: 'camelCaseEnum',
252 NON_CHARACTERS: 'Non-Characters',
253 _5NUM_FIRST: '5NumFirst',
254 _3JUST_PLAIN_OLD_MEAN: '3Just-plainOld_MEAN',
258 * @param {{
259 * foo: (boolean|undefined),
260 * bar: number,
261 * baz: {
262 * depth: number
264 * }} inlineObj Evil inline object! With a super duper duper long string
265 * description that causes problems!
266 * @param {function({
267 * str: string
268 * }):void} callback The callback to this heinous method
269 * @return {{
270 * str: string,
271 * int: number
272 * }}
273 * @see https://developer.chrome.com/extensions/fakeJson#method-funcWithInlineObj
275 chrome.fakeJson.funcWithInlineObj = function(inlineObj, callback) {};
279 class JsExternGeneratorTest(unittest.TestCase):
280 def _GetNamespace(self, fake_content, filename, is_idl):
281 """Returns a namespace object for the given content"""
282 api_def = (idl_schema.Process(fake_content, filename) if is_idl
283 else json_parse.Parse(fake_content))
284 m = model.Model()
285 return m.AddNamespace(api_def[0], filename)
287 def setUp(self):
288 self.maxDiff = None # Lets us see the full diff when inequal.
290 def testBasic(self):
291 namespace = self._GetNamespace(fake_idl, 'fake_api.idl', True)
292 self.assertMultiLineEqual(expected_output,
293 JsExternsGenerator().Generate(namespace).Render())
295 def testJsonWithInlineObjects(self):
296 namespace = self._GetNamespace(fake_json, 'fake_api.json', False)
297 self.assertMultiLineEqual(json_expected,
298 JsExternsGenerator().Generate(namespace).Render())
301 if __name__ == '__main__':
302 unittest.main()