1 # Copyright 2014 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.
10 class MockInputApi(object):
13 self
.os_path
= os
.path
15 self
.is_committing
= False
17 def AffectedFiles(self
):
20 def AffectedSourceFiles(self
, fn
):
21 # we'll just pretend everything is a source file for the sake of simplicity
24 def ReadFile(self
, f
):
25 return f
.NewContents()
28 class MockOutputApi(object):
29 class PresubmitResult(object):
30 def __init__(self
, message
, items
=None, long_text
=''):
31 self
.message
= message
33 self
.long_text
= long_text
35 class PresubmitError(PresubmitResult
):
36 def __init__(self
, message
, items
, long_text
=''):
37 MockOutputApi
.PresubmitResult
.__init
__(self
, message
, items
, long_text
)
40 class PresubmitPromptWarning(PresubmitResult
):
41 def __init__(self
, message
, items
, long_text
=''):
42 MockOutputApi
.PresubmitResult
.__init
__(self
, message
, items
, long_text
)
45 class PresubmitNotifyResult(PresubmitResult
):
46 def __init__(self
, message
, items
, long_text
=''):
47 MockOutputApi
.PresubmitResult
.__init
__(self
, message
, items
, long_text
)
50 class PresubmitPromptOrNotify(PresubmitResult
):
51 def __init__(self
, message
, items
, long_text
=''):
52 MockOutputApi
.PresubmitResult
.__init
__(self
, message
, items
, long_text
)
53 self
.type = 'promptOrNotify'
56 class MockFile(object):
57 def __init__(self
, local_path
, new_contents
):
58 self
._local
_path
= local_path
59 self
._new
_contents
= new_contents
60 self
._changed
_contents
= [(i
+ 1, l
) for i
, l
in enumerate(new_contents
)]
62 def ChangedContents(self
):
63 return self
._changed
_contents
65 def NewContents(self
):
66 return self
._new
_contents
69 return self
._local
_path
72 class MockChange(object):
73 def __init__(self
, changed_files
):
74 self
._changed
_files
= changed_files
77 return self
._changed
_files
80 class HistogramOffByOneTest(unittest
.TestCase
):
82 # Take an input and make sure the problems found equals the expectation.
83 def simpleCheck(self
, contents
, expected_errors
):
84 input_api
= MockInputApi()
85 input_api
.files
.append(MockFile('test.cc', contents
))
86 results
= PRESUBMIT
._CheckForHistogramOffByOne
(input_api
, MockOutputApi())
88 self
.assertEqual(1, len(results
))
89 self
.assertEqual(expected_errors
, len(results
[0].items
))
91 self
.assertEqual(0, len(results
))
94 self
.simpleCheck('UMA_HISTOGRAM_ENUMERATION("test", kFoo, kFooMax + 1);', 0)
96 def testValidComments(self
):
97 self
.simpleCheck('UMA_HISTOGRAM_ENUMERATION("test", /*...*/ kFoo, /*...*/'
100 def testValidMultiLine(self
):
101 self
.simpleCheck('UMA_HISTOGRAM_ENUMERATION("test",\n'
105 def testValidMultiLineComments(self
):
106 self
.simpleCheck('UMA_HISTOGRAM_ENUMERATION("test", // This is the name\n'
107 ' kFoo, /* The value */\n'
108 ' kFooMax + 1 /* The max */ );',
111 def testNoPlusOne(self
):
112 self
.simpleCheck('UMA_HISTOGRAM_ENUMERATION("test", kFoo, kFooMax);', 1)
114 def testInvalidWithIgnore(self
):
115 self
.simpleCheck('UMA_HISTOGRAM_ENUMERATION("test", kFoo, kFooMax); '
116 '// PRESUBMIT_IGNORE_UMA_MAX', 0)
119 self
.simpleCheck('UMA_HISTOGRAM_ENUMERATION("test", kFoo, kFoo + 1);', 1)
121 def testNoMaxNoPlusOne(self
):
122 self
.simpleCheck('UMA_HISTOGRAM_ENUMERATION("test", kFoo, kFoo);', 1)
124 def testMultipleErrors(self
):
125 self
.simpleCheck('UMA_HISTOGRAM_ENUMERATION("test", kFoo, kFoo);\n'
126 'printf("hello, world!");\n'
127 'UMA_HISTOGRAM_ENUMERATION("test", kBar, kBarMax);', 2)
129 def testValidAndInvalid(self
):
130 self
.simpleCheck('UMA_HISTOGRAM_ENUMERATION("test", kFoo, kFoo);\n'
131 'UMA_HISTOGRAM_ENUMERATION("test", kFoo, kFooMax + 1);'
132 'UMA_HISTOGRAM_ENUMERATION("test", kBar, kBarMax);', 2)
134 def testInvalidMultiLine(self
):
135 self
.simpleCheck('UMA_HISTOGRAM_ENUMERATION("test",\n'
139 def testInvalidComments(self
):
140 self
.simpleCheck('UMA_HISTOGRAM_ENUMERATION("test", /*...*/, val, /*...*/,'
143 def testInvalidMultiLineComments(self
):
144 self
.simpleCheck('UMA_HISTOGRAM_ENUMERATION("test", // This is the name\n'
145 ' kFoo, /* The value */\n'
146 ' kFooMax + 2 /* The max */ );',
149 if __name__
== '__main__':