root/branches/release-40/t/lib/Test/Deep/HashKeysOnly.pm @ 2583

Revision 2583, 1.4 kB (checked in by mpaschal, 18 months ago)

Add Test::Deep to t/lib, since we already use it in driver-tests.pl
BugzID: 79953

Line 
1use strict;
2use warnings;
3
4package Test::Deep::HashKeysOnly;
5
6use Test::Deep::Ref;
7
8sub init
9{
10        my $self = shift;
11
12        my %keys;
13        @keys{@_} = ();
14        $self->{val} = \%keys;
15        $self->{keys} = [sort @_];
16}
17
18sub descend
19{
20        my $self = shift;
21        my $hash = shift;
22
23        my $data = $self->data;
24        my $exp = $self->{val};
25        my %got;
26        @got{keys %$hash} = ();
27
28        my @missing;
29        my @extra;
30
31        while (my ($key, $value) = each %$exp)
32        {
33                if (exists $got{$key})
34                {
35                        delete $got{$key};
36                }
37                else
38                {
39                        push(@missing, $key);
40                }
41        }
42
43        my @diags;
44        if (@missing and (not $self->ignoreMissing))
45        {
46                push(@diags, "Missing: ".nice_list(\@missing));
47        }
48
49        if (%got and (not $self->ignoreExtra))
50        {
51                push(@diags, "Extra: ".nice_list([keys %got]));
52        }
53
54        if (@diags)
55        {
56                $data->{diag} = join("\n", @diags);
57                return 0;
58        }
59
60        return 1;
61}
62
63sub diagnostics
64{
65        my $self = shift;
66        my ($where, $last) = @_;
67
68        my $type = $self->{IgnoreDupes} ? "Set" : "Bag";
69
70        my $error = $last->{diag};
71        my $diag = <<EOM;
72Comparing hash keys of $where
73$error
74EOM
75
76        return $diag;
77}
78
79sub nice_list
80{
81        my $list = shift;
82
83        return join(", ",
84                (map {"'$_'"} sort @$list),
85        );
86}
87
88sub ignoreMissing
89{
90        return 0;
91}
92
93sub ignoreExtra
94{
95        return 0;
96}
97
98package Test::Deep::SuperHashKeysOnly;
99
100use base 'Test::Deep::HashKeysOnly';
101
102sub ignoreMissing
103{
104        return 0;
105}
106
107sub ignoreExtra
108{
109        return 1;
110}
111
112package Test::Deep::SubHashKeysOnly;
113
114use base 'Test::Deep::HashKeysOnly';
115
116sub ignoreMissing
117{
118        return 1;
119}
120
121sub ignoreExtra
122{
123        return 0;
124}
125
1261;
Note: See TracBrowser for help on using the browser.