Skip to content

Commit

Permalink
Fix tests to run on <5.010
Browse files Browse the repository at this point in the history
  • Loading branch information
cxw42 committed Mar 24, 2019
1 parent 6df34e7 commit 4d06745
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 12 deletions.
3 changes: 2 additions & 1 deletion Changes
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Revision history for Class-Tiny-ConstrainedAccessor

0.000005 2019-??-??
0.000005 2019-03-24 TRIAL
- See changes for v0.000004, including checking constructor parameters.
- Fix tests to run on <5.010

0.000004 2019-03-22
- Check constructor parameters (GH-4)
Expand Down
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ t/MY/Class/TypeTiny.pm
t/MY/Class/TypeTinyBadDefaults.pm
t/MY/Class/TypeTinyBUILD.pm
t/MY/Class/TypeTinyNOBUILD.pm
t/MY/Helpers.pm
t/MY/Kit.pm
t/MY/Tests.pm
t/MY/TypeLib/MooXTypesMooseLike.pm
Expand Down
2 changes: 2 additions & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,10 @@ my %opts = (
'Path::Class' => '0',
},
TEST_REQUIRES => {
'Exporter' => '0',
'Import::Into' => '0',
'lib::relative' => '0.002',
'parent' => '0',
'Test::Builder' => '0',
'Test::Exception' => '0',
'Test::Fatal' => '0', # TODO replace uses of this with T~::Ex~
Expand Down
2 changes: 1 addition & 1 deletion lib/Class/Tiny/ConstrainedAccessor.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use strict;
use warnings;
use Class::Tiny;

our $VERSION = '0.000005';
our $VERSION = '0.000005'; # TRIAL

# Docs {{{1

Expand Down
4 changes: 3 additions & 1 deletion t/MY/Class/MooseXTypes.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package MY::Class::MooseXTypes;
use 5.006;
use strict;
use warnings;
use MY::Helpers;

our @ISA;
use Scalar::Util qw(looks_like_number);

Expand All @@ -18,7 +20,7 @@ BEGIN {
subtype MediumInteger,
as Int,
where { $_ >= 10 and $_ < 20 },
message { ($_ // 'undef') . ' is not an integer on [10,19]' };
message { _dor . ' is not an integer on [10,19]' };

# Sanity check
my $av = eval { MediumInteger->can('assert_valid') };
Expand Down
17 changes: 17 additions & 0 deletions t/MY/Helpers.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package MY::Helpers;
use 5.006;
use strict;
use warnings;

use parent 'Exporter';
our @EXPORT=qw(_dor);

# Helper since <5.010 doesn't have `//`
sub _dor {
my $x = @_ ? $_[0] : $_;
defined $x ? $x :
(defined $_[1] ? $_[1] : 'undef')
} #_dor()

1;

17 changes: 9 additions & 8 deletions t/MY/Tests.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use warnings;
use Test::More;
use Test::Exception;
use Test::Fatal;
use MY::Helpers;

=head1 FUNCTIONS
Expand Down Expand Up @@ -55,46 +56,46 @@ sub test_accessors {
is(
exception { $dut->regular($_) },
undef,
'Regular accepts ' . ($_ // 'undef')
'Regular accepts ' . _dor
) foreach (0, 9, 10, 19, 20, 'some string', undef, \*STDOUT);

# The constrained accessors accept 10..19
is(
exception { $dut->medint($_) },
undef,
'medint accepts ' . ($_ // 'undef')
'medint accepts ' . _dor
) foreach (10..19, "10".."19");

is(
exception { $dut->med_with_default($_) },
undef,
'med_with_default accepts ' . ($_ // 'undef')
'med_with_default accepts ' . _dor
) foreach (10..19, "10".."19");

# The constrained accessors reject numbers outside that range
like(
exception { $dut->medint($_) },
qr/./,
'medint rejects ' . ($_ // 'undef')
'medint rejects ' . _dor
) foreach (0..9, "0".."9", 20..29, "20".."29");

like(
exception { $dut->med_with_default($_) },
qr/./,
'med_with_default rejects ' . ($_ // 'undef')
'med_with_default rejects ' . _dor
) foreach (0..9, "0".."9", 20..29, "20".."29");

# The constrained accessors reject random stuff
like(
exception { $dut->medint($_) },
qr/./,
'medint rejects ' . ($_ // 'undef')
'medint rejects ' . _dor
) foreach ('some string', undef, \*STDOUT);

like(
exception { $dut->med_with_default($_) },
qr/./,
'med_with_default rejects ' . ($_ // 'undef')
'med_with_default rejects ' . _dor
) foreach ('some string', undef, \*STDOUT);
} #test_accessors()

Expand All @@ -113,7 +114,7 @@ in the specified class). The coderef is for flexibility.
sub test_construction {
my $class = shift;
die "Need a class name" unless $class;
my $factory = shift // sub { $class->new(@_) };
my $factory = shift || sub { $class->new(@_) };

# Sanity check: parameters OK
my $obj = $factory->(regular=>1, medint=>10);
Expand Down
3 changes: 2 additions & 1 deletion t/MY/TypeLib/MouseX.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package MY::TypeLib::MouseX;
use 5.006;
use strict;
use warnings;
use MY::Helpers;
# within Mouse, so we put it in a separate package from SampleMouseXTypes.

use Scalar::Util qw(looks_like_number);
Expand All @@ -18,7 +19,7 @@ BEGIN {
subtype MediumInteger,
as Int,
where { $_ >= 10 and $_ < 20 },
message { ($_ // 'undef') . ' is not an integer on [10,19]' };
message { _dor . ' is not an integer on [10,19]' };

# Sanity check
my $av = eval { MediumInteger->can('assert_valid') };
Expand Down

0 comments on commit 4d06745

Please sign in to comment.