Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(169)

Side by Side Diff: third_party/closure_compiler/compiler_customization_test.py

Issue 460163002: Handle property definition by {cr|Object}.defineProperty() in compiler pass (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@C_compiler_pass
Patch Set: rebase onto master Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
10 from processor import FileCache, Processor
10 11
11 12
12 CR_FILE = os.path.join("..", "..", "ui", "webui", "resources", "js", "cr.js") 13 CR_FILE = os.path.join("..", "..", "ui", "webui", "resources", "js", "cr.js")
13 14
14 15
15 def rel_to_abs(rel_path): 16 def rel_to_abs(rel_path):
16 script_path = os.path.dirname(os.path.abspath(__file__)) 17 script_path = os.path.dirname(os.path.abspath(__file__))
17 return os.path.join(script_path, rel_path) 18 return os.path.join(script_path, rel_path)
18 19
19 20
20 class CompilerCustomizationTest(unittest.TestCase): 21 class CompilerCustomizationTest(unittest.TestCase):
21 _CR_DEFINE_DEFINITION = Flattener(rel_to_abs(CR_FILE)).contents 22 _CR_DEFINE_DEFINITION = Processor(rel_to_abs(CR_FILE)).contents
22 23
23 def setUp(self): 24 def setUp(self):
24 self._checker = Checker() 25 self._checker = Checker()
25 26
26 def _runCheckerTest(self, source_code, expected_error): 27 def _runChecker(self, source_code):
27 file_path = "/script.js" 28 file_path = "/script.js"
28 FileCache._cache[file_path] = source_code 29 FileCache._cache[file_path] = source_code
29 _, output = self._checker.check(file_path) 30 return self._checker.check(file_path)
31
32 def _runCheckerTestExpectError(self, source_code, expected_error):
33 _, output = self._runChecker(source_code)
30 34
31 self.assertTrue(expected_error in output, 35 self.assertTrue(expected_error in output,
32 msg="Expected chunk: \n%s\n\nOutput:\n%s\n" % ( 36 msg="Expected chunk: \n%s\n\nOutput:\n%s\n" % (
33 expected_error, output)) 37 expected_error, output))
34 38
39 def _runCheckerTestExpectSuccess(self, source_code):
40 return_code, output = self._runChecker(source_code)
41
42 self.assertTrue(return_code == 0,
43 msg="Expected success, got return code %d\n\nOutput:\n%s\n" % (
44 return_code, output))
45
35 def testGetInstance(self): 46 def testGetInstance(self):
36 self._runCheckerTest(source_code=""" 47 self._runCheckerTestExpectError("""
37 var cr = { 48 var cr = {
38 /** @param {!Function} ctor */ 49 /** @param {!Function} ctor */
39 addSingletonGetter: function(ctor) { 50 addSingletonGetter: function(ctor) {
40 ctor.getInstance = function() { 51 ctor.getInstance = function() {
41 return ctor.instance_ || (ctor.instance_ = new ctor()); 52 return ctor.instance_ || (ctor.instance_ = new ctor());
42 }; 53 };
43 } 54 }
44 }; 55 };
45 56
46 /** @constructor */ 57 /** @constructor */
47 function Class() { 58 function Class() {
48 /** @param {number} num */ 59 /** @param {number} num */
49 this.needsNumber = function(num) {}; 60 this.needsNumber = function(num) {};
50 } 61 }
51 62
52 cr.addSingletonGetter(Class); 63 cr.addSingletonGetter(Class);
53 Class.getInstance().needsNumber("wrong type"); 64 Class.getInstance().needsNumber("wrong type");
54 """, 65 """, "ERROR - actual parameter 1 of Class.needsNumber does not match formal "
55 expected_error="ERROR - actual parameter 1 of Class.needsNumber does " 66 "parameter")
56 "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(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 """, "ERROR - actual parameter 1 of a.b.c.needsNumber does not match formal "
71 "not match formal parameter") 81 "parameter")
72 82
73 def testCrDefineFunctionAssignment(self): 83 def testCrDefineFunctionAssignment(self):
74 self._runCheckerTest(source_code=self._CR_DEFINE_DEFINITION + """ 84 self._runCheckerTestExpectError(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 """, "ERROR - actual parameter 1 of a.b.c.needsNumber does not match formal "
86 "not match formal parameter") 96 "parameter")
87 97
88 def testCrDefineConstructorDefinitionPrototypeMethod(self): 98 def testCrDefineConstructorDefinitionPrototypeMethod(self):
89 self._runCheckerTest(source_code=self._CR_DEFINE_DEFINITION + """ 99 self._runCheckerTestExpectError(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 """, "ERROR - actual parameter 1 of a.b.c.ClassExternalName.prototype.method "
106 "prototype.method does not match formal parameter") 116 "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(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 """, "ERROR - actual parameter 1 of a.b.c.ClassExternalName.prototype.method "
126 "prototype.method does not match formal parameter") 136 "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(self._CR_DEFINE_DEFINITION + """
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 """, "ERROR - actual parameter 1 of needsNumber does not match formal "
144 "match formal parameter") 154 "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(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 """, "ERROR - actual parameter 1 of needsNumber does not match formal "
188 "parameter")
145 189
146 190
147 if __name__ == "__main__": 191 if __name__ == "__main__":
148 unittest.main() 192 unittest.main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698