[Telemetry] Reenable unused-import lint check for telemetry.
[chromium-blink-merge.git] / third_party / closure_compiler / compiler_customization_test.py
blobead5b2e40741f580607fdbaa44a2becf7807392e
1 #!/usr/bin/env python
2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 import os
7 import unittest
9 from checker import Checker
10 from processor import FileCache, Processor
13 ASSERT_FILE = os.path.join("..", "..", "ui", "webui", "resources", "js",
14 "assert.js")
15 CR_FILE = os.path.join("..", "..", "ui", "webui", "resources", "js", "cr.js")
16 UI_FILE = os.path.join("..", "..", "ui", "webui", "resources", "js", "cr",
17 "ui.js")
20 def rel_to_abs(rel_path):
21 script_path = os.path.dirname(os.path.abspath(__file__))
22 return os.path.join(script_path, rel_path)
25 class CompilerCustomizationTest(unittest.TestCase):
26 _ASSERT_DEFINITION = Processor(rel_to_abs(ASSERT_FILE)).contents
27 _CR_DEFINE_DEFINITION = Processor(rel_to_abs(CR_FILE)).contents
28 _CR_UI_DECORATE_DEFINITION = Processor(rel_to_abs(UI_FILE)).contents
30 def setUp(self):
31 self._checker = Checker()
33 def _runChecker(self, source_code):
34 file_path = "/script.js"
35 FileCache._cache[file_path] = source_code
36 return self._checker.check(file_path)
38 def _runCheckerTestExpectError(self, source_code, expected_error):
39 _, stderr = self._runChecker(source_code)
41 self.assertTrue(expected_error in stderr,
42 msg="Expected chunk: \n%s\n\nOutput:\n%s\n" % (
43 expected_error, stderr))
45 def _runCheckerTestExpectSuccess(self, source_code):
46 found_errors, stderr = self._runChecker(source_code)
48 self.assertFalse(found_errors,
49 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr)
51 def testGetInstance(self):
52 self._runCheckerTestExpectError("""
53 var cr = {
54 /** @param {!Function} ctor */
55 addSingletonGetter: function(ctor) {
56 ctor.getInstance = function() {
57 return ctor.instance_ || (ctor.instance_ = new ctor());
62 /** @constructor */
63 function Class() {
64 /** @param {number} num */
65 this.needsNumber = function(num) {};
68 cr.addSingletonGetter(Class);
69 Class.getInstance().needsNumber("wrong type");
70 """, "ERROR - actual parameter 1 of Class.needsNumber does not match formal "
71 "parameter")
73 def testCrDefineFunctionDefinition(self):
74 self._runCheckerTestExpectError(self._CR_DEFINE_DEFINITION + """
75 cr.define('a.b.c', function() {
76 /** @param {number} num */
77 function internalName(num) {}
79 return {
80 needsNumber: internalName
82 });
84 a.b.c.needsNumber("wrong type");
85 """, "ERROR - actual parameter 1 of a.b.c.needsNumber does not match formal "
86 "parameter")
88 def testCrDefineFunctionAssignment(self):
89 self._runCheckerTestExpectError(self._CR_DEFINE_DEFINITION + """
90 cr.define('a.b.c', function() {
91 /** @param {number} num */
92 var internalName = function(num) {};
94 return {
95 needsNumber: internalName
97 });
99 a.b.c.needsNumber("wrong type");
100 """, "ERROR - actual parameter 1 of a.b.c.needsNumber does not match formal "
101 "parameter")
103 def testCrDefineConstructorDefinitionPrototypeMethod(self):
104 self._runCheckerTestExpectError(self._CR_DEFINE_DEFINITION + """
105 cr.define('a.b.c', function() {
106 /** @constructor */
107 function ClassInternalName() {}
109 ClassInternalName.prototype = {
110 /** @param {number} num */
111 method: function(num) {}
114 return {
115 ClassExternalName: ClassInternalName
119 new a.b.c.ClassExternalName().method("wrong type");
120 """, "ERROR - actual parameter 1 of a.b.c.ClassExternalName.prototype.method "
121 "does not match formal parameter")
123 def testCrDefineConstructorAssignmentPrototypeMethod(self):
124 self._runCheckerTestExpectError(self._CR_DEFINE_DEFINITION + """
125 cr.define('a.b.c', function() {
126 /** @constructor */
127 var ClassInternalName = function() {};
129 ClassInternalName.prototype = {
130 /** @param {number} num */
131 method: function(num) {}
134 return {
135 ClassExternalName: ClassInternalName
139 new a.b.c.ClassExternalName().method("wrong type");
140 """, "ERROR - actual parameter 1 of a.b.c.ClassExternalName.prototype.method "
141 "does not match formal parameter")
143 def testCrDefineEnum(self):
144 self._runCheckerTestExpectError(self._CR_DEFINE_DEFINITION + """
145 cr.define('a.b.c', function() {
146 /** @enum {string} */
147 var internalNameForEnum = {key: 'wrong_type'};
149 return {
150 exportedEnum: internalNameForEnum
154 /** @param {number} num */
155 function needsNumber(num) {}
157 needsNumber(a.b.c.exportedEnum.key);
158 """, "ERROR - actual parameter 1 of needsNumber does not match formal "
159 "parameter")
161 def testObjectDefineProperty(self):
162 self._runCheckerTestExpectSuccess("""
163 /** @constructor */
164 function Class() {}
166 Object.defineProperty(Class.prototype, 'myProperty', {});
168 alert(new Class().myProperty);
169 """)
171 def testCrDefineProperty(self):
172 self._runCheckerTestExpectSuccess(self._CR_DEFINE_DEFINITION + """
173 /** @constructor */
174 function Class() {}
176 cr.defineProperty(Class.prototype, 'myProperty', cr.PropertyKind.JS);
178 alert(new Class().myProperty);
179 """)
181 def testCrDefinePropertyTypeChecking(self):
182 self._runCheckerTestExpectError(self._CR_DEFINE_DEFINITION + """
183 /** @constructor */
184 function Class() {}
186 cr.defineProperty(Class.prototype, 'booleanProp', cr.PropertyKind.BOOL_ATTR);
188 /** @param {number} num */
189 function needsNumber(num) {}
191 needsNumber(new Class().booleanProp);
192 """, "ERROR - actual parameter 1 of needsNumber does not match formal "
193 "parameter")
195 def testCrDefineOnCrWorks(self):
196 self._runCheckerTestExpectSuccess(self._CR_DEFINE_DEFINITION + """
197 cr.define('cr', function() {
198 return {};
200 """)
202 def testAssertWorks(self):
203 self._runCheckerTestExpectSuccess(self._ASSERT_DEFINITION + """
204 /** @return {?string} */
205 function f() {
206 return "string";
209 /** @type {!string} */
210 var a = assert(f());
211 """)
213 def testAssertInstanceofWorks(self):
214 self._runCheckerTestExpectSuccess(self._ASSERT_DEFINITION + """
215 /** @constructor */
216 function Class() {}
218 /** @return {Class} */
219 function f() {
220 var a = document.createElement('div');
221 return assertInstanceof(a, Class);
223 """)
225 def testCrUiDecorateWorks(self):
226 self._runCheckerTestExpectSuccess(self._CR_DEFINE_DEFINITION +
227 self._CR_UI_DECORATE_DEFINITION + """
228 /** @constructor */
229 function Class() {}
231 /** @return {Class} */
232 function f() {
233 var a = document.createElement('div');
234 cr.ui.decorate(a, Class);
235 return a;
237 """)
240 if __name__ == "__main__":
241 unittest.main()