OLD | NEW |
(Empty) | |
| 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. |
| 5 |
| 6 import idl_schema |
| 7 import json_parse |
| 8 from js_interface_generator import JsInterfaceGenerator |
| 9 from datetime import datetime |
| 10 import model |
| 11 import sys |
| 12 import unittest |
| 13 |
| 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. |
| 19 |
| 20 // A totally fake API. |
| 21 namespace fakeApi { |
| 22 enum Grk { |
| 23 ALPHA, |
| 24 BETA, |
| 25 GAMMA, |
| 26 DELTA |
| 27 }; |
| 28 |
| 29 dictionary Bar { |
| 30 long num; |
| 31 }; |
| 32 |
| 33 dictionary Baz { |
| 34 DOMString str; |
| 35 long num; |
| 36 boolean b; |
| 37 Grk letter; |
| 38 Grk? optionalLetter; |
| 39 long[] arr; |
| 40 Bar[]? optionalObjArr; |
| 41 Grk[] enumArr; |
| 42 any[] anythingGoes; |
| 43 Bar obj; |
| 44 long? maybe; |
| 45 (DOMString or Grk or long[]) choice; |
| 46 object plainObj; |
| 47 }; |
| 48 |
| 49 callback VoidCallback = void(); |
| 50 |
| 51 callback BazGrkCallback = void(Baz baz, Grk greek); |
| 52 |
| 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); |
| 58 |
| 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 bazGrk(optional BazGrkCallback callback); |
| 63 |
| 64 [deprecated="Use a new method."] static DOMString returnString(); |
| 65 }; |
| 66 |
| 67 interface Events { |
| 68 // Fired when we realize it's a trap! |
| 69 static void onTrapDetected(Baz baz); |
| 70 }; |
| 71 }; |
| 72 """ |
| 73 |
| 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. |
| 78 |
| 79 // This file was generated by: |
| 80 // %s. |
| 81 |
| 82 /** @fileoverview Interface for fakeApi that can be overriden. */ |
| 83 |
| 84 assertNotReached('Interface file for Closure Compiler should not be executed.'); |
| 85 |
| 86 /** @interface */ |
| 87 function FakeApi() {} |
| 88 |
| 89 FakeApi.prototype = { |
| 90 /** |
| 91 * Does something exciting! And what's more, this is a multiline function |
| 92 * comment! It goes onto multiple lines! |
| 93 * @param {!chrome.fakeApi.Baz} baz The baz to use. |
| 94 * @param {function():void} callback |
| 95 * @see https://developer.chrome.com/extensions/fakeApi#method-doSomething |
| 96 */ |
| 97 doSomething: assertNotReached, |
| 98 |
| 99 /** |
| 100 * @param {function(!chrome.fakeApi.Baz, !chrome.fakeApi.Grk):void=} callback |
| 101 * The callback which will most assuredly in all cases be called; that is, |
| 102 * of course, iff such a callback was provided and is not at all null. |
| 103 * @see https://developer.chrome.com/extensions/fakeApi#method-bazGrk |
| 104 */ |
| 105 bazGrk: assertNotReached, |
| 106 |
| 107 /** |
| 108 * Fired when we realize it's a trap! |
| 109 * @type {!ChromeEvent} |
| 110 * @see https://developer.chrome.com/extensions/fakeApi#event-onTrapDetected |
| 111 */ |
| 112 onTrapDetected: new ChromeEvent(), |
| 113 };""" % (datetime.now().year, sys.argv[0])) |
| 114 |
| 115 class JsExternGeneratorTest(unittest.TestCase): |
| 116 def _GetNamespace(self, fake_content, filename): |
| 117 """Returns a namespace object for the given content""" |
| 118 api_def = idl_schema.Process(fake_content, filename) |
| 119 m = model.Model() |
| 120 return m.AddNamespace(api_def[0], filename) |
| 121 |
| 122 def setUp(self): |
| 123 self.maxDiff = None # Lets us see the full diff when inequal. |
| 124 |
| 125 def testBasic(self): |
| 126 namespace = self._GetNamespace(fake_idl, 'fake_api.idl') |
| 127 self.assertMultiLineEqual( |
| 128 expected_output, |
| 129 JsInterfaceGenerator().Generate(namespace).Render()) |
| 130 |
| 131 |
| 132 if __name__ == '__main__': |
| 133 unittest.main() |
OLD | NEW |