Skip to content

Commit

Permalink
add support for testing arbitrary attribute attributes
Browse files Browse the repository at this point in the history
...eg those added to an attribute metaclass by way of an attribute trait.
  • Loading branch information
rsrchboy committed Oct 28, 2012
1 parent 58e6686 commit 3140ba4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
12 changes: 10 additions & 2 deletions lib/Test/Moose/More.pm
Expand Up @@ -526,8 +526,16 @@ sub _attribute_options_ok {
;
}

fail "unknown attribute option: $_"
for sort keys %opts;
for my $opt (sort keys %opts) {

do { fail "unknown attribute option: $opt"; next }
unless $att->meta->find_attribute_by_name($opt);

$check->($opt);
}

#fail "unknown attribute option: $_"
#for sort keys %opts;

return;
}
Expand Down
33 changes: 27 additions & 6 deletions t/validate_attribute.t
Expand Up @@ -6,21 +6,27 @@ use Test::More;
use Test::Moose::More;
use TAP::SimpleOutput 'counters';

{ package TestRole; use Moose::Role; use namespace::autoclean; }
{
package TestRole;
use Moose::Role;
use namespace::autoclean;

has thinger => (is => 'ro', predicate => 'has_thinger');
}
{
package TestClass;

use Moose;
use namespace::autoclean;

has foo => (
traits => [ 'TestRole' ],
is => 'ro',
isa => 'Int',
traits => [ 'TestRole' ],
is => 'ro',
isa => 'Int',
builder => '_build_foo',
lazy => 1,
lazy => 1,
thinger => 'foo',
);

}

# initial tests, covering the most straight-forward cases (IMHO)
Expand All @@ -46,6 +52,10 @@ note 'validate attribute validation';
test_out $_ok->('foo has a init_arg');
test_out $_ok->('foo option init_arg correct');
test_out $_ok->('foo is lazy');
test_out $_nok->('unknown attribute option: binger');
test_fail 3;
test_out $_ok->('foo has a thinger');
test_out $_ok->('foo option thinger correct');
validate_attribute TestClass => foo => (
-does => [ 'TestRole' ],
-isa => [ 'Moose::Meta::Attribute' ],
Expand All @@ -58,13 +68,16 @@ note 'validate attribute validation';
default => undef,
init_arg => 'foo',
lazy => 1,
thinger => 'foo',
binger => 'bar',
);
test_test 'validate_attribute works correctly';
}


subtest 'a standalone run of validate_attribute' => sub {

note 'of necessity, these exclude the "failing" tests';
validate_attribute TestClass => foo => (
-does => [ 'TestRole' ],
-isa => [ 'Moose::Meta::Attribute' ],
Expand All @@ -77,6 +90,7 @@ subtest 'a standalone run of validate_attribute' => sub {
default => undef,
init_arg => 'foo',
lazy => 1,
thinger => 'foo',
);
};

Expand All @@ -97,6 +111,10 @@ note 'attribute_options_ok validation';
test_out $_ok->('foo has a init_arg');
test_out $_ok->('foo option init_arg correct');
test_out $_ok->('foo is lazy');
test_out $_nok->('unknown attribute option: binger');
test_fail 3;
test_out $_ok->('foo has a thinger');
test_out $_ok->('foo option thinger correct');
attribute_options_ok TestClass => foo => (
traits => [ 'TestRole' ],
isa => 'Int',
Expand All @@ -107,12 +125,15 @@ note 'attribute_options_ok validation';
default => undef,
init_arg => 'foo',
lazy => 1,
thinger => 'foo',
binger => 'bar',
);
test_test 'attribute_options_ok works as expected';
}

subtest 'a standalone run of attribute_options_ok' => sub {

note 'of necessity, these exclude the "failing" tests';
attribute_options_ok TestClass => foo => (
traits => [ 'TestRole' ],
isa => 'Int',
Expand Down

0 comments on commit 3140ba4

Please sign in to comment.