OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import os | 6 import os |
7 import unittest | 7 import unittest |
8 | 8 |
9 from checker import Checker, FileCache, Flattener | 9 from checker import Checker, FileCache, Flattener |
10 | 10 |
11 | 11 |
12 CR_FILE = os.path.join("..", "..", "ui", "webui", "resources", "js", "cr.js") | 12 CR_FILE = os.path.join("..", "..", "ui", "webui", "resources", "js", "cr.js") |
13 | 13 |
14 | 14 |
15 def rel_to_abs(rel_path): | 15 def rel_to_abs(rel_path): |
16 script_path = os.path.dirname(os.path.abspath(__file__)) | 16 script_path = os.path.dirname(os.path.abspath(__file__)) |
17 return os.path.join(script_path, rel_path) | 17 return os.path.join(script_path, rel_path) |
18 | 18 |
19 | 19 |
20 class CompilerCustomizationTest(unittest.TestCase): | 20 class CompilerCustomizationTest(unittest.TestCase): |
21 _CR_DEFINE_DEFINITION = Flattener(rel_to_abs(CR_FILE)).contents | 21 _CR_DEFINE_DEFINITION = Flattener(rel_to_abs(CR_FILE)).contents |
22 | 22 |
23 def setUp(self): | 23 def setUp(self): |
24 self._checker = Checker() | 24 self._checker = Checker() |
25 | 25 |
26 def _runCheckerTest(self, source_code, expected_error): | 26 def _runChecker(self, source_code): |
27 file_path = "/script.js" | 27 file_path = "/script.js" |
28 FileCache._cache[file_path] = source_code | 28 FileCache._cache[file_path] = source_code |
29 _, output = self._checker.check(file_path) | 29 return self._checker.check(file_path) |
30 | |
31 def _runCheckerTestExpectError(self, source_code, expected_error): | |
32 _, output = self._runChecker(source_code) | |
30 | 33 |
31 self.assertTrue(expected_error in output, | 34 self.assertTrue(expected_error in output, |
32 msg="Expected chunk: \n%s\n\nOutput:\n%s\n" % ( | 35 msg="Expected chunk: \n%s\n\nOutput:\n%s\n" % ( |
33 expected_error, output)) | 36 expected_error, output)) |
34 | 37 |
38 def _runCheckerTestExpectSuccess(self, source_code): | |
39 return_code, output = self._runChecker(source_code) | |
40 | |
41 self.assertTrue(return_code == 0, | |
42 msg="Expected success, got return code %d\n\nOutput:\n%s\n" % ( | |
43 return_code, output)) | |
44 | |
35 def testGetInstance(self): | 45 def testGetInstance(self): |
36 self._runCheckerTest(source_code=""" | 46 self._runCheckerTestExpectError(source_code=""" |
37 var cr = { | 47 var cr = { |
38 /** @param {!Function} ctor */ | 48 /** @param {!Function} ctor */ |
39 addSingletonGetter: function(ctor) { | 49 addSingletonGetter: function(ctor) { |
40 ctor.getInstance = function() { | 50 ctor.getInstance = function() { |
41 return ctor.instance_ || (ctor.instance_ = new ctor()); | 51 return ctor.instance_ || (ctor.instance_ = new ctor()); |
42 }; | 52 }; |
43 } | 53 } |
44 }; | 54 }; |
45 | 55 |
46 /** @constructor */ | 56 /** @constructor */ |
47 function Class() { | 57 function Class() { |
48 /** @param {number} num */ | 58 /** @param {number} num */ |
49 this.needsNumber = function(num) {}; | 59 this.needsNumber = function(num) {}; |
50 } | 60 } |
51 | 61 |
52 cr.addSingletonGetter(Class); | 62 cr.addSingletonGetter(Class); |
53 Class.getInstance().needsNumber("wrong type"); | 63 Class.getInstance().needsNumber("wrong type"); |
54 """, | 64 """, |
55 expected_error="ERROR - actual parameter 1 of Class.needsNumber does " | 65 expected_error="ERROR - actual parameter 1 of Class.needsNumber does " |
56 "not match formal parameter") | 66 "not match formal parameter") |
57 | 67 |
58 def testCrDefineFunctionDefinition(self): | 68 def testCrDefineFunctionDefinition(self): |
59 self._runCheckerTest(source_code=self._CR_DEFINE_DEFINITION + """ | 69 self._runCheckerTestExpectError(source_code=self._CR_DEFINE_DEFINITION + """ |
60 cr.define('a.b.c', function() { | 70 cr.define('a.b.c', function() { |
61 /** @param {number} num */ | 71 /** @param {number} num */ |
62 function internalName(num) {} | 72 function internalName(num) {} |
63 | 73 |
64 return { | 74 return { |
65 needsNumber: internalName | 75 needsNumber: internalName |
66 }; | 76 }; |
67 }); | 77 }); |
68 | 78 |
69 a.b.c.needsNumber("wrong type"); | 79 a.b.c.needsNumber("wrong type"); |
70 """, expected_error="ERROR - actual parameter 1 of a.b.c.needsNumber does " | 80 """, expected_error="ERROR - actual parameter 1 of a.b.c.needsNumber does " |
71 "not match formal parameter") | 81 "not match formal parameter") |
72 | 82 |
73 def testCrDefineFunctionAssignment(self): | 83 def testCrDefineFunctionAssignment(self): |
74 self._runCheckerTest(source_code=self._CR_DEFINE_DEFINITION + """ | 84 self._runCheckerTestExpectError(source_code=self._CR_DEFINE_DEFINITION + """ |
75 cr.define('a.b.c', function() { | 85 cr.define('a.b.c', function() { |
76 /** @param {number} num */ | 86 /** @param {number} num */ |
77 var internalName = function(num) {}; | 87 var internalName = function(num) {}; |
78 | 88 |
79 return { | 89 return { |
80 needsNumber: internalName | 90 needsNumber: internalName |
81 }; | 91 }; |
82 }); | 92 }); |
83 | 93 |
84 a.b.c.needsNumber("wrong type"); | 94 a.b.c.needsNumber("wrong type"); |
85 """, expected_error="ERROR - actual parameter 1 of a.b.c.needsNumber does " | 95 """, expected_error="ERROR - actual parameter 1 of a.b.c.needsNumber does " |
86 "not match formal parameter") | 96 "not match formal parameter") |
87 | 97 |
88 def testCrDefineConstructorDefinitionPrototypeMethod(self): | 98 def testCrDefineConstructorDefinitionPrototypeMethod(self): |
89 self._runCheckerTest(source_code=self._CR_DEFINE_DEFINITION + """ | 99 self._runCheckerTestExpectError(source_code=self._CR_DEFINE_DEFINITION + """ |
90 cr.define('a.b.c', function() { | 100 cr.define('a.b.c', function() { |
91 /** @constructor */ | 101 /** @constructor */ |
92 function ClassInternalName() {} | 102 function ClassInternalName() {} |
93 | 103 |
94 ClassInternalName.prototype = { | 104 ClassInternalName.prototype = { |
95 /** @param {number} num */ | 105 /** @param {number} num */ |
96 method: function(num) {} | 106 method: function(num) {} |
97 }; | 107 }; |
98 | 108 |
99 return { | 109 return { |
100 ClassExternalName: ClassInternalName | 110 ClassExternalName: ClassInternalName |
101 }; | 111 }; |
102 }); | 112 }); |
103 | 113 |
104 new a.b.c.ClassExternalName().method("wrong type"); | 114 new a.b.c.ClassExternalName().method("wrong type"); |
105 """, expected_error="ERROR - actual parameter 1 of a.b.c.ClassExternalName." | 115 """, expected_error="ERROR - actual parameter 1 of a.b.c.ClassExternalName." |
106 "prototype.method does not match formal parameter") | 116 "prototype.method does not match formal parameter") |
107 | 117 |
108 def testCrDefineConstructorAssignmentPrototypeMethod(self): | 118 def testCrDefineConstructorAssignmentPrototypeMethod(self): |
109 self._runCheckerTest(source_code=self._CR_DEFINE_DEFINITION + """ | 119 self._runCheckerTestExpectError(source_code=self._CR_DEFINE_DEFINITION + """ |
110 cr.define('a.b.c', function() { | 120 cr.define('a.b.c', function() { |
111 /** @constructor */ | 121 /** @constructor */ |
112 var ClassInternalName = function() {}; | 122 var ClassInternalName = function() {}; |
113 | 123 |
114 ClassInternalName.prototype = { | 124 ClassInternalName.prototype = { |
115 /** @param {number} num */ | 125 /** @param {number} num */ |
116 method: function(num) {} | 126 method: function(num) {} |
117 }; | 127 }; |
118 | 128 |
119 return { | 129 return { |
120 ClassExternalName: ClassInternalName | 130 ClassExternalName: ClassInternalName |
121 }; | 131 }; |
122 }); | 132 }); |
123 | 133 |
124 new a.b.c.ClassExternalName().method("wrong type"); | 134 new a.b.c.ClassExternalName().method("wrong type"); |
125 """, expected_error="ERROR - actual parameter 1 of a.b.c.ClassExternalName." | 135 """, expected_error="ERROR - actual parameter 1 of a.b.c.ClassExternalName." |
126 "prototype.method does not match formal parameter") | 136 "prototype.method does not match formal parameter") |
127 | 137 |
128 def testCrDefineEnum(self): | 138 def testCrDefineEnum(self): |
129 self._runCheckerTest(source_code=self._CR_DEFINE_DEFINITION + """ | 139 self._runCheckerTestExpectError(source_code=self._CR_DEFINE_DEFINITION + """ |
Dan Beam
2014/08/13 20:41:29
is there any reason why you're naming the params?
Vitaly Pavlenko
2014/08/13 21:12:03
You're right. I generally like to name the params
| |
130 cr.define('a.b.c', function() { | 140 cr.define('a.b.c', function() { |
131 /** @enum {string} */ | 141 /** @enum {string} */ |
132 var internalNameForEnum = {key: 'wrong_type'}; | 142 var internalNameForEnum = {key: 'wrong_type'}; |
133 | 143 |
134 return { | 144 return { |
135 exportedEnum: internalNameForEnum | 145 exportedEnum: internalNameForEnum |
136 }; | 146 }; |
137 }); | 147 }); |
138 | 148 |
139 /** @param {number} num */ | 149 /** @param {number} num */ |
140 function needsNumber(num) {} | 150 function needsNumber(num) {} |
141 | 151 |
142 needsNumber(a.b.c.exportedEnum.key); | 152 needsNumber(a.b.c.exportedEnum.key); |
143 """, expected_error="ERROR - actual parameter 1 of needsNumber does not " | 153 """, expected_error="ERROR - actual parameter 1 of needsNumber does not match " |
144 "match formal parameter") | 154 "formal parameter") |
155 | |
156 def testObjectDefineProperty(self): | |
157 self._runCheckerTestExpectSuccess(""" | |
158 /** @constructor */ | |
159 function Class() {} | |
160 | |
161 Object.defineProperty(Class.prototype, 'myProperty', {}); | |
162 | |
163 alert(new Class().myProperty); | |
164 """) | |
165 | |
166 def testCrDefineProperty(self): | |
167 self._runCheckerTestExpectSuccess(self._CR_DEFINE_DEFINITION + """ | |
168 /** @constructor */ | |
169 function Class() {} | |
170 | |
171 cr.defineProperty(Class.prototype, 'myProperty', cr.PropertyKind.JS); | |
172 | |
173 alert(new Class().myProperty); | |
174 """) | |
175 | |
176 def testCrDefinePropertyTypeChecking(self): | |
177 self._runCheckerTestExpectError(source_code=self._CR_DEFINE_DEFINITION + """ | |
178 /** @constructor */ | |
179 function Class() {} | |
180 | |
181 cr.defineProperty(Class.prototype, 'booleanProp', cr.PropertyKind.BOOL_ATTR); | |
182 | |
183 /** @param {number} num */ | |
184 function needsNumber(num) {} | |
185 | |
186 needsNumber(new Class().booleanProp); | |
187 """, expected_error="ERROR - actual parameter 1 of needsNumber does not match " | |
188 "formal parameter") | |
145 | 189 |
146 | 190 |
147 if __name__ == "__main__": | 191 if __name__ == "__main__": |
148 unittest.main() | 192 unittest.main() |
OLD | NEW |