8 use Test::More tests => 34;
10 use_ok( 'ExtUtils::Packlist' );
12 is( ref(ExtUtils::Packlist::mkfh()), 'GLOB', 'mkfh() should return a FH' );
15 my $pl = ExtUtils::Packlist->new();
16 isa_ok( $pl, 'ExtUtils::Packlist' );
17 is( ref tied %$pl, 'ExtUtils::Packlist', 'obj should be tied underneath' );
20 $pl = ExtUtils::Packlist::TIEHASH( 'tieclass', 'packfile' );
21 is( ref($pl), 'tieclass', 'TIEHASH() should bless into class' );
22 is( $pl->{packfile}, 'packfile', 'TIEHASH() should store packfile name' );
25 ExtUtils::Packlist::STORE($pl, 'key', 'value');
26 is( $pl->{data}{key}, 'value', 'STORE() should stuff stuff in data member' );
29 $pl->{data}{foo} = 'bar';
30 is( ExtUtils::Packlist::FETCH($pl, 'foo'), 'bar', 'check FETCH()' );
33 # test FIRSTKEY and NEXTKEY
35 $pl->{data}{bar} = 'baz';
36 skip('not enough keys to test FIRSTKEY', 2) unless %{ $pl->{data} } > 2;
38 # get the first and second key
39 my ($first, $second) = keys %{ $pl->{data} };
41 # now get a couple of extra keys, to mess with the hash iterator
43 for (keys %{ $pl->{data} } ) {
47 # finally, see if it really can get the first key again
48 is( ExtUtils::Packlist::FIRSTKEY($pl), $first,
49 'FIRSTKEY() should be consistent' );
51 is( ExtUtils::Packlist::NEXTKEY($pl), $second,
52 'and NEXTKEY() should also be consistent' );
56 ok( ExtUtils::Packlist::EXISTS($pl, 'bar'), 'EXISTS() should find keys' );
59 ExtUtils::Packlist::DELETE($pl, 'bar');
60 ok( !(exists $pl->{data}{bar}), 'DELETE() should delete cleanly' );
63 ExtUtils::Packlist::CLEAR($pl);
64 is( keys %{ $pl->{data} }, 0, 'CLEAR() should wipe out data' );
67 # DESTROY does nothing...
68 can_ok( 'ExtUtils::Packlist', 'DESTROY' );
71 # write is a little more complicated
72 eval { ExtUtils::Packlist::write({}) };
73 like( $@, qr/No packlist filename/, 'write() should croak without packfile' );
75 eval { ExtUtils::Packlist::write({}, 'eplist') };
76 my $file_is_ready = $@ ? 0 : 1;
77 ok( $file_is_ready, 'write() can write a file' );
82 skip('cannot write files, some tests difficult', 3) unless $file_is_ready;
84 # set this file to read-only
88 skip("can write readonly files", 1) if -w 'eplist';
90 eval { ExtUtils::Packlist::write({}, 'eplist') };
91 like( $@, qr/Can't open file/, 'write() should croak on open failure' );
94 #'now set it back (tick here fixes vim syntax highlighting ;)
97 # and some test data to be read
106 eval { ExtUtils::Packlist::write($pl, 'eplist') };
107 is( $@, '', 'write() should normally succeed' );
108 is( $pl->{packfile}, 'eplist', 'write() should set packfile name' );
110 $file_is_ready = open(IN, 'eplist');
114 eval { ExtUtils::Packlist::read({}) };
115 like( $@, qr/^No packlist filename/, 'read() should croak without packfile' );
118 eval { ExtUtils::Packlist::read({}, 'abadfilename') };
119 like( $@, qr/^Can't open file/, 'read() should croak with bad packfile name' );
120 #'open packfile for reading
123 # and more read() tests
125 skip("cannot open file for reading: $!", 5) unless $file_is_ready;
126 my $file = do { local $/ = <IN> };
128 like( $file, qr/single\n/, 'key with value should be available' );
129 like( $file, qr!/\./abc\n!, 'key with no value should also be present' );
130 like( $file, qr/hash.+baz=bup/, 'key with hash value should be present' );
131 like( $file, qr/hash.+foo=bar/, 'second embedded hash value should appear');
134 eval{ ExtUtils::Packlist::read($pl, 'eplist') };
135 is( $@, '', 'read() should normally succeed' );
136 is( $pl->{data}{single}, undef, 'single keys should have undef value' );
137 is( ref($pl->{data}{hash}), 'HASH', 'multivalue keys should become hashes');
139 is( $pl->{data}{hash}{foo}, 'bar', 'hash values should be set' );
140 ok( exists $pl->{data}{'/abc'}, 'read() should resolve /./ to / in keys' );
142 # give validate a valid and an invalid file to find
148 is( ExtUtils::Packlist::validate($pl), 1,
149 'validate() should find missing files' );
150 ExtUtils::Packlist::validate($pl, 1);
151 ok( !exists $pl->{data}{fake},
152 'validate() should remove missing files when prompted' );
154 # one more new() test, to see if it calls read() successfully
155 $pl = ExtUtils::Packlist->new('eplist');
159 # packlist_file, $pl should be set from write test
160 is( ExtUtils::Packlist::packlist_file({ packfile => 'pl' }), 'pl',
161 'packlist_file() should fetch packlist from passed hash' );
162 is( ExtUtils::Packlist::packlist_file($pl), 'eplist',
163 'packlist_file() should fetch packlist from ExtUtils::Packlist object' );
166 1 while unlink qw( eplist );