Skip to content

Commit

Permalink
Merge pull request #17 from KATT/master
Browse files Browse the repository at this point in the history
Prevent adding a state which name is already persistent
  • Loading branch information
blakewatters committed Mar 14, 2014
2 parents 4bc1377 + b4751e8 commit c8283da
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Code/TKStateMachine.m
Expand Up @@ -110,6 +110,8 @@ - (void)addState:(TKState *)state
{
TKRaiseIfActive();
if (! [state isKindOfClass:[TKState class]]) [NSException raise:NSInvalidArgumentException format:@"Expected a `TKState` object, instead got a `%@` (%@)", [state class], state];
if ([self stateNamed: state.name]) [NSException raise:NSInvalidArgumentException format:@"State with name `%@` already exists", state.name];

if (self.initialState == nil) self.initialState = state;
[self.mutableStates addObject:state];
}
Expand Down
12 changes: 11 additions & 1 deletion Specs/TKStateMachineSpec.m
Expand Up @@ -69,6 +69,17 @@ - (void)startTryingToPickUpCollegeGirls {}
[stateMachine addState:state];
});


context(@"which name is already present", ^{
it(@"raises an exception", ^{
TKState *singleState = [TKState stateWithName:@"Single"];

[[theBlock(^{
[stateMachine addState:singleState];
}) should] raiseWithName:NSInvalidArgumentException reason:@"State with name `Single` already exists"];
});
});

it(@"has a state count of 1", ^{
[[stateMachine.states should] haveCountOf:1];
});
Expand Down Expand Up @@ -182,7 +193,6 @@ - (void)startTryingToPickUpCollegeGirls {}

context(@"when the state machine has not been started", ^{
beforeEach(^{
[stateMachine addState:[TKState stateWithName:@"Dating"]];
stateMachine.initialState = [stateMachine stateNamed:@"Dating"];
});

Expand Down

0 comments on commit c8283da

Please sign in to comment.