1package POE::Test::Sequence;
2
3use warnings;
4use strict;
5
6use Carp qw(croak);
7use POE;
8
9use vars qw($VERSION);
10$VERSION = '1.368'; # NOTE - Should be #.### (three decimal places)
11
12sub new {
13  my ($class, %args) = @_;
14
15  my $sequence = delete $args{sequence};
16  croak "sequence required" unless defined $sequence;
17
18  return bless {
19    sequence   => $sequence,
20    test_count => scalar( @$sequence ),
21  }, $class;
22}
23
24sub next {
25  my ($self, $event, $parameter) = @_;
26
27  my $expected_result = shift @{ $self->{sequence} };
28  unless (defined $expected_result) {
29    Test::More::fail(
30      "Got an unexpected result ($event, $parameter). Time to bye."
31    );
32    exit;
33  }
34
35  my $next_action = pop @$expected_result;
36
37  Test::More::note "Testing (@$expected_result)";
38
39  Test::More::is_deeply( [ $event, $parameter ], $expected_result );
40
41  return $next_action || sub { undef };
42}
43
44sub test_count {
45  return $_[0]{test_count};
46}
47
48sub create_generic_session {
49  my ($self) = @_;
50
51  POE::Session->create(
52    inline_states => {
53      _start   => sub { goto $self->next( $_[STATE], 0 ) },
54      _default => sub { goto $self->next( $_[ARG0],  0 ) },
55    }
56  );
57}
58
591;
60
61__END__
62
63=head1 NAME
64
65POE::Test::Sequence - POE test helper to verify a sequence of events
66
67=head1 SYNOPSIS
68
69  Sorry, there isn't a synopsis at this time.
70
71  However, see t/90_regression/whjackson-followtail.t in POE's test
72  suite for a full example.
73
74=head1 DESCRIPTION
75
76POE::Test::Sequence is a test helper that abstracts a lot of the
77tedious trickery needed to verify the relative ordering of events.
78
79With this module, one can test the sequence of events without
80necessarily relying on specific times elapsing between them.
81
82=head2 create_generic_session
83
84The create_generic_session() method creates a POE::Session that routes
85all vents through the POE::Test::Sequence object.  It returns the
86POE::Session object, but the test program does not need to store it
87anywhere.  In fact, it's recommended not to do that without
88understanding the implications.
89
90The implications can be found in the documentation for POE::Kernel and
91POE::Session.
92
93An example of create_generic_session() can be found in
94POE's t/90_regression/leolo-alarm-adjust.t test program.
95
96=head2 new
97
98Create a new sequence object.  Takes named parameter pairs, currently
99just "sequence", which references an array of steps.  Each step is an
100array reference containing the expected event, a required parameter to
101that event, and a code reference for the optional next step to take
102after testing for that event.
103
104  my $sequence = POE::Test::Sequence->new(
105    sequence => [
106    [ got_idle_event => 0, sub { append_to_log("text") } ],
107    ...,
108  ]
109  );
110
111next() uses the first two step elements to verify that steps are
112occurring in the order in which they should.  The third element is
113returned by next() and is suitable for use as a goto() target.  See
114the next() method for more details.
115
116=head2 next
117
118The next() method requires an event name and a scalar parameter.
119These are compared to the first two elements of the next sequence step
120to make sure events are happening in the order in which they should.
121
122  sub handle_start_event {
123    goto $sequence->next("got_start_event", 0);
124  }
125
126=head2 test_count
127
128test_count() returns the number of test steps in the sequence object.
129It's intended to be used for test planning.
130
131  use Test::More;
132  my $sequence = POE::Test::Sequence->new( ... );
133  plan tests => $sequence->test_count();
134
135=head1 BUGS
136
137create_generic_session() is hard-coded to pass only the event name and
138the numeric value 0 to next().  This is fine for only the most generic
139sequences.
140
141=head1 AUTHORS & LICENSING
142
143Please see L<POE> for more information about authors, contributors,
144and POE's licensing.
145
146=cut
147
148# vim: ts=2 sw=2 filetype=perl expandtab
149