Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2011 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.AstValidator.ViolationHandler;
20 import com.google.javascript.rhino.InputId;
21 import com.google.javascript.rhino.Node;
22 import com.google.javascript.rhino.Token;
23 import com.google.javascript.rhino.jstype.SimpleSourceFile;
24
25
26 /**
27 * @author [email protected] (John Lenz)
28 */
29 public class AstValidatorTest extends CompilerTestCase {
30
31 private boolean lastCheckWasValid = true;
32
33 @Override
34 protected CompilerPass getProcessor(Compiler compiler) {
35 return createValidator();
36 }
37
38 private AstValidator createValidator() {
39 lastCheckWasValid = true;
40 return new AstValidator(new ViolationHandler() {
41 @Override
42 public void handleViolation(String message, Node n) {
43 lastCheckWasValid = false;
44 }
45 });
46 }
47
48 @Override
49 protected int getNumRepetitions() {
50 return 1;
51 }
52
53 @Override
54 protected void setUp() throws Exception {
55 super.enableAstValidation(false);
56 super.disableNormalize();
57 super.enableLineNumberCheck(false);
58 super.setUp();
59 }
60
61 public void testForIn() {
62 valid("for(var a in b);");
63 valid("for(var a = 1 in b);");
64 valid("for(a in b);");
65 valid("for(a in []);");
66 valid("for(a in {});");
67 }
68
69 public void testDebugger() {
70 valid("debugger;");
71 }
72
73 public void testValidScript() {
74 Node n = new Node(Token.SCRIPT);
75 expectInvalid(n, Check.SCRIPT);
76 n.setInputId(new InputId("something_input"));
77 n.setStaticSourceFile(new SimpleSourceFile("something", false));
78 expectValid(n, Check.SCRIPT);
79 expectInvalid(n, Check.STATEMENT);
80 expectInvalid(n, Check.EXPRESSION);
81 }
82
83 public void testValidStatement1() {
84 Node n = new Node(Token.RETURN);
85 expectInvalid(n, Check.EXPRESSION);
86 expectValid(n, Check.STATEMENT);
87 expectInvalid(n, Check.SCRIPT);
88 }
89
90 public void testValidExpression1() {
91 Node n = new Node(Token.ARRAYLIT, new Node(Token.EMPTY));
92 expectValid(n, Check.EXPRESSION);
93 expectInvalid(n, Check.STATEMENT);
94 expectInvalid(n, Check.SCRIPT);
95 }
96
97 public void testValidExpression2() {
98 Node n = new Node(Token.NOT, new Node(Token.TRUE));
99 expectValid(n, Check.EXPRESSION);
100 expectInvalid(n, Check.STATEMENT);
101 expectInvalid(n, Check.SCRIPT);
102 }
103
104 public void testInvalidEmptyStatement() {
105 Node n = new Node(Token.EMPTY, new Node(Token.TRUE));
106 expectInvalid(n, Check.STATEMENT);
107 n.detachChildren();
108 expectValid(n, Check.STATEMENT);
109 }
110
111 private void valid(String code) {
112 testSame(code);
113 assertTrue(lastCheckWasValid);
114 }
115
116 private enum Check {
117 SCRIPT,
118 STATEMENT,
119 EXPRESSION
120 }
121
122 private boolean doCheck(Node n, Check level) {
123 AstValidator validator = createValidator();
124 switch (level) {
125 case SCRIPT:
126 validator.validateScript(n);
127 break;
128 case STATEMENT:
129 validator.validateStatement(n);
130 break;
131 case EXPRESSION:
132 validator.validateExpression(n);
133 break;
134 }
135 return lastCheckWasValid;
136 }
137
138 private void expectInvalid(Node n, Check level) {
139 assertFalse(doCheck(n, level));
140 }
141
142 private void expectValid(Node n, Check level) {
143 assertTrue(doCheck(n, level));
144 }
145 }