3 # Copyright 2008 The Closure Linter Authors. All Rights Reserved.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS-IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """Medium tests for the gpylint auto-fixer."""
19 __author__ = 'robbyw@google.com (Robby Walker)'
23 import gflags as flags
24 import unittest as googletest
25 from closure_linter import checker
26 from closure_linter import error_fixer
28 _RESOURCE_PREFIX = 'closure_linter/testdata'
30 flags.FLAGS.strict = True
31 flags.FLAGS.limited_doc_files = ('dummy.js', 'externs.js')
32 flags.FLAGS.closurized_namespaces = ('goog', 'dummy')
35 class FixJsStyleTest(googletest.TestCase):
36 """Test case to for gjslint auto-fixing."""
38 def testFixJsStyle(self):
39 test_cases = [['fixjsstyle.in.js', 'fixjsstyle.out.js'],
40 ['indentation.js', 'fixjsstyle.indentation.out.js']]
41 for [running_input_file, running_output_file] in test_cases:
43 golden_filename = None
44 current_filename = None
46 input_filename = '%s/%s' % (_RESOURCE_PREFIX, running_input_file)
47 current_filename = input_filename
49 golden_filename = '%s/%s' % (_RESOURCE_PREFIX, running_output_file)
50 current_filename = golden_filename
52 raise IOError('Could not find testdata resource for %s: %s' %
53 (current_filename, ex))
55 if running_input_file == 'fixjsstyle.in.js':
56 with open(input_filename) as f:
60 self.assertTrue(line == line.rstrip(), '%s file should not end '
61 'with a new line.' % (input_filename))
63 # Autofix the file, sending output to a fake file.
64 actual = StringIO.StringIO()
65 style_checker = checker.JavaScriptStyleChecker(
66 error_fixer.ErrorFixer(actual))
67 style_checker.Check(input_filename)
69 # Now compare the files.
71 expected = open(golden_filename, 'r')
73 self.assertEqual(actual.readlines(), expected.readlines())
75 def testMissingExtraAndUnsortedRequires(self):
76 """Tests handling of missing extra and unsorted goog.require statements."""
78 "goog.require('dummy.aa');",
79 "goog.require('dummy.Cc');",
80 "goog.require('dummy.Dd');",
82 "var x = new dummy.Bb();",
83 "dummy.Cc.someMethod();",
84 "dummy.aa.someMethod();",
88 "goog.require('dummy.Bb');",
89 "goog.require('dummy.Cc');",
90 "goog.require('dummy.aa');",
92 "var x = new dummy.Bb();",
93 "dummy.Cc.someMethod();",
94 "dummy.aa.someMethod();",
97 self._AssertFixes(original, expected)
99 def testMissingExtraAndUnsortedProvides(self):
100 """Tests handling of missing extra and unsorted goog.provide statements."""
102 "goog.provide('dummy.aa');",
103 "goog.provide('dummy.Cc');",
104 "goog.provide('dummy.Dd');",
106 "dummy.Cc = function() {};",
107 "dummy.Bb = function() {};",
108 "dummy.aa.someMethod = function();",
112 "goog.provide('dummy.Bb');",
113 "goog.provide('dummy.Cc');",
114 "goog.provide('dummy.aa');",
116 "dummy.Cc = function() {};",
117 "dummy.Bb = function() {};",
118 "dummy.aa.someMethod = function();",
121 self._AssertFixes(original, expected)
123 def testNoRequires(self):
124 """Tests positioning of missing requires without existing requires."""
126 "goog.provide('dummy.Something');",
128 "dummy.Something = function() {};",
130 "var x = new dummy.Bb();",
134 "goog.provide('dummy.Something');",
136 "goog.require('dummy.Bb');",
138 "dummy.Something = function() {};",
140 "var x = new dummy.Bb();",
143 self._AssertFixes(original, expected)
145 def testNoProvides(self):
146 """Tests positioning of missing provides without existing provides."""
148 "goog.require('dummy.Bb');",
150 "dummy.Something = function() {};",
152 "var x = new dummy.Bb();",
156 "goog.provide('dummy.Something');",
158 "goog.require('dummy.Bb');",
160 "dummy.Something = function() {};",
162 "var x = new dummy.Bb();",
165 self._AssertFixes(original, expected)
167 def testGoogScopeIndentation(self):
168 """Tests Handling a typical end-of-scope indentation fix."""
170 'goog.scope(function() {',
171 ' // TODO(brain): Take over the world.',
176 'goog.scope(function() {',
177 '// TODO(brain): Take over the world.',
181 self._AssertFixes(original, expected)
183 def testMissingEndOfScopeComment(self):
184 """Tests Handling a missing comment at end of goog.scope."""
186 'goog.scope(function() {',
191 'goog.scope(function() {',
195 self._AssertFixes(original, expected)
197 def testMissingEndOfScopeCommentWithOtherComment(self):
198 """Tests handling an irrelevant comment at end of goog.scope."""
200 'goog.scope(function() {',
201 "}); // I don't belong here!",
205 'goog.scope(function() {',
209 self._AssertFixes(original, expected)
211 def testMalformedEndOfScopeComment(self):
212 """Tests Handling a malformed comment at end of goog.scope."""
214 'goog.scope(function() {',
215 '}); // goog.scope FTW',
219 'goog.scope(function() {',
223 self._AssertFixes(original, expected)
225 def _AssertFixes(self, original, expected):
226 """Asserts that the error fixer corrects original to expected."""
227 original = self._GetHeader() + original
228 expected = self._GetHeader() + expected
230 actual = StringIO.StringIO()
231 style_checker = checker.JavaScriptStyleChecker(
232 error_fixer.ErrorFixer(actual))
233 style_checker.CheckLines('testing.js', original, False)
236 expected = [x + '\n' for x in expected]
238 self.assertListEqual(actual.readlines(), expected)
240 def _GetHeader(self):
241 """Returns a fake header for a JavaScript file."""
243 "// Copyright 2011 Google Inc. All Rights Reserved.",
246 " * @fileoverview Fake file overview.",
247 " * @author fake@google.com (Fake Person)",
253 if __name__ == '__main__':