4 if( $ENV{PERL_CORE} ) {
11 use Test::More tests => 34;
13 use_ok( 'ExtUtils::Packlist' );
15 is( ref(ExtUtils::Packlist::mkfh()), 'GLOB', 'mkfh() should return a FH' );
18 my $pl = ExtUtils::Packlist->new();
19 isa_ok( $pl, 'ExtUtils::Packlist' );
20 is( ref tied %$pl, 'ExtUtils::Packlist', 'obj should be tied underneath' );
23 $pl = ExtUtils::Packlist::TIEHASH( 'tieclass', 'packfile' );
24 is( ref($pl), 'tieclass', 'TIEHASH() should bless into class' );
25 is( $pl->{packfile}, 'packfile', 'TIEHASH() should store packfile name' );
28 ExtUtils::Packlist::STORE($pl, 'key', 'value');
29 is( $pl->{data}{key}, 'value', 'STORE() should stuff stuff in data member' );
32 $pl->{data}{foo} = 'bar';
33 is( ExtUtils::Packlist::FETCH($pl, 'foo'), 'bar', 'check FETCH()' );
36 # test FIRSTKEY and NEXTKEY
38 $pl->{data}{bar} = 'baz';
39 skip('not enough keys to test FIRSTKEY', 2)
40 unless keys %{ $pl->{data} } > 2;
42 # get the first and second key
43 my ($first, $second) = keys %{ $pl->{data} };
45 # now get a couple of extra keys, to mess with the hash iterator
47 for (keys %{ $pl->{data} } ) {
51 # finally, see if it really can get the first key again
52 is( ExtUtils::Packlist::FIRSTKEY($pl), $first,
53 'FIRSTKEY() should be consistent' );
55 is( ExtUtils::Packlist::NEXTKEY($pl), $second,
56 'and NEXTKEY() should also be consistent' );
60 ok( ExtUtils::Packlist::EXISTS($pl, 'bar'), 'EXISTS() should find keys' );
63 ExtUtils::Packlist::DELETE($pl, 'bar');
64 ok( !(exists $pl->{data}{bar}), 'DELETE() should delete cleanly' );
67 ExtUtils::Packlist::CLEAR($pl);
68 is( keys %{ $pl->{data} }, 0, 'CLEAR() should wipe out data' );
71 # DESTROY does nothing...
72 can_ok( 'ExtUtils::Packlist', 'DESTROY' );
75 # write is a little more complicated
76 eval { ExtUtils::Packlist::write({}) };
77 like( $@, qr/No packlist filename/, 'write() should croak without packfile' );
79 eval { ExtUtils::Packlist::write({}, 'eplist') };
80 my $file_is_ready = $@ ? 0 : 1;
81 ok( $file_is_ready, 'write() can write a file' );
86 skip('cannot write files, some tests difficult', 3) unless $file_is_ready;
88 # set this file to read-only
92 skip("can write readonly files", 1) if -w 'eplist';
94 eval { ExtUtils::Packlist::write({}, 'eplist') };
95 like( $@, qr/Can't open file/, 'write() should croak on open failure' );
98 #'now set it back (tick here fixes vim syntax highlighting ;)
101 # and some test data to be read
110 eval { ExtUtils::Packlist::write($pl, 'eplist') };
111 is( $@, '', 'write() should normally succeed' );
112 is( $pl->{packfile}, 'eplist', 'write() should set packfile name' );
114 $file_is_ready = open(IN, 'eplist');
118 eval { ExtUtils::Packlist::read({}) };
119 like( $@, qr/^No packlist filename/, 'read() should croak without packfile' );
122 eval { ExtUtils::Packlist::read({}, 'abadfilename') };
123 like( $@, qr/^Can't open file/, 'read() should croak with bad packfile name' );
124 #'open packfile for reading
127 # and more read() tests
129 skip("cannot open file for reading: $!", 5) unless $file_is_ready;
130 my $file = do { local $/ = <IN> };
132 like( $file, qr/single\n/, 'key with value should be available' );
133 like( $file, qr!/\./abc\n!, 'key with no value should also be present' );
134 like( $file, qr/hash.+baz=bup/, 'key with hash value should be present' );
135 like( $file, qr/hash.+foo=bar/, 'second embedded hash value should appear');
138 eval{ ExtUtils::Packlist::read($pl, 'eplist') };
139 is( $@, '', 'read() should normally succeed' );
140 is( $pl->{data}{single}, undef, 'single keys should have undef value' );
141 is( ref($pl->{data}{hash}), 'HASH', 'multivalue keys should become hashes');
143 is( $pl->{data}{hash}{foo}, 'bar', 'hash values should be set' );
144 ok( exists $pl->{data}{'/abc'}, 'read() should resolve /./ to / in keys' );
146 # give validate a valid and an invalid file to find
152 is( ExtUtils::Packlist::validate($pl), 1,
153 'validate() should find missing files' );
154 ExtUtils::Packlist::validate($pl, 1);
155 ok( !exists $pl->{data}{fake},
156 'validate() should remove missing files when prompted' );
158 # one more new() test, to see if it calls read() successfully
159 $pl = ExtUtils::Packlist->new('eplist');
163 # packlist_file, $pl should be set from write test
164 is( ExtUtils::Packlist::packlist_file({ packfile => 'pl' }), 'pl',
165 'packlist_file() should fetch packlist from passed hash' );
166 is( ExtUtils::Packlist::packlist_file($pl), 'eplist',
167 'packlist_file() should fetch packlist from ExtUtils::Packlist object' );
170 1 while unlink qw( eplist );