From 674d0cd93bd328db0075c660030401d94c10c1e0 Mon Sep 17 00:00:00 2001 From: Nicholas Clark Date: Tue, 6 Sep 2011 13:36:07 +0200 Subject: [PATCH] Refactor ext/POSIX/t/termios.t * Only import termios.h functions and constants from POSIX * Loop over STDIN, STDOUT, STDERR instead of duplicating code. * Avoid a needless defined? test, as isa_ok() handles undef. * Switch to done_testing(), which also allows @getters to be inlined and eliminated. * The various get*() methods return integer values, so check this. * Enable warnings. --- ext/POSIX/t/termios.t | 68 +++++++++++++++++++-------------------------------- 1 file changed, 25 insertions(+), 43 deletions(-) diff --git a/ext/POSIX/t/termios.t b/ext/POSIX/t/termios.t index 7c3deb6..9e00127 100644 --- a/ext/POSIX/t/termios.t +++ b/ext/POSIX/t/termios.t @@ -1,66 +1,48 @@ -#!perl -T +#!perl -Tw -BEGIN { - use Config; - use Test::More; - plan skip_all => "POSIX is unavailable" - if $Config{'extensions'} !~ m!\bPOSIX\b!; -} use strict; -use POSIX; +use Config; +use Test::More; + BEGIN { - plan skip_all => "POSIX::Termios not implemented" - if !eval "POSIX::Termios->new;1" - and $@=~/not implemented/; + plan skip_all => "POSIX is unavailable" + if $Config{extensions} !~ m!\bPOSIX\b!; } +use POSIX ':termios_h'; -my @getters = qw(getcflag getiflag getispeed getlflag getoflag getospeed); +plan skip_all => $@ + if !eval "POSIX::Termios->new; 1" && $@ =~ /termios not implemented/; -plan tests => 3 + 2 * (3 + NCCS() + @getters); - -my $r; # create a new object my $termios = eval { POSIX::Termios->new }; is( $@, '', "calling POSIX::Termios->new" ); -ok( defined $termios, "\tchecking if the object is defined" ); isa_ok( $termios, "POSIX::Termios", "\tchecking the type of the object" ); # testing getattr() - -SKIP: { - -t STDIN or skip("STDIN not a tty", 2); - $r = eval { $termios->getattr(0) }; - is( $@, '', "calling getattr(0)" ); - ok( defined $r, "\tchecking if the returned value is defined: $r" ); -} - -SKIP: { - -t STDOUT or skip("STDOUT not a tty", 2); - $r = eval { $termios->getattr(1) }; - is( $@, '', "calling getattr(1)" ); - ok( defined $r, "\tchecking if the returned value is defined: $r" ); -} - -SKIP: { - -t STDERR or skip("STDERR not a tty", 2); - $r = eval { $termios->getattr(2) }; - is( $@, '', "calling getattr(2)" ); - ok( defined $r, "\tchecking if the returned value is defined: $r" ); +foreach my $name (qw(STDIN STDOUT STDERR)) { + my $handle = $::{$name}; + SKIP: { + skip("$name not a tty", 2) unless -t $handle; + my $fileno = fileno $handle; + my $r = eval { $termios->getattr($fileno) }; + is($@, '', "calling getattr($fileno) for $name"); + isnt($r, undef, "returned value ($r) is defined"); + } } # testing getcc() -for my $i (0..NCCS()-1) { - $r = eval { $termios->getcc($i) }; +for my $i (0..NCCS-1) { + my $r = eval { $termios->getcc($i) }; is( $@, '', "calling getcc($i)" ); - ok( defined $r, "\tchecking if the returned value is defined: $r" ); + like($r, qr/\A-?[0-9]+\z/, 'returns an integer'); } -# testing getcflag() -for my $method (@getters) { - $r = eval { $termios->$method() }; +for my $method (qw(getcflag getiflag getispeed getlflag getoflag getospeed)) { + my $r = eval { $termios->$method() }; is( $@, '', "calling $method()" ); - ok( defined $r, "\tchecking if the returned value is defined: $r" ); + like($r, qr/\A-?[0-9]+\z/, 'returns an integer'); } +done_testing(); -- 1.8.3.1