| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 26 |
|
| 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 |
|
| 69 |
assertEquals(minimize(example), example); |
| 70 |
|
| 71 |
|
| 72 |
assertEquals(example.length(), cost(source)); |
| 73 |
} |
| 74 |
} |