|
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 | |
|---|
| 1 | use strict; |
|---|
| 2 | use warnings; |
|---|
| 3 | |
|---|
| 4 | package Test::Deep::HashKeysOnly; |
|---|
| 5 | |
|---|
| 6 | use Test::Deep::Ref; |
|---|
| 7 | |
|---|
| 8 | sub init |
|---|
| 9 | { |
|---|
| 10 | my $self = shift; |
|---|
| 11 | |
|---|
| 12 | my %keys; |
|---|
| 13 | @keys{@_} = (); |
|---|
| 14 | $self->{val} = \%keys; |
|---|
| 15 | $self->{keys} = [sort @_]; |
|---|
| 16 | } |
|---|
| 17 | |
|---|
| 18 | sub 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 | |
|---|
| 63 | sub 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; |
|---|
| 72 | Comparing hash keys of $where |
|---|
| 73 | $error |
|---|
| 74 | EOM |
|---|
| 75 | |
|---|
| 76 | return $diag; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | sub nice_list |
|---|
| 80 | { |
|---|
| 81 | my $list = shift; |
|---|
| 82 | |
|---|
| 83 | return join(", ", |
|---|
| 84 | (map {"'$_'"} sort @$list), |
|---|
| 85 | ); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | sub ignoreMissing |
|---|
| 89 | { |
|---|
| 90 | return 0; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | sub ignoreExtra |
|---|
| 94 | { |
|---|
| 95 | return 0; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | package Test::Deep::SuperHashKeysOnly; |
|---|
| 99 | |
|---|
| 100 | use base 'Test::Deep::HashKeysOnly'; |
|---|
| 101 | |
|---|
| 102 | sub ignoreMissing |
|---|
| 103 | { |
|---|
| 104 | return 0; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | sub ignoreExtra |
|---|
| 108 | { |
|---|
| 109 | return 1; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | package Test::Deep::SubHashKeysOnly; |
|---|
| 113 | |
|---|
| 114 | use base 'Test::Deep::HashKeysOnly'; |
|---|
| 115 | |
|---|
| 116 | sub ignoreMissing |
|---|
| 117 | { |
|---|
| 118 | return 1; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | sub ignoreExtra |
|---|
| 122 | { |
|---|
| 123 | return 0; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | 1; |
|---|