9
use Scalar::Util 'refaddr';
12
my $foo = bless {}, 'Foo';
13
my $foo2 = bless {}, 'Foo';
14
my $bar = bless {}, 'Bar';
16
ok $foo->mo->checksum;
18
is $foo->mo->checksum, $foo2->mo->checksum;
19
isnt $foo->mo->checksum, $bar->mo->checksum;
21
throws_ok { $foo->mo->checksum( algorithm => 'foo' ) } qr/^algorithm must be/;
22
throws_ok { $foo->mo->checksum( format => 'foo' ) } qr/^format must be/;
26
my @formats = qw(hex base64 binary);
27
my @algos = qw(md5 sha1);
28
my @refs = ( $foo, $foo2, $bar );
30
foreach my $algorithm (@algos) {
31
foreach my $format (@formats) {
32
foreach my $ref (@refs) {
33
$digests{ refaddr $ref }{$format}{$algorithm}
34
= $digests{$format}{$algorithm}{ refaddr $ref }
35
= $ref->mo->checksum( algorithm => $algorithm, format => $format );
40
# All checksums of equivalent objects should be identical
41
is_deeply( [ $digests{ refaddr $foo} ], [ $digests{ refaddr $foo2 } ] );
44
binary => { md5 => 16, sha1 => 20 },
45
base64 => { md5 => 22, sha1 => 27 },
46
hex => { md5 => 32, sha1 => 40 },
49
# Checksums should have the expected character length
50
# (this is mostly to test we're passing the options correctly)
51
foreach my $algorithm (@algos) {
52
foreach my $format (@formats) {
53
is( length $_, $length{$format}{$algorithm} ) for values %{ $digests{$format}{$algorithm} };
57
# Method should work fine with non-blessed references and scalars
58
my $ref = { this => "is some reference" };
59
my $ref2 = { this => "is some reference" };
60
my $ref3 = [qw( this is some reference )];
62
ok $ref->mo->checksum;
63
is $ref->mo->checksum, $ref2->mo->checksum;
64
isnt $ref->mo->checksum, $ref3->mo->checksum;
67
isnt 42->mo->checksum, "foo"->mo->checksum;