Home | History | Annotate | Download | only in closure_linter
      1 #!/usr/bin/env python
      2 #
      3 # Copyright 2012 The Closure Linter Authors. All Rights Reserved.
      4 #
      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
      8 #
      9 #      http://www.apache.org/licenses/LICENSE-2.0
     10 #
     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.
     16 
     17 """Unit tests for RequireProvideSorter."""
     18 
     19 
     20 
     21 import unittest as googletest
     22 from closure_linter import ecmametadatapass
     23 from closure_linter import javascripttokenizer
     24 from closure_linter import javascripttokens
     25 from closure_linter import requireprovidesorter
     26 
     27 # pylint: disable-msg=C6409
     28 TokenType = javascripttokens.JavaScriptTokenType
     29 
     30 
     31 class RequireProvideSorterTest(googletest.TestCase):
     32   """Tests for RequireProvideSorter."""
     33 
     34   _tokenizer = javascripttokenizer.JavaScriptTokenizer()
     35   _metadata_pass = ecmametadatapass.EcmaMetaDataPass()
     36 
     37   def testFixRequires_removeBlankLines(self):
     38     """Tests that blank lines are omitted in sorted goog.require statements."""
     39     input_lines = [
     40         'goog.provide(\'package.subpackage.Whatever\');',
     41         '',
     42         'goog.require(\'package.subpackage.ClassB\');',
     43         '',
     44         'goog.require(\'package.subpackage.ClassA\');'
     45     ]
     46     expected_lines = [
     47         'goog.provide(\'package.subpackage.Whatever\');',
     48         '',
     49         'goog.require(\'package.subpackage.ClassA\');',
     50         'goog.require(\'package.subpackage.ClassB\');'
     51     ]
     52     token = self._tokenizer.TokenizeFile(input_lines)
     53     self._metadata_pass.Reset()
     54     self._metadata_pass.Process(token)
     55 
     56     sorter = requireprovidesorter.RequireProvideSorter()
     57     sorter.FixRequires(token)
     58 
     59     self.assertEquals(expected_lines, self._GetLines(token))
     60 
     61   def _GetLines(self, token):
     62     """Returns an array of lines based on the specified token stream."""
     63     lines = []
     64     line = ''
     65     while token:
     66       line += token.string
     67       if token.IsLastInLine():
     68         lines.append(line)
     69         line = ''
     70       token = token.next
     71     return lines
     72 
     73 if __name__ == '__main__':
     74   googletest.main()
     75