Debian Codegraph @

test/com/google/javascript/jscomp/GenerateExportsTest.java

raw symbols SCIP
1 /*
2 * Copyright 2008 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 /**
20 * Generate exports unit test.
21 *
22 */
23 public class GenerateExportsTest extends CompilerTestCase {
24
25 private static final String EXTERNS =
26 "function google_exportSymbol(a, b) {}; " +
27 "goog.exportProperty = function(a, b, c) {}; ";
28
29 public GenerateExportsTest() {
30 super(EXTERNS);
31 }
32
33 @Override
34 protected CompilerPass getProcessor(Compiler compiler) {
35 return new GenerateExports(compiler,
36 "google_exportSymbol", "goog.exportProperty");
37 }
38
39 @Override
40 protected int getNumRepetitions() {
41 // This pass only runs once.
42 return 1;
43 }
44
45 @Override
46 public void setUp() throws Exception {
47 super.setUp();
48 super.enableLineNumberCheck(false);
49 }
50
51 public void testExportSymbol() {
52 test("/** @export */function foo() {}",
53 "function foo(){}google_exportSymbol(\"foo\",foo)");
54 }
55
56 public void testExportSymbolAndProperties() {
57 test("/** @export */function foo() {}" +
58 "/** @export */foo.prototype.bar = function() {}",
59 "function foo(){}" +
60 "google_exportSymbol(\"foo\",foo);" +
61 "foo.prototype.bar=function(){};" +
62 "goog.exportProperty(foo.prototype,\"bar\",foo.prototype.bar)");
63 }
64
65 public void testExportSymbolAndConstantProperties() {
66 test("/** @export */function foo() {}" +
67 "/** @export */foo.BAR = 5;",
68 "function foo(){}" +
69 "google_exportSymbol(\"foo\",foo);" +
70 "foo.BAR=5;" +
71 "goog.exportProperty(foo,\"BAR\",foo.BAR)");
72 }
73
74 public void testExportVars() {
75 test("/** @export */var FOO = 5",
76 "var FOO=5;" +
77 "google_exportSymbol(\"FOO\",FOO)");
78 }
79
80 public void testNoExport() {
81 test("var FOO = 5", "var FOO=5");
82 }
83
84 /**
85 * Nested assignments are ambiguous and therefore not supported.
86 * @see FindExportableNodes
87 */
88 public void testNestedVarAssign() {
89 test("var BAR;\n/** @export */var FOO = BAR = 5",
90 null, FindExportableNodes.NON_GLOBAL_ERROR);
91 }
92
93 /**
94 * Nested assignments are ambiguous and therefore not supported.
95 * @see FindExportableNodes
96 */
97 public void testNestedAssign() {
98 test("var BAR;var FOO = {};\n/** @export */FOO.test = BAR = 5",
99 null, FindExportableNodes.NON_GLOBAL_ERROR);
100 }
101
102 public void testNonGlobalScopeExport() {
103 test("(function() { /** @export */var FOO = 5 })()",
104 null, FindExportableNodes.NON_GLOBAL_ERROR);
105 }
106
107 public void testExportClass() {
108 test("/** @export */ function G() {} foo();",
109 "function G() {} google_exportSymbol('G', G); foo();");
110 }
111
112 public void testExportSubclass() {
113 test("var goog = {}; function F() {}" +
114 "/** @export */ function G() {} goog.inherits(G, F);",
115 "var goog = {}; function F() {}" +
116 "function G() {} goog.inherits(G, F); google_exportSymbol('G', G);");
117 }
118
119 public void testExportEnum() {
120 // TODO(johnlenz): Issue 310, should the values also be externed?
121 test("/** @enum {string}\n @export */ var E = {A:1, B:2};",
122 "/** @enum {string}\n @export */ var E = {A:1, B:2};" +
123 "google_exportSymbol('E', E);");
124 }
125 }