Debian Codegraph @

test/com/google/javascript/jscomp/SanityCheckTest.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.AbstractCompiler.LifeCycleStage;
20 import com.google.javascript.rhino.Node;
21 import com.google.javascript.rhino.Token;
22
23 /**
24 * @author [email protected] (Nick Santos)
25 */
26 public class SanityCheckTest extends CompilerTestCase {
27
28 private CompilerPass otherPass = null;
29
30 public SanityCheckTest() {
31 super("", false);
32 }
33
34 @Override public void setUp() {
35 otherPass = null;
36 }
37
38 @Override protected int getNumRepetitions() {
39 return 1;
40 }
41
42 @Override public CompilerPass getProcessor(final Compiler compiler) {
43 return new CompilerPass() {
44 @Override public void process(Node externs, Node root) {
45 otherPass.process(externs, root);
46 (new SanityCheck(compiler)).process(externs, root);
47 }
48 };
49 }
50
51 public void testUnnormalizeNodeTypes() throws Exception {
52 otherPass = new CompilerPass() {
53 @Override public void process(Node externs, Node root) {
54 getLastCompiler().reportCodeChange();
55 root.getFirstChild().addChildToBack(
56 new Node(Token.IF, new Node(Token.TRUE), new Node(Token.EMPTY)));
57 }
58 };
59
60 boolean exceptionCaught = false;
61 try {
62 test("var x = 3;", "var x=3;0;0");
63 } catch (IllegalStateException e) {
64 assertEquals("Expected BLOCK but was EMPTY Reference node EMPTY",
65 e.getMessage());
66 exceptionCaught = true;
67 }
68 assertTrue(exceptionCaught);
69 }
70
71 public void testUnnormalized() throws Exception {
72 otherPass = new CompilerPass() {
73 @Override public void process(Node externs, Node root) {
74 getLastCompiler().setLifeCycleStage(LifeCycleStage.NORMALIZED);
75 }
76 };
77
78 boolean exceptionCaught = false;
79 try {
80 test("while(1){}", "while(1){}");
81 } catch (RuntimeException e) {
82 assertTrue(e.getMessage().contains(
83 "Normalize constraints violated:\nWHILE node"));
84 exceptionCaught = true;
85 }
86 assertTrue(exceptionCaught);
87 }
88
89 public void testConstantAnnotationMismatch() throws Exception {
90 otherPass = new CompilerPass() {
91 @Override public void process(Node externs, Node root) {
92 getLastCompiler().reportCodeChange();
93 Node name = Node.newString(Token.NAME, "x");
94 name.putBooleanProp(Node.IS_CONSTANT_NAME, true);
95 root.getFirstChild().addChildToBack(new Node(Token.EXPR_RESULT, name));
96 getLastCompiler().setLifeCycleStage(LifeCycleStage.NORMALIZED);
97 }
98 };
99
100 boolean exceptionCaught = false;
101 try {
102 test("var x;", "var x; x;");
103 } catch (RuntimeException e) {
104 assertTrue(e.getMessage().contains(
105 "The name x is not consistently annotated as constant."));
106 exceptionCaught = true;
107 }
108 assertTrue(exceptionCaught);
109 }
110 }