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 from js_externs_generator import JsExternsGenerator |
| 8 from datetime import datetime |
| 9 import model |
| 10 import unittest |
| 11 |
| 12 |
| 13 # The contents of a fake idl file. |
| 14 fake_idl = """ |
| 15 // Copyright %s The Chromium Authors. All rights reserved. |
| 16 // Use of this source code is governed by a BSD-style license that can be |
| 17 // found in the LICENSE file. |
| 18 |
| 19 // A totally fake API. |
| 20 namespace fakeApi { |
| 21 enum Greek { |
| 22 ALPHA, |
| 23 BETA, |
| 24 GAMMA, |
| 25 DELTA |
| 26 }; |
| 27 |
| 28 dictionary Bar { |
| 29 long num; |
| 30 }; |
| 31 |
| 32 dictionary Baz { |
| 33 DOMString str; |
| 34 long num; |
| 35 boolean b; |
| 36 Greek letter; |
| 37 long[] arr; |
| 38 Bar obj; |
| 39 long? maybe; |
| 40 }; |
| 41 |
| 42 callback VoidCallback = void(); |
| 43 |
| 44 interface Functions { |
| 45 // Does something exciting! |
| 46 // |baz| : The baz to use. |
| 47 static void doSomething(Baz baz, optional VoidCallback callback); |
| 48 }; |
| 49 }; |
| 50 """ |
| 51 |
| 52 # The output we expect from our fake idl file. |
| 53 expected_output = """// Copyright %s The Chromium Authors. All rights reserved. |
| 54 // Use of this source code is governed by a BSD-style license that can be |
| 55 // found in the LICENSE file. |
| 56 |
| 57 /** @fileoverview Externs generated from namespace: fakeApi */ |
| 58 |
| 59 /** |
| 60 * @const |
| 61 */ |
| 62 chrome.fakeApi = {}; |
| 63 |
| 64 /** |
| 65 * @enum {string} |
| 66 */ |
| 67 chrome.fakeApi.Greek = { |
| 68 ALPHA: 'ALPHA', |
| 69 BETA: 'BETA', |
| 70 GAMMA: 'GAMMA', |
| 71 DELTA: 'DELTA', |
| 72 }; |
| 73 |
| 74 /** |
| 75 * @typedef {{ |
| 76 * num: number |
| 77 * }} |
| 78 */ |
| 79 var Bar; |
| 80 |
| 81 /** |
| 82 * @typedef {{ |
| 83 * str: string, |
| 84 * num: number, |
| 85 * b: boolean, |
| 86 * letter: chrome.fakeApi.Greek, |
| 87 * arr: Array, |
| 88 * obj: Bar, |
| 89 * maybe: (number|undefined) |
| 90 * }} |
| 91 */ |
| 92 var Baz; |
| 93 |
| 94 /** |
| 95 * Does something exciting! |
| 96 * @param {Baz} baz The baz to use. |
| 97 * @param {Function=} callback |
| 98 */ |
| 99 chrome.fakeApi.doSomething = function(baz, callback) {}; |
| 100 """ % datetime.now().year |
| 101 |
| 102 |
| 103 class JsExternGeneratorTest(unittest.TestCase): |
| 104 def testBasic(self): |
| 105 filename = 'fake_api.idl' |
| 106 api_def = idl_schema.Process(fake_idl, filename) |
| 107 m = model.Model() |
| 108 namespace = m.AddNamespace(api_def[0], filename) |
| 109 self.assertEquals(expected_output, |
| 110 JsExternsGenerator().Generate(namespace).Render()) |
| 111 |
| 112 |
| 113 if __name__ == '__main__': |
| 114 unittest.main() |
OLD | NEW |