| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
|
| 20 |
|
| 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 |
|
| 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 |
|
| 86 |
|
| 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 |
|
| 95 |
|
| 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 |
|
| 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 |
} |