Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions Code/TKStateMachine.m
Original file line number Diff line number Diff line change
Expand Up @@ -185,23 +185,28 @@ - (void)activate
if (self.initialState.didEnterStateBlock) self.initialState.didEnterStateBlock(self.initialState, nil);
}

- (BOOL)canFireEvent:(id)eventOrEventName
- (TKEvent *)eventWithEventOrName:(id)eventOrEventName
{
if (! [eventOrEventName isKindOfClass:[TKEvent class]] && ![eventOrEventName isKindOfClass:[NSString class]]) [NSException raise:NSInvalidArgumentException format:@"Expected a `TKEvent` object or `NSString` object specifying the name of an event, instead got a `%@` (%@)", [eventOrEventName class], eventOrEventName];
TKEvent *event = [eventOrEventName isKindOfClass:[TKEvent class]] ? eventOrEventName : [self eventNamed:eventOrEventName];
if (! event) [NSException raise:NSInvalidArgumentException format:@"Cannot find an Event named '%@'", eventOrEventName];
return [event.sourceStates containsObject:self.currentState];

return event;
}

- (BOOL)canFireEvent:(id)eventOrEventName
{
return [[self eventWithEventOrName:eventOrEventName].sourceStates containsObject:self.currentState];
}

- (BOOL)fireEvent:(id)eventOrEventName userInfo:(NSDictionary *)userInfo error:(NSError *__autoreleasing *)error
{
if (! self.isActive) [self activate];
if (! [eventOrEventName isKindOfClass:[TKEvent class]] && ![eventOrEventName isKindOfClass:[NSString class]]) [NSException raise:NSInvalidArgumentException format:@"Expected a `TKEvent` object or `NSString` object specifying the name of an event, instead got a `%@` (%@)", [eventOrEventName class], eventOrEventName];
TKEvent *event = [eventOrEventName isKindOfClass:[TKEvent class]] ? eventOrEventName : [self eventNamed:eventOrEventName];
if (! event) [NSException raise:NSInvalidArgumentException format:@"Cannot find an Event named '%@'", eventOrEventName];

TKEvent *event = [self eventWithEventOrName:eventOrEventName];

// Check that this transition is permitted
if (event.sourceStates != nil && ![event.sourceStates containsObject:self.currentState]) {
if (event.sourceStates != nil && ![self canFireEvent:event]) {
NSString *failureReason = [NSString stringWithFormat:@"An attempt was made to fire the '%@' event while in the '%@' state, but the event can only be fired from the following states: %@", event.name, self.currentState.name, [[event.sourceStates valueForKey:@"name"] componentsJoinedByString:@", "]];
NSDictionary *userInfo = @{ NSLocalizedDescriptionKey: @"The event cannot be fired from the current state.", NSLocalizedFailureReasonErrorKey: failureReason };
if (error) *error = [NSError errorWithDomain:TKErrorDomain code:TKInvalidTransitionError userInfo:userInfo];
Expand Down
2 changes: 1 addition & 1 deletion Specs/TKStateMachineSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ - (void)startTryingToPickUpCollegeGirls {}
it(@"raises an NSInvalidArgumentException", ^{
[[theBlock(^{
[stateMachine addState:(TKState *)@1234];
}) should] raiseWithName:NSInvalidArgumentException reason:@"Expected a `TKState` object or `NSString` object specifying the name of a state, instead got a `__NSCFNumber` (1234)"];
}) should] raiseWithName:NSInvalidArgumentException reason:@"Expected a `TKState` object, instead got a `__NSCFNumber` (1234)"];
});
});
});
Expand Down