Benedict Wong | 7ac69f1 | 2018-01-02 10:46:10 -0800 | [diff] [blame] | 1 | # Copyright 2017 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | def GetPadLength(block_size, length): |
Benedict Wong | ad7a31a | 2018-07-03 15:27:26 -0700 | [diff] [blame^] | 16 | return (block_size - (length % block_size)) % block_size |
| 17 | |
| 18 | |
| 19 | def InjectParameterizedTest(cls, param_list, name_generator): |
| 20 | """Injects parameterized tests into the provided class |
| 21 | |
| 22 | This method searches for all tests that start with the name "ParamTest", |
| 23 | and injects a test method for each set of parameters in param_list. Names |
| 24 | are generated via the use of the name_generator. |
| 25 | |
| 26 | Args: |
| 27 | cls: the class for which to inject all parameterized tests |
| 28 | param_list: a list of tuples, where each tuple is a combination of |
| 29 | of parameters to test (i.e. representing a single test case) |
| 30 | name_generator: A function that takes a combination of parameters and |
| 31 | returns a string that identifies the test case. |
| 32 | """ |
| 33 | param_test_names = [name for name in dir(cls) if name.startswith("ParamTest")] |
| 34 | |
| 35 | # Force param_list to an actual list; otherwise itertools.Product will hit |
| 36 | # the end, resulting in only the first ParamTest* method actually being |
| 37 | # parameterized |
| 38 | param_list = list(param_list) |
| 39 | |
| 40 | # Parameterize each test method starting with "ParamTest" |
| 41 | for test_name in param_test_names: |
| 42 | func = getattr(cls, test_name) |
| 43 | |
| 44 | for params in param_list: |
| 45 | # Give the test method a readable, debuggable name. |
| 46 | param_string = name_generator(*params) |
| 47 | new_name = "%s_%s" % (func.__name__.replace("ParamTest", "test"), |
| 48 | param_string) |
| 49 | new_name = new_name.replace("(", "-").replace(")", "") # remove parens |
| 50 | |
| 51 | # Inject the test method |
| 52 | setattr(cls, new_name, _GetTestClosure(func, params)) |
| 53 | |
| 54 | |
| 55 | def _GetTestClosure(func, params): |
| 56 | """ Creates a no-argument test method for the given function and parameters. |
| 57 | |
| 58 | This is required to be separate from the InjectParameterizedTest method, due |
| 59 | to some interesting scoping issues with internal function declarations. If |
| 60 | left in InjectParameterizedTest, all the tests end up using the same |
| 61 | instance of TestClosure |
| 62 | |
| 63 | Args: |
| 64 | func: the function for which this test closure should run |
| 65 | params: the parameters for the run of this test function |
| 66 | """ |
| 67 | |
| 68 | def TestClosure(self): |
| 69 | func(self, *params) |
| 70 | |
| 71 | return TestClosure |