~ubuntu-branches/ubuntu/trusty/libperl5i-perl/trusty-proposed

« back to all changes in this revision

Viewing changes to t/Meta/checksum.t

  • Committer: Bazaar Package Importer
  • Author(s): Ivan Kohler
  • Date: 2010-05-08 17:42:00 UTC
  • Revision ID: james.westby@ubuntu.com-20100508174200-7ogg0zrimh9gvcuw
Tags: upstream-2.1.1
ImportĀ upstreamĀ versionĀ 2.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env perl
 
2
 
 
3
use perl5i::latest;
 
4
 
 
5
use lib 't/lib';
 
6
use Test::More;
 
7
use Test::perl5i;
 
8
 
 
9
use Scalar::Util 'refaddr';
 
10
 
 
11
 
 
12
my $foo  = bless {}, 'Foo';
 
13
my $foo2 = bless {}, 'Foo';
 
14
my $bar  = bless {}, 'Bar';
 
15
 
 
16
ok $foo->mo->checksum;
 
17
 
 
18
is $foo->mo->checksum,   $foo2->mo->checksum;
 
19
isnt $foo->mo->checksum, $bar->mo->checksum;
 
20
 
 
21
throws_ok { $foo->mo->checksum( algorithm => 'foo' ) } qr/^algorithm must be/;
 
22
throws_ok { $foo->mo->checksum( format    => 'foo' ) } qr/^format must be/;
 
23
 
 
24
my %digests;
 
25
 
 
26
my @formats = qw(hex base64 binary);
 
27
my @algos   = qw(md5 sha1);
 
28
my @refs    = ( $foo, $foo2, $bar );
 
29
 
 
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 );
 
36
        }
 
37
    }
 
38
}
 
39
 
 
40
# All checksums of equivalent objects should be identical
 
41
is_deeply( [ $digests{ refaddr $foo} ], [ $digests{ refaddr $foo2 } ] );
 
42
 
 
43
my %length = (
 
44
    binary => { md5 => 16, sha1 => 20 },
 
45
    base64 => { md5 => 22, sha1 => 27 },
 
46
    hex    => { md5 => 32, sha1 => 40 },
 
47
);
 
48
 
 
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} };
 
54
    }
 
55
}
 
56
 
 
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 )];
 
61
 
 
62
ok $ref->mo->checksum;
 
63
is $ref->mo->checksum,   $ref2->mo->checksum;
 
64
isnt $ref->mo->checksum, $ref3->mo->checksum;
 
65
 
 
66
ok 42->mo->checksum;
 
67
isnt 42->mo->checksum, "foo"->mo->checksum;
 
68
 
 
69
done_testing();