From 834c61514d3dc3eb32aa31f14431c851a8cc62d3 Mon Sep 17 00:00:00 2001 From: Gabor Szabo Date: Wed, 30 Nov 2011 08:22:41 +0200 Subject: [PATCH] cleanup of the test code --- t/compat.t | 1 - t/eta-linear.t | 41 ++++++++++----------- t/name.t | 97 +++++++++++++++++++++++--------------------------- t/v1-message.t | 34 ++++++++---------- t/v2-message.t | 42 +++++++++++----------- t/v2-mobile.t | 12 +++---- t/v2-simple.t | 50 +++++++++++++------------- t/zero.t | 39 ++++++++++---------- 8 files changed, 151 insertions(+), 165 deletions(-) diff --git a/t/compat.t b/t/compat.t index 8f71b18..da8c7d7 100644 --- a/t/compat.t +++ b/t/compat.t @@ -12,7 +12,6 @@ and is intended to test compatibility with that version. # Utility ----------------------------- -use Data::Dumper qw( ); use Test::More tests => 9; use Term::ProgressBar; diff --git a/t/eta-linear.t b/t/eta-linear.t index 8c07d2d..3d4f06b 100644 --- a/t/eta-linear.t +++ b/t/eta-linear.t @@ -1,6 +1,7 @@ # (X)Emacs mode: -*- cperl -*- use strict; +use warnings; =head1 Unit Test Package for Term::ProgressBar @@ -8,16 +9,10 @@ This package tests the basic functionality of Term::ProgressBar. =cut -use Data::Dumper qw( Dumper ); use Test::More tests => 9; use Test::Exception; -=head2 Test 1: compilation - -This test confirms that the test script and the modules it calls compiled -successfully. - -=cut +use Capture::Tiny qw(capture); use Term::ProgressBar; @@ -42,28 +37,28 @@ Update it it from 1 to 10. =cut -use Capture::Tiny qw(capture); - -my ($out, $err) = capture { - my $p; - lives_ok { +{ + my ($out, $err) = capture { + my $p; + lives_ok { $p = Term::ProgressBar->new({count => 10, name => 'fred', ETA => 'linear'}); } 'Count 1-10 (1)'; - lives_ok { for (1..5) { $p->update($_); sleep 1 } } + lives_ok { for (1..5) { $p->update($_); sleep 1 } } 'Count 1-10 (2)'; - lives_ok { $p->message('Hello Mum!') } 'Count 1-10 (3)'; - lives_ok { for (6..10) { $p->update($_); sleep 1 } } 'Count 1-10 (4)'; -}; -print $out; + lives_ok { $p->message('Hello Mum!') } 'Count 1-10 (3)'; + lives_ok { for (6..10) { $p->update($_); sleep 1 } } 'Count 1-10 (4)'; + }; + print $out; + my @lines = grep $_ ne '', split /[\n\r]+/, $err; - print Dumper \@lines + diag explain \@lines if $ENV{TEST_DEBUG}; ok grep $_ eq 'Hello Mum!', @lines; - like $lines[-1], qr/\[=+\]/, 'Count 1-10 (6)'; - like $lines[-1], qr/^fred: \s*100%/, 'Count 1-10 (7)'; - like $lines[-1], qr/D[ \d]\dh\d{2}m\d{2}s$/, 'Count 1-10 (8)'; - like $lines[-2], qr/ Left$/, 'Count 1-10 (9)'; - + like $lines[-1], qr/\[=+\]/, 'Count 1-10 (6)'; + like $lines[-1], qr/^fred: \s*100%/, 'Count 1-10 (7)'; + like $lines[-1], qr/D[ \d]\dh\d{2}m\d{2}s$/, 'Count 1-10 (8)'; + like $lines[-2], qr/ Left$/, 'Count 1-10 (9)'; +} # ---------------------------------------------------------------------------- diff --git a/t/name.t b/t/name.t index ff4dfbf..e89f486 100644 --- a/t/name.t +++ b/t/name.t @@ -1,6 +1,7 @@ # (X)Emacs mode: -*- cperl -*- use strict; +use warnings; =head1 Unit Test Package for Term::ProgressBar @@ -8,21 +9,14 @@ This package tests the name functionality of Term::ProgressBar. =cut -use Data::Dumper qw( Dumper ); -use Test::More tests => 18; +use Test::More tests => 20; use Test::Exception; -use constant MESSAGE1 => 'The Gospel of St. Jude'; -use constant NAME1 => 'Algenon'; -use constant NAME2 => 'Smegma'; - - -=head2 Test 1: compilation - -This test confirms that the test script and the modules it calls compiled -successfully. +use Capture::Tiny qw(capture); -=cut +my $MESSAGE1 = 'The Gospel of St. Jude'; +my $NAME1 = 'Algenon'; +my $NAME2 = 'Smegma'; use_ok 'Term::ProgressBar'; @@ -47,49 +41,47 @@ Update it it from 1 to 10. =cut -use Capture::Tiny qw(capture); - { my $p; -my ($out, $err) = capture { - lives_ok { - $p = Term::ProgressBar->new({count => 10, name => NAME1}); + my ($out, $err) = capture { + lives_ok { + $p = Term::ProgressBar->new({count => 10, name => $NAME1}); } 'Count 1-10 ( 1)'; - lives_ok { $p->update($_) for 1..3 } 'Count 1-10 ( 2)'; -}; -print $out; + lives_ok { $p->update($_) for 1..3 } 'Count 1-10 ( 2)'; + }; + print $out; $err =~ s!^.*\r!!gm; - print STDERR "ERR (1) :\n$err\nlength: ", length($err), "\n" + diag "ERR (1) :\n$err\nlength: " . length($err) if $ENV{TEST_DEBUG}; my @lines = split /\n/, $err; - like $lines[-1], qr/^@{[NAME1()]}: \s*\b30%/, 'Count 1-10 ( 3)'; + like $lines[-1], qr/^@{[$NAME1]}: \s*\b30%/, 'Count 1-10 ( 3)'; my ($bar, $space) = $lines[-1] =~ /\[(=*)(\s*)\]/; my $length = length($bar) + length($space); print STDERR ("LENGTHS (1) :BAR:", length($bar), ":SPACE:", length($space), "\n") if $ENV{TEST_DEBUG}; my $barexpect = $length * 0.3; - my $ok = length($bar) > $barexpect -1 && length($bar) < $barexpect+1; - ok $ok; + cmp_ok length($bar), '>', $barexpect -1; + cmp_ok length($bar), '<', $barexpect+1; -($out, $err) = capture { - lives_ok { $p->message(MESSAGE1) } 'Count 1-10 ( 5)'; - lives_ok { $p->update($_) for 6..10 } 'Count 1-10 ( 6)'; -}; -print $out; + ($out, $err) = capture { + lives_ok { $p->message($MESSAGE1) } 'Count 1-10 ( 5)'; + lives_ok { $p->update($_) for 6..10 } 'Count 1-10 ( 6)'; + }; + print $out; $err =~ s!^.*\r!!gm; - print STDERR "ERR (2) :\n$err\nlength: ", length($err), "\n" + diag "ERR (2) :\n$err\nlength: " . length($err) if $ENV{TEST_DEBUG}; @lines = split /\n/, $err; - is $lines[0], MESSAGE1, 'Count 1-10 ( 7)'; + is $lines[0], $MESSAGE1, 'Count 1-10 ( 7)'; like $lines[-1], qr/\[=+\]/, 'Count 1-10 ( 8)'; - like $lines[-1], qr/^@{[NAME1()]}: \s*100%/, 'Count 1-10 ( 9)'; + like $lines[-1], qr/^@{[$NAME1]}: \s*100%/, 'Count 1-10 ( 9)'; } # ------------------------------------- @@ -114,40 +106,41 @@ Use v1 mode { my $p; -my ($out, $err) = capture { - lives_ok { $p = Term::ProgressBar->new(NAME2, 10); } 'Count 1-10 ( 1)'; - lives_ok { $p->update($_) for 1..3 } 'Count 1-10 ( 2)'; -}; -print $out; + my ($out, $err) = capture { + lives_ok { $p = Term::ProgressBar->new($NAME2, 10); } 'Count 1-10 ( 1)'; + lives_ok { $p->update($_) for 1..3 } 'Count 1-10 ( 2)'; + }; + print $out; $err =~ s!^.*\r!!gm; - print STDERR "ERR (1) :\n$err\nlength: ", length($err), "\n" + diag "ERR (1) :\n$err\nlength: " . length($err) if $ENV{TEST_DEBUG}; my @lines = split /\n/, $err; - like $lines[-1], qr/^@{[NAME2()]}: \s*\b30%/, 'Count 1-10 ( 3)'; + like $lines[-1], qr/^@{[$NAME2]}: \s*\b30%/, 'Count 1-10 ( 3)'; my ($bar, $space) = $lines[-1] =~ /(\#*)(\s*)/; my $length = length($bar) + length($space); - print STDERR - ("LENGTHS (1) :BAR:", length($bar), ":SPACE:", length($space), "\n") - if $ENV{TEST_DEBUG}; + diag + ("LENGTHS (1) :BAR:" . length($bar) . ":SPACE:" . length($space)) + if $ENV{TEST_DEBUG}; my $barexpect = $length * 0.3; - my $ok = length($bar) > $barexpect -1 && length($bar) < $barexpect+1; - ok $ok; + cmp_ok length($bar), '>', $barexpect -1; + cmp_ok length($bar), '<', $barexpect+1; -($out, $err) = capture { - lives_ok { $p->message(MESSAGE1) } 'Count 1-10 ( 5)'; - lives_ok { $p->update($_) for 6..10 } 'Count 1-10 ( 6)'; -}; -print $out; + ($out, $err) = capture { + lives_ok { $p->message($MESSAGE1) } 'Count 1-10 ( 5)'; + lives_ok { $p->update($_) for 6..10 } 'Count 1-10 ( 6)'; + }; + print $out; + $err =~ s!^.*\r!!gm; - print STDERR "ERR (2) :\n$err\nlength: ", length($err), "\n" + diag "ERR (2) :\n$err\nlength: " . length($err) if $ENV{TEST_DEBUG}; @lines = split /\n/, $err; - like $lines[-1], qr/^@{[NAME2()]}: \s*\d+% \#*$/, 'Count 1-10 ( 8)'; - like $lines[-1], qr/^@{[NAME2()]}: \s*100%/, 'Count 1-10 ( 9)'; + like $lines[-1], qr/^@{[$NAME2]}: \s*\d+% \#*$/, 'Count 1-10 ( 8)'; + like $lines[-1], qr/^@{[$NAME2]}: \s*100%/, 'Count 1-10 ( 9)'; } # ------------------------------------- diff --git a/t/v1-message.t b/t/v1-message.t index 95ca985..3428ff8 100644 --- a/t/v1-message.t +++ b/t/v1-message.t @@ -1,6 +1,7 @@ # (X)Emacs mode: -*- cperl -*- use strict; +use warnings; =head1 Unit Test Package for Term::ProgressBar @@ -8,18 +9,12 @@ This package tests the basic functionality of Term::ProgressBar. =cut -use Data::Dumper qw( Dumper ); use Test::More tests => 8; use Test::Exception; -use constant MESSAGE1 => 'Walking on the Milky Way'; - -=head2 Test 1: compilation +use Capture::Tiny qw(capture); -This test confirms that the test script and the modules it calls compiled -successfully. - -=cut +my $MESSAGE1 = 'Walking on the Milky Way'; use_ok 'Term::ProgressBar'; @@ -42,23 +37,24 @@ Update it it from 1 to 10. (6) Check bar number is 100% =cut -use Capture::Tiny qw(capture); -my ($out, $err) = capture { - my $p; - lives_ok { $p = Term::ProgressBar->new('bob', 10); } 'Count 1-10 (1)'; - lives_ok { $p->update($_) for 1..5 } 'Count 1-10 (2)'; - lives_ok { $p->message(MESSAGE1) } 'Count 1-10 (3)'; - lives_ok { $p->update($_) for 6..10 } 'Count 1-10 (4)'; -}; -print $out; +{ + my ($out, $err) = capture { + my $p; + lives_ok { $p = Term::ProgressBar->new('bob', 10); } 'Count 1-10 (1)'; + lives_ok { $p->update($_) for 1..5 } 'Count 1-10 (2)'; + lives_ok { $p->message($MESSAGE1) } 'Count 1-10 (3)'; + lives_ok { $p->update($_) for 6..10 } 'Count 1-10 (4)'; + }; + print $out; $err =~ s!^.*\r!!gm; - print STDERR "ERR:\n$err\nlength: ", length($err), "\n" + diag "ERR:\n$err\nlength: ", length($err) if $ENV{TEST_DEBUG}; my @lines = split /\n/, $err; - is $lines[0], MESSAGE1; + is $lines[0], $MESSAGE1; like $lines[-1], qr/bob:\s+\d+% \#+/, 'Count 1-10 (6)'; like $lines[-1], qr/^bob:\s+100%/, 'Count 1-10 (7)'; +} diff --git a/t/v2-message.t b/t/v2-message.t index 6b68734..10430b3 100644 --- a/t/v2-message.t +++ b/t/v2-message.t @@ -1,6 +1,7 @@ # (X)Emacs mode: -*- cperl -*- use strict; +use warnings; =head1 Unit Test Package for Term::ProgressBar @@ -8,14 +9,12 @@ This package tests the basic functionality of Term::ProgressBar. =cut -use Data::Dumper 2.101 qw( Dumper ); use Test::More tests => 11; use Test::Exception; -use constant MESSAGE1 => 'Walking on the Milky Way'; - use Capture::Tiny qw(capture); +my $MESSAGE1 = 'Walking on the Milky Way'; =head2 Test 1: compilation @@ -44,25 +43,26 @@ Update it it from 1 to 10. Output a message halfway through. (7) Check bar number is 100% =cut - -my ($out, $err) = capture { - my $p; - lives_ok { $p = Term::ProgressBar->new(10); } 'Count 1-10 (1)'; - lives_ok { $p->update($_) for 1..5 } 'Count 1-10 (2)'; - lives_ok { $p->message(MESSAGE1) } 'Count 1-10 (3)'; - lives_ok { $p->update($_) for 6..10 } 'Count 1-10 (4)'; -}; -print $out; +{ + my ($out, $err) = capture { + my $p; + lives_ok { $p = Term::ProgressBar->new(10); } 'Count 1-10 (1)'; + lives_ok { $p->update($_) for 1..5 } 'Count 1-10 (2)'; + lives_ok { $p->message($MESSAGE1) } 'Count 1-10 (3)'; + lives_ok { $p->update($_) for 6..10 } 'Count 1-10 (4)'; + }; + print $out; $err =~ s!^.*\r!!gm; - print STDERR "ERR:\n$err\nlength: ", length($err), "\n" + diag "ERR:\n$err\nlength: " . length($err) if $ENV{TEST_DEBUG}; my @lines = split /\n/, $err; - is $lines[0], MESSAGE1; + is $lines[0], $MESSAGE1; like $lines[-1], qr/\[=+\]/, 'Count 1-10 (5)'; like $lines[-1], qr/^\s*100%/, 'Count 1-10 (6)'; +} # ------------------------------------- @@ -77,15 +77,17 @@ This is to check that message preserves the progress bar value correctly. =cut -($out, $err) = capture { - my $p; - lives_ok { $p = Term::ProgressBar->new(100); } 'Message Check ( 1)'; - lives_ok { for (0..100) { $p->update($_); $p->message("Hello") } } 'Message Check ( 2)'; -}; -print $out; +{ + my ($out, $err) = capture { + my $p; + lives_ok { $p = Term::ProgressBar->new(100); } 'Message Check ( 1)'; + lives_ok { for (0..100) { $p->update($_); $p->message("Hello") } } 'Message Check ( 2)'; + }; + print $out; my @err_lines = split /\n/, $err; (my $last_line = $err_lines[-1]) =~ tr/\r//d; is substr($last_line, 0, 4), '100%', 'Message Check ( 3)'; +} # ---------------------------------------------------------------------------- diff --git a/t/v2-mobile.t b/t/v2-mobile.t index 5176a52..dd42ea3 100644 --- a/t/v2-mobile.t +++ b/t/v2-mobile.t @@ -1,6 +1,7 @@ # (X)Emacs mode: -*- cperl -*- use strict; +use warnings; =head1 Unit Test Package for Term::ProgressBar @@ -8,7 +9,6 @@ This package tests the moving target functionality of Term::ProgressBar. =cut -use Data::Dumper qw( Dumper ); use Test::More tests => 7; use Test::Exception; @@ -45,11 +45,11 @@ my ($out, $err) = capture { }; print $out; - $err =~ s!^.*\r!!gm; - print STDERR "ERR:\n$err\nlength: ", length($err), "\n" +$err =~ s!^.*\r!!gm; +diag "ERR:\n$err\nlength: " . length($err) if $ENV{TEST_DEBUG}; - my @lines = split /\n/, $err; +my @lines = split /\n/, $err; - like $lines[-1], qr/\[=+\]/, 'Count 1-20 (5)'; - like $lines[-1], qr/^\s*100%/, 'Count 1-20 (6)'; +like $lines[-1], qr/\[=+\]/, 'Count 1-20 (5)'; +like $lines[-1], qr/^\s*100%/, 'Count 1-20 (6)'; diff --git a/t/v2-simple.t b/t/v2-simple.t index 4816ab9..439594f 100644 --- a/t/v2-simple.t +++ b/t/v2-simple.t @@ -1,6 +1,7 @@ # (X)Emacs mode: -*- cperl -*- use strict; +use warnings; =head1 Unit Test Package for Term::ProgressBar @@ -8,7 +9,6 @@ This package tests the basic functionality of Term::ProgressBar. =cut -use Data::Dumper qw( Dumper ); use Test::More tests => 31; use Test::Exception; @@ -33,15 +33,15 @@ Update it it from 1 to 10. =cut { - my $p; - -my ($out, $err) = capture { - lives_ok { $p = Term::ProgressBar->new(10); } 'Count 1-10 (1)'; - lives_ok { $p->update($_) for 1..10 } 'Count 1-10 (2)'; -}; -print $out; - my @lines = grep $_ ne '', split /\r/, $err; - print Dumper \@lines + my ($out, $err) = capture { + my $p; + lives_ok { $p = Term::ProgressBar->new(10); } 'Count 1-10 (1)'; + lives_ok { $p->update($_) for 1..10 } 'Count 1-10 (2)'; + }; + print $out; + + my @lines = grep {$_ ne ''} split /\r/, $err; + diag explain \@lines if $ENV{TEST_DEBUG}; like $lines[-1], qr/\[=+\]/, 'Count 1-10 (3)'; like $lines[-1], qr/^\s*100%/, 'Count 1-10 (4)'; @@ -64,16 +64,15 @@ Update it it from 1 to 9. =cut { - my $p; - -my ($out, $err) = capture { - lives_ok { $p = Term::ProgressBar->new(10); } 'Count 1-9 (1)'; - lives_ok { $p->update($_) for 1..9 } 'Count 1-9 (2)'; -}; -print $out; + my ($out, $err) = capture { + my $p; + lives_ok { $p = Term::ProgressBar->new(10); } 'Count 1-9 (1)'; + lives_ok { $p->update($_) for 1..9 } 'Count 1-9 (2)'; + }; + print $out; my @lines = grep $_ ne '', split /\r/, $err; - print Dumper \@lines + diag explain \@lines if $ENV{TEST_DEBUG}; like $lines[-1], qr/\[=+ +\]/, 'Count 1-9 (3)'; like $lines[-1], qr/^\s*90%/, 'Count 1-9 (4)'; @@ -90,13 +89,14 @@ percentage or displayed bar). =cut { -my ($out, $err) = capture { - my $b = Term::ProgressBar->new(1000000); - $b->update($_) foreach (0, 1); -}; -print $out; - my @lines = grep $_ ne '', split /\r/, $err; - print Dumper \@lines + my ($out, $err) = capture { + my $tp = Term::ProgressBar->new(1000000); + $tp->update($_) foreach (0, 1); + }; + #print $out; + + my @lines = grep {$_ ne ''} split /\r/, $err; + diag explain \@lines if $ENV{TEST_DEBUG}; is scalar @lines, 1; } diff --git a/t/zero.t b/t/zero.t index ffabda9..fda3d35 100644 --- a/t/zero.t +++ b/t/zero.t @@ -1,6 +1,7 @@ # (X)Emacs mode: -*- cperl -*- use strict; +use warnings; =head1 Unit Test Package for Term::ProgressBar @@ -8,7 +9,6 @@ This package tests the zero-progress handling of progress bar. =cut -use Data::Dumper qw( Dumper ); use Test::More tests => 9; use Test::Exception; @@ -34,15 +34,15 @@ Update it it from 1 to 10. { my $p; - my $name; -my ($out, $err) = capture { - $name = 'doing nothing'; - lives_ok { $p = Term::ProgressBar->new($name, 0); } 'V1 mode ( 1)'; - lives_ok { $p->update($_) for 1..10 } 'V1 mode ( 2)'; -}; -print $out; - my @lines = grep $_ ne '', split /\r/, $err; - print Dumper \@lines + my $name = 'doing nothing'; + my ($out, $err) = capture { + lives_ok { $p = Term::ProgressBar->new($name, 0); } 'V1 mode ( 1)'; + lives_ok { $p->update($_) for 1..10 } 'V1 mode ( 2)'; + }; + print $out; + + my @lines = grep { $_ ne ''} split /\r/, $err; + diag explain @lines if $ENV{TEST_DEBUG}; like $lines[-1], qr/^$name:/, 'V1 mode ( 3)'; like $lines[-1], qr/\(nothing to do\)/, 'V1 mode ( 4)'; @@ -65,16 +65,17 @@ Update it it from 1 to 10. { my $p; my $name = 'zero'; -my ($out, $err) = capture { - lives_ok { $p = Term::ProgressBar->new({ count => 0, name => $name }); } 'V2 mode ( 1)'; - lives_ok { $p->update($_) for 1..10 } 'V2 mode ( 2)'; -}; -print $out; - my @lines = grep $_ ne '', split /\r/, $err; - print Dumper \@lines + my ($out, $err) = capture { + lives_ok { $p = Term::ProgressBar->new({ count => 0, name => $name }); } 'V2 mode ( 1)'; + lives_ok { $p->update($_) for 1..10 } 'V2 mode ( 2)'; + }; + print $out; + + my @lines = grep {$_ ne ''} split /\r/, $err; + diag explain @lines if $ENV{TEST_DEBUG}; - like $lines[-1], qr/^$name:/, 'V2 mode ( 3)'; - like $lines[-1], qr/\(nothing to do\)/, 'V2 mode ( 4)'; + like $lines[-1], qr/^$name:/, 'V2 mode ( 3)'; + like $lines[-1], qr/\(nothing to do\)/, 'V2 mode ( 4)'; } # ---------------------------------------------------------------------------- -- 2.39.2