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 _
, output
= self
._runChecker
(source_code
)
41 self
.assertTrue(expected_error
in output
,
42 msg
="Expected chunk: \n%s\n\nOutput:\n%s\n" % (
43 expected_error
, output
))
45 def _runCheckerTestExpectSuccess(self
, source_code
):
46 return_code
, output
= self
._runChecker
(source_code
)
48 self
.assertTrue(return_code
== 0,
49 msg
="Expected success, got return code %d\n\nOutput:\n%s\n" % (
52 def testGetInstance(self
):
53 self
._runCheckerTestExpectError
("""
55 /** @param {!Function} ctor */
56 addSingletonGetter: function(ctor) {
57 ctor.getInstance = function() {
58 return ctor.instance_ || (ctor.instance_ = new ctor());
65 /** @param {number} num */
66 this.needsNumber = function(num) {};
69 cr.addSingletonGetter(Class);
70 Class.getInstance().needsNumber("wrong type");
71 """, "ERROR - actual parameter 1 of Class.needsNumber does not match formal "
74 def testCrDefineFunctionDefinition(self
):
75 self
._runCheckerTestExpectError
(self
._CR
_DEFINE
_DEFINITION
+ """
76 cr.define('a.b.c', function() {
77 /** @param {number} num */
78 function internalName(num) {}
81 needsNumber: internalName
85 a.b.c.needsNumber("wrong type");
86 """, "ERROR - actual parameter 1 of a.b.c.needsNumber does not match formal "
89 def testCrDefineFunctionAssignment(self
):
90 self
._runCheckerTestExpectError
(self
._CR
_DEFINE
_DEFINITION
+ """
91 cr.define('a.b.c', function() {
92 /** @param {number} num */
93 var internalName = function(num) {};
96 needsNumber: internalName
100 a.b.c.needsNumber("wrong type");
101 """, "ERROR - actual parameter 1 of a.b.c.needsNumber does not match formal "
104 def testCrDefineConstructorDefinitionPrototypeMethod(self
):
105 self
._runCheckerTestExpectError
(self
._CR
_DEFINE
_DEFINITION
+ """
106 cr.define('a.b.c', function() {
108 function ClassInternalName() {}
110 ClassInternalName.prototype = {
111 /** @param {number} num */
112 method: function(num) {}
116 ClassExternalName: ClassInternalName
120 new a.b.c.ClassExternalName().method("wrong type");
121 """, "ERROR - actual parameter 1 of a.b.c.ClassExternalName.prototype.method "
122 "does not match formal parameter")
124 def testCrDefineConstructorAssignmentPrototypeMethod(self
):
125 self
._runCheckerTestExpectError
(self
._CR
_DEFINE
_DEFINITION
+ """
126 cr.define('a.b.c', function() {
128 var ClassInternalName = function() {};
130 ClassInternalName.prototype = {
131 /** @param {number} num */
132 method: function(num) {}
136 ClassExternalName: ClassInternalName
140 new a.b.c.ClassExternalName().method("wrong type");
141 """, "ERROR - actual parameter 1 of a.b.c.ClassExternalName.prototype.method "
142 "does not match formal parameter")
144 def testCrDefineEnum(self
):
145 self
._runCheckerTestExpectError
(self
._CR
_DEFINE
_DEFINITION
+ """
146 cr.define('a.b.c', function() {
147 /** @enum {string} */
148 var internalNameForEnum = {key: 'wrong_type'};
151 exportedEnum: internalNameForEnum
155 /** @param {number} num */
156 function needsNumber(num) {}
158 needsNumber(a.b.c.exportedEnum.key);
159 """, "ERROR - actual parameter 1 of needsNumber does not match formal "
162 def testObjectDefineProperty(self
):
163 self
._runCheckerTestExpectSuccess
("""
167 Object.defineProperty(Class.prototype, 'myProperty', {});
169 alert(new Class().myProperty);
172 def testCrDefineProperty(self
):
173 self
._runCheckerTestExpectSuccess
(self
._CR
_DEFINE
_DEFINITION
+ """
177 cr.defineProperty(Class.prototype, 'myProperty', cr.PropertyKind.JS);
179 alert(new Class().myProperty);
182 def testCrDefinePropertyTypeChecking(self
):
183 self
._runCheckerTestExpectError
(self
._CR
_DEFINE
_DEFINITION
+ """
187 cr.defineProperty(Class.prototype, 'booleanProp', cr.PropertyKind.BOOL_ATTR);
189 /** @param {number} num */
190 function needsNumber(num) {}
192 needsNumber(new Class().booleanProp);
193 """, "ERROR - actual parameter 1 of needsNumber does not match formal "
196 def testCrDefineOnCrWorks(self
):
197 self
._runCheckerTestExpectSuccess
(self
._CR
_DEFINE
_DEFINITION
+ """
198 cr.define('cr', function() {
203 def testAssertWorks(self
):
204 self
._runCheckerTestExpectSuccess
(self
._ASSERT
_DEFINITION
+ """
205 /** @return {?string} */
210 /** @type {!string} */
214 def testAssertInstanceofWorks(self
):
215 self
._runCheckerTestExpectSuccess
(self
._ASSERT
_DEFINITION
+ """
219 /** @return {Class} */
221 var a = document.createElement('div');
222 return assertInstanceof(a, Class);
226 def testCrUiDecorateWorks(self
):
227 self
._runCheckerTestExpectSuccess
(self
._CR
_DEFINE
_DEFINITION
+
228 self
._CR
_UI
_DECORATE
_DEFINITION
+ """
232 /** @return {Class} */
234 var a = document.createElement('div');
235 cr.ui.decorate(a, Class);
241 if __name__
== "__main__":