Permalink
Browse files

Merge pull request #88 from square/setup-teardown

Add steps to set up and tear down
  • Loading branch information...
2 parents ecd504e + 7dad8fd commit e461ef7992cd351c6e037add3403455f0465f9da @puls puls committed Nov 1, 2011
Showing with 133 additions and 9 deletions.
  1. +1 −1 Additions/UIView-KIFAdditions.m
  2. +50 −2 Classes/KIFTestScenario.h
  3. +82 −6 Classes/KIFTestScenario.m
@@ -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];
@@ -21,25 +21,41 @@
*/
@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
@discussion Set the KIF_SCENARIO_FILTER environment variable to skip all scenarios not matching the variable's value
@@ -56,6 +72,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.
@discussion This is lazily called the first time the steps property is accessed. Subclasses can use model information to customize the set of steps that are returned.
@@ -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;
@@ -19,10 +24,13 @@ - (void)_initializeStepsIfNeeded;
@end
+
@implementation KIFTestScenario
@synthesize description;
@synthesize steps;
+@synthesize stepsToSetUp;
+@synthesize stepsToTearDown;
@synthesize skippedByFilter;
#pragma mark Static Methods
@@ -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
@@ -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];
}
@@ -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;
@@ -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];
}
}

0 comments on commit e461ef7

Please sign in to comment.