Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this makes state_class override more useful #2

Merged
merged 2 commits into from May 2, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/FSA/Rules.pm
Expand Up @@ -304,6 +304,7 @@ sub new {
};

$params->{state_class} ||= 'FSA::State';
$params->{state_args} ||= {};
while (@_) {
my $state = shift;
my $def = shift;
Expand All @@ -320,7 +321,7 @@ sub new {
}

# Create the state object and cache the state data.
my $obj = $params->{state_class}->new;
my $obj = $params->{state_class}->new(%{$params->{state_args}});
$def->{name} = $state;
$def->{machine} = $self;
$fsa->{table}{$state} = $obj;
Expand Down
21 changes: 20 additions & 1 deletion t/base.t
Expand Up @@ -2,7 +2,7 @@

use strict;
#use Test::More 'no_plan';
use Test::More tests => 316;
use Test::More tests => 327;

my $CLASS;
BEGIN {
Expand Down Expand Up @@ -637,6 +637,25 @@ ok $foo = $fsa->states('foo'), 'Get "foo" state';
isa_ok $foo, 'FSA::Stately';
isa_ok $foo, 'FSA::State';

ok $fsa = $CLASS->new( { start => 1,
state_class => 'FSA::Stately',
state_args => { myarg => 'bar'} },
foo => { rules => [ bar => 1 ]},
bar => {},
),
"Construct with state_class";

ok $foo = $fsa->states('foo'), 'Get "foo" state';
isa_ok $foo, 'FSA::Stately';
isa_ok $foo, 'FSA::State';
isa_ok $fsa->curr_state, 'FSA::Stately';
is $fsa->curr_state->name, 'foo';
is $fsa->curr_state->{myarg}, 'bar';
ok $fsa->try_switch;
isa_ok $fsa->curr_state, 'FSA::Stately';
is $fsa->curr_state->name, 'bar';
is $fsa->curr_state->{myarg}, 'bar';

# test that messages get set even if a state dies
$fsa = $CLASS->new(
alpha => {
Expand Down