Acme-MetaSyntactic
view release on metacpan or search on metacpan
lib/Test/MetaSyntactic.pm view on Meta::CPAN
return @metas;
}
# return the list of all theme items
sub _theme_items {
my ($theme) = @_;
# assume the module has already been loaded
no strict 'refs';
my $class = "Acme::MetaSyntactic::$theme";
my @items
= $class->isa('Acme::MetaSyntactic::List')
? @{"$class\::List"}
: $class->isa('Acme::MetaSyntactic::MultiList')
? map @$_, values %{"$class\::MultiList"}
: ();
return @items;
}
sub _check_file_lines {
my ($theme, $file, $mesg, $cb ) = @_;
my $tb = __PACKAGE__->builder;
$tb->plan( tests => 1 );
local $Test::Builder::Level = $Test::Builder::Level + 1;
# try to find a source file if none given
$file ||= { Acme::MetaSyntactic->_find_themes(_starting_points) }->{$theme};
SKIP: {
my ($fh, $skip);
if ( $file ) {
open $fh, $file or do { $skip = "Can't open $file: $!"; };
}
else {
$skip = "This test needs the source file for $theme";
}
if( $skip ) {
$tb->skip($skip);
last SKIP;
}
my @lines = $cb->( <$fh> );
$tb->is_num( scalar @lines, 0, sprintf $mesg, $file );
map $tb->diag( $_ ), "Failed lines:\n", map " $_", @lines if @lines;
close $fh;
}
}
#
# individual subtest functions
#
# t/01load.t
# t/51useall.t
sub subtest_load {
my ($theme) = @_;
my $tb = __PACKAGE__->builder;
$tb->plan( tests => 2 );
# load in the current process
my ( $pkg, $error ) = _load( $theme, 1 );
$tb->ok( !$error, "use Acme::MetaSyntactic::$theme;" );
$tb->diag($error) if $error;
# load in isolation
local $ENV{PERL5LIB} = join $Config::Config{path_sep} || ';', @INC;
`$^X -MAcme::MetaSyntactic::$theme -e1`;
$tb->is_eq( $? >> 8, 0, "perl -MAcme::MetaSyntactic::$theme -e1" );
}
# t/02fixme.t
sub subtest_fixme {
my ( $theme, $file ) = @_;
$file = '' if !defined $file;
_check_file_lines(
$theme, $file,
"No FIXME found in %s",
sub { grep /\bFIXME\b/, @_ }
);
}
sub subtest_encoding {
my ( $theme, $file ) = @_;
$file = '' if !defined $file;
_check_file_lines(
$theme, $file,
"%s should have an =encoding line if it contains non-us-ascii characters",
sub {
my @non_ascii = grep /[^\x00-\x7f]/, @_;
my @encoding = grep /^=encoding \S+/, @_;
return @encoding ? () : @non_ascii;
}
);
}
# t/08theme.t
sub subtest_theme {
my ($theme) = @_;
my $tb = __PACKAGE__->builder;
$tb->plan( tests => 2 );
$tb->is_eq( eval { "Acme::MetaSyntactic::$theme"->theme },
$theme, "theme() for Acme::MetaSyntactic::$theme" );
$tb->is_eq( eval { "Acme::MetaSyntactic::$theme"->new->theme },
$theme, "theme() for Acme::MetaSyntactic::$theme" );
}
# t/17import.t
sub subtest_import {
my ($theme) = @_;
my $tb = __PACKAGE__->builder;
$tb->plan( tests => my $tests = 2 );
SKIP: {
if ( $theme =~ /^(?:any|random)$/ ) {
$tb->skip("Not testing import for theme $theme") for 1 .. $tests;
last SKIP;
}
else {
my ($pkg) = _load( $theme, 1 );
lib/Test/MetaSyntactic.pm view on Meta::CPAN
}
}
}
1;
__END__
=head1 NAME
Test::MetaSyntactic - Common tests for Acme::MetaSyntactic themes
=head1 SYNOPSIS
# add this as t/meta.t
use Test::MetaSyntatic;
all_themes_ok();
=head1 DESCRIPTION
This module provides the minimum set of tests that any Acme::MetaSyntactic theme
should pass.
The goal is to make is easier for theme creators build a distribution and ensure
theirs themes will work as expected when installed.
=head1 EXPORTED FUNCTIONS
=head2 all_themes_ok( @lib )
Will find all themes under the directories listed in C<@lib>, and run C<theme_ok()>
on them.
C<@lib> is optional (it will try to find themes in F<blib/lib> or F<lib> if not provided).
=head2 theme_ok( $theme, $source )
Will run all tests on the given C<$theme>. Some tests require access to the source, but
they will be skipped if C<$source> is not provided.
If the C<subtest_load()> test fails, no further test will be run.
=head1 SUBTESTS
The individual tests are run as subtests. All substests but C<subtest_load()>
assume that the module can be successfully loaded.
=head2 subtest_fixme( $theme, $source )
Checks that the theme source file does not contain the word "FIXME".
=head2 subtest_encoding( $theme, $source )
Checks that the theme source files contains an C<=encoding> line if
it contains some non us-ascii characters.
=head2 subtest_load( $theme )
Tries to load the theme module.
First in the currently running process, and then in isolation inside
its own environment.
=head2 subtest_version( $theme )
Checks that the theme has a C<$VERSION>.
=head2 subtest_format( $theme )
Checks that each metasyntactic name in the theme is a valid Perl
variable name.
=head2 subtest_length( $theme )
Checks that each name in the theme has valid length.
=head2 subtest_data( $theme, $source )
Checks that the C<__DATA__> section (if any) of the theme source is
properly formatted.
=head2 subtest_import( $theme )
Checks that the exported C<meta$theme> function returns an item from
C<$theme>.
=head2 subtest_noimport( $theme )
Checks that C<use Acme::MetaSyntactic::I<$theme> ()> does not export
the C<meta$theme> function.
=head2 subtest_theme( $theme )
Checks that the C<theme()> function returns the theme name.
=head2 subtest_remote( $theme )
For themes with a remote list, checks that the remote list (if any)
is identical to the current list of items in the theme.
This subtest will only be run if C<RELEASE_TESTING>
(or C<AUTHOR_TESTING>, for backward compatibility reasons) is true and
C<AUTOMATED_TESTING> is false. Requires L<LWP::UserAgent>.
=head1 AUTHOR
Philippe Bruhat (BooK), C<< <book@cpan.org> >>
=head1 COPYRIGHT
Copyright 2012-2017 Philippe Bruhat (BooK), All Rights Reserved.
=head1 LICENSE
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
( run in 1.673 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )