Skip to content

Commit

Permalink
also handle unblessed subrefs - partially for now
Browse files Browse the repository at this point in the history
  • Loading branch information
karenetheridge committed Feb 25, 2013
1 parent 2edab74 commit e8ca9cc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
7 changes: 5 additions & 2 deletions lib/Test/Deep/Type.pm
Expand Up @@ -31,7 +31,10 @@ sub descend
sub diag_message
{
my ($self, $where) = @_;
return "Validating $where as a " . $self->_type_name($self->{type}) . ' type';
my $name = $self->_type_name($self->{type});
return "Validating $where as a"
. (defined $name ? ' ' . $name : 'n unknown')
. ' type';
}

# we do not define a diagnostics sub, so we get the one produced by deep_diag
Expand Down Expand Up @@ -82,7 +85,7 @@ sub _type_name
return $class if defined $class;

# plain old subref perhaps?
return 'unknown type';
return;
}

1;
Expand Down
35 changes: 33 additions & 2 deletions t/01-basic.t
Expand Up @@ -2,7 +2,7 @@ use strict;
use warnings FATAL => 'all';

use Test::Tester 0.108;
use Test::More tests => 33;
use Test::More tests => 47;
use Test::NoWarnings 1.04 ':early';
use Test::Deep;
use Test::Deep::Type;
Expand Down Expand Up @@ -41,18 +41,35 @@ ok(TypeHiLite->('hi'), 'validation succeeds (no error)');
ok(!TypeHiLite->('hello'), 'validation fails with a simple bool');


# the next type is a plain old unblessed coderef, returning a simple boolean
# "did this validate"
sub TypeHiTiny
{
sub {
my $val = shift;
return if not defined $val;
return 1 if $val eq 'hi'; # validated: no error
return;
};
}

ok(TypeHiTiny->('hi'), 'validation succeeds (no error)');
ok(!TypeHiTiny->('hello'), 'validation fails with a simple bool');


check_tests(
sub {
cmp_deeply({ greeting => 'hi' }, { greeting => is_type(TypeHi) }, 'hi validates as a TypeHi');
cmp_deeply({ greeting => 'hi' }, { greeting => is_type(TypeHiLite) }, 'hi validates as a TypeHiLite');
cmp_deeply({ greeting => 'hi' }, { greeting => is_type(TypeHiTiny) }, 'hi validates as a TypeHiTiny');
},
[ map { +{
actual_ok => 1,
ok => 1,
diag => '',
name => "hi validates as a $_",
type => '',
} } qw(TypeHi TypeHiLite) ],
} } qw(TypeHi TypeHiLite TypeHiTiny) ],
'validation successful',
);

Expand All @@ -61,6 +78,9 @@ my ($premature, @results) = run_tests(
sub {
cmp_deeply({ greeting => 'hello' }, { greeting => is_type(TypeHi) }, 'hello validates as a TypeHi?');
cmp_deeply({ greeting => 'hello' }, { greeting => is_type(TypeHiLite) }, 'hello validates as a TypeHiLite?');
cmp_deeply({ greeting => 'hello' }, { greeting => is_type(TypeHiTiny) }, 'hello validates as a TypeHiTiny?');
# TODO
# cmp_deeply({ greeting => 'hello' }, { greeting => is_type('not a ref!') }, 'hello validates against an arbitrary subref?');
},
);

Expand All @@ -87,6 +107,17 @@ EOM
Validating \$data->{"greeting"} as a TypeHiLite type
got : failed
expect : no error
EOM
},
{
actual_ok => 0,
ok => 0,
name => "hello validates as a TypeHiTiny?",
type => '',
diag => <<EOM,
Validating \$data->{"greeting"} as an unknown type
got : failed
expect : no error
EOM
},
],
Expand Down

0 comments on commit e8ca9cc

Please sign in to comment.