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 tempfile |
7 import unittest | 8 import unittest |
8 | 9 |
9 from compile import Checker | 10 from compile import Checker |
10 from processor import FileCache, Processor | 11 from processor import FileCache, Processor |
11 | 12 |
12 | 13 |
13 _SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) | 14 _SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
14 _SRC_DIR = os.path.join(_SCRIPT_DIR, os.pardir, os.pardir) | 15 _SRC_DIR = os.path.join(_SCRIPT_DIR, os.pardir, os.pardir) |
15 _RESOURCES_DIR = os.path.join(_SRC_DIR, "ui", "webui", "resources", "js") | 16 _RESOURCES_DIR = os.path.join(_SRC_DIR, "ui", "webui", "resources", "js") |
16 _ASSERT_JS = os.path.join(_RESOURCES_DIR, "assert.js") | 17 _ASSERT_JS = os.path.join(_RESOURCES_DIR, "assert.js") |
17 _CR_JS = os.path.join(_RESOURCES_DIR, "cr.js") | 18 _CR_JS = os.path.join(_RESOURCES_DIR, "cr.js") |
18 _CR_UI_JS = os.path.join(_RESOURCES_DIR, "cr", "ui.js") | 19 _CR_UI_JS = os.path.join(_RESOURCES_DIR, "cr", "ui.js") |
19 _POLYMER_EXTERNS = os.path.join(_SRC_DIR, "third_party", "polymer", "v0_8", | 20 _POLYMER_EXTERNS = os.path.join(_SRC_DIR, "third_party", "polymer", "v0_8", |
20 "components-chromium", "polymer-externs", | 21 "components-chromium", "polymer-externs", |
21 "polymer.externs.js") | 22 "polymer.externs.js") |
22 | 23 |
23 | 24 |
24 class CompilerCustomizationTest(unittest.TestCase): | 25 class CompilerTest(unittest.TestCase): |
25 _ASSERT_DEFINITION = Processor(_ASSERT_JS).contents | 26 _ASSERT_DEFINITION = Processor(_ASSERT_JS).contents |
26 _CR_DEFINE_DEFINITION = Processor(_CR_JS).contents | 27 _CR_DEFINE_DEFINITION = Processor(_CR_JS).contents |
27 _CR_UI_DECORATE_DEFINITION = Processor(_CR_UI_JS).contents | 28 _CR_UI_DECORATE_DEFINITION = Processor(_CR_UI_JS).contents |
28 | 29 |
29 def setUp(self): | 30 def setUp(self): |
30 self._checker = Checker() | 31 self._checker = Checker() |
| 32 self._tmp_files = [] |
31 | 33 |
32 def _runChecker(self, source_code): | 34 def tearDown(self): |
| 35 for file in self._tmp_files: |
| 36 if os.path.exists(file): |
| 37 os.remove(file) |
| 38 |
| 39 def _runChecker(self, source_code, output_wrapper=None): |
33 file_path = "/script.js" | 40 file_path = "/script.js" |
34 FileCache._cache[file_path] = source_code | 41 FileCache._cache[file_path] = source_code |
35 return self._checker.check(file_path, externs=[_POLYMER_EXTERNS]) | 42 out_file, out_map = self._createOutFiles() |
| 43 |
| 44 found_errors, stderr = self._checker.check(file_path, |
| 45 externs=[_POLYMER_EXTERNS], |
| 46 out_file=out_file, |
| 47 output_wrapper=output_wrapper) |
| 48 return found_errors, stderr, out_file, out_map |
36 | 49 |
37 def _runCheckerTestExpectError(self, source_code, expected_error): | 50 def _runCheckerTestExpectError(self, source_code, expected_error): |
38 _, stderr = self._runChecker(source_code) | 51 _, stderr, out_file, out_map = self._runChecker(source_code) |
39 | 52 |
40 self.assertTrue(expected_error in stderr, | 53 self.assertTrue(expected_error in stderr, |
41 msg="Expected chunk: \n%s\n\nOutput:\n%s\n" % ( | 54 msg="Expected chunk: \n%s\n\nOutput:\n%s\n" % ( |
42 expected_error, stderr)) | 55 expected_error, stderr)) |
| 56 self.assertFalse(os.path.exists(out_file)) |
| 57 self.assertFalse(os.path.exists(out_map)) |
43 | 58 |
44 def _runCheckerTestExpectSuccess(self, source_code): | 59 def _runCheckerTestExpectSuccess(self, source_code, expected_output=None, |
45 found_errors, stderr = self._runChecker(source_code) | 60 output_wrapper=None): |
| 61 found_errors, stderr, out_file, out_map = self._runChecker(source_code, |
| 62 output_wrapper) |
46 | 63 |
47 self.assertFalse(found_errors, | 64 self.assertFalse(found_errors, |
48 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) | 65 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) |
49 | 66 |
| 67 self.assertTrue(os.path.exists(out_map)) |
| 68 self.assertTrue(os.path.exists(out_file)) |
| 69 if expected_output: |
| 70 with open(out_file, "r") as file: |
| 71 self.assertEquals(file.read(), expected_output) |
| 72 |
| 73 def _createOutFiles(self): |
| 74 out_file = tempfile.NamedTemporaryFile(delete=False) |
| 75 out_map = "%s.map" % out_file.name |
| 76 |
| 77 self._tmp_files.append(out_file.name) |
| 78 self._tmp_files.append(out_map) |
| 79 return out_file.name, out_map |
| 80 |
50 def testGetInstance(self): | 81 def testGetInstance(self): |
51 self._runCheckerTestExpectError(""" | 82 self._runCheckerTestExpectError(""" |
52 var cr = { | 83 var cr = { |
53 /** @param {!Function} ctor */ | 84 /** @param {!Function} ctor */ |
54 addSingletonGetter: function(ctor) { | 85 addSingletonGetter: function(ctor) { |
55 ctor.getInstance = function() { | 86 ctor.getInstance = function() { |
56 return ctor.instance_ || (ctor.instance_ = new ctor()); | 87 return ctor.instance_ || (ctor.instance_ = new ctor()); |
57 }; | 88 }; |
58 } | 89 } |
59 }; | 90 }; |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 function Class() {} | 259 function Class() {} |
229 | 260 |
230 /** @return {Class} */ | 261 /** @return {Class} */ |
231 function f() { | 262 function f() { |
232 var a = document.createElement('div'); | 263 var a = document.createElement('div'); |
233 cr.ui.decorate(a, Class); | 264 cr.ui.decorate(a, Class); |
234 return a; | 265 return a; |
235 } | 266 } |
236 """) | 267 """) |
237 | 268 |
| 269 def testValidScriptCompilation(self): |
| 270 self._runCheckerTestExpectSuccess(""" |
| 271 var testScript = function() { |
| 272 console.log("hello world") |
| 273 }; |
| 274 """, |
| 275 """'use strict';var testScript=function(){console.log("hello world")};\n""") |
| 276 |
| 277 def testOutputWrapper(self): |
| 278 source_code = """ |
| 279 var testScript = function() { |
| 280 console.log("hello world"); |
| 281 }; |
| 282 """ |
| 283 expected_output = ("""(function(){'use strict';var testScript=function()""" |
| 284 """{console.log("hello world")};})();\n""") |
| 285 output_wrapper="(function(){%output%})();" |
| 286 self._runCheckerTestExpectSuccess(source_code, expected_output, |
| 287 output_wrapper=output_wrapper) |
| 288 |
| 289 def testCheckMultiple(self): |
| 290 source_file1 = tempfile.NamedTemporaryFile(delete=False) |
| 291 with open(source_file1.name, "w") as f: |
| 292 f.write(""" |
| 293 goog.provide('testScript'); |
| 294 |
| 295 var testScript = function() {}; |
| 296 """) |
| 297 self._tmp_files.append(source_file1.name) |
| 298 |
| 299 source_file2 = tempfile.NamedTemporaryFile(delete=False) |
| 300 with open(source_file2.name, "w") as f: |
| 301 f.write(""" |
| 302 goog.require('testScript'); |
| 303 |
| 304 testScript(); |
| 305 """) |
| 306 self._tmp_files.append(source_file2.name) |
| 307 |
| 308 out_file, out_map = self._createOutFiles() |
| 309 sources = [source_file1.name, source_file2.name] |
| 310 externs = [_POLYMER_EXTERNS] |
| 311 found_errors, stderr = self._checker.check_multiple(sources, |
| 312 externs=externs, |
| 313 out_file=out_file) |
| 314 self.assertFalse(found_errors, |
| 315 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) |
| 316 |
| 317 expected_output = "'use strict';var testScript=function(){};testScript();\n" |
| 318 self.assertTrue(os.path.exists(out_map)) |
| 319 self.assertTrue(os.path.exists(out_file)) |
| 320 with open(out_file, "r") as file: |
| 321 self.assertEquals(file.read(), expected_output) |
| 322 |
238 | 323 |
239 if __name__ == "__main__": | 324 if __name__ == "__main__": |
240 unittest.main() | 325 unittest.main() |
OLD | NEW |