|
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 def NativePath(pathname): | |
12 """Convert POSIX style separators to native style.""" | |
13 return os.path.sep.join(pathname.split('/')) | |
14 | |
bradn
2011/09/29 20:25:30
Add a line here.
| |
15 class MockSimplePath(object): | |
16 def isdir(self, pathname): | |
17 return True | |
18 | |
19 def realpath(self, pathname): | |
20 return pathname | |
21 | |
22 def exists(self, pathname): | |
23 return True | |
24 | |
25 | |
26 class MockScanner(object): | |
27 def __init__(self, filelists): | |
28 self.filelists = filelists | |
29 | |
30 def ScanFile(self, filename): | |
31 if not self.filelists: | |
32 return [] | |
33 return self.filelists.pop() | |
34 | |
35 | |
36 class MockResolver(object): | |
37 def FindFile(self, filename): | |
38 return filename | |
39 | |
40 | |
41 class ScannerUnitTest(unittest.TestCase): | |
42 | |
43 def testScanData(self): | |
44 scanner = scan_sources.Scanner() | |
45 test = """ | |
46 #This is not an include | |
47 #include is <bogus> | |
48 #include <x1> | |
49 #include "x2" | |
50 #include <x3> | |
51 #include "x4" | |
52 # include <x5> | |
53 # include "x6" | |
54 # include "x7" | |
55 Not | |
56 """ | |
57 results = scanner.ScanData(test) | |
58 self.assertEqual(results, ['x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7']) | |
59 | |
60 def testResolverAddDirectories(self): | |
61 resolver = scan_sources.Resolver(MockSimplePath()) | |
62 resolver.AddDirectories(['Dir1 Dir2', 'Dir3', 'Dir4', 'Dir5']) | |
63 results = resolver.GetDirectories() | |
64 self.assertEqual(results, ['Dir1','Dir2','Dir3','Dir4','Dir5']) | |
65 | |
66 def testResolverRelative(self): | |
67 resolver = scan_sources.Resolver() | |
68 tests = [ | |
69 ('/foo/bar','/foo/bar/file1','file1'), | |
70 ('/foo/bar/extra', '/foo/bar/file2', '../file2'), | |
71 ('/foo/bar', '/foo/bar/extra/file3', 'extra/file3'), | |
72 ] | |
73 for (base, full, rel) in tests: | |
74 # Convert to native paths | |
75 full = NativePath(full) | |
76 base = NativePath(base) | |
77 rel = NativePath(rel) | |
78 self.assertEqual(rel, resolver.RealToRelative(full, base)) | |
bradn
2011/09/29 20:25:30
Will this test do the right thing on windows?
In f
| |
79 | |
80 def testWorkQ(self): | |
81 filelists = [['file1', 'file4', 'file2'], ['file3'], ['file5', 'file2']] | |
82 resolver = MockResolver() | |
83 scanner = MockScanner(filelists) | |
84 workq = scan_sources.WorkQueue(resolver, scanner) | |
85 workq.PushIfNew('file3') | |
86 result = workq.Run() | |
87 | |
88 flat = set([item for item in [sublist for sublist in filelists]]) | |
89 flat = [item for item in flat] | |
90 | |
91 flat = sorted(flat) | |
92 result = sorted(flat) | |
93 self.assertEqual(flat, result) | |
94 | |
bradn
2011/09/29 20:25:30
Add a line here.
| |
95 if __name__ == '__main__': | |
96 unittest.main() | |
97 | |
bradn
2011/09/29 20:25:30
Drop this line.
| |
OLD | NEW |