1#!/usr/bin/env python3 2# Copyright 2020 The Pigweed Authors 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); you may not 5# use this file except in compliance with the License. You may obtain a copy of 6# the License at 7# 8# https://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13# License for the specific language governing permissions and limitations under 14# the License. 15"""Tests for presubmit tools.""" 16 17import unittest 18 19from pw_presubmit import presubmit 20 21 22def _fake_function_1(_): 23 """Fake presubmit function.""" 24 25 26def _fake_function_2(_): 27 """Fake presubmit function.""" 28 29 30def _all_substeps(program): 31 substeps = {} 32 for step in program: 33 # pylint: disable=protected-access 34 for sub in step.substeps(): 35 substeps[sub.name or step.name] = sub._func 36 # pylint: enable=protected-access 37 return substeps 38 39 40class ProgramsTest(unittest.TestCase): 41 """Tests the presubmit Programs abstraction.""" 42 43 def setUp(self): 44 self._programs = presubmit.Programs( 45 first=[_fake_function_1, (), [(_fake_function_2,)]], 46 second=[_fake_function_2], 47 ) 48 49 def test_empty(self): 50 self.assertEqual({}, presubmit.Programs()) 51 52 def test_access_present_members_first(self): 53 self.assertEqual('first', self._programs['first'].name) 54 self.assertEqual( 55 ('_fake_function_1', '_fake_function_2'), 56 tuple(x.name for x in self._programs['first']), 57 ) 58 59 self.assertEqual(2, len(self._programs['first'])) 60 substeps = _all_substeps( 61 self._programs['first'] # pylint: disable=protected-access 62 ).values() 63 self.assertEqual(2, len(substeps)) 64 self.assertEqual((_fake_function_1, _fake_function_2), tuple(substeps)) 65 66 def test_access_present_members_second(self): 67 self.assertEqual('second', self._programs['second'].name) 68 self.assertEqual( 69 ('_fake_function_2',), 70 tuple(x.name for x in self._programs['second']), 71 ) 72 73 self.assertEqual(1, len(self._programs['second'])) 74 substeps = _all_substeps( 75 self._programs['second'] # pylint: disable=protected-access 76 ).values() 77 self.assertEqual(1, len(substeps)) 78 self.assertEqual((_fake_function_2,), tuple(substeps)) 79 80 def test_access_missing_member(self): 81 with self.assertRaises(KeyError): 82 _ = self._programs['not_there'] 83 84 def test_all_steps(self): 85 all_steps = self._programs.all_steps() 86 self.assertEqual(len(all_steps), 2) 87 all_substeps = _all_substeps(all_steps.values()) 88 self.assertEqual(len(all_substeps), 2) 89 90 # pylint: disable=protected-access 91 self.assertEqual(all_substeps['_fake_function_1'], _fake_function_1) 92 self.assertEqual(all_substeps['_fake_function_2'], _fake_function_2) 93 # pylint: enable=protected-access 94 95 96if __name__ == '__main__': 97 unittest.main() 98