OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright 2015 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 import os | |
7 import unittest | |
8 | |
9 from compile import Checker | |
10 from processor import FileCache | |
11 | |
12 _SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) | |
13 _SRC_DIR = os.path.join(_SCRIPT_DIR, os.pardir, os.pardir) | |
14 _POLYMER_EXTERNS = os.path.join(_SRC_DIR, "third_party", "polymer", "v0_8", | |
15 "components-chromium", "polymer-externs", | |
16 "polymer.externs.js") | |
Dan Beam
2015/05/19 17:05:38
can we merge this in with the customization test?
Theresa
2015/05/19 17:48:46
Done.
| |
17 | |
18 class CompileTest(unittest.TestCase): | |
19 | |
20 def setUp(self): | |
21 self._checker = Checker() | |
22 | |
23 def _runChecker(self, source_code, output_wrapper=None): | |
24 source_file = "/tmp/script.js" | |
25 FileCache._cache[source_file] = source_code | |
26 out_file, out_map = self._createOutFiles() | |
27 | |
28 found_errors, stderr = self._checker.check(source_file, | |
29 externs=[_POLYMER_EXTERNS], | |
30 out_file=out_file, | |
31 output_wrapper=output_wrapper) | |
32 return found_errors, stderr, out_file, out_map | |
33 | |
34 def _runCheckerTestExpectError(self, source_code, expected_error): | |
35 _, stderr, out_file, out_map = self._runChecker(source_code) | |
36 | |
37 self.assertTrue(expected_error in stderr, | |
38 msg="Expected chunk: \n%s\n\nOutput:\n%s\n" % ( | |
39 expected_error, stderr)) | |
40 self.assertFalse(os.path.exists(out_file)) | |
41 self.assertFalse(os.path.exists(out_map)) | |
42 | |
43 def _runCheckerTestExpectSuccess(self, source_code, expected_output, | |
44 output_wrapper=None): | |
45 found_errors, stderr, out_file, out_map = self._runChecker(source_code, | |
46 output_wrapper) | |
47 | |
48 self.assertFalse(found_errors, | |
49 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) | |
50 | |
51 self.assertTrue(os.path.exists(out_map)) | |
52 self.assertTrue(os.path.exists(out_file)) | |
53 with open(out_file, 'r') as file: | |
54 self.assertEquals(file.read(), expected_output) | |
55 | |
56 self._removeFiles([out_file, out_map]) | |
57 | |
58 def _createOutFiles(self): | |
59 out_file = "/tmp/gen/script.js" | |
60 out_map = "/tmp/gen/script.js.map" | |
61 | |
62 out_dir = os.path.dirname(out_file) | |
63 if not os.path.exists(out_dir): | |
64 os.makedirs(out_dir) | |
65 | |
66 return out_file, out_map | |
67 | |
68 def _removeFiles(self, files): | |
69 for file in files: | |
70 if(os.path.exists(file)): | |
71 os.remove(file) | |
72 | |
73 def testValidScriptCompilation(self): | |
74 self._runCheckerTestExpectSuccess(""" | |
75 var testScript = function() { console.log("hello world") };""", | |
76 """'use strict';var testScript=function(){console.log("hello world")};\n""") | |
77 | |
78 def testInvalidScriptCompalition(self): | |
79 self._runCheckerTestExpectError(""" | |
80 console.log(nonExistentVar); | |
81 """, "ERROR - variable nonExistentVar is undeclared") | |
82 | |
83 def testOutputWrapper(self): | |
84 self._runCheckerTestExpectSuccess(""" | |
85 var testScript = function() { console.log("hello world") }; | |
86 """, """(function(){'use strict';var testScript=function() | |
87 {console.log("hello world")};})();\n""", | |
88 output_wrapper="(function(){%output%})();") | |
89 | |
90 def testCheckMultiple(self): | |
91 source_file = "/tmp/script.js" | |
92 with open(source_file, 'w') as f: | |
93 f.write(""" | |
94 goog.require('testScript'); | |
95 | |
96 testScript(); | |
97 """) | |
98 | |
99 source_file2 = "/tmp/script2.js" | |
100 with open(source_file2, 'w') as f: | |
101 f.write(""" | |
102 goog.provide('testScript'); | |
103 | |
104 var testScript = function(){}; | |
105 """) | |
106 | |
107 out_file, out_map = self._createOutFiles() | |
108 sources = [source_file, source_file2] | |
109 externs = [_POLYMER_EXTERNS] | |
110 found_errors, stderr = self._checker.check_multiple(sources, | |
111 externs=externs, | |
112 out_file=out_file) | |
113 self.assertFalse(found_errors, | |
114 msg="Expected success, but got failure\n\nOutput:\n%s\n" % stderr) | |
115 | |
116 expected_output = "'use strict';var testScript=function(){};testScript();\n" | |
117 self.assertTrue(os.path.exists(out_map)) | |
118 self.assertTrue(os.path.exists(out_file)) | |
119 with open(out_file, 'r') as file: | |
120 self.assertEquals(file.read(), expected_output) | |
121 | |
122 self._removeFiles([source_file, source_file2, out_file, out_map]) | |
123 | |
124 | |
125 if __name__ == "__main__": | |
126 unittest.main() | |
OLD | NEW |