Home | History | Annotate | Download | only in closure_linter
      1 #!/usr/bin/env python
      2 #
      3 # Copyright 2008 The Closure Linter Authors. All Rights Reserved.
      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 """Unit tests for the runner module."""
     17 
     18 __author__ = ('nnaze (at] google.com (Nathan Naze)')
     19 
     20 import StringIO
     21 
     22 
     23 import mox
     24 
     25 
     26 import unittest as googletest
     27 
     28 from closure_linter import errors
     29 from closure_linter import runner
     30 from closure_linter.common import error
     31 from closure_linter.common import errorhandler
     32 from closure_linter.common import tokens
     33 
     34 
     35 class LimitedDocTest(googletest.TestCase):
     36 
     37   def testIsLimitedDocCheck(self):
     38     self.assertTrue(runner._IsLimitedDocCheck('foo_test.js', ['_test.js']))
     39     self.assertFalse(runner._IsLimitedDocCheck('foo_bar.js', ['_test.js']))
     40 
     41     self.assertTrue(runner._IsLimitedDocCheck(
     42         'foo_moo.js', ['moo.js', 'quack.js']))
     43     self.assertFalse(runner._IsLimitedDocCheck(
     44         'foo_moo.js', ['woof.js', 'quack.js']))
     45 
     46 
     47 class RunnerTest(googletest.TestCase):
     48 
     49   def setUp(self):
     50     self.mox = mox.Mox()
     51 
     52   def testRunOnMissingFile(self):
     53     mock_error_handler = self.mox.CreateMock(errorhandler.ErrorHandler)
     54 
     55     def ValidateError(err):
     56       return (isinstance(err, error.Error) and
     57               err.code is errors.FILE_NOT_FOUND and
     58               err.token is None)
     59 
     60     mock_error_handler.HandleFile('does_not_exist.js', None)
     61     mock_error_handler.HandleError(mox.Func(ValidateError))
     62     mock_error_handler.FinishFile()
     63 
     64     self.mox.ReplayAll()
     65 
     66     runner.Run('does_not_exist.js', mock_error_handler)
     67 
     68     self.mox.VerifyAll()
     69 
     70   def testBadTokenization(self):
     71     mock_error_handler = self.mox.CreateMock(errorhandler.ErrorHandler)
     72 
     73     def ValidateError(err):
     74       return (isinstance(err, error.Error) and
     75               err.code is errors.FILE_IN_BLOCK and
     76               err.token.string == '}')
     77 
     78     mock_error_handler.HandleFile('foo.js', mox.IsA(tokens.Token))
     79     mock_error_handler.HandleError(mox.Func(ValidateError))
     80     mock_error_handler.HandleError(mox.IsA(error.Error))
     81     mock_error_handler.FinishFile()
     82 
     83     self.mox.ReplayAll()
     84 
     85     source = StringIO.StringIO(_BAD_TOKENIZATION_SCRIPT)
     86     runner.Run('foo.js', mock_error_handler, source)
     87 
     88     self.mox.VerifyAll()
     89 
     90 
     91 _BAD_TOKENIZATION_SCRIPT = """
     92 function foo () {
     93   var a = 3;
     94   var b = 2;
     95   return b + a; /* Comment not closed
     96 }
     97 """
     98 
     99 
    100 if __name__ == '__main__':
    101   googletest.main()
    102