From: Hinrik Örn Sigurðsson Date: Mon, 25 Jan 2010 18:57:35 +0000 (+0000) Subject: Add test for strange --version handling X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=da43301a259a4a028fe3e79baddb89bc09153462;p=gitmo%2FMooseX-Getopt.git Add test for strange --version handling --version, -v, and -V are currently being handled very strangely, even more so when MooseX::SimpleConfig is used. --- diff --git a/ChangeLog b/ChangeLog index 7f63092..c638065 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ Revision history for Perl extension MooseX-Getopt +0.27 + * Test suite: + - Add t/011_version_options.t for testing if -v/--version/-V are handled + correctly + 0.26 Thu. Dec 10 2009 * MooseX::Getopt::Basic - Fix bug with attribute names containing upper case letters. diff --git a/Makefile.PL b/Makefile.PL index d3b9e47..ee89114 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -21,6 +21,7 @@ requires 'Getopt::Long::Descriptive' => '0.077'; build_requires 'Test::Moose'; build_requires 'Test::More' => '0.62'; build_requires 'Test::Exception' => '0.21'; +build_requires 'Capture::Tiny' => '0'; author_requires 'Test::Pod' => 1.14; author_requires 'Test::Pod::Coverage' => '1.04'; diff --git a/t/011_version_options.t b/t/011_version_options.t new file mode 100644 index 0000000..2385ebb --- /dev/null +++ b/t/011_version_options.t @@ -0,0 +1,35 @@ +use strict; +use warnings; +use Capture::Tiny 'capture'; +use File::Spec::Functions 'catfile'; +use Test::More; + +my $HAVE_SIMPLECONFIG = eval { + require MooseX::SimpleConfig; + return 1; +}; + +# none of the options should be known +for my $opt (qw(-v --version -V)) { + my $script = catfile('t', 'version_no_options.pl'); + my (undef, $stderr) = capture { system $^X, $script, $opt }; + like($stderr, qr/^Unknown option/, "Option $opt is unknown"); +} + +# only -V should be unknown, the other two should return our custom string +for my $test (qw(version_with_options.pl version_with_simpleconfig.pl)) { + my $script = catfile('t', $test); + + next if $test eq 'version_with_simpleconfig.pl' && !$HAVE_SIMPLECONFIG; + + my ($v, undef) = capture { system $^X, $script, '-v' }; + like($v, qr/^SUCCESS/, "Option -v is correct"); + + my ($version, undef) = capture { system $^X, $script, '--version' }; + like($version, qr/^SUCCESS/, "Option --version is correct"); + + my (undef, $V) = capture { system $^X, $script, '-V' }; + like($V, qr/^Unknown option/, "Option -V is unknown"); +} + +done_testing(); diff --git a/t/version_no_options.pl b/t/version_no_options.pl new file mode 100644 index 0000000..73b6289 --- /dev/null +++ b/t/version_no_options.pl @@ -0,0 +1,7 @@ +package NoOptions; +use Moose; + +with 'MooseX::Getopt'; + +package main; +NoOptions->new_with_options; diff --git a/t/version_with_options.pl b/t/version_with_options.pl new file mode 100644 index 0000000..d3c7916 --- /dev/null +++ b/t/version_with_options.pl @@ -0,0 +1,24 @@ +package WithOptions; +use Moose; + +with 'MooseX::Getopt'; + +has print_version => ( + traits => [qw(Getopt)], + isa => 'Bool', + is => 'ro', + cmd_flag => 'version', + cmd_aliases => 'v', +); + +sub run { + my ($self) = @_; + + if ($self->print_version) { + print "SUCCESS\n"; + exit; + } +} + +package main; +WithOptions->new_with_options; diff --git a/t/version_with_simpleconfig.pl b/t/version_with_simpleconfig.pl new file mode 100644 index 0000000..a6efd49 --- /dev/null +++ b/t/version_with_simpleconfig.pl @@ -0,0 +1,33 @@ +package WithOptionsAndSimpleConfig; +use Moose; + +with 'MooseX::Getopt'; + +has print_version => ( + traits => [qw(Getopt)], + isa => 'Bool', + is => 'ro', + cmd_flag => 'version', + cmd_aliases => 'v', +); + +has configfile => ( + traits => [qw(NoGetopt)], + isa => 'Str', + coerce => 1, + is => 'ro', +); + +with 'MooseX::SimpleConfig'; + +sub run { + my ($self) = @_; + + if ($self->print_version) { + print "SUCCESS\n"; + exit; + } +} + +package main; +WithOptionsAndSimpleConfig->new_with_options;