Skip to content

Commit

Permalink
Add strong enforcement that the currentState never transitions to `…
Browse files Browse the repository at this point in the history
…nil`
  • Loading branch information
Blake Watters committed Jun 24, 2015
1 parent a85b966 commit 46dcde7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
9 changes: 8 additions & 1 deletion Code/TKStateMachine.m
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ - (void)setInitialState:(TKState *)initialState
_initialState = initialState;
}

- (void)setCurrentState:(TKState *)currentState
{
if (currentState == nil) {
[NSException raise:NSInvalidArgumentException format:@"Cannot assign currentState to `nil`: Expected a `TKState` object. (%@)", self];
}
_currentState = currentState;
}

- (NSSet *)states
{
return [NSSet setWithSet:self.mutableStates];
Expand Down Expand Up @@ -288,7 +296,6 @@ - (id)copyWithZone:(NSZone *)zone
{
TKStateMachine *copiedStateMachine = [[[self class] allocWithZone:zone] init];
copiedStateMachine.active = NO;
copiedStateMachine.currentState = nil;
copiedStateMachine.initialState = self.initialState;

for (TKState *state in self.states) {
Expand Down
18 changes: 17 additions & 1 deletion Specs/TKStateMachineSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ - (void)startTryingToPickUpCollegeGirls {}
[stateMachine.initialState shouldBeNil];
});

it(@"has a nil current state", ^{
[stateMachine.currentState shouldBeNil];
});

it(@"has no events", ^{
[[stateMachine.events should] haveCountOf:0];
});
Expand Down Expand Up @@ -263,11 +267,23 @@ - (void)startTryingToPickUpCollegeGirls {}
stateMachine.initialState = [stateMachine stateNamed:@"Dating"];
[stateMachine activate];
});

context(@"when attempting to set the currentState to `nil`", ^{
it(@"should raise", ^{
__block NSException *exception;
@try {
[stateMachine setValue:nil forKey:@"currentState"];
} @catch (NSException *anException) {
exception = anException;
}
[[exception.name should] equal:NSInvalidArgumentException];
[[exception.reason should] startWithString:@"Cannot assign currentState to `nil`: Expected a `TKState` object."];
});
});

it(@"invokes callbacks with a TKTransition describing the state change", ^{
__block TKTransition *blockTransition;
[singleState setWillEnterStateBlock:^(TKState *state, TKTransition *transition) {
NSLog(@"dsfdsfds");
blockTransition = transition;
}];
NSError *error = nil;
Expand Down

0 comments on commit 46dcde7

Please sign in to comment.