Skip to content
Newer
Older
100644 97 lines (79 sloc) 1.83 KB
e0d6b99 @rcaputo Explicitly export watches() and emits() from their corresponding Refl…
authored
1 # vim: ts=2 sw=2 noexpandtab
4a78509 @nperez Implement Reflex::Sender and basic test for it.
nperez authored
2 use warnings;
3 use strict;
4 use Test::More;
5 use Reflex;
6
7 {
8 package Contained;
9 use Moose;
10 extends 'Reflex::Base';
11
12 sub foo {
5c57d49 @rcaputo !!! Experiment with Reflex events being objects.
authored
13 my ($self, %args) = @_;
14 $self->emit( -name => 'foo_event', %args );
4a78509 @nperez Implement Reflex::Sender and basic test for it.
nperez authored
15 }
16 }
17
18 {
19 package Container;
20 use Moose;
21 use Reflex::Callbacks('cb_method');
22 extends 'Reflex::Base';
23
24 has things => (is => 'ro', isa => 'HashRef', default => sub { {} });
25
26 sub store {
27 my ($self, $thing) = @_;
28 $self->watch($thing, 'foo_event' => cb_method($self, 'foo_event'));
29 $self->things->{$thing} = $thing;
30 }
31
32 sub remove {
33 my ($self, $thing) = @_;
34 $self->ignore($thing);
35 delete($self->things->{$thing});
36 }
37
38 sub foo_event {
5c57d49 @rcaputo !!! Experiment with Reflex events being objects.
authored
39 my ($self, $event) = @_;
40 $self->re_emit( $event, -name => 'foo_event' );
41 $self->remove($event->get_last_emitter());
4a78509 @nperez Implement Reflex::Sender and basic test for it.
nperez authored
42 }
43 }
44
45 {
46 package Harness;
47 use Moose;
48 use Reflex::Callbacks('cb_method');
49 extends 'Reflex::Base';
50
51 has container => (
52 is => 'ro',
53 isa => 'Container',
54 lazy => 1,
55 builder => '_build_container'
56 );
57
58 sub _build_container {
59 my $self = shift;
60 my $cont = Container->new();
61 for (0 .. 9) {
62 $cont->store(Contained->new());
63 }
64
65 $self->watch($cont, 'foo_event' => cb_method($self, 'foo_handler'));
66
67 return $cont;
68 }
69
70 my $baz = 0;
71
72 sub foo_handler {
5c57d49 @rcaputo !!! Experiment with Reflex events being objects.
authored
73 my ($self, $event) = @_;
74 my $source = $event->get_first_emitter();
4a78509 @nperez Implement Reflex::Sender and basic test for it.
nperez authored
75 Test::More::isa_ok($source, 'Contained', 'got the source of the event');
5c57d49 @rcaputo !!! Experiment with Reflex events being objects.
authored
76 my $last = $event->get_last_emitter();
4a78509 @nperez Implement Reflex::Sender and basic test for it.
nperez authored
77 Test::More::isa_ok(
78 $last, 'Container',
79 'got the final emitter in the stack'
80 );
5c57d49 @rcaputo !!! Experiment with Reflex events being objects.
authored
81 my @all = $event->get_all_emitters();
4a78509 @nperez Implement Reflex::Sender and basic test for it.
nperez authored
82 Test::More::is(scalar(@all), 2, 'got the right number of emitters');
83
84 if ($baz++ == 9) {
85 $self->ignore($self->container);
86 }
87 }
88 }
89
90 my $harness = Harness->new();
91 foreach my $thing (values %{$harness->container->things}) {
92 $thing->foo();
93 }
94
95 $harness->run_all();
96 done_testing();
Something went wrong with that request. Please try again.