1
exercism fetch python flatten-array

flatten_array_test.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import unittest

from flatten_array import flatten


# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0

class FlattenArrayTests(unittest.TestCase):

    def test_no_nesting(self):
        self.assertEqual(flatten([0, 1, 2]), [0, 1, 2])

    def test_flatten_integers(self):
        inputs = [1, [2, 3, 4, 5, 6, 7], 8]
        expected = [1, 2, 3, 4, 5, 6, 7, 8]
        self.assertEqual(flatten(inputs), expected)

    def test_five_level_nesting(self):
        inputs = [0, 2, [[2, 3], 8, 100, 4, [[[50]]]], -2]
        expected = [0, 2, 2, 3, 8, 100, 4, 50, -2]
        self.assertEqual(flatten(inputs), expected)

    def test_six_level_nesting(self):
        inputs = [1, [2, [[3]], [4, [[5]]], 6, 7], 8]
        expected = [1, 2, 3, 4, 5, 6, 7, 8]
        self.assertEqual(flatten(inputs), expected)

    def test_with_none_values(self):
        inputs = [0, 2, [[2, 3], 8, [[100]], None, [[None]]], -2]
        expected = [0, 2, 2, 3, 8, 100, -2]
        self.assertEqual(flatten(inputs), expected)

    def test_all_values_are_none(self):
        inputs = [None, [[[None]]], None, None, [[None, None], None], None]
        expected = []
        self.assertEqual(flatten(inputs), expected)

    # Additional tests for this track
    def test_empty_nested_lists(self):
        self.assertEqual(flatten([[()]]), [])

    def test_strings(self):
        self.assertEqual(flatten(['0', ['1', '2']]), ['0', '1', '2'])


if __name__ == '__main__':
    unittest.main()