|
| 1 | +import unittest |
| 2 | +import sys |
| 3 | +from contextlib import contextmanager |
| 4 | +from os import path |
| 5 | +sys.path.append(path.abspath(path.join(path.dirname(__file__), |
| 6 | + '..', '..', 'tools'))) |
| 7 | +try: |
| 8 | + from StringIO import StringIO |
| 9 | +except ImportError: |
| 10 | + from io import StringIO |
| 11 | + |
| 12 | +from checkimports import is_valid |
| 13 | + |
| 14 | +@contextmanager |
| 15 | +def captured_output(): |
| 16 | + tmp_out, tmp_err = StringIO(), StringIO() |
| 17 | + old_out, old_err = sys.stdout, sys.stderr |
| 18 | + try: |
| 19 | + sys.stdout, sys.stderr = tmp_out, tmp_err |
| 20 | + yield sys.stdout, sys.stderr |
| 21 | + finally: |
| 22 | + sys.stdout, sys.stderr = old_out, old_err |
| 23 | + tmp_out.close() |
| 24 | + tmp_err.close() |
| 25 | + |
| 26 | +class CheckImportsTest(unittest.TestCase): |
| 27 | + fixturesDir = path.join(path.dirname(__file__), '..', '..', |
| 28 | + 'test', 'fixtures', 'tools', 'checkimports') |
| 29 | + |
| 30 | + def test_unused_and_unsorted(self): |
| 31 | + with captured_output() as (out, err): |
| 32 | + self.assertEqual(is_valid(path.join(self.fixturesDir, 'invalid.cc')), |
| 33 | + False) |
| 34 | + output = out.getvalue() |
| 35 | + self.assertIn('does not use "Local"', output); |
| 36 | + self.assertIn('using statements aren\'t sorted in', output); |
| 37 | + self.assertIn('Line 1: Actual: v8::MaybeLocal, Expected: v8::Array', |
| 38 | + output); |
| 39 | + self.assertIn('Line 2: Actual: v8::Array, Expected: v8::Local', |
| 40 | + output); |
| 41 | + self.assertIn('Line 3: Actual: v8::Local, Expected: v8::MaybeLocal', |
| 42 | + output); |
| 43 | + |
| 44 | + def test_unused_complex(self): |
| 45 | + with captured_output() as (out, err): |
| 46 | + self.assertEqual(is_valid(path.join(self.fixturesDir, 'maybe.cc')), |
| 47 | + False) |
| 48 | + output = out.getvalue() |
| 49 | + self.assertIn('does not use "Local"', output); |
| 50 | + |
| 51 | + def test_unused_simple(self): |
| 52 | + with captured_output() as (out, err): |
| 53 | + self.assertEqual(is_valid(path.join(self.fixturesDir, 'unused.cc')), |
| 54 | + False) |
| 55 | + output = out.getvalue() |
| 56 | + self.assertIn('does not use "Context"', output); |
| 57 | + |
| 58 | + def test_unsorted(self): |
| 59 | + with captured_output() as (out, err): |
| 60 | + self.assertEqual(is_valid(path.join(self.fixturesDir, 'unsorted.cc')), |
| 61 | + False) |
| 62 | + output = out.getvalue() |
| 63 | + self.assertIn('using statements aren\'t sorted in', output); |
| 64 | + self.assertIn('Line 1: Actual: v8::MaybeLocal, Expected: v8::Array', |
| 65 | + output); |
| 66 | + self.assertIn('Line 2: Actual: v8::Array, Expected: v8::MaybeLocal', |
| 67 | + output); |
| 68 | + |
| 69 | + def test_valid(self): |
| 70 | + with captured_output() as (out, err): |
| 71 | + self.assertEqual(is_valid(path.join(self.fixturesDir, 'valid.cc')), |
| 72 | + True) |
| 73 | + output = out.getvalue() |
| 74 | + self.assertEqual(output, ''); |
| 75 | + |
| 76 | +if __name__ == '__main__': |
| 77 | + unittest.main() |
0 commit comments