Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2010 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 * @author [email protected] (John Lenz)
21 */
22 public class CheckRegExpTest extends CompilerTestCase {
23 CheckRegExp last = null;
24
25 public CheckRegExpTest() {
26 super("var RegExp;", true);
27 }
28
29 @Override
30 protected CompilerPass getProcessor(Compiler compiler) {
31 compiler.options.setWarningLevel(
32 DiagnosticGroups.CHECK_REGEXP, CheckLevel.WARNING);
33 last = new CheckRegExp(compiler);
34 return last;
35 }
36
37 private void testReference(String code, boolean expected) {
38 testSame(code, (expected) ? CheckRegExp.REGEXP_REFERENCE : null);
39 assertEquals(expected, last.isGlobalRegExpPropertiesUsed());
40 }
41
42 public void testRegExp() {
43 // Creating regexp's is ok
44 testReference("RegExp();", false);
45 testReference("var x = RegExp();", false);
46 testReference("new RegExp();", false);
47 testReference("var x = new RegExp();", false);
48
49 // Checking for RegExp instances is OK, as well.
50 testReference("x instanceof RegExp;", false);
51
52 // Any other reference isn't
53 testReference("RegExp.test();", true);
54 testReference("var x = RegExp.test();", true);
55 testReference("RegExp.exec();", true);
56 testReference("RegExp.$1;", true);
57 testReference("RegExp.foobar;", true);
58 testReference("delete RegExp;", true);
59
60 // Aliases aren't allowed
61 testReference("var x = RegExp;", true);
62 testReference("f(RegExp);", true);
63 testReference("new f(RegExp);", true);
64 testReference("var x = RegExp; x.test()", true);
65
66 // No RegExp reference is ok
67 testReference("var x;", false);
68
69 // Local RegExp is ok
70 testReference("function f() {var RegExp; RegExp.test();}", false);
71 }
72 }