MooseX-OmniTrigger
view release on metacpan or search on metacpan
t/002-native.t view on Meta::CPAN
has foo_array => (is => 'rw', traits => ['Array'], default => sub { [] }, omnitrigger => \&_capture_changes, handles => {
foo_array_push => 'push' ,
foo_array_pop => 'pop' ,
foo_array_unshift => 'unshift',
foo_array_shift => 'shift' ,
foo_array_splice => 'splice' ,
foo_array_set => 'set' ,
foo_array_delete => 'delete' ,
foo_array_insert => 'insert' ,
foo_array_clear => 'clear' ,
});
has foo_bool => (is => 'rw', traits => ['Bool'], default => 0, omnitrigger => \&_capture_changes, handles => {
foo_bool_set => 'set' ,
foo_bool_unset => 'unset' ,
foo_bool_toggle => 'toggle',
});
has foo_code => (is => 'rw', traits => ['Code'], default => sub { sub { 1 } }, omnitrigger => \&_capture_changes);
has foo_counter => (is => 'rw', traits => ['Counter'], default => 0, omnitrigger => \&_capture_changes, handles => {
foo_counter_set => 'set' ,
foo_counter_inc => 'inc' ,
foo_counter_dec => 'dec' ,
foo_counter_reset => 'reset',
});
has foo_hash => (is => 'rw', traits => ['Hash'], default => sub { {} }, omnitrigger => \&_capture_changes, handles => {
foo_hash_set => 'set' ,
foo_hash_delete => 'delete',
foo_hash_clear => 'clear' ,
});
has foo_number => (is => 'rw', traits => ['Number'], default => 42, omnitrigger => \&_capture_changes, handles => {
foo_number_set => 'set',
foo_number_add => 'add',
foo_number_sub => 'sub',
foo_number_mul => 'mul',
foo_number_div => 'div',
foo_number_mod => 'mod',
foo_number_abs => 'abs',
});
has foo_string => (is => 'rw', traits => ['String'], default => 'ABCDE', omnitrigger => \&_capture_changes, handles => {
foo_string_inc => 'inc' ,
foo_string_append => 'append' ,
foo_string_prepend => 'prepend',
foo_string_replace => 'replace',
foo_string_chop => 'chop' ,
foo_string_chomp => 'chomp' ,
foo_string_clear => 'clear' ,
foo_string_substr => 'substr' ,
});
has changes => (is => 'ro', isa => 'HashRef', default => sub { {} });
sub _capture_changes {
my ($self, $attr_name, $new, $old) = (shift, @_);
$self->changes->{$attr_name} = [
@$old ? $old->[0] : 'NOVAL',
@$new ? $new->[0] : 'NOVAL',
];
}
END
eval(<<"END");
{ package MyClass1; use Moose; use MooseX::OmniTrigger; $stuff; }
{ package MyRole1 ; use Moose::Role; use MooseX::OmniTrigger; $stuff; }
{ package MyRole2 ; use Moose::Role; with 'MyRole1'; }
{ package MyClass2; use Moose ; with 'MyRole2'; }
END
{
my $x = $@;
is(exception { die($x) if $x }, undef, 'nothing blew up') or done_testing and exit;
}
CLASS: for my $class (qw(MyClass1 MyClass2)) {
TEST: {
print("# $class ", $class->meta->is_mutable ? 'MUTABLE' : 'IMMUTABLE', "\n");
my $obj;
is(exception { $obj = $class->new }, undef, 'nothing blew up') or next CLASS;
# Array
$obj->foo_array_push(1, 2, 3);
is_deeply($obj->changes->{foo_array}, [[] => [1, 2, 3]], 'omnitrigger fires for Array.push; oldval is shallow clone');
$obj->foo_array_pop;
is_deeply($obj->changes->{foo_array}, [[1, 2, 3] => [1, 2]], 'omnitrigger fires for Array.pop; oldval is shallow clone');
$obj->foo_array_unshift(0);
is_deeply($obj->changes->{foo_array}, [[1, 2] => [0, 1, 2]], 'omnitrigger fires for Array.unshift; oldval is shallow clone');
$obj->foo_array_shift;
is_deeply($obj->changes->{foo_array}, [[0, 1, 2] => [1, 2]], 'omnitrigger fires for Array.shift; oldval is shallow clone');
$obj->foo_array_splice(1, 0, 1.5);
is_deeply($obj->changes->{foo_array}, [[1, 2] => [1, 1.5, 2]], 'omnitrigger fires for Array.splice; oldval is shallow clone');
$obj->foo_array_set(0, 'ONE');
( run in 0.569 second using v1.01-cache-2.11-cpan-ceb78f64989 )