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 compile import Checker | 9 from compile import Checker |
10 from processor import FileCache, Processor | 10 from processor import FileCache, Processor |
11 | 11 |
12 | 12 |
13 _SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) | 13 _SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
14 _SRC_DIR = os.path.join(_SCRIPT_DIR, os.pardir, os.pardir) | 14 _SRC_DIR = os.path.join(_SCRIPT_DIR, os.pardir, os.pardir) |
15 _RESOURCES_DIR = os.path.join(_SRC_DIR, "ui", "webui", "resources", "js") | 15 _RESOURCES_DIR = os.path.join(_SRC_DIR, "ui", "webui", "resources", "js") |
16 _ASSERT_JS = os.path.join(_RESOURCES_DIR, "assert.js") | 16 _ASSERT_JS = os.path.join(_RESOURCES_DIR, "assert.js") |
17 _CR_JS = os.path.join(_RESOURCES_DIR, "cr.js") | 17 _CR_JS = os.path.join(_RESOURCES_DIR, "cr.js") |
18 _CR_UI_JS = os.path.join(_RESOURCES_DIR, "cr", "ui.js") | 18 _CR_UI_JS = os.path.join(_RESOURCES_DIR, "cr", "ui.js") |
19 _POLYMER_EXTERNS = os.path.join(_SRC_DIR, "third_party", "polymer", "v0_8", | 19 _POLYMER_EXTERNS = os.path.join(_SRC_DIR, "third_party", "polymer", "v0_8", |
20 "components-chromium", "polymer-externs", | 20 "components-chromium", "polymer-externs", |
21 "polymer.externs.js") | 21 "polymer.externs.js") |
22 | 22 |
23 | 23 |
24 class CompilerCustomizationTest(unittest.TestCase): | 24 class CompilerTest(unittest.TestCase): |
25 _ASSERT_DEFINITION = Processor(_ASSERT_JS).contents | 25 _ASSERT_DEFINITION = Processor(_ASSERT_JS).contents |
26 _CR_DEFINE_DEFINITION = Processor(_CR_JS).contents | 26 _CR_DEFINE_DEFINITION = Processor(_CR_JS).contents |
27 _CR_UI_DECORATE_DEFINITION = Processor(_CR_UI_JS).contents | 27 _CR_UI_DECORATE_DEFINITION = Processor(_CR_UI_JS).contents |
28 | 28 |
29 def setUp(self): | 29 def setUp(self): |
30 self._checker = Checker() | 30 self._checker = Checker() |
31 | 31 |
Dan Beam
2015/05/19 18:23:33
def tearDown(self):
# remove all files regardles
Theresa
2015/05/19 19:21:02
Done.
| |
32 def _runChecker(self, source_code): | 32 def _runChecker(self, source_code, output_wrapper=None): |
33 file_path = "/script.js" | 33 file_path = "/script.js" |
34 FileCache._cache[file_path] = source_code | 34 FileCache._cache[file_path] = source_code |
35 return self._checker.check(file_path, externs=[_POLYMER_EXTERNS]) | 35 out_file, out_map = self._createOutFiles() |
36 | |
37 found_errors, stderr = self._checker.check(file_path, | |
38 externs=[_POLYMER_EXTERNS], | |
39 out_file=out_file, | |
40 output_wrapper=output_wrapper) | |
41 return found_errors, stderr, out_file, out_map | |
36 | 42 |
37 def _runCheckerTestExpectError(self, source_code, expected_error): | 43 def _runCheckerTestExpectError(self, source_code, expected_error): |
38 _, stderr = self._runChecker(source_code) | 44 _, stderr, out_file, out_map = self._runChecker(source_code) |
39 | 45 |
40 self.assertTrue(expected_error in stderr, | 46 self.assertTrue(expected_error in stderr, |
41 msg="Expected chunk: \n%s\n\nOutput:\n%s\n" % ( | 47 msg="Expected chunk: \n%s\n\nOutput:\n%s\n" % ( |
42 expected_error, stderr)) | 48 expected_error, stderr)) |
49 self.assertFalse(os.path.exists(out_file)) | |
50 self.assertFalse(os.path.exists(out_map)) | |
43 | 51 |
44 def _runCheckerTestExpectSuccess(self, source_code): | 52 def _runCheckerTestExpectSuccess(self, source_code, expected_output=None, |
45 found_errors, stderr = self._runChecker(source_code) | 53 output_wrapper=None): |
54 found_errors, stderr, out_file, out_map = self._runChecker(source_code, | |
55 output_wrapper) | |
46 | 56 |
47 self.assertFalse(found_errors, | 57 self.assertFalse(found_errors, |
48 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) | 58 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) |
49 | 59 |
60 self.assertTrue(os.path.exists(out_map)) | |
61 self.assertTrue(os.path.exists(out_file)) | |
62 if expected_output: | |
63 with open(out_file, 'r') as file: | |
Dan Beam
2015/05/19 18:23:33
' -> " everywhere feasible
Theresa
2015/05/19 19:21:02
Done.
| |
64 self.assertEquals(file.read(), expected_output) | |
65 | |
66 self._removeFiles([out_file, out_map]) | |
67 | |
68 def _createOutFiles(self): | |
Dan Beam
2015/05/19 18:23:32
why do we need this? shouldn't they be created by
Theresa
2015/05/19 18:29:23
The directory is created in main if it doesn't alr
Theresa
2015/05/19 19:21:02
Done.
Dan Beam
2015/05/20 02:01:00
why not move this to check() instead of main()?
ht
Dan Beam
2015/05/20 02:05:33
sorry, make a _ensure_out_file_exists() (or someth
Theresa
2015/05/20 02:07:37
I moved it to _run_js_check() since it's not actua
| |
69 out_file = "/tmp/gen/script.js" | |
70 out_map = "/tmp/gen/script.js.map" | |
71 | |
72 out_dir = os.path.dirname(out_file) | |
73 if not os.path.exists(out_dir): | |
74 os.makedirs(out_dir) | |
75 | |
76 return out_file, out_map | |
77 | |
78 def _removeFiles(self, files): | |
79 for file in files: | |
80 if(os.path.exists(file)): | |
Dan Beam
2015/05/19 18:23:33
the python gods do not approve of your c-like synt
Theresa
2015/05/19 19:21:02
Done.
| |
81 os.remove(file) | |
82 | |
50 def testGetInstance(self): | 83 def testGetInstance(self): |
51 self._runCheckerTestExpectError(""" | 84 self._runCheckerTestExpectError(""" |
52 var cr = { | 85 var cr = { |
53 /** @param {!Function} ctor */ | 86 /** @param {!Function} ctor */ |
54 addSingletonGetter: function(ctor) { | 87 addSingletonGetter: function(ctor) { |
55 ctor.getInstance = function() { | 88 ctor.getInstance = function() { |
56 return ctor.instance_ || (ctor.instance_ = new ctor()); | 89 return ctor.instance_ || (ctor.instance_ = new ctor()); |
57 }; | 90 }; |
58 } | 91 } |
59 }; | 92 }; |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
228 function Class() {} | 261 function Class() {} |
229 | 262 |
230 /** @return {Class} */ | 263 /** @return {Class} */ |
231 function f() { | 264 function f() { |
232 var a = document.createElement('div'); | 265 var a = document.createElement('div'); |
233 cr.ui.decorate(a, Class); | 266 cr.ui.decorate(a, Class); |
234 return a; | 267 return a; |
235 } | 268 } |
236 """) | 269 """) |
237 | 270 |
271 def testValidScriptCompilation(self): | |
272 self._runCheckerTestExpectSuccess(""" | |
273 var testScript = function() { console.log("hello world") };""", | |
Dan Beam
2015/05/19 18:23:33
can you put some \n in here so the difference is l
Theresa
2015/05/19 19:21:02
Done.
| |
274 """'use strict';var testScript=function(){console.log("hello world")};\n""") | |
Dan Beam
2015/05/19 18:23:33
hmmm, why is 'use strict'; prepended?
Theresa
2015/05/19 18:29:23
Because the closure compiler always prepends it? I
Theresa
2015/05/19 19:21:01
I'm going to guess it's from the _COMMON_CLOSURE_A
| |
275 | |
276 def testOutputWrapper(self): | |
277 self._runCheckerTestExpectSuccess(""" | |
Dan Beam
2015/05/19 18:23:33
indent off
Theresa
2015/05/19 19:21:01
Done.
| |
278 var testScript = function() { console.log("hello world") }; | |
279 """, """(function(){'use strict';var testScript=function()""" | |
280 + """{console.log("hello world")};})();\n""", | |
281 output_wrapper="(function(){%output%})();") | |
Dan Beam
2015/05/19 18:23:33
make this prettier
Theresa
2015/05/19 19:21:01
Done.
| |
282 | |
283 def testCheckMultiple(self): | |
284 source_file = "/tmp/script.js" | |
Dan Beam
2015/05/19 18:23:32
this should use tempfile instead
https://docs.pyth
Theresa
2015/05/19 19:21:01
Done.
| |
285 with open(source_file, 'w') as f: | |
286 f.write(""" | |
Dan Beam
2015/05/19 18:23:33
why are we actually writing these files rather tha
Theresa
2015/05/19 18:29:22
I tried using FileCache._chache and the jar failed
Theresa
2015/05/19 19:21:01
Tried again and it still failed. I think the diffe
| |
287 goog.require('testScript'); | |
288 | |
289 testScript(); | |
290 """) | |
291 | |
292 source_file2 = "/tmp/script2.js" | |
293 with open(source_file2, 'w') as f: | |
294 f.write(""" | |
295 goog.provide('testScript'); | |
296 | |
297 var testScript = function(){}; | |
298 """) | |
Dan Beam
2015/05/19 18:23:33
can you reverse the order of source_file/source_fi
Theresa
2015/05/19 19:21:01
Done.
| |
299 | |
300 out_file, out_map = self._createOutFiles() | |
301 sources = [source_file, source_file2] | |
302 externs = [_POLYMER_EXTERNS] | |
303 found_errors, stderr = self._checker.check_multiple(sources, | |
304 externs=externs, | |
305 out_file=out_file) | |
306 self.assertFalse(found_errors, | |
307 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) | |
308 | |
309 expected_output = "'use strict';var testScript=function(){};testScript();\n" | |
310 self.assertTrue(os.path.exists(out_map)) | |
311 self.assertTrue(os.path.exists(out_file)) | |
312 with open(out_file, 'r') as file: | |
313 self.assertEquals(file.read(), expected_output) | |
314 | |
315 self._removeFiles([source_file, source_file2, out_file, out_map]) | |
316 | |
238 | 317 |
239 if __name__ == "__main__": | 318 if __name__ == "__main__": |
240 unittest.main() | 319 unittest.main() |
OLD | NEW |