1# Data::Walk - Traverse Perl data structures.
2# Copyright (C) 2005-2016 Guido Flohr <guido.flohr@cantanea.com>,
3# all rights reserved.
4
5# This program is free software; you can redistribute it and/or modify it
6# under the terms of the GNU Library General Public License as published
7# by the Free Software Foundation; either version 2, or (at your option)
8# any later version.
9
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13# Library General Public License for more details.
14
15# You should have received a copy of the GNU Library General Public
16# License along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18# USA.
19
20use strict;
21
22use Test;
23use Data::Walk;
24
25BEGIN {
26    plan tests => 52;
27}
28
29my ($data, $item, $count, $wanted, @hashdata);
30
31$data = "foobar";
32$item;
33$count = 0;
34$wanted = sub {
35    ++$count;
36    $item = $_;
37};
38walk $wanted, $data;
39ok $count, 1;
40ok $item, $data;
41
42$data = [ (0 .. 4) ];
43$count = 0;
44$wanted = sub {
45    ok($Data::Walk::type, 'ARRAY') unless ref $_;
46    ++$count;
47};
48walk $wanted, $data;
49ok $count, 1 + @{$data};
50
51@hashdata = qw (a b c d e);
52$data = { map { $_ => $_ } @hashdata };
53$count = 0;
54$wanted = sub {
55    ok($Data::Walk::type, 'HASH')unless ref $_;
56    ++$count;
57};
58walk $wanted, $data;
59ok $count, 1 + 2 * @hashdata;
60
61@hashdata = qw (a b c d e);
62$data = { map { $_ => $_ } @hashdata };
63my @list = (0 .. 4);
64$data->{list} = [ @list ];
65$count = 0;
66$wanted = sub {
67    ++$count;
68};
69walk $wanted, $data;
70ok $count, 1 + 2 * @hashdata + 2 + @list;
71
72$data = [ (0 .. 4) ];
73bless $data;
74$count = 0;
75$wanted = sub {
76    $DB::single = 1;
77    ok($Data::Walk::type, 'ARRAY') unless ref $_;
78    ++$count;
79};
80walk $wanted, $data;
81ok $count, 1 + @{$data};
82
83@hashdata = qw (a b c d e);
84$data = { map { $_ => $_ } @hashdata };
85bless $data;
86
87$count = 0;
88$wanted = sub {
89    ok($Data::Walk::type, 'HASH') unless ref $_;
90    ++$count;
91};
92walk $wanted, $data;
93ok $count, 1 + 2 * @hashdata;
94
95@hashdata = qw (a b c d e);
96$data = { map { $_ => $_ } @hashdata };
97@list = (0 .. 4);
98$data->{list} = [ @list ];
99bless $data;
100bless $data->{list};
101
102$count = 0;
103$wanted = sub {
104    ++$count;
105};
106walk $wanted, $data;
107ok $count, 1 + 2 * @hashdata + 2 + @list;
108
109$data = [[[[[ 1 ], 11], 111], 1111], 11111];
110my $wasref = 1;
111my $last = '';
112$wanted = sub {
113    my $isref = ref $_;
114
115    ok ($wasref || (!$wasref && !$isref));
116
117    $last = $_;
118    $wasref = $isref;
119};
120walk $wanted, $data;
121ok !$wasref;
122
123# The test data is constructed so that each node that is an
124# array reference has a number of elements equal to its depth.
125# Scalars are also equal to their depth.
126$data = [
127	    [
128                3, [ 4, 4, 4, ],
129	    ],
130	];
131
132$wanted = sub {
133    if (ref $_) {
134	my $num = @$_;
135	ok $Data::Walk::depth, $num;
136    } else {
137	$Data::Walk::depth, $_;
138    }
139};
140walk $wanted, $data;
141