1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
#!/usr/bin/env python3
# Copyright 2015 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import idl_schema
import json_parse
from js_interface_generator import JsInterfaceGenerator
from datetime import datetime
import model
import sys
import unittest
# The contents of a fake idl file.
fake_idl = """
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// A totally fake API.
namespace fakeApi {
enum Greek {
ALPHA,
BETA,
GAMMA,
DELTA
};
dictionary Bar {
long num;
};
dictionary Baz {
DOMString str;
long num;
boolean b;
Greek letter;
Greek? optionalLetter;
long[] arr;
Bar[]? optionalObjArr;
Greek[] enumArr;
any[] anythingGoes;
Bar obj;
long? maybe;
(DOMString or Greek or long[]) choice;
object plainObj;
};
callback VoidCallback = void();
callback BazGreekCallback = void(Baz baz, Greek greek);
interface Functions {
// Does something exciting! And what's more, this is a multiline function
// comment! It goes onto multiple lines!
// |baz| : The baz to use.
static void doSomething(Baz baz, VoidCallback callback);
// |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.
static void bazGreek(optional BazGreekCallback callback);
[deprecated="Use a new method."] static DOMString returnString();
};
interface Events {
// Fired when we realize it's a trap!
static void onTrapDetected(Baz baz);
};
};
"""
# The output we expect from our fake idl file.
fake_idl_output = ("""// Copyright %s The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file was generated by:
// %s.
/** @fileoverview Interface for fakeApi that can be overriden. */
/** @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: 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
*/
bazGreek: function(callback) {},
};
/**
* Fired when we realize it's a trap!
* @type {!ChromeEvent}
* @see https://developer.chrome.com/extensions/fakeApi#event-onTrapDetected
*/
FakeApi.prototype.onTrapDetected;""" % (datetime.now().year,
sys.argv[0].replace('\\', '/')))
class JsExternGeneratorTest(unittest.TestCase):
def _GetNamespace(self, fake_content, filename):
"""Returns a namespace object for the given content"""
api_def = idl_schema.Process(fake_content, filename)
m = model.Model()
return m.AddNamespace(api_def[0], filename)
def setUp(self):
self.maxDiff = None # Lets us see the full diff when inequal.
def testBasic(self):
namespace = self._GetNamespace(fake_idl, 'fake_api.idl')
self.assertMultiLineEqual(
fake_idl_output,
JsInterfaceGenerator().Generate(namespace).Render())
if __name__ == '__main__':
unittest.main()
|