Permalink
Browse files

Convert the callback examples into tests.

  • Loading branch information...
1 parent c00d096 commit 0b515ca08383f66e6539d2227aaa8487e57037b9 @rcaputo committed Jun 16, 2010
View
@@ -31,15 +31,16 @@
use strict;
use lib qw(../lib);
-use ExampleHelpers qw(eg_say);
+use Test::More tests => 4;
+
use Reflex::Callbacks qw(cb_coderef);
use ThingWithCallbacks;
# Create a thing that will invoke callbacks. This syntax uses
# contextually specified coderef callbacks.
my $thing_one = ThingWithCallbacks->new(
- on_event => sub { eg_say("contextual callback invoked") },
+ on_event => sub { pass("contextual callback invoked") },
);
$thing_one->run();
@@ -48,17 +49,18 @@
# verbosity.
my $thing_two = ThingWithCallbacks->new(
- on_event => cb_coderef(sub { eg_say("explicit callback invoked") }),
+ on_event => cb_coderef(sub { pass("explicit callback invoked") }),
);
$thing_two->run();
# cb_coderef is prototyped so it can replace "sub".
my $thing_three = ThingWithCallbacks->new(
- on_event => cb_coderef { eg_say("explicit callback (no sub) invoked") },
+ on_event => cb_coderef { pass("explicit callback (no sub) invoked") },
);
$thing_three->run();
+pass("object ran to completion");
exit;
View
@@ -13,6 +13,8 @@
use strict;
use lib qw(../lib);
+use Test::More tests => 2;
+
# Create a thing that will invoke callbacks. This syntax uses
# explicitly specified cb_method() callbacks. There is no
# nonambiguous implicit syntax at this time. Suggestions are welcome.
@@ -21,7 +23,6 @@
package Object;
use Moose;
- use ExampleHelpers qw(eg_say);
use Reflex::Callbacks qw(cb_method);
use ThingWithCallbacks;
@@ -39,7 +40,7 @@
sub handle_event {
my ($self, $arg) = @_;
- eg_say("object handled event");
+ Test::More::pass("object handled event");
}
sub run_thing {
@@ -50,3 +51,5 @@
my $o = Object->new();
$o->run_thing();
+
+pass("object ran to completion");
View
@@ -13,6 +13,8 @@
use strict;
use lib qw(../lib);
+use Test::More tests => 6;
+
# Create a thing that will invoke callbacks. This syntax uses
# explicitly specified cb_object() callbacks and a scalar for the
# methods list. cb_method() would be slightly more efficient in this
@@ -25,7 +27,6 @@
package ScalarHandlerObject;
use Moose;
- use ExampleHelpers qw(eg_say);
use Reflex::Callbacks qw(cb_object);
use ThingWithCallbacks;
@@ -43,7 +44,7 @@
sub event {
my ($self, $arg) = @_;
- eg_say("$self - scalar object handled event");
+ Test::More::pass("$self - scalar object handled event");
}
sub run_thing {
@@ -55,6 +56,8 @@
my $sho = ScalarHandlerObject->new();
$sho->run_thing();
+pass("$sho - scalar handler object ran to completion");
+
# In this case, an object handles a list of callbacks. Each callback
# method is named after the event it handles.
#
@@ -65,7 +68,6 @@
package ArrayHandlerObject;
use Moose;
- use ExampleHelpers qw(eg_say);
use Reflex::Callbacks qw(cb_object);
use ThingWithCallbacks;
@@ -83,7 +85,7 @@
sub event {
my ($self, $arg) = @_;
- eg_say("$self - array object handled event");
+ Test::More::pass("$self - array object handled event");
}
sub run_thing {
@@ -95,6 +97,8 @@
my $aho = ArrayHandlerObject->new();
$aho->run_thing();
+pass("$aho - array handler object ran to completion");
+
# In this case, an object handles a hash of callbacks. Hash keys are
# event names, and the values are the corresponding handler method
# names. The hash gives classes flexibility in the methods they use.
@@ -106,7 +110,6 @@
package HashHandlerObject;
use Moose;
- use ExampleHelpers qw(eg_say);
use Reflex::Callbacks qw(cb_object);
use ThingWithCallbacks;
@@ -124,7 +127,7 @@
sub handle_event {
my ($self, $arg) = @_;
- eg_say("$self - hash object handled event");
+ Test::More::pass("$self - hash object handled event");
}
sub run_thing {
@@ -135,3 +138,7 @@
my $hho = HashHandlerObject->new();
$hho->run_thing();
+
+pass("$hho - hash handler object ran to completion");
+
+exit;
View
@@ -13,6 +13,8 @@
use strict;
use lib qw(../lib);
+use Test::More tests => 6;
+
# Create a thing that will invoke callbacks. This syntax uses
# explicitly specified cb_class() callbacks and a scalar for the
# methods list. cb_method() would be slightly more efficient in this
@@ -25,7 +27,6 @@
package ScalarHandlerClass;
use Moose;
- use ExampleHelpers qw(eg_say);
use Reflex::Callbacks qw(cb_class);
use ThingWithCallbacks;
@@ -43,7 +44,7 @@
sub event {
my ($self, $arg) = @_;
- eg_say("$self - scalar class handled event");
+ Test::More::pass("$self - scalar class handled event");
}
sub run_thing {
@@ -55,6 +56,8 @@
my $sho = ScalarHandlerClass->new();
$sho->run_thing();
+pass("$sho - scalar handler object ran to completion");
+
# In this case, a class handles a list of callbacks. Each callback
# method is named after the event it handles.
#
@@ -65,7 +68,6 @@
package ArrayHandlerClass;
use Moose;
- use ExampleHelpers qw(eg_say);
use Reflex::Callbacks qw(cb_class);
use ThingWithCallbacks;
@@ -83,7 +85,7 @@
sub event {
my ($self, $arg) = @_;
- eg_say("$self - array class handled event");
+ Test::More::pass("$self - array class handled event");
}
sub run_thing {
@@ -95,6 +97,8 @@
my $aho = ArrayHandlerClass->new();
$aho->run_thing();
+pass("$aho - array handler object ran to completion");
+
# In this case, a class handles a hash of callbacks. Hash keys are
# event names, and the values are the corresponding handler method
# names. The hash gives classes flexibility in the methods they use.
@@ -106,7 +110,6 @@
package HashHandlerClass;
use Moose;
- use ExampleHelpers qw(eg_say);
use Reflex::Callbacks qw(cb_class);
use ThingWithCallbacks;
@@ -124,7 +127,7 @@
sub handle_event {
my ($self, $arg) = @_;
- eg_say("$self - hash class handled event");
+ Test::More::pass("$self - hash class handled event");
}
sub run_thing {
@@ -135,3 +138,7 @@
my $hho = HashHandlerClass->new();
$hho->run_thing();
+
+pass("$hho - hash handler object ran to completion");
+
+exit;
View
@@ -16,6 +16,8 @@
use strict;
use lib qw(../lib);
+use Test::More tests => 4;
+
# Create a thing that will invoke callbacks. cb_role() defines the
# thing's role within the RoleHandlerObject.
#
@@ -26,7 +28,6 @@
package RoleHandlerObject;
use Moose;
- use ExampleHelpers qw(eg_say);
use Reflex::Callbacks qw(cb_role);
use ThingWithCallbacks;
@@ -42,7 +43,7 @@
sub on_thing_event {
my ($self, $arg) = @_;
- eg_say("$self - role object handled event");
+ Test::More::pass("$self - role object handled event");
}
sub run_thing {
@@ -51,16 +52,17 @@
}
}
-my $sho = RoleHandlerObject->new();
-$sho->run_thing();
+my $rho = RoleHandlerObject->new();
+$rho->run_thing();
+
+pass("$rho - role handler object ran to completion");
# This form invokes a class methods.
{
package RoleHandlerClass;
use Moose;
- use ExampleHelpers qw(eg_say);
use Reflex::Callbacks qw(cb_role);
use ThingWithCallbacks;
@@ -76,7 +78,7 @@
sub on_thing_event {
my ($self, $arg) = @_;
- eg_say("$self - role class handled event");
+ Test::More::pass("$self - role class handled event");
}
sub run_thing {
@@ -85,5 +87,7 @@
}
}
-my $shc = RoleHandlerClass->new();
-$shc->run_thing();
+my $rhc = RoleHandlerClass->new();
+$rhc->run_thing();
+
+pass("$rhc - role handler class ran to completion");
View
@@ -17,6 +17,8 @@
use strict;
use lib qw(../lib);
+use Test::More tests => 3;
+
# Create a thing that will invoke callbacks.
{
@@ -47,11 +49,12 @@
}
use Reflex::Callbacks qw(cb_promise);
-use ExampleHelpers qw(eg_say);
my $promise;
my $pt = PromiseThing->new( cb_promise(\$promise) );
-while (my $event = $promise->wait()) {
- eg_say("wait() returned an event ($event->{name})");
+for (1..3) {
+ my $event = $promise->wait();
+ last unless $event;
+ pass("wait($_) returned an event ($event->{name})");
}
@@ -243,6 +243,8 @@ sub BUILD {
}
# TODO - Who is the watcher?
+ # TODO - observe() takes multiple event/callback pairs. We can
+ # combine them into a hash and call observe() once.
$self->observe($self, $1 => $value);
next CALLBACK;
}
@@ -255,9 +257,9 @@ sub BUILD {
# Self is being observed. Register the observation with self.
sub observe {
- my ($self, $observed, %args) = @_;
+ my ($self, $observed, %callbacks) = @_;
- while (my ($event, $callback) = each %args) {
+ while (my ($event, $callback) = each %callbacks) {
$event =~ s/^on_//;
my $observation = {

0 comments on commit 0b515ca

Please sign in to comment.