Debian Codegraph @

test/com/google/javascript/jscomp/InlineCostEstimatorTest.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 import com.google.javascript.rhino.Node;
20
21 import junit.framework.TestCase;
22
23
24 /**
25 * Unit test for {@link InlineCostEstimator}.
26 * @author [email protected] (John Lenz)
27 */
28 public class InlineCostEstimatorTest extends TestCase {
29
30 static Node parse(String js) {
31 Compiler compiler = new Compiler();
32 Node n = compiler.parseTestCode(js);
33 assertEquals(0, compiler.getErrorCount());
34 return n;
35 }
36
37 static String minimize(String js) {
38 CompilerOptions options = new CompilerOptions();
39 options.setLineLengthThreshold(Integer.MAX_VALUE);
40 return new CodePrinter.Builder(parse(js)).
41 setCompilerOptions(options).
42 build();
43 }
44
45 static long cost(String js) {
46 return InlineCostEstimator.getCost(parse(js));
47 }
48
49 public void testCost() {
50 checkCost("1", "1");
51 checkCost("true", "1");
52 checkCost("false", "1");
53 checkCost("a", "xx");
54 checkCost("a + b", "xx+xx");
55 checkCost("foo()", "xx()");
56 checkCost("foo(a,b)", "xx(xx,xx)");
57 checkCost("10 + foo(a,b)", "0+xx(xx,xx)");
58 checkCost("1 + foo(a,b)", "1+xx(xx,xx)");
59 checkCost("a ? 1 : 0", "xx?1:0");
60 checkCost("a.b", "xx.xx");
61 checkCost("new Obj()", "new xx");
62 checkCost("function a() {return \"monkey\"}",
63 "function xx(){return\"monkey\"}");
64 }
65
66 private void checkCost(String source, String example) {
67
68 // The example string should have been minified already.
69 assertEquals(minimize(example), example);
70
71 // cost estimate should be the same as the length of the example string.
72 assertEquals(example.length(), cost(source));
73 }
74 }