Skip to content

Commit

Permalink
Merge pull request kif-framework#88 from square/setup-teardown
Browse files Browse the repository at this point in the history
Add steps to set up and tear down
  • Loading branch information
puls committed Nov 1, 2011
2 parents ecd504e + 7dad8fd commit e461ef7
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Additions/UIView-KIFAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ - (NSArray *)subviewsWithClassNamePrefix:(NSString *)prefix;
NSMutableArray *result = [NSMutableArray array];

// Breadth-first population of matching subviews
// First travers the next level of subviews, adding matches.
// First traverse the next level of subviews, adding matches.
for (UIView *view in self.subviews) {
if ([NSStringFromClass([view class]) hasPrefix:prefix]) {
[result addObject:view];
Expand Down
52 changes: 50 additions & 2 deletions Classes/KIFTestScenario.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,40 @@
*/
@interface KIFTestScenario : NSObject {
NSMutableArray *steps;
NSMutableArray *stepsToSetUp;
NSMutableArray *stepsToTearDown;
NSString *description;
BOOL skippedByFilter;
}

/*!
@property description
@abstract A description of what the scenario tests.
@discussion This should be a thorough description of what the scenario is testing so that if the test fails, it is clear which test it was.
@discussion This should be a thorough description of what the scenario is testing so that if the test fails it is clear which test it was.
*/
@property (nonatomic, retain) NSString *description;

/*!
@property steps
@abstract The steps that comprise the scenario.
@discussion The steps are instances of KIFTestStep (or a subclass).
@discussion The steps are instances of KIFTestStep (or a subclass). This method returns all steps, including the steps to set up and the steps to tear down.
*/
@property (nonatomic, readonly, retain) NSArray *steps;

/*!
@property stepsToSetUp
@abstract Steps that will be executed at the beginning of the scenario.
@discussion The steps to set up are an array of KIFTestStep (or subclass) instances that will be executed at the beginning of the scenario, before the steps specified in the -steps property. When initializing the scenario these steps are defaulted to the steps specified by +defaultStepsToSetUp, but may be overridden by setting them directly using this property.
*/
@property (nonatomic, copy) NSArray *stepsToSetUp;

/*!
@property stepsToTearDown
@abstract Steps that will be executed at the end of the scenario.
@discussion The steps to tear down are an array of KIFTestStep (or subclass) instances that will be executed at the end of the scenario, after the steps specified in the -steps property. When initializing the scenario these steps are defaulted to the steps specified by +defaultStepsToTearDown, but may be overridden by setting them directly using this property.
*/
@property (nonatomic, copy) NSArray *stepsToTearDown;

/*!
@property skippedByFilter
@abstract Whether this scenario is being skipped
Expand All @@ -55,6 +71,38 @@
*/
+ (id)scenarioWithDescription:(NSString *)description;

/*!
@method setDefaultStepsToSetUp:
@abstract Set the default setup steps that will be added to new scenarios.
@param The default setup steps.
@discussion When initializing a new scenario these steps are set as the stepsToSetUp on the scenario.
*/
+ (void)setDefaultStepsToSetUp:(NSArray *)steps;

/*!
@method defaultStepsToSetUp
@abstract The default setup steps that will be added to new scenarios.
@result The default setup steps.
@discussion When initializing a new scenario these steps are set as the stepsToSetUp on the scenario. These default steps can be set using +setDefaultStepsToSetUp:
*/
+ (NSArray *)defaultStepsToSetUp;

/*!
@method setDefaultStepsToTearDown:
@abstract Set the default tear down steps that will be added to new scenarios.
@param The default tear down steps.
@discussion When initializing a new scenario these steps are set as the stepsToTearDown on the scenario.
*/
+ (void)setDefaultStepsToTearDown:(NSArray *)steps;

/*!
@method defaultStepsToTearDown
@abstract The default tear down steps that will be added to new scenarios.
@result The default tear down steps.
@discussion When initializing a new scenario these steps are set as the stepsToTearDown on the scenario. These default steps can be set using +setDefaultStepsToTearDown:
*/
+ (NSArray *)defaultStepsToTearDown;

/*!
@method initializeSteps;
@abstract A place for subclasses to add steps.
Expand Down
88 changes: 82 additions & 6 deletions Classes/KIFTestScenario.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
#import "KIFTestScenario.h"
#import "KIFTestStep.h"


static NSArray *defaultStepsToSetUp = nil;
static NSArray *defaultStepsToTearDown = nil;


@interface KIFTestScenario ()

@property (nonatomic, readwrite, retain) NSArray *steps;
Expand All @@ -19,10 +24,13 @@ - (void)_initializeStepsIfNeeded;

@end


@implementation KIFTestScenario

@synthesize description;
@synthesize steps;
@synthesize stepsToSetUp;
@synthesize stepsToTearDown;
@synthesize skippedByFilter;

#pragma mark Static Methods
Expand All @@ -35,9 +43,40 @@ + (id)scenarioWithDescription:(NSString *)description
if (filter) {
scenario.skippedByFilter = ([description rangeOfString:filter options:NSRegularExpressionSearch].location == NSNotFound);
}

return [scenario autorelease];
}

+ (void)setDefaultStepsToSetUp:(NSArray *)steps;
{
if (defaultStepsToSetUp == steps) {
return;
}

[defaultStepsToSetUp release];
defaultStepsToSetUp = [steps copy];
}

+ (NSArray *)defaultStepsToSetUp;
{
return defaultStepsToSetUp;
}

+ (void)setDefaultStepsToTearDown:(NSArray *)steps;
{
if (defaultStepsToTearDown == steps) {
return;
}

[defaultStepsToTearDown release];
defaultStepsToTearDown = [steps copy];
}

+ (NSArray *)defaultStepsToTearDown;
{
return defaultStepsToTearDown;
}

#pragma mark Initialization

- (id)init
Expand All @@ -47,13 +86,18 @@ - (id)init
return nil;
}

stepsToSetUp = [defaultStepsToSetUp copy];
stepsToTearDown = [defaultStepsToTearDown copy];

return self;
}

- (void)dealloc
{
self.steps = nil;
self.description = nil;
[steps release]; steps = nil;
[stepsToSetUp release]; stepsToSetUp = nil;
[stepsToTearDown release]; stepsToTearDown = nil;
[description release]; description = nil;

[super dealloc];
}
Expand All @@ -76,7 +120,7 @@ - (void)addStep:(KIFTestStep *)step;
NSAssert(![steps containsObject:step], @"The step %@ is already added", step);

[self _initializeStepsIfNeeded];
[steps addObject:step];
[steps insertObject:step atIndex:(steps.count - self.stepsToTearDown.count)];
}

- (void)addStepsFromArray:(NSArray *)inSteps;
Expand All @@ -86,15 +130,47 @@ - (void)addStepsFromArray:(NSArray *)inSteps;
}

[self _initializeStepsIfNeeded];
[steps addObjectsFromArray:inSteps];
[steps insertObjects:inSteps atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(steps.count - self.stepsToTearDown.count, inSteps.count)]];
}

- (void)setStepsToSetUp:(NSArray *)inStepsToSetUp;
{
if ([stepsToSetUp isEqual:inStepsToSetUp]) {
return;
}

// Remove the old set up steps and add the new ones
// If steps hasn't been set up yet, that's fine
[steps removeObjectsInRange:NSMakeRange(0, stepsToSetUp.count)];
[steps insertObjects:inStepsToSetUp atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, inStepsToSetUp.count)]];

[stepsToSetUp release];
stepsToSetUp = [inStepsToSetUp copy];
}

- (void)setStepsToTearDown:(NSArray *)inStepsToTearDown;
{
if ([stepsToTearDown isEqual:inStepsToTearDown]) {
return;
}

// Remove the old tear down steps and add the new ones
// If steps hasn't been set up yet, that's fine
[steps removeObjectsInRange:NSMakeRange(steps.count - stepsToTearDown.count, stepsToTearDown.count)];
[steps insertObjects:inStepsToTearDown atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(steps.count, inStepsToTearDown.count)]];

[stepsToTearDown release];
stepsToTearDown = [inStepsToTearDown copy];
}

#pragma mark Private Methods

- (void)_initializeStepsIfNeeded
- (void)_initializeStepsIfNeeded;
{
if (!steps && !self.skippedByFilter) {
self.steps = [NSMutableArray array];
NSMutableArray *initialSteps = [NSMutableArray arrayWithArray:self.stepsToSetUp];
[initialSteps addObjectsFromArray:self.stepsToTearDown];
self.steps = initialSteps;
[self initializeSteps];
}
}
Expand Down

0 comments on commit e461ef7

Please sign in to comment.