From 018a2706a66cd68b4dd9bbfb82b28c769a0f14d8 Mon Sep 17 00:00:00 2001 From: NachoSoto Date: Wed, 12 Mar 2014 17:45:54 +0000 Subject: [PATCH 1/2] Fixed tests --- Specs/TKStateMachineSpec.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Specs/TKStateMachineSpec.m b/Specs/TKStateMachineSpec.m index eed3552..9b7b3c7 100644 --- a/Specs/TKStateMachineSpec.m +++ b/Specs/TKStateMachineSpec.m @@ -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)"]; }); }); }); From cb5c73c544bcb55b8513ef17292ae8d18ee854bc Mon Sep 17 00:00:00 2001 From: NachoSoto Date: Wed, 12 Mar 2014 17:48:03 +0000 Subject: [PATCH 2/2] Removed duplicated code in TKStateMachine --- Code/TKStateMachine.m | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Code/TKStateMachine.m b/Code/TKStateMachine.m index 94f836c..6ad7732 100644 --- a/Code/TKStateMachine.m +++ b/Code/TKStateMachine.m @@ -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];