1# Copyright 2020 The PDFium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import re 6 7 8class MockInputApi(object): 9 """Mock class for the InputApi class. 10 11 This class can be used for unittests for presubmit by initializing the files 12 attribute as the list of changed files. 13 """ 14 15 def __init__(self): 16 self.files = [] 17 self.re = re 18 19 def AffectedFiles(self, file_filter=None, include_deletes=False): 20 # pylint: disable=unused-argument 21 return self.files 22 23 24class MockOutputApi(object): 25 """Mock class for the OutputApi class. 26 27 An instance of this class can be passed to presubmit unittests for outputting 28 various types of results. 29 """ 30 31 class PresubmitResult(object): 32 33 def __init__(self, message, items=None, long_text=''): 34 self.message = message 35 self.items = items 36 self.long_text = long_text 37 38 def __repr__(self): 39 return self.message 40 41 class PresubmitError(PresubmitResult): 42 43 def __init__(self, message, items=None, long_text=''): 44 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) 45 self.type = 'error' 46 47 class PresubmitPromptWarning(PresubmitResult): 48 49 def __init__(self, message, items=None, long_text=''): 50 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text) 51 self.type = 'warning' 52 53 54class MockFile(object): 55 """Mock class for the File class. 56 57 This class can be used to form the mock list of changed files in 58 MockInputApi for presubmit unittests. 59 """ 60 61 def __init__(self, 62 local_path, 63 new_contents=None, 64 old_contents=None, 65 action='A'): 66 self._local_path = local_path 67 if new_contents is None: 68 new_contents = [] 69 self._new_contents = new_contents 70 self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)] 71 self._action = action 72 self._old_contents = old_contents 73 74 def ChangedContents(self): 75 return self._changed_contents 76 77 def LocalPath(self): 78 return self._local_path 79