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..7e9d37b44d339651ddcf2ab2b08b81faaee676a7 |
--- /dev/null |
+++ b/tools/json_schema_compiler/js_externs_generator_test.py |
@@ -0,0 +1,114 @@ |
+#!/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 |
+ |
+ |
+# 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 |
+ |
+ |
+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()) |
+ |
+ |
+if __name__ == '__main__': |
+ unittest.main() |