Chromium Code Reviews| Index: tools/json_schema_compiler/js_externs_generator_test.py |
| diff --git a/tools/json_schema_compiler/js_externs_generator_test.py b/tools/json_schema_compiler/js_externs_generator_test.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..4cd294e300ffa4c475d0a41403b8d1ab260cc832 |
| --- /dev/null |
| +++ b/tools/json_schema_compiler/js_externs_generator_test.py |
| @@ -0,0 +1,111 @@ |
| +#!/usr/bin/env python |
| +# Copyright 2015 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. |
| + |
| +import idl_schema |
| +from js_externs_generator import JsExternsGenerator |
| +from datetime import datetime |
| +import model |
| +import unittest |
| + |
|
Dan Beam
2015/03/24 21:19:18
nit: \n\n
Devlin
2015/03/24 21:26:26
Done.
|
| +# The contents of a fake idl file. |
| +fake_idl = """ |
| +// 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. |
| + |
| +// 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; |
| + long[] arr; |
| + Bar obj; |
| + long? maybe; |
| + }; |
| + |
| + callback VoidCallback = void(); |
| + |
| + interface Functions { |
| + // Does something exciting! |
| + // |baz| : The baz to use. |
| + static void doSomething(Baz baz, optional VoidCallback callback); |
| + }; |
| +}; |
| +""" |
| + |
| +# The output we expect from our fake idl file. |
| +expected_output = """// 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. |
| + |
| +/** @fileoverview Externs generated from namespace: fakeApi */ |
| + |
| +/** |
| + * @const |
| + */ |
| +chrome.fakeApi = {}; |
| + |
| +/** |
| + * @enum {string} |
| + */ |
| +chrome.fakeApi.Greek = { |
| + ALPHA: 'ALPHA', |
| + BETA: 'BETA', |
| + GAMMA: 'GAMMA', |
| + DELTA: 'DELTA', |
| +}; |
| + |
| +/** |
| + * @typedef {{ |
| + * num: number |
| + * }} |
| + */ |
| +var Bar; |
| + |
| +/** |
| + * @typedef {{ |
| + * str: string, |
| + * num: number, |
| + * b: boolean, |
| + * letter: chrome.fakeApi.Greek, |
| + * arr: Array, |
| + * obj: Bar, |
| + * maybe: (number|undefined) |
| + * }} |
| + */ |
| +var Baz; |
| + |
| +/** |
| + * Does something exciting! |
| + * @param {Baz} baz The baz to use. |
| + * @param {Function=} callback |
| + */ |
| +chrome.fakeApi.doSomething = function(baz, callback) {}; |
| +""" % datetime.now().year |
| + |
|
Dan Beam
2015/03/24 21:19:18
nit: \n\n
Devlin
2015/03/24 21:26:26
Done.
|
| +class JsExternGeneratorTest(unittest.TestCase): |
| + def testBasic(self): |
| + filename = 'fake_api.idl' |
| + api_def = idl_schema.Process(fake_idl, filename) |
| + m = model.Model() |
| + namespace = m.AddNamespace(api_def[0], filename) |
| + self.assertEquals(expected_output, |
| + JsExternsGenerator().Generate(namespace).Render()) |
| + |
|
Dan Beam
2015/03/24 21:19:18
nit: \n\n
Devlin
2015/03/24 21:26:26
Done.
|
| +if __name__ == '__main__': |
| + unittest.main() |