Chromium Code Reviews| 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 Greek { | |
| 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 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; | |
| 47 }; | |
| 48 | |
| 49 callback VoidCallback = void(); | |
| 50 | |
| 51 callback BazGreekCallback = void(Baz baz, Greek 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 bazGreek(optional BazGreekCallback 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 = JsInterfaceGenerator.GetHeader(sys.argv[0], 'fakeApi') + """ | |
| 76 | |
| 77 /** @interface */ | |
| 78 function FakeApi() {} | |
| 79 | |
| 80 FakeApi.prototype = { | |
| 81 /** | |
| 82 * Does something exciting! And what's more, this is a multiline function | |
|
Devlin
2015/12/02 17:58:28
Will these closure docs conflict with the externs?
stevenjb
2015/12/02 18:07:40
Any @param/@return needs to match, that's pretty m
| |
| 83 * comment! It goes onto multiple lines! | |
| 84 * @param {!chrome.fakeApi.Baz} baz The baz to use. | |
| 85 * @param {function():void} callback | |
| 86 */ | |
| 87 doSomething: assertNotReached, | |
| 88 | |
| 89 /** | |
| 90 * @param {function(!chrome.fakeApi.Baz, !chrome.fakeApi.Greek):void=} callbac k | |
| 91 * The callback which will most assuredly in all cases be called; that is, | |
| 92 * of course, iff such a callback was provided and is not at all null. | |
| 93 */ | |
| 94 bazGreek: assertNotReached, | |
| 95 | |
| 96 /** | |
| 97 * Fired when we realize it's a trap! | |
| 98 * @type {!ChromeEvent} | |
| 99 */ | |
| 100 onTrapDetected: new ChromeEvent(), | |
| 101 | |
| 102 };""" | |
| 103 | |
| 104 class JsExternGeneratorTest(unittest.TestCase): | |
| 105 def _GetNamespace(self, fake_content, filename): | |
| 106 """Returns a namespace object for the given content""" | |
| 107 api_def = idl_schema.Process(fake_content, filename) | |
| 108 m = model.Model() | |
| 109 return m.AddNamespace(api_def[0], filename) | |
| 110 | |
| 111 def setUp(self): | |
| 112 self.maxDiff = None # Lets us see the full diff when inequal. | |
| 113 | |
| 114 def testBasic(self): | |
| 115 namespace = self._GetNamespace(fake_idl, 'fake_api.idl') | |
| 116 self.assertMultiLineEqual( | |
| 117 expected_output, | |
| 118 JsInterfaceGenerator().Generate(namespace).Render()) | |
| 119 | |
| 120 | |
| 121 if __name__ == '__main__': | |
| 122 unittest.main() | |
| OLD | NEW |