From: Dave Rolsky Date: Sat, 25 Sep 2010 19:09:13 +0000 (-0500) Subject: Improve tests for Code trait X-Git-Tag: 1.15~96 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7a9e42d00b95b34089276e209b7ac186a6c363cb;p=gitmo%2FMoose.git Improve tests for Code trait --- diff --git a/t/070_native_traits/007_trait_code.t b/t/070_native_traits/007_trait_code.t index 8969d72..ebaf17b 100644 --- a/t/070_native_traits/007_trait_code.t +++ b/t/070_native_traits/007_trait_code.t @@ -1,48 +1,105 @@ use strict; use warnings; +use Moose (); use Test::More; +use Test::Exception; +use Test::Moose; { - package Thingy; - use Moose; - - has callback => ( - traits => ['Code'], - isa => 'CodeRef', - required => 1, - handles => { 'invoke_callback' => 'execute' }, - ); + my $name = 'Foo1'; + + sub build_class { + my ( $attr1, $attr2, $attr3 ) = @_; + + my $class = Moose::Meta::Class->create( + $name++, + superclasses => ['Moose::Object'], + ); + + $class->add_attribute( + callback => ( + traits => ['Code'], + isa => 'CodeRef', + required => 1, + handles => { 'invoke_callback' => 'execute' }, + %{ $attr1 || {} }, + ) + ); + + $class->add_attribute( + callback_method => ( + traits => ['Code'], + isa => 'CodeRef', + required => 1, + handles => { 'invoke_method_callback' => 'execute_method' }, + %{ $attr2 || {} }, + ) + ); + + $class->add_attribute( + multiplier => ( + traits => ['Code'], + isa => 'CodeRef', + required => 1, + handles => { 'multiply' => 'execute' }, + %{ $attr3 || {} }, + ) + ); + + return $class->name; + } +} + +{ + my $i; - has callback_method => ( - traits => ['Code'], - isa => 'CodeRef', - required => 1, - handles => { 'invoke_method_callback' => 'execute_method' }, + my %subs = ( + callback => sub { ++$i }, + callback_method => sub { shift->multiply(@_) }, + multiplier => sub { $_[0] * 2 }, ); - has multiplier => ( - traits => ['Code'], - isa => 'CodeRef', - required => 1, - handles => { 'multiply' => 'execute' }, + run_tests( build_class, \$i, \%subs ); + + run_tests( + build_class( + { + lazy => 1, default => sub { $subs{callback} } + }, { + lazy => 1, default => sub { $subs{callback_method} } + }, { + lazy => 1, default => sub { $subs{multiplier} } + }, + ), + \$i, ); } -my $i = 0; -my $thingy = Thingy->new( - callback => sub { ++$i }, - multiplier => sub { $_[0] * 2 }, - callback_method => sub { shift->multiply(@_) }, -); - -is($i, 0); -$thingy->invoke_callback; -is($i, 1); -is($thingy->multiply(3), 6); -is($thingy->invoke_method_callback(3), 6); - -ok(!$thingy->can($_), "Code trait didn't create reader method for $_") - for qw(callback callback_method multiplier); +sub run_tests { + my ( $class, $iref, @args ) = @_; + + ok( + !$class->can($_), + "Code trait didn't create reader method for $_" + ) for qw(callback callback_method multiplier); + + with_immutable { + ${$iref} = 0; + my $obj = $class->new(@args); + + $obj->invoke_callback; + + is( ${$iref}, 1, '$i is 1 after invoke_callback' ); + + is( + $obj->invoke_method_callback(3), 6, + 'invoke_method_callback calls multiply with @_' + ); + + is( $obj->multiply(3), 6, 'multiple double value' ); + } + $class; +} done_testing;