Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2012 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 /**
20 * Tests for CheckSuspiciousCode
21 */
22 public class CheckSuspiciousCodeTest extends CompilerTestCase {
23 public CheckSuspiciousCodeTest() {
24 this.parseTypeInfo = true;
25 }
26
27 @Override
28 protected CompilerPass getProcessor(Compiler compiler) {
29 return new CombinedCompilerPass(compiler,
30 new CheckSuspiciousCode());
31 }
32
33 @Override
34 protected int getNumRepetitions() {
35 return 1;
36 }
37
38 public void test(String js, DiagnosticType error) {
39 test(js, js, null, error);
40 }
41
42 public void testSuspiciousSemi() {
43 final DiagnosticType e = CheckSuspiciousCode.SUSPICIOUS_SEMICOLON;
44 final DiagnosticType ok = null; // code is 'ok', verify no warning
45
46 test("if(x()) x = y;", ok);
47 test("if(x()); x = y;", e); // I've had this bug, damned ;
48 test("if(x()){} x = y;", ok);
49
50 test("if(x()) x = y; else y=z;", ok);
51 test("if(x()); else y=z;", e);
52 test("if(x()){} else y=z;", ok);
53 test("if(x()) x = y; else;", e);
54 test("if(x()) x = y; else {}", ok);
55
56 test("while(x()) x = y;", ok);
57 test("while(x()); x = y;", e);
58 test("while(x()){} x = y;", ok);
59 test("while(x()); {x = y}", e);
60 test("while(x()){} {x = y}", ok);
61
62 test("for(;;) x = y;", ok);
63 test("for(;;); x = y;", e);
64 test("for(;;){} x = y;", ok);
65 test("for(x in y) x = y;", ok);
66 test("for(x in y); x = y;", e);
67 test("for(x in y){} x = y;", ok);
68 }
69
70 private void testReportNaN(String js) {
71 testSame(js, CheckSuspiciousCode.SUSPICIOUS_COMPARISON_WITH_NAN);
72 }
73
74 public void testComparison1() {
75 testReportNaN("x == NaN");
76 testReportNaN("x != NaN");
77 testReportNaN("x === NaN");
78 testReportNaN("x !== NaN");
79 testReportNaN("x < NaN");
80 testReportNaN("x <= NaN");
81 testReportNaN("x > NaN");
82 testReportNaN("x >= NaN");
83 }
84
85 public void testComparison2() {
86 testReportNaN("NaN == x");
87 testReportNaN("NaN != x");
88 testReportNaN("NaN === x");
89 testReportNaN("NaN !== x");
90 testReportNaN("NaN < x");
91 testReportNaN("NaN <= x");
92 testReportNaN("NaN > x");
93 testReportNaN("NaN >= x");
94 }
95
96 public void testComparison3() {
97 testReportNaN("x == 0/0");
98 testReportNaN("x != 0/0");
99 testReportNaN("x === 0/0");
100 testReportNaN("x !== 0/0");
101 testReportNaN("x < 0/0");
102 testReportNaN("x <= 0/0");
103 testReportNaN("x > 0/0");
104 testReportNaN("x >= 0/0");
105 }
106
107 public void testComparison4() {
108 testReportNaN("0/0 == x");
109 testReportNaN("0/0 != x");
110 testReportNaN("0/0 === x");
111 testReportNaN("0/0 !== x");
112 testReportNaN("0/0 < x");
113 testReportNaN("0/0 <= x");
114 testReportNaN("0/0 > x");
115 testReportNaN("0/0 >= x");
116 }
117 }