Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2012 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 import com.google.common.base.Joiner;
20 import com.google.common.collect.ImmutableList;
21 import com.google.common.collect.Lists;
22 import com.google.javascript.rhino.Node;
23
24 import junit.framework.TestCase;
25
26 import java.util.List;
27
28 /**
29 * Framework for end-to-end test cases.
30 *
31 * @author [email protected] (Nick Santos)
32 */
33 abstract class IntegrationTestCase extends TestCase {
34
35 /** Externs for the test */
36 protected final List<SourceFile> DEFAULT_EXTERNS = ImmutableList.of(
37 SourceFile.fromCode("externs",
38 "var arguments;\n"
39 + "/** @constructor */ function Window() {}\n"
40 + "/** @type {string} */ Window.prototype.name;\n"
41 + "/** @type {string} */ Window.prototype.offsetWidth;\n"
42 + "/** @type {Window} */ var window;\n"
43 + "/** @nosideeffects */ function noSideEffects() {}\n"
44 + "/** @constructor\n * @nosideeffects */ function Widget() {}\n"
45 + "/** @modifies {this} */ Widget.prototype.go = function() {};\n"
46 + "/** @return {string} */ var widgetToken = function() {};\n"
47 + "function alert(message) {}"
48 + "function Object() {}"
49 + "Object.seal;"));
50
51 protected List<SourceFile> externs = DEFAULT_EXTERNS;
52
53 // The most recently used compiler.
54 protected Compiler lastCompiler;
55
56 protected boolean normalizeResults = false;
57
58 @Override
59 public void setUp() {
60 externs = DEFAULT_EXTERNS;
61 lastCompiler = null;
62 normalizeResults = false;
63 }
64
65 protected void testSame(CompilerOptions options, String original) {
66 testSame(options, new String[] { original });
67 }
68
69 protected void testSame(CompilerOptions options, String[] original) {
70 test(options, original, original);
71 }
72
73 /**
74 * Asserts that when compiling with the given compiler options,
75 * {@code original} is transformed into {@code compiled}.
76 */
77 protected void test(CompilerOptions options,
78 String original, String compiled) {
79 test(options, new String[] { original }, new String[] { compiled });
80 }
81
82 /**
83 * Asserts that when compiling with the given compiler options,
84 * {@code original} is transformed into {@code compiled}.
85 */
86 protected void test(CompilerOptions options,
87 String[] original, String[] compiled) {
88 Compiler compiler = compile(options, original);
89 assertEquals("Expected no warnings or errors\n" +
90 "Errors: \n" + Joiner.on("\n").join(compiler.getErrors()) +
91 "Warnings: \n" + Joiner.on("\n").join(compiler.getWarnings()),
92 0, compiler.getErrors().length + compiler.getWarnings().length);
93
94 Node root = compiler.getRoot().getLastChild();
95 Node expectedRoot = parse(compiled, options, normalizeResults);
96 String explanation = expectedRoot.checkTreeEquals(root);
97 assertNull("\nExpected: " + compiler.toSource(expectedRoot) +
98 "\nResult: " + compiler.toSource(root) +
99 "\n" + explanation, explanation);
100 }
101
102 /**
103 * Asserts that when compiling with the given compiler options,
104 * there is an error or warning.
105 */
106 protected void test(CompilerOptions options,
107 String original, DiagnosticType warning) {
108 test(options, new String[] { original }, warning);
109 }
110
111 protected void test(CompilerOptions options,
112 String original, String compiled, DiagnosticType warning) {
113 test(options, new String[] { original }, new String[] { compiled },
114 warning);
115 }
116
117 protected void test(CompilerOptions options,
118 String[] original, DiagnosticType warning) {
119 test(options, original, null, warning);
120 }
121
122 /**
123 * Asserts that when compiling with the given compiler options,
124 * there is an error or warning.
125 */
126 protected void test(CompilerOptions options,
127 String[] original, String[] compiled, DiagnosticType warning) {
128 Compiler compiler = compile(options, original);
129 checkUnexpectedErrorsOrWarnings(compiler, 1);
130 assertEquals("Expected exactly one warning or error",
131 1, compiler.getErrors().length + compiler.getWarnings().length);
132 if (compiler.getErrors().length > 0) {
133 assertEquals(warning, compiler.getErrors()[0].getType());
134 } else {
135 assertEquals(warning, compiler.getWarnings()[0].getType());
136 }
137
138 if (compiled != null) {
139 Node root = compiler.getRoot().getLastChild();
140 Node expectedRoot = parse(compiled, options, normalizeResults);
141 String explanation = expectedRoot.checkTreeEquals(root);
142 assertNull("\nExpected: " + compiler.toSource(expectedRoot) +
143 "\nResult: " + compiler.toSource(root) +
144 "\n" + explanation, explanation);
145 }
146 }
147
148 /**
149 * Asserts that when compiling with the given compiler options,
150 * there is an error or warning.
151 */
152 protected void test(CompilerOptions options,
153 String[] original, String[] compiled, DiagnosticType[] warnings) {
154 Compiler compiler = compile(options, original);
155 checkUnexpectedErrorsOrWarnings(compiler, warnings.length);
156
157 if (compiled != null) {
158 Node root = compiler.getRoot().getLastChild();
159 Node expectedRoot = parse(compiled, options, normalizeResults);
160 String explanation = expectedRoot.checkTreeEquals(root);
161 assertNull("\nExpected: " + compiler.toSource(expectedRoot) +
162 "\nResult: " + compiler.toSource(root) +
163 "\n" + explanation, explanation);
164 }
165 }
166
167 protected void checkUnexpectedErrorsOrWarnings(
168 Compiler compiler, int expected) {
169 int actual = compiler.getErrors().length + compiler.getWarnings().length;
170 if (actual != expected) {
171 String msg = "";
172 for (JSError err : compiler.getErrors()) {
173 msg += "Error:" + err.toString() + "\n";
174 }
175 for (JSError err : compiler.getWarnings()) {
176 msg += "Warning:" + err.toString() + "\n";
177 }
178 assertEquals("Unexpected warnings or errors.\n " + msg,
179 expected, actual);
180 }
181 }
182
183 protected Compiler compile(CompilerOptions options, String original) {
184 return compile(options, new String[] { original });
185 }
186
187 protected Compiler compile(CompilerOptions options, String[] original) {
188 Compiler compiler = lastCompiler = new Compiler();
189 List<SourceFile> inputs = Lists.newArrayList();
190 for (int i = 0; i < original.length; i++) {
191 inputs.add(SourceFile.fromCode("input" + i, original[i]));
192 }
193 compiler.compileModules(
194 externs, Lists.newArrayList(CompilerTestCase.createModuleChain(original)),
195 options);
196 return compiler;
197 }
198
199 protected Node parse(
200 String[] original, CompilerOptions options, boolean normalize) {
201 Compiler compiler = new Compiler();
202 List<SourceFile> inputs = Lists.newArrayList();
203 for (int i = 0; i < original.length; i++) {
204 inputs.add(SourceFile.fromCode("input" + i, original[i]));
205 }
206 compiler.init(externs, inputs, options);
207 checkUnexpectedErrorsOrWarnings(compiler, 0);
208 Node all = compiler.parseInputs();
209 checkUnexpectedErrorsOrWarnings(compiler, 0);
210 Node n = all.getLastChild();
211 Node externs = all.getFirstChild();
212
213 (new CreateSyntheticBlocks(
214 compiler, "synStart", "synEnd")).process(externs, n);
215
216 if (normalize) {
217 compiler.normalize();
218 }
219
220 return n;
221 }
222
223 /** Creates a CompilerOptions object with google coding conventions. */
224 abstract CompilerOptions createCompilerOptions();
225 }