Created
March 27, 2014 00:26
-
-
Save preaction/9797116 to your computer and use it in GitHub Desktop.
Build your own Test Kit with Import::Into. Now with self-tests!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package My::Test; | |
use strict; | |
use warnings; | |
use Import::Into; | |
use Module::Runtime qw( use_module ); | |
sub import { | |
my ( $class, %args ) = @_; | |
my %modules = ( | |
'strict' => [], | |
'warnings' => [], | |
'Test::More' => [], | |
'Test::Exception' => [], | |
'Test::Differences' => [], | |
'Test::Deep' => [], | |
'Test::Warn' => [], | |
'Test::MockObject' => [], | |
'Test::MockObject::Extends' => [], | |
'Test::MockModule' => [], | |
'Data::Dumper' => [], | |
'File::Temp' => [], | |
'Carp' => [], | |
'Readonly' => [], | |
); | |
for my $mod ( keys %modules ) { | |
use_module( $mod )->import::into( scalar caller, @{$modules{$mod}} ); | |
} | |
} | |
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use Test::More; | |
## no critic ProhibitStringyEval ProhibitNoStrict ProhibitNoWarnings | |
subtest 'strict' => sub { | |
# disable strict so My::Test has to explicitly re-enable it | |
# pragmas cannot be hidden by a package statement | |
no strict; | |
eval 'use My::Test; @m = ( "one" );'; | |
ok $@, 'code that fails strict dies: ' . $@; | |
}; | |
subtest 'warnings' => sub { | |
# disable warnings so My::Test has to explicitly re-enable it | |
# pragmas cannot be hidden by a package statement | |
no warnings; | |
my @warnings; | |
local $SIG{__WARN__} = sub { push @warnings, @_ }; | |
eval 'use My::Test; my $foo = "one" . undef;'; | |
is scalar @warnings, 1; | |
like $warnings[0], qr/uninitialized/; | |
}; | |
## no critic ProhibitPrototypes | |
sub does_import_sub($$$) { | |
my ( $module, $imported_module, $imported_sub ) = @_; | |
return subtest "$module imports $imported_module sub $imported_sub" => sub { | |
ok eval "package ${module}::${imported_module}; use $module; return __PACKAGE__->can('$imported_sub')"; | |
ok !$@; | |
}; | |
} | |
sub does_import_class($$) { | |
my ( $module, $imported_class ) = @_; | |
return subtest "$module imports $imported_class" => sub { | |
ok eval "package ${module}::${imported_class}; use $module; return $imported_class->can('new')"; | |
ok !$@; | |
}; | |
} | |
does_import_sub 'My::Test', 'Test::More', 'ok'; | |
does_import_sub 'My::Test', 'Test::Exception', 'dies_ok'; | |
does_import_sub 'My::Test', 'Test::Differences', 'eq_or_diff'; | |
does_import_sub 'My::Test', 'Test::Deep', 'cmp_deeply'; | |
does_import_sub 'My::Test', 'Test::Warn', 'warning_is'; | |
does_import_class 'My::Test', 'Test::MockObject'; | |
does_import_class 'My::Test', 'Test::MockObject::Extends'; | |
does_import_class 'My::Test', 'Test::MockModule'; | |
does_import_sub 'My::Test', 'Data::Dumper', 'Dumper'; | |
does_import_class 'My::Test', 'File::Temp'; | |
does_import_sub 'My::Test', 'Carp', 'carp'; | |
does_import_sub 'My::Test', 'Readonly', 'Readonly'; | |
done_testing; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment