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

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

Issue 1128843007: Add tests for third_party/closure_compiler/compile.py (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Changes based on last review Created 5 years, 7 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 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
Dan Beam 2015/05/19 23:04:38 nit: remove \n
Theresa 2015/05/20 01:54:38 Done.
39
40 def _runChecker(self, source_code, output_wrapper=None):
33 file_path = "/script.js" 41 file_path = "/script.js"
34 FileCache._cache[file_path] = source_code 42 FileCache._cache[file_path] = source_code
35 return self._checker.check(file_path, externs=[_POLYMER_EXTERNS]) 43 out_file, out_map = self._createOutDir()
44
45 found_errors, stderr = self._checker.check(file_path,
46 externs=[_POLYMER_EXTERNS],
47 out_file=out_file,
48 output_wrapper=output_wrapper)
49 return found_errors, stderr, out_file, out_map
36 50
37 def _runCheckerTestExpectError(self, source_code, expected_error): 51 def _runCheckerTestExpectError(self, source_code, expected_error):
38 _, stderr = self._runChecker(source_code) 52 _, stderr, out_file, out_map = self._runChecker(source_code)
39 53
40 self.assertTrue(expected_error in stderr, 54 self.assertTrue(expected_error in stderr,
41 msg="Expected chunk: \n%s\n\nOutput:\n%s\n" % ( 55 msg="Expected chunk: \n%s\n\nOutput:\n%s\n" % (
42 expected_error, stderr)) 56 expected_error, stderr))
57 self.assertFalse(os.path.exists(out_file))
58 self.assertFalse(os.path.exists(out_map))
43 59
44 def _runCheckerTestExpectSuccess(self, source_code): 60 def _runCheckerTestExpectSuccess(self, source_code, expected_output=None,
45 found_errors, stderr = self._runChecker(source_code) 61 output_wrapper=None):
62 found_errors, stderr, out_file, out_map = self._runChecker(source_code,
63 output_wrapper)
46 64
47 self.assertFalse(found_errors, 65 self.assertFalse(found_errors,
48 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) 66 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr)
49 67
68 self.assertTrue(os.path.exists(out_map))
69 self.assertTrue(os.path.exists(out_file))
70 if expected_output:
71 with open(out_file, "r") as file:
72 self.assertEquals(file.read(), expected_output)
73
74 def _createOutDir(self):
75 out_file = "/tmp/gen/script.js"
Dan Beam 2015/05/19 23:04:39 Also needs to use tempfile
Theresa 2015/05/20 01:54:38 Done. out_file and out_map need the same base name
76 out_map = "/tmp/gen/script.js.map"
77
78 out_dir = os.path.dirname(out_file)
79 if not os.path.exists(out_dir):
80 os.makedirs(out_dir)
81
82 self._tmp_files.append(out_file)
83 self._tmp_files.append(out_map)
84 return out_file, out_map
85
50 def testGetInstance(self): 86 def testGetInstance(self):
51 self._runCheckerTestExpectError(""" 87 self._runCheckerTestExpectError("""
52 var cr = { 88 var cr = {
53 /** @param {!Function} ctor */ 89 /** @param {!Function} ctor */
54 addSingletonGetter: function(ctor) { 90 addSingletonGetter: function(ctor) {
55 ctor.getInstance = function() { 91 ctor.getInstance = function() {
56 return ctor.instance_ || (ctor.instance_ = new ctor()); 92 return ctor.instance_ || (ctor.instance_ = new ctor());
57 }; 93 };
58 } 94 }
59 }; 95 };
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 function Class() {} 264 function Class() {}
229 265
230 /** @return {Class} */ 266 /** @return {Class} */
231 function f() { 267 function f() {
232 var a = document.createElement('div'); 268 var a = document.createElement('div');
233 cr.ui.decorate(a, Class); 269 cr.ui.decorate(a, Class);
234 return a; 270 return a;
235 } 271 }
236 """) 272 """)
237 273
274 def testValidScriptCompilation(self):
275 self._runCheckerTestExpectSuccess("""
276 var testScript = function() {
277 console.log("hello world")
278 };
279 """,
280 """'use strict';var testScript=function(){console.log("hello world")};\n""")
281
282 def testOutputWrapper(self):
283 source_code = """
284 var testScript = function() {
285 console.log("hello world");
286 };
287 """
288 expected_output = ("""(function(){'use strict';var testScript=function()"""
289 + """{console.log("hello world")};})();\n""")
Dan Beam 2015/05/19 23:04:39 I don't think you need the +, also indent more
Theresa 2015/05/20 01:54:38 Done.
290 output_wrapper="(function(){%output%})();"
291 self._runCheckerTestExpectSuccess(source_code, expected_output,
292 output_wrapper=output_wrapper)
293
294 def testCheckMultiple(self):
295 source_file1 = tempfile.NamedTemporaryFile(delete=False)
296 with open(source_file1.name, "w") as f:
297 f.write("""
298 goog.provide('testScript');
299
300 var testScript = function() {};
301 """)
302 self._tmp_files.append(source_file1.name)
303
304 source_file2 = tempfile.NamedTemporaryFile(delete=False)
305 with open(source_file2.name, "w") as f:
306 f.write("""
307 goog.require('testScript');
308
309 testScript();
310 """)
311 self._tmp_files.append(source_file2.name)
312
313 out_file, out_map = self._createOutDir()
314 sources = [source_file1.name, source_file2.name]
315 externs = [_POLYMER_EXTERNS]
316 found_errors, stderr = self._checker.check_multiple(sources,
317 externs=externs,
318 out_file=out_file)
319 self.assertFalse(found_errors,
320 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr)
321
322 expected_output = "'use strict';var testScript=function(){};testScript();\n"
323 self.assertTrue(os.path.exists(out_map))
324 self.assertTrue(os.path.exists(out_file))
325 with open(out_file, "r") as file:
326 self.assertEquals(file.read(), expected_output)
327
238 328
239 if __name__ == "__main__": 329 if __name__ == "__main__":
240 unittest.main() 330 unittest.main()
OLDNEW
« no previous file with comments | « third_party/closure_compiler/compiler_customization_test.py ('k') | third_party/closure_compiler/run_tests.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698