Fix crash evaluating rules for stale WebContents in ChromeContentRulesRegistry
[chromium-blink-merge.git] / tools / json_schema_compiler / js_externs_generator_test.py
blob1c9f6478d136408710cbfff49678c16cd7b174ec
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 "types": [ {
167 "id": "CrazyEnum",
168 "type": "string",
169 "enum": ["camelCaseEnum", "Non-Characters", "5NumFirst", \
170 "3Just-plainOld_MEAN"]
171 } ],
172 "functions": [ {
173 "name": "funcWithInlineObj",
174 "type": "function",
175 "parameters": [
177 "type": "object",
178 "name": "inlineObj",
179 "description": "Evil inline object! With a super duper duper long\
180 string description that causes problems!",
181 "properties": {
182 "foo": {
183 "type": "boolean",
184 "optional": "true",
185 "description": "The foo."
187 "bar": {
188 "type": "integer",
189 "description": "The bar."
191 "baz": {
192 "type": "object",
193 "description": "Inception object.",
194 "properties": {
195 "depth": {
196 "type": "integer"
203 "name": "callback",
204 "type": "function",
205 "parameters": [
207 "type": "object",
208 "name": "returnObj",
209 "properties": {
210 "str": { "type": "string"}
214 "description": "The callback to this heinous method"
217 "returns": {
218 "type": "object",
219 "properties": {
220 "str": { "type": "string" },
221 "int": { "type": "number" }
226 ]"""
228 json_expected = """// Copyright %s The Chromium Authors. All rights reserved.
229 // Use of this source code is governed by a BSD-style license that can be
230 // found in the LICENSE file.
232 /** @fileoverview Externs generated from namespace: fakeJson */
235 * @const
237 chrome.fakeJson = {};
240 * @enum {string}
241 * @see https://developer.chrome.com/extensions/fakeJson#type-CrazyEnum
243 chrome.fakeJson.CrazyEnum = {
244 CAMEL_CASE_ENUM: 'camelCaseEnum',
245 NON_CHARACTERS: 'Non-Characters',
246 _5NUM_FIRST: '5NumFirst',
247 _3JUST_PLAIN_OLD_MEAN: '3Just-plainOld_MEAN',
251 * @param {{
252 * foo: (boolean|undefined),
253 * bar: number,
254 * baz: {
255 * depth: number
257 * }} inlineObj Evil inline object! With a super duper duper long string
258 * description that causes problems!
259 * @param {function({
260 * str: string
261 * }):void} callback The callback to this heinous method
262 * @return {{
263 * str: string,
264 * int: number
265 * }}
266 * @see https://developer.chrome.com/extensions/fakeJson#method-funcWithInlineObj
268 chrome.fakeJson.funcWithInlineObj = function(inlineObj, callback) {};
269 """ % datetime.now().year
272 class JsExternGeneratorTest(unittest.TestCase):
273 def _GetNamespace(self, fake_content, filename, is_idl):
274 """Returns a namespace object for the given content"""
275 api_def = (idl_schema.Process(fake_content, filename) if is_idl
276 else json_parse.Parse(fake_content))
277 m = model.Model()
278 return m.AddNamespace(api_def[0], filename)
280 def setUp(self):
281 self.maxDiff = None # Lets us see the full diff when inequal.
283 def testBasic(self):
284 namespace = self._GetNamespace(fake_idl, 'fake_api.idl', True)
285 self.assertMultiLineEqual(expected_output,
286 JsExternsGenerator().Generate(namespace).Render())
288 def testJsonWithInlineObjects(self):
289 namespace = self._GetNamespace(fake_json, 'fake_api.json', False)
290 self.assertMultiLineEqual(json_expected,
291 JsExternsGenerator().Generate(namespace).Render())
294 if __name__ == '__main__':
295 unittest.main()