Skip to content

Commit

Permalink
fix throws_ok properly considering type of expected value
Browse files Browse the repository at this point in the history
  • Loading branch information
jsf116 committed Mar 2, 2022
1 parent 4a20af7 commit 6f557a2
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 11 deletions.
9 changes: 6 additions & 3 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
Changelog for Test-Expander

1.0.6 2022-03-02 11:32:43 UTC
- throws_ok fixed properly considering the difference between RegEx and scalar as expected value.

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.
- 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.
- 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.
- Propagate $VERSION to the submodule.

1.0.2 2021-11-05 19:54:23 UTC
- Deactivate experimental features depending on Perl version.
Expand Down
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ t/Test/Expander/is_deeply.t
t/Test/Expander/lives_ok.t
t/Test/Expander/new_ok.t
t/Test/Expander/NoCLASS/NoMETHOD.t
t/Test/Expander/NoCLASS/NoMETHOD_only.t
t/Test/Expander/require_ok.t
t/Test/Expander/throws_ok.t
t/Test/Expander/use_ok.t
Expand Down
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ Please report any bugs or feature requests through the web interface at

# COPYRIGHT AND LICENSE

Copyright (c) 2021 Jurij Fajnberg
Copyright (c) 2021, 2022 Jurij Fajnberg

This program is free software; you can redistribute it and/or modify it under the same terms
as the Perl 5 programming language system itself.
8 changes: 6 additions & 2 deletions lib/Test/Expander.pm
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
## no critic (ProhibitStringyEval ProhibitSubroutinePrototypes RequireLocalizedPunctuationVars)
package Test::Expander;

our $VERSION = '1.0.5'; ## no critic (RequireUseStrict, RequireUseWarnings)
# The versioning is conform with https://semver.org
our $VERSION = '1.0.6'; ## no critic (RequireUseStrict, RequireUseWarnings)

use v5.14;
use warnings
Expand Down Expand Up @@ -175,8 +176,11 @@ sub throws_ok (&$;$) {
my ($coderef, $expecting, $description) = @_;

eval { $coderef->() };
my $exception = $@;
my $expectedType = ref($expecting);

return like($@, qr/$expecting/, $description);
return $expectedType eq 'Regexp' ? like ($exception, $expecting, $description)
: isa_ok($exception, [ $expecting ], $description);
}

sub use_ok ($;@) {
Expand Down
9 changes: 7 additions & 2 deletions lib/Test/Expander/Constants.pm
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
## no critic (RequireVersionVar)
package Test::Expander::Constants;

our $VERSION = '1.0.6'; ## no critic (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 Exporter qw(import);
use PadWalker qw(peek_our);
use Scalar::Readonly qw(readonly_on);

readonly_on($VERSION);

const our $ANY_EXTENSION => qr/ \. [^.]+ $/x;
const our $CLASS_HIERARCHY_LEVEL => qr/^( \w+ ) (?: :: ( .+ ) )?/x;
Expand Down
14 changes: 11 additions & 3 deletions t/Test/Expander/throws_ok.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ use warnings
FATAL => qw(all),
NONFATAL => qw(deprecated exec internal malloc newline once portable redefine recursion uninitialized);

use Test::Builder::Tester tests => 1;
use Test::Builder::Tester tests => 2;

use Test::Expander;

my $title = 'execution';
my ($expected, $title);

$title = 'RegEx expected (stringified exception comparison)';
test_out("ok 1 - $title");
my $expected = 'DIE TEST';
$expected = qr/DIE TEST/;
throws_ok(sub { die($expected) }, $expected, $title);
test_test($title);

$title = 'scalar expected (exception class comparison)';
test_out("ok 1 - $title");
$expected = 'DIE_TEST';
throws_ok(sub { die(bless({}, $expected)) }, $expected, $title);
test_test($title);

0 comments on commit 6f557a2

Please sign in to comment.