Acme-MetaSyntactic
view release on metacpan or search on metacpan
lib/Test/MetaSyntactic.pm view on Meta::CPAN
package Test::MetaSyntactic;
use strict;
use warnings;
use Acme::MetaSyntactic ();
use Config ();
use base 'Test::Builder::Module';
our @EXPORT = qw( all_themes_ok theme_ok );
our $VERSION = '1.005';
#
# exported functions
#
sub all_themes_ok {
my (@lib) = @_;
@lib = _starting_points() if !@lib;
my %source = Acme::MetaSyntactic->_find_themes(@lib);
my $tb = __PACKAGE__->builder;
local $Test::Builder::Level = $Test::Builder::Level + 1;
$tb->plan( tests => scalar keys %source );
my @fail;
theme_ok( $_, $source{$_} ) or push @fail, $_ for sort keys %source;
$tb->diag("Test suite failed for the following:") if @fail;
$tb->diag("- $_") for @fail;
}
sub theme_ok {
my @args = @_;
my $tb = __PACKAGE__->builder;
local $Test::Builder::Level = $Test::Builder::Level + 1;
# all subtests
my $theme = $args[0];
$tb->subtest(
$theme,
sub {
$tb->subtest( "$theme fixme", sub { subtest_fixme(@args); } );
$tb->subtest( "$theme encoding", sub { subtest_encoding(@args); } );
$tb->subtest( "$theme load", sub { subtest_load(@args); } )
or return;
$tb->subtest( "$theme version", sub { subtest_version(@args); } );
$tb->subtest( "$theme data", sub { subtest_data(@args); } );
$tb->subtest( "$theme format", sub { subtest_format(@args); } );
$tb->subtest( "$theme length", sub { subtest_length(@args); } );
$tb->subtest( "$theme import", sub { subtest_import(@args); } );
$tb->subtest( "$theme noimport", sub { subtest_noimport(@args); } );
$tb->subtest( "$theme theme", sub { subtest_theme(@args); } );
$tb->subtest( "$theme remote", sub { subtest_remote(@args); } );
$tb->done_testing;
}
);
}
#
# useful internal functions
#
# some starting points to look for theme modules
sub _starting_points {
return 'blib/lib' if -e 'blib/lib';
return 'lib';
}
# load the theme in a random namespace
{
my $num = 0;
sub _load {
my ( $theme, $do_import ) = @_;
my $module = "Acme::MetaSyntactic::$theme";
my $pkg = sprintf "Acme::MetaSyntactic::SCRATCH_%04d", $num++;
my $code = $do_import
? "package $pkg; use $module; 1;"
: "package $pkg; use $module (); 1;";
my $ok = eval $code;
return ( $pkg, !$ok && $@ );
}
}
# return a list of [ AMS object, details ]
sub _theme_sublists {
my ($theme) = @_;
my @metas;
# assume the module has already been loaded
no strict 'refs';
my $class = "Acme::MetaSyntactic::$theme";
if( $class->isa('Acme::MetaSyntactic::Locale') ) {
for my $lang ( "Acme::MetaSyntactic::$theme"->languages() ) {
push @metas,
[ "Acme::MetaSyntactic::$theme"->new( lang => $lang ),
"$theme, $lang locale" ];
}
}
elsif( $class->isa('Acme::MetaSyntactic::MultiList') ) {
for my $cat ( "Acme::MetaSyntactic::$theme"->categories() ) {
push @metas,
[ "Acme::MetaSyntactic::$theme"->new( category => $cat ),
"$theme, $cat category" ];
}
}
else {
push @metas, [ "Acme::MetaSyntactic::$theme"->new(), $theme ];
}
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,
( run in 0.740 second using v1.01-cache-2.11-cpan-39bf76dae61 )