def test_walk_nested_lists_of_ints(): w = Walker() isum = [0] output = [] def vint(i, w): isum[0] += i output.append(i) def vlist(l, w): for i in l: w.walk(i) w.visitor_map[int] = vint w.visitor_map[list] = vlist isum2 = [0] def test_walk(obj): isum2[0] += list_sum(obj) w.walk(obj) assert isum==isum2 test_walk(123) test_walk([123, 9, 23]) test_walk([123, 9, 23, 234, [[[[2, [[4, [5]]]]]]], 987]) test_walk([123, 9, 23, 234, 987, [0, 1]]) assert list_sum(output)==isum2[0] # testing with a subclass of int should also work: class subint(int): pass test_walk([subint(234)]) assert list_sum(output)==isum2[0]
def test_default_visitor(): w = Walker() assert not w.visitor_map.get(DEFAULT) out = [] def default(obj, walker): assert walker is w out.append(obj) w.visitor_map[DEFAULT] = default assert isinstance(w.visitor_map.get(DEFAULT), CallbackVisitor) assert w.visitor_map.get(DEFAULT).callback == default data = range(10)+['a', 'b', 2.3, None] #various types for i in data: w.walk(i) assert out==data
def __init__(self): Walker.__init__(self) self.output = []