Skip to content

Commit

Permalink
apply more perlish naming
Browse files Browse the repository at this point in the history
  • Loading branch information
jsf116 committed Jun 23, 2023
1 parent 0ba7026 commit 50d285d
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 104 deletions.
102 changes: 50 additions & 52 deletions lib/Test/Expander.pm
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,14 @@ use Test2::Tools::Explain;
use Test2::V0 qw();

use Test::Expander::Constants qw(
$ANY_EXTENSION
$CLASS_HIERARCHY_LEVEL
$DIE $ERROR_WAS $EXCEPTION_PREFIX
$FALSE
$INVALID_DIRECTORY $INVALID_ENV_ENTRY $INVALID_VALUE
$KEEP_ENV_VAR
$NEW_FAILED $NEW_SUCCEEDED $NOTE
$REPLACEMENT $REQUIRE_DESCRIPTION $REQUIRE_IMPLEMENTATION
$SEARCH_PATTERN $SET_ENV_VAR $SET_TO
$TOP_DIR_IN_PATH $TRUE
$UNEXPECTED_EXCEPTION $UNKNOWN_OPTION $USE_DESCRIPTION $USE_IMPLEMENTATION
$VERSION_NUMBER
$DIE $FALSE
$FMT_INVALID_DIRECTORY $FMT_INVALID_ENV_ENTRY $FMT_INVALID_VALUE $FMT_KEEP_ENV_VAR $FMT_NEW_FAILED
$FMT_NEW_SUCCEEDED $FMT_REPLACEMENT $FMT_REQUIRE_DESCRIPTION $FMT_REQUIRE_IMPLEMENTATION $FMT_SEARCH_PATTERN
$FMT_SET_ENV_VAR $FMT_SET_TO $FMT_UNKNOWN_OPTION $FMT_USE_DESCRIPTION $FMT_USE_IMPLEMENTATION
$MSG_ERROR_WAS $MSG_UNEXPECTED_EXCEPTION
$NOTE
$REGEX_ANY_EXTENSION $REGEX_CLASS_HIERARCHY_LEVEL $REGEX_TOP_DIR_IN_PATH $REGEX_VERSION_NUMBER
$TRUE
%CONSTANTS_TO_EXPORT
);

Expand Down Expand Up @@ -68,11 +64,11 @@ sub import {
while( my @currentFrame = caller( $frameIndex++ ) ) {
$testFile = path( $currentFrame[ 1 ] ) =~ s{^/}{}r;
}
my $options = _parseOptions( \@exports, $testFile );
my $options = _parse_options( \@exports, $testFile );

_setEnv( $options->{ -target }, $testFile );
_set_env( $options->{ -target }, $testFile );

_exportSymbols( $options );
_export_symbols( $options );
Test2::V0->import( %$options );

Importer->import_into( $class, scalar( caller ), () );
Expand All @@ -90,7 +86,7 @@ sub lives_ok ( &;$ ) {
my ( $coderef, $description ) = @_;

eval { $coderef->() };
diag( $UNEXPECTED_EXCEPTION . $@ ) if $@;
diag( $MSG_UNEXPECTED_EXCEPTION . $@ ) if $@;

return ok( !$@, $description );
}
Expand All @@ -100,7 +96,7 @@ sub new_ok {

$args ||= [];
my $obj = eval { $class->new( @$args ) };
ok( !$@, _newTestMessage( $class ) );
ok( !$@, _new_test_message( $class ) );

return $obj;
}
Expand All @@ -109,8 +105,8 @@ sub require_ok {
my ( $module ) = @_;

my $package = caller;
my $requireResult = eval( sprintf( $REQUIRE_IMPLEMENTATION, $package, $module ) );
ok( $requireResult, sprintf( $REQUIRE_DESCRIPTION, $module, _error() ) );
my $requireResult = eval( sprintf( $FMT_REQUIRE_IMPLEMENTATION, $package, $module ) );
ok( $requireResult, sprintf( $FMT_REQUIRE_DESCRIPTION, $module, _error() ) );

return $requireResult;
}
Expand All @@ -132,23 +128,25 @@ sub use_ok ( $;@ ) {
my ( $package, $filename, $line ) = caller( 0 );
$filename =~ y/\n\r/_/; # taken over from Test::More

my $requireResult = eval( sprintf( $USE_IMPLEMENTATION, $package, $module, _useImports( \@imports ) ) );
my $requireResult = eval( sprintf( $FMT_USE_IMPLEMENTATION, $package, $module, _use_imports( \@imports ) ) );
ok(
$requireResult,
sprintf( $USE_DESCRIPTION, $module, _error( $SEARCH_PATTERN, sprintf( $REPLACEMENT, $filename, $line ) ) )
sprintf(
$FMT_USE_DESCRIPTION, $module, _error( $FMT_SEARCH_PATTERN, sprintf( $FMT_REPLACEMENT, $filename, $line ) )
)
);

return $requireResult;
}

sub _determineTestee {
sub _determine_testee {
my ( $options, $testFile ) = @_;

if ( $options->{ -lib } ) {
foreach my $directory ( @{ $options->{ -lib } } ) {
$DIE->( $INVALID_DIRECTORY, $directory, 'invalid type' ) if ref( $directory );
$DIE->( $FMT_INVALID_DIRECTORY, $directory, 'invalid type' ) if ref( $directory );
my $incEntry = eval( $directory );
$DIE->( $INVALID_DIRECTORY, $directory, $@ ) if $@;
$DIE->( $FMT_INVALID_DIRECTORY, $directory, $@ ) if $@;
unshift( @INC, $incEntry );
}
delete( $options->{ -lib } );
Expand All @@ -158,11 +156,11 @@ sub _determineTestee {
delete( $options->{ -method } );
}
else {
$METHOD = path( $testFile )->basename( $ANY_EXTENSION );
$METHOD = path( $testFile )->basename( $REGEX_ANY_EXTENSION );
}

unless ( exists( $options->{ -target } ) ) { # Try to determine class / module autmatically
my ( $testRoot ) = $testFile =~ $TOP_DIR_IN_PATH;
my ( $testRoot ) = $testFile =~ $REGEX_TOP_DIR_IN_PATH;
my $testee = path( $testFile )->relative( $testRoot )->parent;
$options->{ -target } = $testee =~ s{/}{::}gr if grep { path( $_ )->child( $testee . '.pm' )->is_file } @INC;
}
Expand All @@ -176,20 +174,20 @@ sub _error {

return '' if $@ eq '';

my $error = $ERROR_WAS . $@ =~ s/\n$//mr;
my $error = $MSG_ERROR_WAS . $@ =~ s/\n$//mr;
$error =~ s/$searchString/$replacementString/m if defined( $searchString );
return $error;
}

sub _exportSymbols {
sub _export_symbols {
my ( $options ) = @_;

foreach my $var ( sort keys( %CONSTANTS_TO_EXPORT ) ) { # Export defined constants
no strict qw( refs ); ## no critic (ProhibitProlongedStrictureOverride)
my $value = eval( "${ \$var }" ) or next;
readonly_on( ${ __PACKAGE__ . '::' . $var =~ s/^.//r } );
push( @EXPORT, $var );
$NOTE->( $SET_TO, $var, $CONSTANTS_TO_EXPORT{ $var }->( $value, $CLASS ) );
$NOTE->( $FMT_SET_TO, $var, $CONSTANTS_TO_EXPORT{ $var }->( $value, $CLASS ) );

if ( $var eq '$CLASS' ) { # Export method constants only if class is known
$METHOD_REF = $CLASS->can( $METHOD );
Expand All @@ -200,26 +198,26 @@ sub _exportSymbols {
return;
}

sub _newTestMessage {
sub _new_test_message {
my ( $class ) = @_;

return $@ ? sprintf( $NEW_FAILED, $class, _error() ) : sprintf( $NEW_SUCCEEDED, $class, $class );
return $@ ? sprintf( $FMT_NEW_FAILED, $class, _error() ) : sprintf( $FMT_NEW_SUCCEEDED, $class, $class );
}

sub _parseOptions {
sub _parse_options {
my ( $exports, $testFile ) = @_;

my $options = {};
while ( my $optionName = shift( @$exports ) ) {
given ( $optionName ) {
when ( '-lib' ) {
my $optionValue = shift( @$exports );
$DIE->( $INVALID_VALUE, $optionName, $optionValue ) if ref( $optionValue ) ne 'ARRAY';
$DIE->( $FMT_INVALID_VALUE, $optionName, $optionValue ) if ref( $optionValue ) ne 'ARRAY';
$options->{ -lib } = $optionValue;
}
when ( '-method' ) {
my $optionValue = shift( @$exports );
$DIE-> ( $INVALID_VALUE, $optionName, $optionValue ) if ref( $optionValue );
$DIE-> ( $FMT_INVALID_VALUE, $optionName, $optionValue ) if ref( $optionValue );
$METHOD = $options->{ -method } = $optionValue;
}
when ( '-target' ) {
Expand All @@ -228,28 +226,28 @@ sub _parseOptions {
}
when ( '-tempdir' ) {
my $optionValue = shift( @$exports );
$DIE->( $INVALID_VALUE, $optionName, $optionValue ) if ref( $optionValue ) ne 'HASH';
$DIE->( $FMT_INVALID_VALUE, $optionName, $optionValue ) if ref( $optionValue ) ne 'HASH';
$TEMP_DIR = tempdir( CLEANUP => 1, %$optionValue );
}
when ( '-tempfile' ) {
my $optionValue = shift( @$exports );
$DIE->( $INVALID_VALUE, $optionName, $optionValue ) if ref( $optionValue ) ne 'HASH';
$DIE->( $FMT_INVALID_VALUE, $optionName, $optionValue ) if ref( $optionValue ) ne 'HASH';
my $fileHandle;
( $fileHandle, $TEMP_FILE ) = tempfile( UNLINK => 1, %$optionValue );
}
when ( /^-\w/ ) {
$options->{ $optionName } = shift( @$exports );
}
default {
$DIE->( $UNKNOWN_OPTION, $optionName, shift( @$exports ) // '' );
$DIE->( $FMT_UNKNOWN_OPTION, $optionName, shift( @$exports ) // '' );
}
}
}

return _determineTestee( $options, $testFile );
return _determine_testee( $options, $testFile );
}

sub _readEnvFile {
sub _read_env_file {
my ( $envFile ) = @_;

my @lines = path( $envFile )->lines( { chomp => 1 } );
Expand All @@ -259,33 +257,33 @@ sub _readEnvFile {
next unless $line =~ /^ (?<name> \w+) \s* (?: = \s* (?<value> \S .*) | $ )/x;
if ( exists( $+{ value } ) ) {
$env{ $+{ name } } = eval( $+{ value } );
$DIE->( $INVALID_ENV_ENTRY, $index, $envFile, $line, $@ ) if $@;
$NOTE->( $SET_ENV_VAR, $+{ name }, $env{ $+{ name } }, $envFile );
$DIE->( $FMT_INVALID_ENV_ENTRY, $index, $envFile, $line, $@ ) if $@;
$NOTE->( $FMT_SET_ENV_VAR, $+{ name }, $env{ $+{ name } }, $envFile );
}
elsif ( exists( $ENV{ $+{ name } } ) ) {
$env{ $+{ name } } = $ENV{ $+{ name } };
$NOTE->( $KEEP_ENV_VAR, $+{ name }, $ENV{ $+{ name } } );
$NOTE->( $FMT_KEEP_ENV_VAR, $+{ name }, $ENV{ $+{ name } } );
}
}

return \%env;
}

sub _setEnv {
sub _set_env {
my ( $class, $testFile ) = @_;

my $envFound = $FALSE;
my $newEnv = {};
{
local $CWD = $testFile =~ s{/.*}{}r; ## no critic (ProhibitLocalVars)
( $envFound, $newEnv ) = _setEnvHierarchically( $class, $envFound, $newEnv );
( $envFound, $newEnv ) = _set_env_hierarchically( $class, $envFound, $newEnv );
}

my $envFile = $testFile =~ s/$ANY_EXTENSION/.env/r;
my $envFile = $testFile =~ s/$REGEX_ANY_EXTENSION/.env/r;

if ( path( $envFile )->is_file ) {
$envFound = $TRUE unless $envFound;
my $methodEnv = _readEnvFile( $envFile );
my $methodEnv = _read_env_file( $envFile );
@$newEnv{ keys( %$methodEnv ) } = values( %$methodEnv );
}

Expand All @@ -294,30 +292,30 @@ sub _setEnv {
return;
}

sub _setEnvHierarchically {
sub _set_env_hierarchically {
my ( $class, $envFound, $newEnv ) = @_;

return ( $envFound, $newEnv ) unless $class;

my $classTopLevel;
( $classTopLevel, $class ) = $class =~ $CLASS_HIERARCHY_LEVEL;
( $classTopLevel, $class ) = $class =~ $REGEX_CLASS_HIERARCHY_LEVEL;

return ( $FALSE, {} ) unless path( $classTopLevel )->is_dir;

my $envFile = $classTopLevel . '.env';
if ( path( $envFile )->is_file ) {
$envFound = $TRUE unless $envFound;
$newEnv = { %$newEnv, %{ _readEnvFile( $envFile ) } };
$newEnv = { %$newEnv, %{ _read_env_file( $envFile ) } };
}

local $CWD = $classTopLevel; ## no critic (ProhibitLocalVars)
return _setEnvHierarchically( $class, $envFound, $newEnv );
return _set_env_hierarchically( $class, $envFound, $newEnv );
}

sub _useImports {
sub _use_imports {
my ( $imports ) = @_;

return @$imports == 1 && $imports->[ 0 ] =~ $VERSION_NUMBER ? ' ' . $imports->[ 0 ] : '';
return @$imports == 1 && $imports->[ 0 ] =~ $REGEX_VERSION_NUMBER ? ' ' . $imports->[ 0 ] : '';
}

1;
62 changes: 35 additions & 27 deletions lib/Test/Expander/Constants.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,41 @@ use Test2::Tools::Basic;

readonly_on( $VERSION );

const our $ANY_EXTENSION => qr/ \. [^.]+ $/x;
const our $CLASS_HIERARCHY_LEVEL => qr/^( \w+ ) (?: :: ( .+ ) )?/x;
const our $DIE => sub { die( sprintf( $_[ 0 ], @_[ 1 .. $#_ ] ) ) };
const our $ERROR_WAS => ' Error was: ';
const our $EXCEPTION_PREFIX => 'BEGIN failed--compilation aborted at ';
const our $FALSE => 0;
const our $INVALID_DIRECTORY => "Invalid directory name / expression '%s' supplied along with option '-lib'%s\n";
const our $INVALID_ENV_ENTRY => "Erroneous line %d of '%s' containing '%s': %s\n";
const our $INVALID_VALUE => "Option '%s' passed along with invalid value '%s'\n";
const our $KEEP_ENV_VAR => "Keep environment variable '%s' containing '%s'";
const our $NEW_FAILED => '%s->new died.%s';
const our $NEW_SUCCEEDED => "An object of class '%s' isa '%s'";
const our $NOTE => sub { my ( $format, @args ) = @_; note( sprintf( $format, @args ) ) };
const our $REPLACEMENT => $EXCEPTION_PREFIX . '%s line %s.';
const our $REQUIRE_DESCRIPTION => 'require %s;%s';
const our $REQUIRE_IMPLEMENTATION => 'package %s; require %s';
const our $SEARCH_PATTERN => $EXCEPTION_PREFIX . '.*$';
const our $SET_ENV_VAR => "Set environment variable '%s' to '%s' from file '%s'";
const our $SET_TO => "Set %s to '%s'";
const our $TOP_DIR_IN_PATH => qr{^ ( [^/]+ ) }x;
const our $TRUE => 1;
const our $UNEXPECTED_EXCEPTION => 'Unexpectedly caught exception: ';
const our $UNKNOWN_OPTION => "Unknown option '%s' => '%s' supplied.\n";
const our $USE_DESCRIPTION => 'use %s;%s';
const our $USE_IMPLEMENTATION => 'package %s; use %s%s; 1';
const our $VERSION_NUMBER => qr/^ \d+ (?: \. \d+ )* $/x;
const our %CONSTANTS_TO_EXPORT => (
const our $DIE => sub { die( sprintf( $_[ 0 ], @_[ 1 .. $#_ ] ) ) };

const our $EXCEPTION_PREFIX => 'BEGIN failed--compilation aborted at ';

const our $FALSE => 0;

const our $FMT_INVALID_DIRECTORY => "Invalid directory name / expression '%s' supplied with option '-lib'%s\n";
const our $FMT_INVALID_ENV_ENTRY => "Erroneous line %d of '%s' containing '%s': %s\n";
const our $FMT_INVALID_VALUE => "Option '%s' passed along with invalid value '%s'\n";
const our $FMT_KEEP_ENV_VAR => "Keep environment variable '%s' containing '%s'";
const our $FMT_NEW_FAILED => '%s->new died.%s';
const our $FMT_NEW_SUCCEEDED => "An object of class '%s' isa '%s'";
const our $FMT_REPLACEMENT => $EXCEPTION_PREFIX . '%s line %s.';
const our $FMT_REQUIRE_DESCRIPTION => 'require %s;%s';
const our $FMT_REQUIRE_IMPLEMENTATION => 'package %s; require %s';
const our $FMT_SEARCH_PATTERN => $EXCEPTION_PREFIX . '.*$';
const our $FMT_SET_ENV_VAR => "Set environment variable '%s' to '%s' from file '%s'";
const our $FMT_SET_TO => "Set %s to '%s'";
const our $FMT_UNKNOWN_OPTION => "Unknown option '%s' => '%s' supplied.\n";
const our $FMT_USE_DESCRIPTION => 'use %s;%s';
const our $FMT_USE_IMPLEMENTATION => 'package %s; use %s%s; 1';

const our $MSG_ERROR_WAS => ' Error was: ';
const our $MSG_UNEXPECTED_EXCEPTION => 'Unexpectedly caught exception: ';

const our $NOTE => sub { my ( $format, @args ) = @_; note( sprintf( $format, @args ) ) };

const our $REGEX_ANY_EXTENSION => qr/ \. [^.]+ $/x;
const our $REGEX_CLASS_HIERARCHY_LEVEL => qr/^( \w+ ) (?: :: ( .+ ) )?/x;
const our $REGEX_TOP_DIR_IN_PATH => qr{^ ( [^/]+ ) }x;
const our $REGEX_VERSION_NUMBER => qr/^ \d+ (?: \. \d+ )* $/x;

const our $TRUE => 1;

const our %CONSTANTS_TO_EXPORT => (
'$CLASS' => sub { $_[ 0 ] },
'$METHOD' => sub { $_[ 0 ] },
'$METHOD_REF' => sub { '\&' . $_[ 1 ] . '::' . svref_2object( $_[ 0 ] )->GV->NAME },
Expand Down
6 changes: 3 additions & 3 deletions t/Test/Expander/_error.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use warnings
FATAL => qw( all ),
NONFATAL => qw( deprecated exec internal malloc newline once portable redefine recursion uninitialized );

use Test::Expander::Constants qw( $ERROR_WAS );
use Test::Expander::Constants qw( $MSG_ERROR_WAS );
use constant TEST_CASES => {
'no exception' => { exception => '', args => [], output => '' },
'exception raised, no replacement required' => { exception => 'ABC', args => [], output => "${ ERROR_WAS }ABC" },
'exception raised, replacement required' => { exception => 'ABC', args => [ qw( B b ) ], output => "${ ERROR_WAS }AbC" },
'exception raised, no replacement required' => { exception => 'ABC', args => [], output => $MSG_ERROR_WAS . 'ABC' },
'exception raised, replacement required' => { exception => 'ABC', args => [ qw( B b ) ], output => $MSG_ERROR_WAS . 'AbC' },
};
use Test::Builder::Tester tests => scalar( keys( %{ TEST_CASES() } ) );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use warnings
FATAL => qw( all ),
NONFATAL => qw( deprecated exec internal malloc newline once portable redefine recursion uninitialized );

use Test::Expander::Constants qw( $NEW_FAILED $NEW_SUCCEEDED );
use Test::Expander::Constants qw( $FMT_NEW_FAILED $FMT_NEW_SUCCEEDED );
use constant TEST_CASES => {
"'new' succeeded" => { exception => '', output => $NEW_SUCCEEDED },
"'new' failed" => { exception => 'ABC', output => $NEW_FAILED },
"'new' succeeded" => { exception => '', output => $FMT_NEW_SUCCEEDED },
"'new' failed" => { exception => 'ABC', output => $FMT_NEW_FAILED },
};
use Test::Builder::Tester tests => scalar( keys( %{ TEST_CASES() } ) );

Expand All @@ -18,6 +18,6 @@ foreach my $title ( keys( %{ TEST_CASES() } ) ) {
test_out( "ok 1 - $title" );
$@ = TEST_CASES->{ $title }->{ exception };
my $expected = TEST_CASES->{ $title }->{ output } =~ s/%s/.*/gr;
like( Test::Expander::_newTestMessage( 'CLASS' ), qr/$expected/, $title );
like( Test::Expander::_new_test_message( 'CLASS' ), qr/$expected/, $title );
test_test( $title );
}
2 changes: 1 addition & 1 deletion t/Test/Expander/_setEnv.t → t/Test/Expander/_set_env.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use File::chdir;

use Test::Expander -tempdir => {}, -srand => time;

$METHOD //= '_setEnv';
$METHOD //= '_set_env';
$METHOD_REF //= $CLASS->can( $METHOD );
can_ok( $CLASS, $METHOD );

Expand Down
Loading

0 comments on commit 50d285d

Please sign in to comment.