OLD | NEW |
(Empty) | |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 from file_system import FileSystem |
| 6 |
| 7 class MockFileSystem(FileSystem): |
| 8 '''Wraps a FileSystem to add simple mock behaviour - asserting how often |
| 9 Stat/Read calls are being made to it. The Read/Stat implementations |
| 10 themselves are provided by a delegate FileSystem. |
| 11 ''' |
| 12 def __init__(self, file_system): |
| 13 self._file_system = file_system |
| 14 self._read_count = 0 |
| 15 self._stat_count = 0 |
| 16 |
| 17 # |
| 18 # FileSystem implementation. |
| 19 # |
| 20 |
| 21 def Read(self, paths, binary=False): |
| 22 self._read_count += 1 |
| 23 return self._file_system.Read(paths, binary=binary) |
| 24 |
| 25 def Stat(self, path): |
| 26 self._stat_count += 1 |
| 27 return self._file_system.Stat(path) |
| 28 |
| 29 def GetIdentity(self): |
| 30 return self._file_system.GetIdentity() |
| 31 |
| 32 def __str__(self): |
| 33 return repr(self) |
| 34 |
| 35 def __repr__(self): |
| 36 return 'MockFileSystem(read_count=%s, stat_count=%s)' % ( |
| 37 self._read_count, self._stat_count) |
| 38 |
| 39 # |
| 40 # Testing methods. |
| 41 # |
| 42 |
| 43 def GetReadCount(self): |
| 44 return self._read_count |
| 45 |
| 46 def GetStatCount(self): |
| 47 return self._stat_count |
| 48 |
| 49 def CheckAndReset(self, stat_count=0, read_count=0): |
| 50 '''Returns a tuple (success, error). Use in tests like: |
| 51 self.assertTrue(*object_store.CheckAndReset(...)) |
| 52 ''' |
| 53 errors = [] |
| 54 for desc, expected, actual in ( |
| 55 ('read_count', read_count, self._read_count), |
| 56 ('stat_count', stat_count, self._stat_count)): |
| 57 if actual != expected: |
| 58 errors.append('%s: expected %s got %s' % (desc, expected, actual)) |
| 59 try: |
| 60 return (len(errors) == 0, ', '.join(errors)) |
| 61 finally: |
| 62 self.Reset() |
| 63 |
| 64 def Reset(self): |
| 65 self._read_count = 0 |
| 66 self._stat_count = 0 |
OLD | NEW |