Debian Codegraph @

test/com/google/javascript/jscomp/DenormalizeTest.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
17 package com.google.javascript.jscomp;
18
19 import com.google.javascript.jscomp.Normalize.NormalizeStatements;
20 import com.google.javascript.rhino.Node;
21
22 /**
23 * @author [email protected] (John Lenz)
24 *
25 */
26 public class DenormalizeTest extends CompilerTestCase {
27 @Override
28 public CompilerPass getProcessor(final Compiler compiler) {
29 return new NormalizeAndDenormalizePass(compiler);
30 }
31
32 @Override
33 protected int getNumRepetitions() {
34 // The normalize pass is only run once.
35 return 1;
36 }
37
38 public void testFor() {
39 // Verify assignments are moved into the FOR init node.
40 test("a = 0; for(; a < 2 ; a++) foo()",
41 "for(a = 0; a < 2 ; a++) foo();");
42 // Verify vars are are moved into the FOR init node.
43 test("var a = 0; for(; c < b ; c++) foo()",
44 "for(var a = 0; c < b ; c++) foo()");
45
46 // We don't handle labels yet.
47 testSame("var a = 0; a:for(; c < b ; c++) foo()");
48 testSame("var a = 0; a:b:for(; c < b ; c++) foo()");
49
50 // Verify FOR inside IFs.
51 test("if(x){var a = 0; for(; c < b; c++) foo()}",
52 "if(x){for(var a = 0; c < b; c++) foo()}");
53
54 // Any other expression.
55 test("init(); for(; a < 2 ; a++) foo()",
56 "for(init(); a < 2 ; a++) foo();");
57
58 // Other statements are left as is.
59 test("function f(){ var a; for(; a < 2 ; a++) foo() }",
60 "function f(){ for(var a; a < 2 ; a++) foo() }");
61 testSame("function f(){ return; for(; a < 2 ; a++) foo() }");
62 }
63
64 public void testForIn() {
65 test("var a; for(a in b) foo()", "for (var a in b) foo()");
66 testSame("a = 0; for(a in b) foo()");
67 testSame("var a = 0; for(a in b) foo()");
68
69 // We don't handle labels yet.
70 testSame("var a; a:for(a in b) foo()");
71 testSame("var a; a:b:for(a in b) foo()");
72
73 // Verify FOR inside IFs.
74 test("if(x){var a; for(a in b) foo()}",
75 "if(x){for(var a in b) foo()}");
76
77 // Any other expression.
78 testSame("init(); for(a in b) foo()");
79
80 // Other statements are left as is.
81 testSame("function f(){ return; for(a in b) foo() }");
82 }
83
84 public void testInOperatorNotInsideFor() {
85 // in operators shouldn't be moved into for loops.
86 // Some JavaScript interpreters (such as the NetFront Access browser
87 // embedded in the PlayStation 3) will not parse an in operator in
88 // a for loop, even if it's protected by parentheses.
89
90 // Make sure the in operator doesn't get moved into the for loop.
91 testSame("function f(){ var a; var i=\"length\" in a;" +
92 "for(; a < 2 ; a++) foo() }");
93 // Same, but with parens around the operator.
94 testSame("function f(){ var a; var i=(\"length\" in a);" +
95 "for(; a < 2 ; a++) foo() }");
96 // Make sure Normalize yanks the variable initializer out, and
97 // Denormalize doesn't put it back.
98 test("function f(){" +
99 "var b,a=0; for (var i=(\"length\" in b);a<2; a++) foo()}",
100 "function f(){var b; var a=0;var i=(\"length\" in b);" +
101 "for (;a<2;a++) foo()}");
102 }
103
104 /**
105 * Create a class to combine the Normalize and Denormalize passes.
106 * This is needed because the enableNormalize() call on CompilerTestCase
107 * causes normalization of the result *and* the expected string, and
108 * we really don't want the compiler twisting the expected code around.
109 */
110 public class NormalizeAndDenormalizePass implements CompilerPass {
111 Denormalize denormalizePass;
112 NormalizeStatements normalizePass;
113 AbstractCompiler compiler;
114
115 public NormalizeAndDenormalizePass(AbstractCompiler compiler) {
116 this.compiler = compiler;
117 denormalizePass = new Denormalize(compiler);
118 normalizePass = new NormalizeStatements(compiler, false);
119 }
120
121 @Override
122 public void process(Node externs, Node root) {
123 NodeTraversal.traverse(compiler, root, normalizePass);
124 NodeTraversal.traverse(compiler, root, denormalizePass);
125 }
126 }
127
128 }