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 general purpose tools.""" 16 17import unittest 18 19from pw_presubmit import tools 20 21 22class FlattenTest(unittest.TestCase): 23 """Tests the flatten function, which flattens iterables.""" 24 25 def test_empty(self): 26 self.assertEqual([], list(tools.flatten())) 27 self.assertEqual([], list(tools.flatten([]))) 28 self.assertEqual([], list(tools.flatten([], ()))) 29 self.assertEqual([], list(tools.flatten([[], (), [[]]], ((), [])))) 30 31 def test_no_nesting(self): 32 self.assertEqual( 33 ['a', 'bcd', 123, 45.6], list(tools.flatten('a', 'bcd', 123, 45.6)) 34 ) 35 self.assertEqual( 36 ['a', 'bcd', 123, 45.6], 37 list(tools.flatten(['a', 'bcd', 123, 45.6])), 38 ) 39 self.assertEqual( 40 ['a', 'bcd', 123, 45.6], 41 list(tools.flatten(['a', 'bcd'], [123, 45.6])), 42 ) 43 44 def test_nesting(self): 45 self.assertEqual( 46 ['a', 'bcd', 123, 45.6], 47 list(tools.flatten('a', ['bcd'], [123], 45.6)), 48 ) 49 self.assertEqual( 50 ['a', 'bcd', 123, 45.6], 51 list(tools.flatten([['a', ('bcd', [123])], 45.6])), 52 ) 53 self.assertEqual( 54 ['a', 'bcd', 123, 45.6], 55 list(tools.flatten([('a', 'bcd')], [[[[123]]], 45.6])), 56 ) 57 58 59if __name__ == '__main__': 60 unittest.main() 61