diff options
author | Gurusamy Sarathy <gsar@cpan.org> | 2000-02-27 21:57:40 +0000 |
---|---|---|
committer | Gurusamy Sarathy <gsar@cpan.org> | 2000-02-27 21:57:40 +0000 |
commit | 479ba38336aaacaf3a7e00a4662d83e2dc833197 (patch) | |
tree | 3080fad85b071cb55633ac23717fd5cf28bd64e0 /t | |
parent | 741d59ba12a3c356a2e1dfd4bcdc0396b843a43e (diff) | |
download | perl-479ba38336aaacaf3a7e00a4662d83e2dc833197.tar.gz |
support fields::new() and fields::phash() to create pseudo-hash
objects and plain pseudo-hashes respectively (this avoids users
from having to diddle %FIELDS directly); update documentation to
suit (from original fields::phash() implementation by Peter
Scott <Peter@PSDT.com>)
p4raw-id: //depot/perl@5293
Diffstat (limited to 't')
-rwxr-xr-x | t/lib/fields.t | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/t/lib/fields.t b/t/lib/fields.t index 01f93892b0..310967fcbe 100755 --- a/t/lib/fields.t +++ b/t/lib/fields.t @@ -66,8 +66,7 @@ use fields qw(b1 d1 _b1 _d1); # hide b1 package main; -sub fstr -{ +sub fstr { my $h = shift; my @tmp; for my $k (sort {$h->{$a} <=> $h->{$b}} keys %$h) { @@ -90,7 +89,7 @@ my %expect = ( 'Foo::Bar::Baz' => 'b1:1,b2:2,b3:3,foo:4,bar:5,baz:6', ); -print "1..", int(keys %expect)+7, "\n"; +print "1..", int(keys %expect)+13, "\n"; my $testno = 0; while (my($class, $exp) = each %expect) { no strict 'refs'; @@ -125,9 +124,25 @@ print "ok ", ++$testno, "\n"; print "not " unless "@$obj1{'b1','_b1','b1'}" eq "28 44 28"; print "ok ", ++$testno, "\n"; +my $ph = fields::phash(a => 1, b => 2, c => 3); +print "not " unless fstr($ph) eq 'a:1,b:2,c:3'; +print "ok ", ++$testno, "\n"; + +$ph = fields::phash([qw/a b c/], [1, 2, 3]); +print "not " unless fstr($ph) eq 'a:1,b:2,c:3'; +print "ok ", ++$testno, "\n"; + +$ph = fields::phash([qw/a b c/], [1]); +print "not " if exists $ph->{b} or exists $ph->{c} or !exists $ph->{a}; +print "ok ", ++$testno, "\n"; + +eval '$ph = fields::phash("odd")'; +print "not " unless $@ && $@ =~ /^Odd number of/; +print "ok ", ++$testno, "\n"; + #fields::_dump(); -# check if +# check if fields autovivify { package Foo; use fields qw(foo bar); @@ -140,3 +155,17 @@ print "ok ", ++$testno, "\n"; print $a->{foo}[1], "\n"; print $a->{bar}->{A}, "\n"; } + +# check if fields autovivify +{ + package Bar; + use fields qw(foo bar); + sub new { return fields::new($_[0]) } + + package main; + my Bar $a = Bar::->new(); + $a->{foo} = ['a', 'ok ' . ++$testno, 'c']; + $a->{bar} = { A => 'ok ' . ++$testno }; + print $a->{foo}[1], "\n"; + print $a->{bar}->{A}, "\n"; +} |