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.
9 from checker
import Checker
10 from processor
import FileCache
, Processor
13 ASSERT_FILE
= os
.path
.join("..", "..", "ui", "webui", "resources", "js",
15 CR_FILE
= os
.path
.join("..", "..", "ui", "webui", "resources", "js", "cr.js")
16 UI_FILE
= os
.path
.join("..", "..", "ui", "webui", "resources", "js", "cr",
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
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
("""
54 /** @param {!Function} ctor */
55 addSingletonGetter: function(ctor) {
56 ctor.getInstance = function() {
57 return ctor.instance_ || (ctor.instance_ = new ctor());
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 "
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) {}
80 needsNumber: internalName
84 a.b.c.needsNumber("wrong type");
85 """, "ERROR - actual parameter 1 of a.b.c.needsNumber does not match formal "
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) {};
95 needsNumber: internalName
99 a.b.c.needsNumber("wrong type");
100 """, "ERROR - actual parameter 1 of a.b.c.needsNumber does not match formal "
103 def testCrDefineConstructorDefinitionPrototypeMethod(self
):
104 self
._runCheckerTestExpectError
(self
._CR
_DEFINE
_DEFINITION
+ """
105 cr.define('a.b.c', function() {
107 function ClassInternalName() {}
109 ClassInternalName.prototype = {
110 /** @param {number} num */
111 method: function(num) {}
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() {
127 var ClassInternalName = function() {};
129 ClassInternalName.prototype = {
130 /** @param {number} num */
131 method: function(num) {}
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'};
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 "
161 def testObjectDefineProperty(self
):
162 self
._runCheckerTestExpectSuccess
("""
166 Object.defineProperty(Class.prototype, 'myProperty', {});
168 alert(new Class().myProperty);
171 def testCrDefineProperty(self
):
172 self
._runCheckerTestExpectSuccess
(self
._CR
_DEFINE
_DEFINITION
+ """
176 cr.defineProperty(Class.prototype, 'myProperty', cr.PropertyKind.JS);
178 alert(new Class().myProperty);
181 def testCrDefinePropertyTypeChecking(self
):
182 self
._runCheckerTestExpectError
(self
._CR
_DEFINE
_DEFINITION
+ """
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 "
195 def testCrDefineOnCrWorks(self
):
196 self
._runCheckerTestExpectSuccess
(self
._CR
_DEFINE
_DEFINITION
+ """
197 cr.define('cr', function() {
202 def testAssertWorks(self
):
203 self
._runCheckerTestExpectSuccess
(self
._ASSERT
_DEFINITION
+ """
204 /** @return {?string} */
209 /** @type {!string} */
213 def testAssertInstanceofWorks(self
):
214 self
._runCheckerTestExpectSuccess
(self
._ASSERT
_DEFINITION
+ """
218 /** @return {Class} */
220 var a = document.createElement('div');
221 return assertInstanceof(a, Class);
225 def testCrUiDecorateWorks(self
):
226 self
._runCheckerTestExpectSuccess
(self
._CR
_DEFINE
_DEFINITION
+
227 self
._CR
_UI
_DECORATE
_DEFINITION
+ """
231 /** @return {Class} */
233 var a = document.createElement('div');
234 cr.ui.decorate(a, Class);
240 if __name__
== "__main__":