Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2009 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 package com.google.javascript.jscomp;
17
18 /**
19 * Tests for ExportTestFunctions.
20 *
21 */
22 public class ExportTestFunctionsTest extends CompilerTestCase {
23
24 private static final String EXTERNS =
25 "function google_exportSymbol(a, b) {}; "
26 + "function google_exportProperty(a, b, c) {};";
27
28 private static final String TEST_FUNCTIONS_WITH_NAMES =
29 "function Foo(arg) {}; "
30 + "function setUp(arg3) {}; "
31 + "function tearDown(arg, arg2) {}; "
32 + "function testBar(arg) {}";
33
34 public ExportTestFunctionsTest() {
35 super(EXTERNS);
36 }
37
38 @Override
39 public void setUp() {
40 super.enableLineNumberCheck(false);
41 }
42
43 @Override
44 protected CompilerPass getProcessor(Compiler compiler) {
45 return new ExportTestFunctions(compiler, "google_exportSymbol",
46 "google_exportProperty");
47 }
48
49 @Override
50 protected int getNumRepetitions() {
51 // This pass only runs once.
52 return 1;
53 }
54
55 public void testFunctionsAreExported() {
56 test(TEST_FUNCTIONS_WITH_NAMES,
57 "function Foo(arg){}; "
58 + "function setUp(arg3){} google_exportSymbol(\"setUp\",setUp);; "
59 + "function tearDown(arg,arg2) {} "
60 + "google_exportSymbol(\"tearDown\",tearDown);; "
61 + "function testBar(arg){} google_exportSymbol(\"testBar\",testBar)"
62 );
63 }
64
65 // Helper functions
66 public void testBasicTestFunctionsAreExported() {
67 test("function Foo() {function testA() {}}",
68 "function Foo() {function testA(){}}");
69 test("function setUp() {}",
70 "function setUp(){} google_exportSymbol('setUp',setUp)");
71 test("function setUpPage() {}",
72 "function setUpPage(){} google_exportSymbol('setUpPage',setUpPage)");
73 test("function tearDown() {}",
74 "function tearDown(){} google_exportSymbol('tearDown',tearDown)");
75 test("function tearDownPage() {}",
76 "function tearDownPage(){} google_exportSymbol('tearDownPage'," +
77 "tearDownPage)");
78 test("function testBar() { function testB() {}}",
79 "function testBar(){function testB(){}}"
80 + "google_exportSymbol('testBar',testBar)");
81 testSame("var testCase = {}; testCase.setUpPage = function() {}");
82 }
83
84 /**
85 * Make sure this works for global functions declared as function expressions:
86 * <pre>
87 * var testFunctionName = function() {
88 * // Implementation
89 * };
90 * </pre>
91 * This format should be supported in addition to function statements.
92 */
93 public void testFunctionExpressionsAreExported() {
94 test("var Foo = function() {var testA = function() {}}",
95 "var Foo = function() {var testA = function() {}}");
96 test("var setUp = function() {}",
97 "var setUp = function() {}; " +
98 "google_exportSymbol('setUp',setUp)");
99 test("var setUpPage = function() {}",
100 "var setUpPage = function() {}; " +
101 "google_exportSymbol('setUpPage',setUpPage)");
102 test("var tearDown = function() {}",
103 "var tearDown = function() {}; " +
104 "google_exportSymbol('tearDown',tearDown)");
105 test("var tearDownPage = function() {}",
106 "var tearDownPage = function() {}; " +
107 "google_exportSymbol('tearDownPage', tearDownPage)");
108 test("var testBar = function() { var testB = function() {}}",
109 "var testBar = function(){ var testB = function() {}}; " +
110 "google_exportSymbol('testBar',testBar)");
111 }
112
113 public void testFunctionAssignmentsAreExported() {
114 test("Foo = {}; Foo.prototype.bar = function() {};",
115 "Foo = {}; Foo.prototype.bar = function() {};");
116
117 test("Foo = {}; Foo.prototype.setUpPage = function() {};",
118 "Foo = {}; Foo.prototype.setUpPage = function() {};"
119 + "google_exportProperty(Foo.prototype, 'setUpPage', "
120 + "Foo.prototype.setUpPage);");
121
122 test("Foo = {}; Foo.prototype.testBar = function() {};",
123 "Foo = {}; Foo.prototype.testBar = function() {};"
124 + "google_exportProperty(Foo.prototype, 'testBar', "
125 + "Foo.prototype.testBar);");
126
127 test("Foo = {}; Foo.prototype.testBar = function() "
128 + "{ var testBaz = function() {}};",
129 "Foo = {}; Foo.prototype.testBar = function() "
130 + "{ var testBaz = function() {}};"
131 + "google_exportProperty(Foo.prototype, 'testBar', "
132 + "Foo.prototype.testBar);");
133
134 test("Foo = {}; Foo.baz.prototype.testBar = function() "
135 + "{ var testBaz = function() {}};",
136 "Foo = {}; Foo.baz.prototype.testBar = function() "
137 + "{ var testBaz = function() {}};"
138 + "google_exportProperty(Foo.baz.prototype, 'testBar', "
139 + "Foo.baz.prototype.testBar);");
140 }
141 }