Debian Codegraph @

test/com/google/javascript/jscomp/CreateSyntheticBlocksTest.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 import com.google.javascript.rhino.Node;
19
20 /**
21 * Tests for {@link CreateSyntheticBlocks}
22 *
23 */
24 public class CreateSyntheticBlocksTest extends CompilerTestCase {
25 private static final String START_MARKER = "startMarker";
26 private static final String END_MARKER = "endMarker";
27
28 public CreateSyntheticBlocksTest() {
29 // Can't use compare as a tree because of the added synthetic blocks.
30 super("", false);
31 }
32
33 @Override
34 public void setUp() {
35 super.enableLineNumberCheck(false);
36 }
37
38 @Override
39 protected CompilerPass getProcessor(final Compiler compiler) {
40 return new CompilerPass() {
41 @Override
42 public void process(Node externs, Node js) {
43 new CreateSyntheticBlocks(compiler, START_MARKER, END_MARKER).process(
44 externs, js);
45 NodeTraversal.traverse(compiler, js, new MinimizeExitPoints(compiler));
46
47 new PeepholeOptimizationsPass(compiler,
48 new PeepholeRemoveDeadCode(),
49 new PeepholeSubstituteAlternateSyntax(true),
50 new PeepholeFoldConstants(true))
51 .process(externs, js);
52 new MinimizeExitPoints(compiler).process(externs, js);
53
54 new Denormalize(compiler).process(externs, js);
55 }
56 };
57 }
58
59 @Override
60 protected int getNumRepetitions() {
61 return 1;
62 }
63
64 // TODO(johnlenz): Add tests to the IntegrationTest.
65
66 public void testFold1() {
67 test("function f() { if (x) return; y(); }",
68 "function f(){x||y()}");
69 }
70
71 public void testFoldWithMarkers1() {
72 testSame("function f(){startMarker();if(x)return;endMarker();y()}");
73 }
74
75 public void testFoldWithMarkers1a() {
76 testSame("function f(){startMarker();if(x)return;endMarker()}");
77 }
78
79 public void testFold2() {
80 test("function f() { if (x) return; y(); if (a) return; b(); }",
81 "function f(){if(!x){y();a||b()}}");
82 }
83
84 public void testFoldWithMarkers2() {
85 testSame("function f(){startMarker(\"FOO\");startMarker(\"BAR\");" +
86 "if(x)return;endMarker(\"BAR\");y();if(a)return;" +
87 "endMarker(\"FOO\");b()}");
88 }
89
90 public void testUnmatchedStartMarker() {
91 testSame("startMarker()", CreateSyntheticBlocks.UNMATCHED_START_MARKER);
92 }
93
94 public void testUnmatchedEndMarker1() {
95 testSame("endMarker()", CreateSyntheticBlocks.UNMATCHED_END_MARKER);
96 }
97
98 public void testUnmatchedEndMarker2() {
99 test("if(y){startMarker();x()}endMarker()",
100 "if(y){startMarker();x()}endMarker()", null,
101 CreateSyntheticBlocks.UNMATCHED_END_MARKER);
102 }
103
104 public void testInvalid1() {
105 test("startMarker() && true",
106 "startMarker()", null,
107 CreateSyntheticBlocks.INVALID_MARKER_USAGE);
108 }
109
110 public void testInvalid2() {
111 test("false && endMarker()",
112 "", null,
113 CreateSyntheticBlocks.INVALID_MARKER_USAGE);
114 }
115
116
117 public void testDenormalize() {
118 testSame("startMarker();for(;;);endMarker()");
119 }
120
121 public void testNonMarkingUse() {
122 testSame("function foo(endMarker){}");
123 testSame("function foo(){startMarker:foo()}");
124 }
125
126 public void testContainingBlockPreservation() {
127 testSame("if(y){startMarker();x();endMarker()}");
128 }
129 }