| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright 2013 The Closure Linter Authors. All Rights Reserved. | |
| 3 # | |
| 4 # Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 # you may not use this file except in compliance with the License. | |
| 6 # You may obtain a copy of the License at | |
| 7 # | |
| 8 # http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 # | |
| 10 # Unless required by applicable law or agreed to in writing, software | |
| 11 # distributed under the License is distributed on an "AS-IS" BASIS, | |
| 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 # See the License for the specific language governing permissions and | |
| 14 # limitations under the License. | |
| 15 | |
| 16 """Medium tests for the gjslint errorrules. | |
| 17 | |
| 18 Currently its just verifying that warnings can't be disabled. | |
| 19 """ | |
| 20 | |
| 21 | |
| 22 | |
| 23 import gflags as flags | |
| 24 import unittest as googletest | |
| 25 | |
| 26 from closure_linter import errors | |
| 27 from closure_linter import runner | |
| 28 from closure_linter.common import erroraccumulator | |
| 29 | |
| 30 flags.FLAGS.strict = True | |
| 31 flags.FLAGS.limited_doc_files = ('dummy.js', 'externs.js') | |
| 32 flags.FLAGS.closurized_namespaces = ('goog', 'dummy') | |
| 33 | |
| 34 | |
| 35 class ErrorRulesTest(googletest.TestCase): | |
| 36 """Test case to for gjslint errorrules.""" | |
| 37 | |
| 38 def testNoMaxLineLengthFlagExists(self): | |
| 39 """Tests that --max_line_length flag does not exists.""" | |
| 40 self.assertTrue('max_line_length' not in flags.FLAGS.FlagDict()) | |
| 41 | |
| 42 def testGetMaxLineLength(self): | |
| 43 """Tests warning are reported for line greater than 80. | |
| 44 """ | |
| 45 | |
| 46 # One line > 100 and one line > 80 and < 100. So should produce two | |
| 47 # line too long error. | |
| 48 original = [ | |
| 49 'goog.require(\'dummy.aa\');', | |
| 50 '', | |
| 51 'function a() {', | |
| 52 ' dummy.aa.i = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13' | |
| 53 ' + 14 + 15 + 16 + 17 + 18 + 19 + 20;', | |
| 54 ' dummy.aa.j = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13' | |
| 55 ' + 14 + 15 + 16 + 17 + 18;', | |
| 56 '}', | |
| 57 '' | |
| 58 ] | |
| 59 | |
| 60 # Expect line too long. | |
| 61 expected = [errors.LINE_TOO_LONG, errors.LINE_TOO_LONG] | |
| 62 | |
| 63 self._AssertErrors(original, expected) | |
| 64 | |
| 65 def testNoDisableFlagExists(self): | |
| 66 """Tests that --disable flag does not exists.""" | |
| 67 self.assertTrue('disable' not in flags.FLAGS.FlagDict()) | |
| 68 | |
| 69 def testWarningsNotDisabled(self): | |
| 70 """Tests warnings are reported when nothing is disabled. | |
| 71 """ | |
| 72 original = [ | |
| 73 'goog.require(\'dummy.aa\');', | |
| 74 'goog.require(\'dummy.Cc\');', | |
| 75 'goog.require(\'dummy.Dd\');', | |
| 76 '', | |
| 77 'function a() {', | |
| 78 ' dummy.aa.i = 1;', | |
| 79 ' dummy.Cc.i = 1;', | |
| 80 ' dummy.Dd.i = 1;', | |
| 81 '}', | |
| 82 ] | |
| 83 | |
| 84 expected = [errors.GOOG_REQUIRES_NOT_ALPHABETIZED, | |
| 85 errors.FILE_MISSING_NEWLINE] | |
| 86 | |
| 87 self._AssertErrors(original, expected) | |
| 88 | |
| 89 def _AssertErrors(self, original, expected_errors, include_header=True): | |
| 90 """Asserts that the error fixer corrects original to expected.""" | |
| 91 if include_header: | |
| 92 original = self._GetHeader() + original | |
| 93 | |
| 94 # Trap gjslint's output parse it to get messages added. | |
| 95 error_accumulator = erroraccumulator.ErrorAccumulator() | |
| 96 runner.Run('testing.js', error_accumulator, source=original) | |
| 97 error_nums = [e.code for e in error_accumulator.GetErrors()] | |
| 98 | |
| 99 error_nums.sort() | |
| 100 expected_errors.sort() | |
| 101 self.assertListEqual(error_nums, expected_errors) | |
| 102 | |
| 103 def _GetHeader(self): | |
| 104 """Returns a fake header for a JavaScript file.""" | |
| 105 return [ | |
| 106 '// Copyright 2011 Google Inc. All Rights Reserved.', | |
| 107 '', | |
| 108 '/**', | |
| 109 ' * @fileoverview Fake file overview.', | |
| 110 ' * @author fake@google.com (Fake Person)', | |
| 111 ' */', | |
| 112 '' | |
| 113 ] | |
| 114 | |
| 115 | |
| 116 if __name__ == '__main__': | |
| 117 googletest.main() | |
| OLD | NEW |