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;