1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#!perl
use Test::More tests => 5;
package foo;
use common::sense;
use base qw/Object::Event/;
sub test : event_cb {
my ($self, $a, $b) = @_;
$self->{res} = $a + $b;
}
sub reset { (shift)->{res} = 0 }
package main;
use common::sense;
my $f = foo->new;
$f->test (10, 20);
is ($f->{res}, 30, 'calling method simply works');
$f->reset;
my $id1 = $f->reg_cb (test => sub {
my ($self, $a, $b) = @_;
$self->{res} *= 2;
$self->{res} += $a + $b;
});
$f->test (10, 20);
is ($f->{res}, 90, 'simple chaining works');
$f->reset;
my $id2 = $f->reg_cb (before_test => sub {
my ($self, $a, $b) = @_;
$self->{res} = $a * $b;
});
$f->test (10, 20);
is ($f->{res}, 90, 'special events work');
$f->reset;
$f->unreg_cb ($id1);
$f->unreg_cb ($id2);
$f->test (20, 30);
is ($f->{res}, 50, 'unreg_cb works');
$f->reset;
$f->event (test => 30, 40);
is ($f->{res}, 70, 'calling event() works too');
|