File: callback.t

package info (click to toggle)
libdata-visitor-perl 0.30-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 204 kB
  • ctags: 49
  • sloc: perl: 1,169; makefile: 2
file content (137 lines) | stat: -rw-r--r-- 1,830 bytes parent folder | download | duplicates (3)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;


use Data::Visitor::Callback;

can_ok('Data::Visitor::Callback', "new");

counters_are( "foo", "string", {
	visit => 1,
	value => 1,
	plain_value => 1,
});

counters_are( undef, "undef", {
	visit => 1,
	value => 1,
	plain_value => 1,
});

counters_are( [], "array", {
	visit => 1,
	ref => 1,
	array => 1,
});

counters_are( {}, "hash", {
	visit => 1,
	ref => 1,
	hash => 1,
});

counters_are( [ "foo" ], "deep array", {
	visit => 2,
	ref => 1,
	array => 1,
	value => 1,
	plain_value => 1,
});

{
	package Mammal;
	package Moose;
	our @ISA = ("Mammal");
}
{
	package Unrelated::Class;
}

counters_are( bless({}, "Moose"), "object", {
	visit => 1,
	object => 1,
	Moose => 1,
	Mammal => 1,
});

counters_are( bless({}, "Mammal"), "object", {
	visit => 1,
	object => 1,
	Mammal => 1,
});

counters_are( \10, "scalar_ref", {
	visit => 2,
	ref => 1,
	'scalar' => 1,
	value => 1,
	plain_value => 1,
});

our $FOO = 1;
our %FOO = ( "foo" => undef );

counters_are( \*FOO, "glob", {
	ref => 3,
	visit => 6,
	'scalar' => 1,
	hash => 1,
	value => 3,
	plain_value => 3,
	'glob' => 1,
});

counters_are( sub { }, "code", {
	visit => 1,
	value => 1,
	ref => 1,
	ref_value => 1,
});

counters_are( qr/foo/, "regex", {
	visit => 1,
	object => 1,
});

sub counters_are {
	my ( $data, $desc, $expected_counters ) = @_;

	my %counters;

	my %callbacks = (
		map {
			my $name = $_;
			$name => sub { $counters{$name}++ }
		} qw(
			visit
			value
			ref
			ref_value
			plain_value
			object
			array
			hash
			glob
			scalar
			Moose
			Mammal
			Unrelated::Class
		),
	);

	my $v = Data::Visitor::Callback->new(
		ignore_return_values => 1,
		%callbacks,
	);

	$v->visit( $data );

	local $Test::Builder::Level = 2;
	is_deeply( \%counters, $expected_counters, $desc );
}

done_testing