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

Unified Diff: tools/json_schema_compiler/js_interface_generator_test.py

Issue 1488773003: Add js_interface_generator for generating extensions interfaces (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Feedback Created 5 years 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 side-by-side diff with in-line comments
Download patch
Index: tools/json_schema_compiler/js_interface_generator_test.py
diff --git a/tools/json_schema_compiler/js_externs_generator_test.py b/tools/json_schema_compiler/js_interface_generator_test.py
similarity index 25%
copy from tools/json_schema_compiler/js_externs_generator_test.py
copy to tools/json_schema_compiler/js_interface_generator_test.py
index cdf49301c7b7372a99a401d2212f6e22d3e39356..8d011d4d946f73981cf352a916eedb3a96ee7359 100755
--- a/tools/json_schema_compiler/js_externs_generator_test.py
+++ b/tools/json_schema_compiler/js_interface_generator_test.py
@@ -5,25 +5,12 @@
import idl_schema
import json_parse
-from js_externs_generator import JsExternsGenerator
+from js_interface_generator import JsInterfaceGenerator
from datetime import datetime
import model
import sys
import unittest
-COPYRIGHT = ("""// Copyright %s The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-""" % datetime.now().year)
-
-INFO = """// This file was generated by:
-// %s.
-// NOTE: The format of types has changed. 'FooType' is now
-// 'chrome.%s.FooType'.
-// Please run the closure compiler before committing changes.
-// See https://code.google.com/p/chromium/wiki/ClosureCompilation.
-"""
-
# The contents of a fake idl file.
fake_idl = """
// Copyright 2014 The Chromium Authors. All rights reserved.
@@ -85,202 +72,40 @@ namespace fakeApi {
"""
# The output we expect from our fake idl file.
-expected_output = COPYRIGHT + "\n" + (INFO % (sys.argv[0], "fakeApi")) + """
-/** @fileoverview Externs generated from namespace: fakeApi */
-
-/**
- * @const
- */
-chrome.fakeApi = {};
-
-/**
- * @enum {string}
- * @see https://developer.chrome.com/extensions/fakeApi#type-Greek
- */
-chrome.fakeApi.Greek = {
- ALPHA: 'ALPHA',
- BETA: 'BETA',
- GAMMA: 'GAMMA',
- DELTA: 'DELTA',
-};
-
-/**
- * @typedef {{
- * num: number
- * }}
- * @see https://developer.chrome.com/extensions/fakeApi#type-Bar
- */
-chrome.fakeApi.Bar;
-
-/**
- * @typedef {{
- * str: string,
- * num: number,
- * b: boolean,
- * letter: !chrome.fakeApi.Greek,
- * optionalLetter: (!chrome.fakeApi.Greek|undefined),
- * arr: !Array<number>,
- * optionalObjArr: (!Array<!chrome.fakeApi.Bar>|undefined),
- * enumArr: !Array<!chrome.fakeApi.Greek>,
- * anythingGoes: !Array<*>,
- * obj: !chrome.fakeApi.Bar,
- * maybe: (number|undefined),
- * choice: (string|!chrome.fakeApi.Greek|!Array<number>),
- * plainObj: Object
- * }}
- * @see https://developer.chrome.com/extensions/fakeApi#type-Baz
- */
-chrome.fakeApi.Baz;
-
-/**
- * Does something exciting! And what's more, this is a multiline function
- * comment! It goes onto multiple lines!
- * @param {!chrome.fakeApi.Baz} baz The baz to use.
- * @param {function():void} callback
- * @see https://developer.chrome.com/extensions/fakeApi#method-doSomething
- */
-chrome.fakeApi.doSomething = function(baz, callback) {};
-
-/**
- * @param {function(!chrome.fakeApi.Baz, !chrome.fakeApi.Greek):void=} callback
- * The callback which will most assuredly in all cases be called; that is,
- * of course, iff such a callback was provided and is not at all null.
- * @see https://developer.chrome.com/extensions/fakeApi#method-bazGreek
- */
-chrome.fakeApi.bazGreek = function(callback) {};
-
-/**
- * @return {string}
- * @deprecated Use a new method.
- * @see https://developer.chrome.com/extensions/fakeApi#method-returnString
- */
-chrome.fakeApi.returnString = function() {};
-
-/**
- * Fired when we realize it's a trap!
- * @type {!ChromeEvent}
- * @see https://developer.chrome.com/extensions/fakeApi#event-onTrapDetected
- */
-chrome.fakeApi.onTrapDetected;
-"""
-
-
-fake_json = """// Copyright 2014 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-[
- {
- "namespace": "fakeJson",
- "description": "Fake JSON API Stuff",
- "types": [ {
- "id": "CrazyEnum",
- "type": "string",
- "enum": ["camelCaseEnum", "Non-Characters", "5NumFirst", \
-"3Just-plainOld_MEAN"]
- } ],
- "functions": [ {
- "name": "funcWithInlineObj",
- "type": "function",
- "parameters": [
- {
- "type": "object",
- "name": "inlineObj",
- "description": "Evil inline object! With a super duper duper long\
- string description that causes problems!",
- "properties": {
- "foo": {
- "type": "boolean",
- "optional": "true",
- "description": "The foo."
- },
- "bar": {
- "type": "integer",
- "description": "The bar."
- },
- "baz": {
- "type": "object",
- "description": "Inception object.",
- "properties": {
- "depth": {
- "type": "integer"
- }
- }
- }
- }
- },
- {
- "name": "callback",
- "type": "function",
- "parameters": [
- {
- "type": "object",
- "name": "returnObj",
- "properties": {
- "str": { "type": "string"}
- }
- }
- ],
- "description": "The callback to this heinous method"
- }
- ],
- "returns": {
- "type": "object",
- "properties": {
- "str": { "type": "string" },
- "int": { "type": "number" }
- }
- }
- } ]
- }
-]"""
-
-json_expected = COPYRIGHT + "\n" + (INFO % (sys.argv[0], "fakeJson")) + """
-/** @fileoverview Externs generated from namespace: fakeJson */
-
-/**
- * @const
- */
-chrome.fakeJson = {};
-
-/**
- * @enum {string}
- * @see https://developer.chrome.com/extensions/fakeJson#type-CrazyEnum
- */
-chrome.fakeJson.CrazyEnum = {
- CAMEL_CASE_ENUM: 'camelCaseEnum',
- NON_CHARACTERS: 'Non-Characters',
- _5NUM_FIRST: '5NumFirst',
- _3JUST_PLAIN_OLD_MEAN: '3Just-plainOld_MEAN',
-};
-
-/**
- * @param {{
- * foo: (boolean|undefined),
- * bar: number,
- * baz: {
- * depth: number
- * }
- * }} inlineObj Evil inline object! With a super duper duper long string
- * description that causes problems!
- * @param {function({
- * str: string
- * }):void} callback The callback to this heinous method
- * @return {{
- * str: string,
- * int: number
- * }}
- * @see https://developer.chrome.com/extensions/fakeJson#method-funcWithInlineObj
- */
-chrome.fakeJson.funcWithInlineObj = function(inlineObj, callback) {};
-"""
-
+expected_output = JsInterfaceGenerator.GetHeader(sys.argv[0], 'fakeApi') + """
+/** @interface */
+function FakeApi() {}
+
+FakeApi.prototype = {
+ /**
+ * Does something exciting! And what's more, this is a multiline function
+ * comment! It goes onto multiple lines!
+ * @param {!chrome.fakeApi.Baz} baz The baz to use.
+ * @param {function():void} callback
+ * @see https://developer.chrome.com/extensions/fakeApi#method-doSomething
+ */
+ doSomething: assertNotReached,
+
+ /**
+ * @param {function(!chrome.fakeApi.Baz, !chrome.fakeApi.Greek):void=} callback
+ * The callback which will most assuredly in all cases be called; that is,
+ * of course, iff such a callback was provided and is not at all null.
+ * @see https://developer.chrome.com/extensions/fakeApi#method-bazGreek
+ */
+ bazGreek: assertNotReached,
+
+ /**
+ * Fired when we realize it's a trap!
+ * @type {!ChromeEvent}
+ * @see https://developer.chrome.com/extensions/fakeApi#event-onTrapDetected
+ */
+ onTrapDetected: new ChromeEvent(),
+};"""
class JsExternGeneratorTest(unittest.TestCase):
- def _GetNamespace(self, fake_content, filename, is_idl):
+ def _GetNamespace(self, fake_content, filename):
"""Returns a namespace object for the given content"""
- api_def = (idl_schema.Process(fake_content, filename) if is_idl
- else json_parse.Parse(fake_content))
+ api_def = idl_schema.Process(fake_content, filename)
m = model.Model()
return m.AddNamespace(api_def[0], filename)
@@ -288,14 +113,10 @@ class JsExternGeneratorTest(unittest.TestCase):
self.maxDiff = None # Lets us see the full diff when inequal.
def testBasic(self):
- namespace = self._GetNamespace(fake_idl, 'fake_api.idl', True)
- self.assertMultiLineEqual(expected_output,
- JsExternsGenerator().Generate(namespace).Render())
-
- def testJsonWithInlineObjects(self):
- namespace = self._GetNamespace(fake_json, 'fake_api.json', False)
- self.assertMultiLineEqual(json_expected,
- JsExternsGenerator().Generate(namespace).Render())
+ namespace = self._GetNamespace(fake_idl, 'fake_api.idl')
+ self.assertMultiLineEqual(
+ expected_output,
+ JsInterfaceGenerator().Generate(namespace).Render())
if __name__ == '__main__':

Powered by Google App Engine
This is Rietveld 408576698