main
 1//
 2//  KWExampleSuite.m
 3//  Kiwi
 4//
 5//  Created by Luke Redpath on 17/10/2011.
 6//  Copyright (c) 2011 Allen Ding. All rights reserved.
 7//
 8
 9#import "KWExampleSuite.h"
10#import "KWExample.h"
11#import "KWStringUtilities.h"
12#import "KWBeforeAllNode.h"
13#import "KWAfterAllNode.h"
14#import "NSMethodSignature+KiwiAdditions.h"
15#import <objc/runtime.h>
16
17#define kKWINVOCATION_EXAMPLE_GROUP_KEY @"__KWExampleGroupKey"
18
19@implementation KWExampleSuite
20
21- (id)initWithRootNode:(KWContextNode *)contextNode
22{
23  if ((self = [super init])) {
24    rootNode = [contextNode retain];
25    examples = [[NSMutableArray alloc] init];
26  }
27  return self;
28}
29
30- (void)dealloc 
31{
32  [examples release];
33  [rootNode release];
34  [super dealloc];
35}
36
37- (void)addExample:(KWExample *)example
38{
39  [examples addObject:example];
40  [example setSuite:self];
41}
42
43- (void)markLastExampleAsLastInContext:(KWContextNode *)context
44{
45  if ([examples count] > 0) {
46    KWExample *lastExample = (KWExample *)[examples lastObject];
47    [lastExample.lastInContexts addObject:context];
48  }
49}
50
51- (NSArray *)invocationsForTestCase;
52{
53  NSMutableArray *invocations = [NSMutableArray array];
54  
55  // Add a single dummy invocation for each example group
56  
57  for (KWExample *exampleGroup in examples) {
58    NSMethodSignature *methodSignature = [NSMethodSignature signatureWithObjCTypes:[KWEncodingForVoidMethod() UTF8String]];
59    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
60    [invocations addObject:invocation];
61    [invocation kw_setExample:exampleGroup];
62  }
63  
64  return invocations;
65}
66
67@end
68
69#pragma mark -
70
71// because SenTest will modify the invocation target, we'll have to store 
72// another reference to the example group so we can retrieve it later
73
74@implementation NSInvocation (KWExampleGroup)
75
76- (void)kw_setExample:(KWExample *)exampleGroup
77{
78  objc_setAssociatedObject(self, kKWINVOCATION_EXAMPLE_GROUP_KEY, exampleGroup, OBJC_ASSOCIATION_RETAIN);    
79}
80
81- (KWExample *)kw_example
82{
83  return objc_getAssociatedObject(self, kKWINVOCATION_EXAMPLE_GROUP_KEY);
84}
85
86@end
87