Exemple #1
0
 def test_scalar(self):
     """
     walk(): scalars shouldn't yield any nodes.
     """
     scalars = [123, 'a', set([1,2,3]), None]
     for s in scalars:
         self.assertEqual(list(walk(s)), [])
Exemple #2
0
 def test_dfs_ordering(self):
     """
     walk(): should return nodes in DFS order.
     """
     d = [
         [
             [0, 1, 2, 3],    # d[0][0]
             4,               # d[0][1]
             [5, [6, 7], 8],  # d[0][2]
         ],
         9,                   # d[1]
     ]
     exp = [
         ( (0,),      d[0]    ),
         ( (0,0),     d[0][0] ),
         ( (0,0,0),   0 ),
         ( (0,0,1),   1 ),
         ( (0,0,2),   2 ),
         ( (0,0,3),   3 ),
         ( (0,1),     4 ),
         ( (0,2),     d[0][2] ),
         ( (0,2,0),   5 ),
         ( (0,2,1),   d[0][2][1] ),
         ( (0,2,1,0), 6 ),
         ( (0,2,1,1), 7 ),
         ( (0,2,2),   8 ),
         ( (1,),      9 ),
     ]
     self.assertEqual(list(walk(d)), exp)
Exemple #3
0
 def test_empty_collections(self):
     """
     walk(): empty collections shouldn't yield any nodes.
     """
     colls = [{}, [], ()]
     for c in colls:
         self.assertEqual(list(walk(c)), [])
Exemple #4
0
    def test_complex_example(self):
        """
        walk(): complex example with nesting, alternates, and cycles.
        """
        # Set up data structure.
        data = dict(
            a = 1,
            b = dict(
                c = 2,
                d = 3,
                e = [ 4, dict(f = 5, g = 6), 7, 8 ],
            ),
            i = [9, 10, 11],
            j = None,
            k = set((12, 13,14)),
        )

        # Add some alternate paths to the same underlying values.
        # These should generate additional nodes.
        data['aa']             = data['a']
        data['b']['e'][1]['h'] = data['i']
        data['z']              = data['b']['e'][-1]

        # Add some cycles.
        # These should NOT generate additional nodes.
        data['b']['aa'] = data
        data['b']['bb'] = data['b']

        # Expected nodes.
        B   = data['b']
        BE  = data['b']['e']
        BE1 = data['b']['e'][1]
        I   = data['i']
        J   = data['j']
        K   = data['k']
        exp =[
            ( ('a',),                 1    ),
            ( ('aa',),                1    ), # Alt path.
            ( ('b',),                 B    ), # dict
            ( ('b', 'c'),             2    ),
            ( ('b', 'd'),             3    ),
            ( ('b', 'e'),             BE   ), # list
            ( ('b', 'e', 0),          4    ),
            ( ('b', 'e', 1),          BE1  ), # dict
            ( ('b', 'e', 1, 'f'),     5    ),
            ( ('b', 'e', 1, 'g'),     6    ),
            ( ('b', 'e', 1, 'h'),     I    ), # Alt path.
            ( ('b', 'e', 1, 'h', 0),  9    ), # Ditto.
            ( ('b', 'e', 1, 'h', 1), 10    ), # Ditto.
            ( ('b', 'e', 1, 'h', 2), 11    ), # Ditto.
            ( ('b', 'e', 2),          7    ),
            ( ('b', 'e', 3),          8    ),
            ( ('i',),                 I    ), # list
            ( ('i', 0),               9    ),
            ( ('i', 1),              10    ),
            ( ('i', 2),              11    ),
            ( ('j',),                 J    ),
            ( ('k',),                 K    ),
            ( ('z',),                 8    ), # Alt path.
        ]

        # Assert.
        got = sorted(node for node in walk(data))
        self.assertEqual(got, exp)