From: Vincent Pit Date: Tue, 5 Aug 2008 12:22:14 +0000 (+0200) Subject: Add support and tests for the get{pw,gr,host,net,proto,serv}* functions. Move ops... X-Git-Tag: v0.02~6 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=8a8dfb1c7d8ea124bf65333fa975667185b4cd73;p=perl%2Fmodules%2FSub-Nary.git Add support and tests for the get{pw,gr,host,net,proto,serv}* functions. Move ops tests in a new t/24-ops.t file --- diff --git a/MANIFEST b/MANIFEST index d09cac0..97be8af 100644 --- a/MANIFEST +++ b/MANIFEST @@ -14,6 +14,7 @@ t/20-return.t t/21-list.t t/22-call.t t/23-branch.t +t/24-ops.t t/90-boilerplate.t t/91-pod.t t/92-pod-coverage.t diff --git a/lib/Sub/Nary.pm b/lib/Sub/Nary.pm index eb26dfc..5c26315 100644 --- a/lib/Sub/Nary.pm +++ b/lib/Sub/Nary.pm @@ -198,18 +198,28 @@ sub add { } my %ops; + $ops{$_} = 1 for scalops; $ops{$_} = 0 for qw/stub nextstate/; $ops{$_} = 1 for qw/padsv/; $ops{$_} = 'list' for qw/padav/; $ops{$_} = 'list' for qw/padhv rv2hv/; $ops{$_} = 'list' for qw/padany flip match entereval readline/; + $ops{each} = { 0 => 0.5, 2 => 0.5 }; $ops{stat} = { 0 => 0.5, 13 => 0.5 }; + $ops{caller} = sub { my @a = caller 0; scalar @a }->(); $ops{localtime} = do { my @a = localtime; scalar @a }; $ops{gmtime} = do { my @a = gmtime; scalar @a }; +$ops{$_} = { 0 => 0.5, 10 => 0.5 } for map "gpw$_", qw/nam uid ent/; +$ops{$_} = { 0 => 0.5, 4 => 0.5 } for map "ggr$_", qw/nam gid ent/; +$ops{$_} = 'list' for qw/ghbyname ghbyaddr ghostent/; +$ops{$_} = { 0 => 0.5, 4 => 0.5 } for qw/gnbyname gnbyaddr gnetent/; +$ops{$_} = { 0 => 0.5, 3 => 0.5 } for qw/gpbyname gpbynumber gprotoent/; +$ops{$_} = { 0 => 0.5, 4 => 0.5 } for qw/gsbyname gsbyport gservent/; + sub enter { my ($self, $cv) = @_; diff --git a/t/20-return.t b/t/20-return.t index 98fecc9..e7e852d 100644 --- a/t/20-return.t +++ b/t/20-return.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 55; +use Test::More tests => 50; use Sub::Nary; @@ -72,19 +72,12 @@ my @tests = ( [ sub { return eval { do { eval { @a } } } }, 'list' ], [ sub { eval 'return 1, 2' }, 'list' ], - - [ sub { return <$x> }, 'list' ], - - [ sub { return -f $0, -r $0 }, 2 ], - - [ sub { return caller 0 }, sub { my @a = caller 0; scalar @a }->() ], - [ sub { return localtime }, do { my @a = localtime; scalar @a } ], - [ sub { return gmtime }, do { my @a = gmtime; scalar @a } ], ); my $i = 1; for (@tests) { my $r = $sn->nary($_->[0]); - is_deeply($r, { $_->[1] => 1 }, 'return test ' . $i); + my $exp = ref $_->[1] ? $_->[1] : { $_->[1] => 1 }; + is_deeply($r, $exp, 'return test ' . $i); ++$i; } diff --git a/t/21-list.t b/t/21-list.t index 62c9bc0..6de15c2 100644 --- a/t/21-list.t +++ b/t/21-list.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 41; +use Test::More tests => 36; use Sub::Nary; @@ -60,19 +60,12 @@ my @tests = ( [ sub { eval { 1, do { eval { @a }, 2 } } }, 'list' ], [ sub { eval '1, 2' }, 'list' ], - - [ sub { <$x> }, 'list' ], - - [ sub { -f $0, -r $0 }, 2 ], - - [ sub { caller 0 }, sub { my @a = caller 0; scalar @a }->() ], - [ sub { localtime }, do { my @a = localtime; scalar @a } ], - [ sub { gmtime }, do { my @a = gmtime; scalar @a } ], ); my $i = 1; for (@tests) { my $r = $sn->nary($_->[0]); - is_deeply($r, { $_->[1] => 1 }, 'list test ' . $i); + my $exp = ref $_->[1] ? $_->[1] : { $_->[1] => 1 }; + is_deeply($r, $exp, 'list test ' . $i); ++$i; } diff --git a/t/22-call.t b/t/22-call.t index fdd3812..de288a6 100644 --- a/t/22-call.t +++ b/t/22-call.t @@ -80,6 +80,7 @@ my @tests = ( my $i = 1; for (@tests) { my $r = $sn->nary($_->[0]); - is_deeply($r, { $_->[1] => 1 }, 'call test ' . $i); + my $exp = ref $_->[1] ? $_->[1] : { $_->[1] => 1 }; + is_deeply($r, $exp, 'call test ' . $i); ++$i; } diff --git a/t/23-branch.t b/t/23-branch.t index 72451e4..8bffe50 100644 --- a/t/23-branch.t +++ b/t/23-branch.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 17; +use Test::More tests => 14; use Sub::Nary; @@ -59,17 +59,13 @@ my @tests = ( [ \&ret1234, { map { $_ => 0.25 } 1 .. 4 } ], [ \&retinif, { 2 => 1 } ], - - [ sub { each %h }, { 0 => 0.5, 2 => 0.5 } ], - [ sub { stat $0 }, { 0 => 0.5, 13 => 0.5 } ], - - [ sub { <*.*> }, { list => 1 / 3, 1 => 2 / 3 } ], ); my $i = 1; for (@tests) { my $r = $sn->nary($_->[0]); - is_deeply($r, $_->[1], 'branch test ' . $i); + my $exp = ref $_->[1] ? $_->[1] : { $_->[1] => 1 }; + is_deeply($r, $exp, 'branch test ' . $i); ++$i; } diff --git a/t/24-ops.t b/t/24-ops.t new file mode 100644 index 0000000..a74fa43 --- /dev/null +++ b/t/24-ops.t @@ -0,0 +1,54 @@ +#!perl -T + +use strict; +use warnings; + +use Test::More tests => 26; + +use Sub::Nary; + +my $sn = Sub::Nary->new(); + +my ($x, %h); + +my @tests = ( + [ sub { return <$x> }, 'list' ], + + [ sub { -f $0, -r $0 }, 2 ], + + [ sub { return caller 0 }, sub { my @a = caller 0; scalar @a }->() ], + [ sub { return localtime }, do { my @a = localtime; scalar @a } ], + [ sub { gmtime }, do { my @a = gmtime; scalar @a } ], + + [ sub { each %h }, { 0 => 0.5, 2 => 0.5 } ], + [ sub { stat $0 }, { 0 => 0.5, 13 => 0.5 } ], + + [ sub { do { getpwnam 'root' } }, { 0 => 0.5, 10 => 0.5 } ], + [ sub { 1; getpwuid '0' }, { 0 => 0.5, 10 => 0.5 } ], + [ sub { eval { return getpwent } }, { 0 => 0.5, 10 => 0.5 } ], + [ sub { do { getgrnam 'root' } }, { 0 => 0.5, 4 => 0.5 } ], + [ sub { 1; getgrgid '0' }, { 0 => 0.5, 4 => 0.5 } ], + [ sub { eval { return getgrent } }, { 0 => 0.5, 4 => 0.5 } ], + [ sub { do { gethostbyname 'localhost' } }, 'list' ], + [ sub { 1; gethostbyaddr '', '' }, 'list' ], + [ sub { eval { return gethostent } }, 'list' ], + [ sub { do { getnetbyname '' } }, { 0 => 0.5, 4 => 0.5 } ], + [ sub { 1; getnetbyaddr '', '' }, { 0 => 0.5, 4 => 0.5 } ], + [ sub { eval { return getnetent } }, { 0 => 0.5, 4 => 0.5 } ], + [ sub { do { getprotobyname 'tcp' } }, { 0 => 0.5, 3 => 0.5 } ], + [ sub { 1; getprotobynumber 6 }, { 0 => 0.5, 3 => 0.5 } ], + [ sub { eval { return getprotoent } }, { 0 => 0.5, 3 => 0.5 } ], + [ sub { do { getservbyname 'ssh', 'tcp' } }, { 0 => 0.5, 4 => 0.5 } ], + [ sub { 1; getservbyport 22, 'tcp' }, { 0 => 0.5, 4 => 0.5 } ], + [ sub { eval { return getservent } }, { 0 => 0.5, 4 => 0.5 } ], + + [ sub { <*.*> }, { list => 1 / 3, 1 => 2 / 3 } ], +); + +my $i = 1; +for (@tests) { + my $r = $sn->nary($_->[0]); + my $exp = ref $_->[1] ? $_->[1] : { $_->[1] => 1 }; + is_deeply($r, $exp, 'ops test ' . $i); + ++$i; +}