Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2008 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 package com.google.javascript.jscomp;
17
18 import static com.google.javascript.jscomp.CheckProvides.MISSING_PROVIDE_WARNING;
19
20 import com.google.javascript.jscomp.CheckLevel;
21
22 /**
23 * Tests for {@link CheckProvides}.
24 *
25 */
26 public class CheckProvidesTest extends CompilerTestCase {
27 @Override
28 protected CompilerPass getProcessor(Compiler compiler) {
29 return new CheckProvides(compiler, CheckLevel.WARNING);
30 }
31
32 public void testIrrelevant() {
33 testSame("var str = 'g4';");
34 }
35
36 public void testHarmlessProcedural() {
37 testSame("goog.provide('X'); /** @constructor */ function X(){};");
38 }
39
40 public void testHarmless() {
41 String js = "goog.provide('X'); /** @constructor */ X = function(){};";
42 testSame(js);
43 }
44
45 public void testNoProvideInnerClass() {
46 testSame(
47 "goog.provide('X');\n" +
48 "/** @constructor */ function X(){};" +
49 "/** @constructor */ X.Y = function(){};");
50 }
51
52 public void testMissingGoogProvide(){
53 String[] js = new String[]{"/** @constructor */ X = function(){};"};
54 String warning = "missing goog.provide('X')";
55 test(js, js, null, MISSING_PROVIDE_WARNING, warning);
56 }
57
58 public void testMissingGoogProvideWithNamespace(){
59 String[] js = new String[]{"goog = {}; " +
60 "/** @constructor */ goog.X = function(){};"};
61 String warning = "missing goog.provide('goog.X')";
62 test(js, js, null, MISSING_PROVIDE_WARNING, warning);
63 }
64
65 public void testGoogProvideInWrongFileShouldCreateWarning(){
66 String bad = "/** @constructor */ X = function(){};";
67 String good = "goog.provide('X'); goog.provide('Y');" +
68 "/** @constructor */ X = function(){};" +
69 "/** @constructor */ Y = function(){};";
70 String[] js = new String[] {good, bad};
71 String warning = "missing goog.provide('X')";
72 test(js, js, null, MISSING_PROVIDE_WARNING, warning);
73 }
74
75 public void testGoogProvideMissingConstructorIsOkForNow(){
76 // TODO(user) to prevent orphan goog.provide calls, the pass would have to
77 // account for enums, static functions and constants
78 testSame(new String[]{"goog.provide('Y'); X = function(){};"});
79 }
80
81 public void testIgnorePrivateConstructor() {
82 String js = "/** @constructor*/ X_ = function(){};";
83 testSame(js);
84 }
85
86 public void testIgnorePrivatelyAnnotatedConstructor() {
87 testSame("/** @private\n@constructor */ X = function(){};");
88 testSame("/** @constructor\n@private */ X = function(){};");
89 }
90 }