main
 1//
 2// Licensed under the terms in License.txt
 3//
 4// Copyright 2010 Allen Ding. All rights reserved.
 5//
 6
 7#import "KWExistVerifier.h"
 8#import "KWFailure.h"
 9#import "KWFormatter.h"
10#import "KWReporting.h"
11
12@interface KWExistVerifier()
13
14#pragma mark -
15#pragma mark Properties
16
17@property (nonatomic, readonly) KWExpectationType expectationType;
18@property (nonatomic, readonly) KWCallSite *callSite;
19@property (nonatomic, readonly) id<KWReporting> reporter;
20
21@end
22
23@implementation KWExistVerifier
24
25#pragma mark -
26#pragma mark Initializing
27
28- (id)initWithExpectationType:(KWExpectationType)anExpectationType callSite:(KWCallSite *)aCallSite reporter:(id<KWReporting>)aReporter {
29    if ((self = [super init])) {
30        expectationType = anExpectationType;
31        callSite = [aCallSite retain];
32        reporter = aReporter;
33    }
34
35    return self;
36}
37
38+ (id)existVerifierWithExpectationType:(KWExpectationType)anExpectationType callSite:(KWCallSite *)aCallSite reporter:(id<KWReporting>)aReporter {
39    return [[[self alloc] initWithExpectationType:anExpectationType callSite:aCallSite reporter:aReporter] autorelease];
40}
41
42- (void)dealloc {
43    [callSite release];
44    [subject release];
45    [super dealloc];
46}
47
48- (NSString *)descriptionForAnonymousItNode
49{
50  if (self.expectationType == KWExpectationTypeShould) {
51    return @"should exist";
52  }
53  return @"should not exist";
54}
55
56#pragma mark -
57#pragma mark Properties
58
59@synthesize expectationType;
60@synthesize callSite;
61@synthesize reporter;
62@synthesize subject;
63
64#pragma mark -
65#pragma mark Ending Examples
66
67- (void)exampleWillEnd {
68    if (self.expectationType == KWExpectationTypeShould && self.subject == nil) {
69        KWFailure *failure = [KWFailure failureWithCallSite:self.callSite message:@"expected subject not to be nil"];
70        [self.reporter reportFailure:failure];
71    } else if (self.expectationType == KWExpectationTypeShouldNot && self.subject != nil) {
72        KWFailure *failure = [KWFailure failureWithCallSite:self.callSite format:@"expected subject to be nil, got %@",
73                                                                                 [KWFormatter formatObject:self.subject]];
74        [self.reporter reportFailure:failure];
75    }
76}
77
78@end