OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 import os |
| 7 import scan_sources |
| 8 import unittest |
| 9 |
| 10 |
| 11 class MockSimplePath(object): |
| 12 def exists(self, pathname): |
| 13 return True |
| 14 |
| 15 def getcwd(self): |
| 16 return '.' |
| 17 |
| 18 def isdir(self, pathname): |
| 19 return True |
| 20 |
| 21 def realpath(self, pathname): |
| 22 return pathname |
| 23 |
| 24 |
| 25 class MockScanner(object): |
| 26 def __init__(self, filelists): |
| 27 self.filelists = filelists |
| 28 |
| 29 def ScanFile(self, filename): |
| 30 if not self.filelists: |
| 31 return [] |
| 32 return self.filelists.pop() |
| 33 |
| 34 |
| 35 class MockResolver(object): |
| 36 def FindFile(self, filename): |
| 37 return filename |
| 38 |
| 39 |
| 40 class ScannerUnitTest(unittest.TestCase): |
| 41 |
| 42 def testScanData(self): |
| 43 scanner = scan_sources.Scanner() |
| 44 test = """ |
| 45 #This is not an include |
| 46 #include is <bogus> |
| 47 #include <x1> |
| 48 #include "x2" |
| 49 #include <x3> |
| 50 #include "x4" |
| 51 # include <x5> |
| 52 # include "x6" |
| 53 # include "x7" |
| 54 Not |
| 55 """ |
| 56 results = scanner.ScanData(test) |
| 57 self.assertEqual(results, ['x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7']) |
| 58 |
| 59 def testResolverAddDirectories(self): |
| 60 resolver = scan_sources.Resolver(MockSimplePath()) |
| 61 resolver.AddDirectories(['Dir1 Dir2', 'Dir3', 'Dir4', 'Dir5']) |
| 62 results = resolver.GetDirectories() |
| 63 self.assertEqual(results, ['Dir1','Dir2','Dir3','Dir4','Dir5']) |
| 64 |
| 65 def testResolverRelative(self): |
| 66 resolver = scan_sources.Resolver() |
| 67 tests = [ |
| 68 ('/foo/bar','/foo/bar/file1','file1'), |
| 69 ('/foo/bar/extra', '/foo/bar/file2', '../file2'), |
| 70 ('/foo/bar', '/foo/bar/extra/file3', 'extra/file3'), |
| 71 ] |
| 72 for (base, full, rel) in tests: |
| 73 self.assertEqual(rel, resolver.RealToRelative(full, base)) |
| 74 |
| 75 def testWorkQ(self): |
| 76 filelists = [['file1', 'file4', 'file2'], ['file3'], ['file5', 'file2']] |
| 77 resolver = MockResolver() |
| 78 scanner = MockScanner(filelists) |
| 79 workq = scan_sources.WorkQueue(resolver, scanner) |
| 80 workq.PushIfNew('file3') |
| 81 result = workq.Run() |
| 82 |
| 83 flat = set([item for item in [sublist for sublist in filelists]]) |
| 84 flat = [item for item in flat] |
| 85 |
| 86 flat = sorted(flat) |
| 87 result = sorted(flat) |
| 88 self.assertEqual(flat, result) |
| 89 |
| 90 if __name__ == '__main__': |
| 91 unittest.main() |
| 92 |
OLD | NEW |