diff options
Diffstat (limited to 't')
-rw-r--r-- | t/01basic.t | 38 | ||||
-rw-r--r-- | t/02renaming.t | 50 | ||||
-rw-r--r-- | t/03generators.t | 38 | ||||
-rw-r--r-- | t/04into.t | 36 | ||||
-rw-r--r-- | t/05shiny.t | 40 | ||||
-rw-r--r-- | t/06notwant.t | 41 | ||||
-rw-r--r-- | t/07regexp.t | 48 | ||||
-rw-r--r-- | t/08tags.t | 66 | ||||
-rw-r--r-- | t/09warnings.t | 83 | ||||
-rw-r--r-- | t/10no.t | 54 |
10 files changed, 494 insertions, 0 deletions
diff --git a/t/01basic.t b/t/01basic.t new file mode 100644 index 0000000..72dd3d7 --- /dev/null +++ b/t/01basic.t @@ -0,0 +1,38 @@ +=pod + +=encoding utf-8 + +=head1 PURPOSE + +Very basic Exporter::Tiny test. + +Check that it allows us to import the functions named in C<< @EXPORT >>. + +=head1 AUTHOR + +Toby Inkster E<lt>tobyink@cpan.orgE<gt>. + +=head1 COPYRIGHT AND LICENCE + +This software is copyright (c) 2013 by Toby Inkster. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + + +=cut + +use strict; +use warnings; +use Test::More tests => 2; + +use lib qw( examples ../examples ); + +use Example::Exporter; + +diag("Perl $]"); + +is fib(6), 8, 'Correctly imported "fib" from Example::Exporter'; + +ok !__PACKAGE__->can('embiggen'), 'Did not inadvertantly import "embiggen"'; + diff --git a/t/02renaming.t b/t/02renaming.t new file mode 100644 index 0000000..857b81c --- /dev/null +++ b/t/02renaming.t @@ -0,0 +1,50 @@ +=pod + +=encoding utf-8 + +=head1 PURPOSE + +Check renaming imported functions. + +=head1 AUTHOR + +Toby Inkster E<lt>tobyink@cpan.orgE<gt>. + +=head1 COPYRIGHT AND LICENCE + +This software is copyright (c) 2013 by Toby Inkster. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +=cut + +use strict; +use warnings; +use Test::More tests => 6; + +BEGIN { *note = *diag unless __PACKAGE__->can("note") }; + +use lib qw( examples ../examples ); + +note "Rename functions using -as"; do { + package Local::AAA; + use Example::Exporter fib => { -as => 'fibonacci' }; + ::is fibonacci(6), 8, 'Correctly imported "fibonacci" from Example::Exporter'; + ::ok !__PACKAGE__->can('fib'), 'Did not inadvertantly import "fib"'; +}; + +note "Rename functions using -prefix"; do { + package Local::BBB; + use Example::Exporter fib => { -prefix => 'my' }; + ::is myfib(6), 8, 'Correctly imported "myfib" from Example::Exporter'; + ::ok !__PACKAGE__->can('fib'), 'Did not inadvertantly import "fib"'; +}; + +note "Rename functions using -suffix"; do { + package Local::CCC; + use Example::Exporter fib => { -suffix => 'onacci' }; + ::is fibonacci(6), 8, 'Correctly imported "fibonacci" from Example::Exporter'; + ::ok !__PACKAGE__->can('fib'), 'Did not inadvertantly import "fib"'; +}; + diff --git a/t/03generators.t b/t/03generators.t new file mode 100644 index 0000000..1f69d10 --- /dev/null +++ b/t/03generators.t @@ -0,0 +1,38 @@ +=pod + +=encoding utf-8 + +=head1 PURPOSE + +Check renaming imported functions. + +=head1 AUTHOR + +Toby Inkster E<lt>tobyink@cpan.orgE<gt>. + +=head1 COPYRIGHT AND LICENCE + +This software is copyright (c) 2013 by Toby Inkster. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +=cut + +use strict; +use warnings; +use Test::More tests => 4; + +use lib qw( examples ../examples ); + +use Example::Exporter + embiggen => {}, + embiggen => { -suffix => '_by_2', amount => 2 }, + embiggen => { -suffix => '_by_42', amount => 42 }; + +is embiggen(10), 11, 'embiggen'; +is embiggen_by_2(10), 12, 'embiggen_by_2'; +is embiggen_by_42(10), 52, 'embiggen_by_42'; + +is prototype(\&embiggen), '$', 'correct prototype'; + diff --git a/t/04into.t b/t/04into.t new file mode 100644 index 0000000..d621fef --- /dev/null +++ b/t/04into.t @@ -0,0 +1,36 @@ +=pod + +=encoding utf-8 + +=head1 PURPOSE + +Check the C<< -into >> option works. + +=head1 AUTHOR + +Toby Inkster E<lt>tobyink@cpan.orgE<gt>. + +=head1 COPYRIGHT AND LICENCE + +This software is copyright (c) 2013 by Toby Inkster. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +=cut + +use strict; +use warnings; +use Test::More tests => 2; + +use lib qw( examples ../examples ); + +{ + package Foo; + use Example::Exporter { into => "Bar" }, qw( fib ); +} + +{ package Bar; } + +ok( not "Foo"->can("fib") ); +ok( "Bar"->can("fib") ); diff --git a/t/05shiny.t b/t/05shiny.t new file mode 100644 index 0000000..9b5875d --- /dev/null +++ b/t/05shiny.t @@ -0,0 +1,40 @@ +=pod + +=encoding utf-8 + +=head1 PURPOSE + +Very basic Exporter::Shiny test. + +=head1 AUTHOR + +Toby Inkster E<lt>tobyink@cpan.orgE<gt>. + +=head1 COPYRIGHT AND LICENCE + +This software is copyright (c) 2014 by Toby Inkster. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + + +=cut + +use strict; +use warnings; +use Test::More tests => 1; + +{ + package Local::Foo; + use Exporter::Shiny qw(foo bar); + sub foo { + return 42; + } + sub bar { + return 666; + } +} + +use Local::Foo qw(foo); + +is(foo(), 42); diff --git a/t/06notwant.t b/t/06notwant.t new file mode 100644 index 0000000..992bbd7 --- /dev/null +++ b/t/06notwant.t @@ -0,0 +1,41 @@ +=pod + +=encoding utf-8 + +=head1 PURPOSE + +Test the C<< !notwant >> notation. + +=head1 AUTHOR + +Toby Inkster E<lt>tobyink@cpan.orgE<gt>. + +=head1 COPYRIGHT AND LICENCE + +This software is copyright (c) 2014 by Toby Inkster. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + + +=cut + +use strict; +use warnings; +use Test::More tests => 1; + +{ + package Local::Foo; + use Exporter::Shiny qw(foo bar); + sub foo { + return 42; + } + sub bar { + return 666; + } +} + +my %imported; +'Local::Foo'->import({ into => \%imported }, qw( -all !foo )); + +is_deeply([sort keys %imported], ['bar']); diff --git a/t/07regexp.t b/t/07regexp.t new file mode 100644 index 0000000..998dc4e --- /dev/null +++ b/t/07regexp.t @@ -0,0 +1,48 @@ +=pod + +=encoding utf-8 + +=head1 PURPOSE + +Test the C<< /regexp/ >> notation. + +=head1 AUTHOR + +Toby Inkster E<lt>tobyink@cpan.orgE<gt>. + +=head1 COPYRIGHT AND LICENCE + +This software is copyright (c) 2014 by Toby Inkster. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + + +=cut + +use strict; +use warnings; +use Test::More tests => 2; + +{ + package Local::Foo; + use Exporter::Shiny qw(foo bar); + sub foo { + return 42; + } + sub bar { + return 666; + } +} + +{ + my %imported; + 'Local::Foo'->import({ into => \%imported }, qw( /^F/i )); + is_deeply([sort keys %imported], ['foo']); +} + +{ + my %imported; + 'Local::Foo'->import({ into => \%imported }, qw( -all !/^F/i )); + is_deeply([sort keys %imported], ['bar']); +} diff --git a/t/08tags.t b/t/08tags.t new file mode 100644 index 0000000..036dc3b --- /dev/null +++ b/t/08tags.t @@ -0,0 +1,66 @@ +=pod + +=encoding utf-8 + +=head1 PURPOSE + +Test that tag expansion works sanely. + +=head1 AUTHOR + +Toby Inkster E<lt>tobyink@cpan.orgE<gt>. + +=head1 COPYRIGHT AND LICENCE + +This software is copyright (c) 2014 by Toby Inkster. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + + +=cut + +use strict; +use warnings; +use Test::More tests => 7; + +BEGIN { + package Local::Foo; + use Exporter::Shiny qw(foo bar); + our %EXPORT_TAGS = ( + first => [ 'foo' => { xxx => 41 }, 'bar' ], + second => [ 'foo', 'bar' ], + upper => [ + 'foo' => { -as => 'O', -prefix => 'F', -suffix => 'O' }, + 'bar' => { -as => 'A', -prefix => 'B', -suffix => 'R' }, + ], + ); + sub _generate_foo { + my $me = shift; + my ($name, $args) = @_; + return sub () { $args->{xxx} }; + } + sub _generate_bar { + my $me = shift; + my ($name, $args) = @_; + return sub () { $args->{xxx} }; + } +}; + +use Local::Foo + -first => { -prefix => 'first_' }, + -second => { -prefix => 'second_', xxx => 666 }, + -first => { -prefix => 'third_', xxx => 42 }; + +is(first_foo, 41); +is(first_bar, undef); + +is(second_foo, 666); +is(second_bar, 666); + +is(third_foo, 42); +is(third_bar, 42); + +use Local::Foo -upper => { -prefix => 'MY', xxx => 999 }; + +is(MYFOO, 999); diff --git a/t/09warnings.t b/t/09warnings.t new file mode 100644 index 0000000..72d40f3 --- /dev/null +++ b/t/09warnings.t @@ -0,0 +1,83 @@ +=pod + +=encoding utf-8 + +=head1 PURPOSE + +Test sub redefinition warnings/errors. + +=head1 AUTHOR + +Toby Inkster E<lt>tobyink@cpan.orgE<gt>. + +=head1 COPYRIGHT AND LICENCE + +This software is copyright (c) 2014 by Toby Inkster. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + + +=cut + +use strict; +use warnings; +use Test::More; + +BEGIN { + eval "use Test::Fatal; use Test::Warnings qw(warning :no_end_test); 1" + or plan skip_all => "test requires Test::Warnings and Test::Fatal"; + + plan tests => 4; +}; + +BEGIN { + package Local::Exporter; + use Exporter::Shiny qw(foo bar); + sub foo { 666 } + sub bar { 999 } +}; + +like( + warning { eval q{ + package Local::Test1; + sub foo { 42 } + use Local::Exporter -all; + 1; + } }, + qr/^Overwriting existing sub 'Local::Test1::foo' with sub 'foo' exported by Local::Exporter/, + 'warning about overwriting sub', +); + +like( + exception { eval q{ + package Local::Test2; + sub foo { 42 } + use Local::Exporter { replace => 'die' }, -all; + 1; + } or die $@ }, + qr/^Refusing to overwrite existing sub 'Local::Test2::foo' with sub 'foo' exported by Local::Exporter/, + '... which can be fatalized', +); + +is_deeply( + warning { eval q{ + package Local::Test3; + sub foo { 42 } + use Local::Exporter { replace => 'die' }, -all; + 1; + } }, + [], + '... or suppressed', +); + +is_deeply( + warning { eval q{ + package Local::Test4; + use Local::Exporter -all; + use Local::Exporter qw(foo); + 1; + } }, + [], + 'but importing the exact same sub twice is OK', +); diff --git a/t/10no.t b/t/10no.t new file mode 100644 index 0000000..66b0f82 --- /dev/null +++ b/t/10no.t @@ -0,0 +1,54 @@ +=pod + +=encoding utf-8 + +=head1 PURPOSE + +Check C<< unimport >> works. + +=head1 AUTHOR + +Toby Inkster E<lt>tobyink@cpan.orgE<gt>. + +=head1 COPYRIGHT AND LICENCE + +This software is copyright (c) 2014 by Toby Inkster. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +=cut + +use strict; +use warnings; +use Test::More tests => 7; + +use lib qw( examples ../examples ); + +{ + package Local::Pkg1; + use Example::Exporter; + ::is( fib(6), 8, 'fib exported' ); + no Example::Exporter; +} + +ok( !Local::Pkg1->can('fib'), 'tidied fib' ); + +{ + package Local::Pkg2; + use Example::Exporter fib => { -as => 'fibo' }; + ::is( fibo(6), 8, 'fibo exported' ); + no Example::Exporter; +} + +ok( !Local::Pkg2->can('fibo'), 'tidied fibo' ); + +{ + package Local::Pkg3; + use Example::Exporter -all; + ::is( fib(6), 8, 'fib exported' ); + ::is( embiggen(6), 7, 'embiggen exported' ); + no Example::Exporter qw( /^F/i ); +} + +ok( Local::Pkg3->can('embiggen') && !Local::Pkg3->can('fib'), 'tidied by regexp' ); |