Skip to content

Commit

Permalink
skip unit tests of $METHOD / $METHOD_REF if these variables are undef…
Browse files Browse the repository at this point in the history
…ined
  • Loading branch information
jsf116 committed Nov 11, 2021
1 parent 16f3292 commit 4a20af7
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 21 deletions.
18 changes: 13 additions & 5 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
Changelog for Test-Expander

1.0.3 2021-07-11 14:39:27 UTC
1.0.5 2021-11-11 20:45:16 UTC
- Skip unit tests of $METHOD / $METHOD_REF if these variables are undefined.
- Improve logging of $METHOD_REF.

1.0.4 2021-11-10 19:54:36 UTC
- Make unit tests independent from directory structure (avoid automated determination of $METHOD / $METHOD_REF).
- Log exported and environment variables to STDOUT after their setup.

1.0.3 2021-11-07 14:39:27 UTC
- Use 'IO::Select' instead of self-implemented dummy class for testing.
- Propagate $VERSION to the submodule.

1.0.2 2021-05-11 19:54:23 UTC
- Deativate experimental features depending on Perl version.
1.0.2 2021-11-05 19:54:23 UTC
- Deactivate experimental features depending on Perl version.

1.0.1 2021-04-11 16:42:38 UTC
1.0.1 2021-11-04 16:42:38 UTC
- Test configuration fixed.
- Documentation improved.

1.0.0 2021-01-11 20:13:45 UTC
1.0.0 2021-11-01 20:13:45 UTC
- Initial release
2 changes: 2 additions & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ WriteMakefile(
MIN_PERL_VERSION => '5.014',
NAME => 'Test::Expander',
PREREQ_PM => {
'B' => 0,
'Const::Fast' => 0,
'Exporter' => 0,
'File::chdir' => 0,
Expand All @@ -25,6 +26,7 @@ WriteMakefile(
'Test::Files' => 0,
'Test::Output' => 0,
'Test::Warn' => 0,
'Test2::Tools::Basic' => 0,
'Test2::Tools::Explain' => 0,
'Test2::V0' => 0,
},
Expand Down
4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ Its content is interpreted as follows:
NAME_VAR = $KNIB::App::MyApp::Constants::ABC
NAME_FUNC = join(' ', $KNIB::App::MyApp::Constants::DEF)

All environment variables set up in this manner are logged to STDOUT
using [note](https://metacpan.org/pod/Test2::Tools::Basic#DIAGNOSTICS).

Another common feature within test suites is the creation of a temporary directory / file used as an
isolated container for some testing actions.
The module options **-tempdir** and **-tempfile** are fully syntactically compatible with
Expand Down Expand Up @@ -178,6 +181,7 @@ if the option **-tempdir** was supplied.
if the option **-tempfile** was supplied.

All variables mentioned above are read-only if they are defined after **use Test::Expander ...**.
In this case they are logged to STDOUT using [note](https://metacpan.org/pod/Test2::Tools::Basic#DIAGNOSTICS).

# AUTHOR

Expand Down
37 changes: 28 additions & 9 deletions lib/Test/Expander.pm
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
## no critic (ProhibitStringyEval ProhibitSubroutinePrototypes RequireLocalizedPunctuationVars)
package Test::Expander;

our $VERSION = '1.0.3'; ## no critic (RequireUseStrict, RequireUseWarnings)
our $VERSION = '1.0.5'; ## no critic (RequireUseStrict, RequireUseWarnings)

use v5.14;
use warnings
FATAL => qw(all),
NONFATAL => qw(deprecated exec internal malloc newline portable recursion);
no if ($] >= 5.018), warnings => 'experimental';

use B qw(svref_2object);
use Const::Fast;
use File::chdir;
use File::Temp qw(tempdir tempfile);
Expand All @@ -18,6 +19,7 @@ use Scalar::Readonly qw(readonly_on);
use Test::Files;
use Test::Output;
use Test::Warn;
use Test2::Tools::Basic;
use Test2::Tools::Explain;
use Test2::V0 ();

Expand Down Expand Up @@ -105,14 +107,30 @@ sub import {
_setEnv($METHOD, $options{-target}, $testFile);

Test2::V0->import(%options);
$METHOD_REF = '-target' ~~ %options ? $CLASS->can($METHOD) : undef;
$METHOD = undef unless($METHOD_REF);

readonly_on($CLASS) if $CLASS;
readonly_on($METHOD) if $METHOD;
readonly_on($METHOD_REF) if $METHOD_REF;
readonly_on($TEMP_DIR) if $TEMP_DIR;
readonly_on($TEMP_FILE) if $TEMP_FILE;
if ($CLASS) {
readonly_on($CLASS);
note(q(Set $CLASS to '), $CLASS, q('));

$METHOD_REF = $CLASS->can($METHOD);
$METHOD = undef unless($METHOD_REF);
}
if ($METHOD) {
readonly_on($METHOD);
note(q(Set $METHOD to '), $METHOD, q('));
}
if ($METHOD_REF) {
readonly_on($METHOD_REF);
note(q(Set $METHOD_REF to '), $METHOD_REF, ' -> &', $CLASS, '::', svref_2object($METHOD_REF)->GV->NAME, q('));
}
if ($TEMP_DIR) {
readonly_on($TEMP_DIR);
note(q(Set $TEMP_DIR to '), $TEMP_DIR, q('));
}
if ($TEMP_FILE) {
readonly_on($TEMP_FILE);
note(q(Set $TEMP_FILE to '), $TEMP_FILE, q('));
}

Importer->import_into($class, scalar(caller), ());

Expand Down Expand Up @@ -201,6 +219,7 @@ sub _readEnvFile {
next unless $line =~ /^ (?<name> \w+) \s* = \s* (?<value> \S .*)/x;
$env{$+{name}} = eval($+{value});
die(sprintf($INVALID_ENV_ENTRY, $index, $envFile, $line, $@)) if $@;
note(q(Set environment variable '), $+{name}, q(' to '), $env{$+{name}}, q(' from file '), $envFile, q('));
}

return \%env;
Expand All @@ -221,7 +240,7 @@ sub _setEnv {
if (path($envFile)->is_file) {
$envFound = $TRUE unless $envFound;
my $methodEnv = _readEnvFile($envFile);
@$newEnv{keys(%$methodEnv)} = values(%$methodEnv)
@$newEnv{keys(%$methodEnv)} = values(%$methodEnv);
}

%ENV = %$newEnv if $envFound;
Expand Down
4 changes: 4 additions & 0 deletions lib/Test/Expander.pod
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ variables and subroutines must not be quoted:

=back

All environment variables set up in this manner are logged to STDOUT
using L<note|https://metacpan.org/pod/Test2::Tools::Basic#DIAGNOSTICS>.

Another common feature within test suites is the creation of a temporary directory / file used as an
isolated container for some testing actions.
The module options B<-tempdir> and B<-tempfile> are fully syntactically compatible with
Expand Down Expand Up @@ -353,6 +356,7 @@ if the option B<-tempfile> was supplied.
=back

All variables mentioned above are read-only if they are defined after B<use Test::Expander ...>.
In this case they are logged to STDOUT using L<note|https://metacpan.org/pod/Test2::Tools::Basic#DIAGNOSTICS>.

=head1 AUTHOR

Expand Down
10 changes: 3 additions & 7 deletions lib/Test/Expander/Constants.pm
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
## no critic (RequireVersionVar)
package Test::Expander::Constants;

our $VERSION = $Test::Expander::VERSION; ## no critic (RequireConstantVersion, RequireUseStrict, RequireUseWarnings)

use v5.14;
use warnings
FATAL => qw(all),
NONFATAL => qw(deprecated exec internal malloc newline portable recursion);

use Const::Fast;
use Exporter qw(import);
use PadWalker qw(peek_our);
use Scalar::Readonly qw(readonly_on);

readonly_on($VERSION);
use Exporter qw(import);
use PadWalker qw(peek_our);

const our $ANY_EXTENSION => qr/ \. [^.]+ $/x;
const our $CLASS_HIERARCHY_LEVEL => qr/^( \w+ ) (?: :: ( .+ ) )?/x;
Expand Down
2 changes: 2 additions & 0 deletions t/Test/Expander/_setEnv.t
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use File::chdir;

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

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

ok(-d $TEMP_DIR, "temporary directory '$TEMP_DIR' created");
Expand Down
1 change: 1 addition & 0 deletions t/Test/Expander/compare_ok.t
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use Test::Builder::Tester tests => 1;

use Test::Expander;

$METHOD //= 'compare_ok';
my $dir = path(__FILE__)->parent->child($METHOD);
my $title = 'execution';
test_out("ok 1 - $title");
Expand Down
3 changes: 3 additions & 0 deletions t/Test/Expander/import.t
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ use Test::Expander -target => 'Test::Expander',
-tempfile => { UNLINK => 1 };
use Test::Expander::Constants qw($INVALID_VALUE $UNKNOWN_OPTION);

$METHOD //= 'import';
$METHOD_REF //= sub {};

foreach my $function (sort @functions) {
my $title = "$CLASS->can('$function')";
test_out("ok 1 - $title");
Expand Down

0 comments on commit 4a20af7

Please sign in to comment.