Skip to content
Browse files

Didn't copy these over when I added to the project

  • Loading branch information...
1 parent 8d23ddb commit 2858b4add9cd72856c709e00bdb4d9ff4055ab44 Luke Redpath committed Jan 13, 2011
Showing with 271 additions and 6 deletions.
  1. +73 −0 AssertEventually.h
  2. +192 −0 AssertEventually.m
  3. +6 −6 MimicSpecDemo.xcodeproj/project.pbxproj
View
73 AssertEventually.h
@@ -0,0 +1,73 @@
+//
+// AssertEventually.h
+// LRResty
+//
+// Created by Luke Redpath on 03/08/2010.
+// Copyright 2010 LJR Software Limited. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "HCMatcher.h"
+
+#define kDEFAULT_PROBE_TIMEOUT 1
+#define kDEFAULT_PROBE_DELAY 0.1
+
+@protocol LRProbe <NSObject>
+- (BOOL)isSatisfied;
+- (void)sample;
+- (NSString *)describeToString:(NSString *)description;
+@end
+
+@interface LRProbePoller : NSObject
+{
+ NSTimeInterval timeoutInterval;
+ NSTimeInterval delayInterval;
+}
+- (id)initWithTimeout:(NSTimeInterval)theTimeout delay:(NSTimeInterval)theDelay;
+- (BOOL)check:(id<LRProbe>)probe;
+@end
+
+@class SenTestCase;
+
+void LR_assertEventuallyWithLocationAndTimeout(SenTestCase *testCase, const char* fileName, int lineNumber, id<LRProbe>probe, NSTimeInterval timeout);
+void LR_assertEventuallyWithLocation(SenTestCase *testCase, const char* fileName, int lineNumber, id<LRProbe>probe);
+
+#define assertEventuallyWithTimeout(probe, timeout) \
+ LR_assertEventuallyWithLocationAndTimeout(self, __FILE__, __LINE__, probe, timeout)
+
+#define assertEventually(probe) \
+ LR_assertEventuallyWithLocation(self, __FILE__, __LINE__, probe)
+
+typedef BOOL (^LRBlockProbeBlock)();
+
+@interface LRBlockProbe : NSObject <LRProbe>
+{
+ LRBlockProbeBlock block;
+ BOOL isSatisfied;
+}
++ (id)probeWithBlock:(LRBlockProbeBlock)block;
+- (id)initWithBlock:(LRBlockProbeBlock)aBlock;
+@end
+
+#define assertEventuallyWithBlockAndTimeout(block,timeout) \
+ assertEventuallyWithTimeout([LRBlockProbe probeWithBlock:block], timeout)
+
+#define assertEventuallyWithBlock(block) \
+ assertEventually([LRBlockProbe probeWithBlock:block])
+
+@interface LRHamcrestProbe : NSObject <LRProbe>
+{
+ id *pointerToActualObject;
+ id<HCMatcher> matcher;
+ BOOL didMatch;
+}
++ (id)probeWithObjectPointer:(id *)objectPtr matcher:(id<HCMatcher>)matcher;
+- (id)initWithObjectPointer:(id *)objectPtr matcher:(id<HCMatcher>)aMatcher;
+- (id)actualObject;
+@end
+
+#define assertEventuallyThatWithTimeout(objectPtr, aMatcher, timeout) \
+ assertEventuallyWithTimeout([LRHamcrestProbe probeWithObjectPointer:objectPtr matcher:aMatcher], timeout)
+
+#define assertEventuallyThat(objectPtr, aMatcher) \
+ assertEventually([LRHamcrestProbe probeWithObjectPointer:objectPtr matcher:aMatcher])
View
192 AssertEventually.m
@@ -0,0 +1,192 @@
+//
+// AssertEventually.m
+// LRResty
+//
+// Created by Luke Redpath on 03/08/2010.
+// Copyright 2010 LJR Software Limited. All rights reserved.
+//
+
+#import "AssertEventually.h"
+#import <SenTestingKit/SenTestCase.h>
+#import "HCStringDescription.h"
+
+@interface LRTimeout : NSObject
+{
+ NSDate *timeoutDate;
+}
+- (id)initWithTimeout:(NSTimeInterval)timeout;
+- (BOOL)hasTimedOut;
+@end
+
+@implementation LRTimeout
+
+- (id)initWithTimeout:(NSTimeInterval)timeout
+{
+ if (self = [super init]) {
+ timeoutDate = [[NSDate alloc] initWithTimeIntervalSinceNow:timeout];
+ }
+ return self;
+}
+
+- (void)dealloc
+{
+ [timeoutDate release];
+ [super dealloc];
+}
+
+- (BOOL)hasTimedOut
+{
+ return [timeoutDate timeIntervalSinceDate:[NSDate date]] < 0;
+}
+
+@end
+
+#pragma mark -
+#pragma mark Core
+
+@implementation LRProbePoller
+
+- (id)initWithTimeout:(NSTimeInterval)theTimeout delay:(NSTimeInterval)theDelay;
+{
+ if (self = [super init]) {
+ timeoutInterval = theTimeout;
+ delayInterval = theDelay;
+ }
+ return self;
+}
+
+- (BOOL)check:(id<LRProbe>)probe;
+{
+ LRTimeout *timeout = [[LRTimeout alloc] initWithTimeout:timeoutInterval];
+
+ while (![probe isSatisfied]) {
+ if ([timeout hasTimedOut]) {
+ [timeout release];
+ return NO;
+ }
+ [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:delayInterval]];
+ [probe sample];
+ }
+ [timeout release];
+
+ return YES;
+}
+
+@end
+
+void LR_assertEventuallyWithLocationAndTimeout(SenTestCase *testCase, const char* fileName, int lineNumber, id<LRProbe>probe, NSTimeInterval timeout)
+{
+ LRProbePoller *poller = [[LRProbePoller alloc] initWithTimeout:timeout delay:kDEFAULT_PROBE_DELAY];
+ if (![poller check:probe]) {
+ NSString *failureMessage = [probe describeToString:[NSString stringWithFormat:@"Probe failed after %d second(s). ", (int)timeout]];
+
+ [testCase failWithException:
+ [NSException failureInFile:[NSString stringWithUTF8String:fileName]
+ atLine:lineNumber
+ withDescription:failureMessage]];
+ }
+ [poller release];
+}
+
+void LR_assertEventuallyWithLocation(SenTestCase *testCase, const char* fileName, int lineNumber, id<LRProbe>probe)
+{
+ LR_assertEventuallyWithLocationAndTimeout(testCase, fileName, lineNumber, probe, kDEFAULT_PROBE_TIMEOUT);
+}
+
+#pragma mark -
+#pragma mark Block support
+
+@implementation LRBlockProbe
+
++ (id)probeWithBlock:(LRBlockProbeBlock)block;
+{
+ return [[[self alloc] initWithBlock:block] autorelease];
+}
+
+- (id)initWithBlock:(LRBlockProbeBlock)aBlock;
+{
+ if (self = [super init]) {
+ block = Block_copy(aBlock);
+ isSatisfied = NO;
+ [self sample];
+ }
+ return self;
+}
+
+- (void)dealloc
+{
+ Block_release(block);
+ [super dealloc];
+}
+
+- (BOOL)isSatisfied;
+{
+ return isSatisfied;
+}
+
+- (void)sample;
+{
+ isSatisfied = block();
+}
+
+- (NSString *)describeToString:(NSString *)description;
+{
+ // FIXME: this is a bit shit and non-descriptive
+ return [description stringByAppendingString:@"Block call did not return positive value."];
+}
+
+@end
+
+#pragma mark -
+#pragma mark Hamcrest support
+
+@implementation LRHamcrestProbe
+
++ (id)probeWithObjectPointer:(id *)objectPtr matcher:(id<HCMatcher>)matcher;
+{
+ return [[[self alloc] initWithObjectPointer:objectPtr matcher:matcher] autorelease];
+}
+
+- (id)initWithObjectPointer:(id *)objectPtr matcher:(id<HCMatcher>)aMatcher;
+{
+ if (self = [super init]) {
+ pointerToActualObject = objectPtr;
+ matcher = [aMatcher retain];
+ [self sample];
+ }
+ return self;
+}
+
+- (void)dealloc
+{
+ [matcher release];
+ [super dealloc];
+}
+
+- (BOOL)isSatisfied;
+{
+ return didMatch;
+}
+
+- (void)sample;
+{
+ didMatch = [matcher matches:[self actualObject]];
+}
+
+- (NSString *)describeToString:(NSString *)description;
+{
+ HCStringDescription* stringDescription = [HCStringDescription stringDescription];
+ [[[[stringDescription appendText:@"Expected "]
+ appendDescriptionOf:matcher]
+ appendText:@", got "]
+ appendValue:[self actualObject]];
+
+ return [description stringByAppendingString:[stringDescription description]];
+}
+
+- (id)actualObject
+{
+ return *pointerToActualObject;
+}
+
+@end
View
12 MimicSpecDemo.xcodeproj/project.pbxproj
@@ -8,6 +8,7 @@
/* Begin PBXBuildFile section */
A34C001312DF4808003E9972 /* LRMimic.m in Sources */ = {isa = PBXBuildFile; fileRef = A34C001212DF4808003E9972 /* LRMimic.m */; };
+ A34C00CF12DF5405003E9972 /* AssertEventually.m in Sources */ = {isa = PBXBuildFile; fileRef = A34C00CE12DF5405003E9972 /* AssertEventually.m */; };
A34CFCA312DE983E003E9972 /* LRResty.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A34CFCA212DE983E003E9972 /* LRResty.framework */; };
A34CFD3512DE985C003E9972 /* KWAfterAllNode.m in Sources */ = {isa = PBXBuildFile; fileRef = A34CFCBC12DE985C003E9972 /* KWAfterAllNode.m */; };
A34CFD3612DE985C003E9972 /* KWAfterEachNode.m in Sources */ = {isa = PBXBuildFile; fileRef = A34CFCBE12DE985C003E9972 /* KWAfterEachNode.m */; };
@@ -66,7 +67,6 @@
A34CFD6B12DE985C003E9972 /* NSObject+KiwiStubAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = A34CFD3012DE985C003E9972 /* NSObject+KiwiStubAdditions.m */; };
A34CFD6C12DE985C003E9972 /* NSObject+KiwiVerifierAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = A34CFD3212DE985C003E9972 /* NSObject+KiwiVerifierAdditions.m */; };
A34CFD6D12DE985C003E9972 /* NSValue+KiwiAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = A34CFD3412DE985C003E9972 /* NSValue+KiwiAdditions.m */; };
- A34CFD9812DE99BF003E9972 /* AssertEventually.m in Sources */ = {isa = PBXBuildFile; fileRef = A34CFD9712DE99BF003E9972 /* AssertEventually.m */; };
A34CFDFB12DE9BB6003E9972 /* HCAllOf.mm in Sources */ = {isa = PBXBuildFile; fileRef = A34CFDAA12DE9BB6003E9972 /* HCAllOf.mm */; };
A34CFDFC12DE9BB6003E9972 /* HCAnyOf.mm in Sources */ = {isa = PBXBuildFile; fileRef = A34CFDAC12DE9BB6003E9972 /* HCAnyOf.mm */; };
A34CFDFD12DE9BB6003E9972 /* HCDescribedAs.mm in Sources */ = {isa = PBXBuildFile; fileRef = A34CFDAE12DE9BB6003E9972 /* HCDescribedAs.mm */; };
@@ -112,6 +112,8 @@
/* Begin PBXFileReference section */
A34C001112DF4808003E9972 /* LRMimic.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LRMimic.h; sourceTree = "<group>"; };
A34C001212DF4808003E9972 /* LRMimic.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LRMimic.m; sourceTree = "<group>"; };
+ A34C00CD12DF5405003E9972 /* AssertEventually.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AssertEventually.h; sourceTree = "<group>"; };
+ A34C00CE12DF5405003E9972 /* AssertEventually.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AssertEventually.m; sourceTree = "<group>"; };
A34CFC9212DE97AD003E9972 /* Specs.octest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Specs.octest; sourceTree = BUILT_PRODUCTS_DIR; };
A34CFC9312DE97AD003E9972 /* Specs-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Specs-Info.plist"; sourceTree = "<group>"; };
A34CFCA212DE983E003E9972 /* LRResty.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = LRResty.framework; sourceTree = "<group>"; };
@@ -240,8 +242,6 @@
A34CFD3212DE985C003E9972 /* NSObject+KiwiVerifierAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+KiwiVerifierAdditions.m"; sourceTree = "<group>"; };
A34CFD3312DE985C003E9972 /* NSValue+KiwiAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSValue+KiwiAdditions.h"; sourceTree = "<group>"; };
A34CFD3412DE985C003E9972 /* NSValue+KiwiAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSValue+KiwiAdditions.m"; sourceTree = "<group>"; };
- A34CFD9612DE99BF003E9972 /* AssertEventually.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AssertEventually.h; path = "../../../../Downloads/gist506353-e41efd3c7021dad3ab3525e18cc519fa32786ba2/AssertEventually.h"; sourceTree = SOURCE_ROOT; };
- A34CFD9712DE99BF003E9972 /* AssertEventually.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AssertEventually.m; path = "../../../../Downloads/gist506353-e41efd3c7021dad3ab3525e18cc519fa32786ba2/AssertEventually.m"; sourceTree = SOURCE_ROOT; };
A34CFDA912DE9BB6003E9972 /* HCAllOf.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HCAllOf.h; sourceTree = "<group>"; };
A34CFDAA12DE9BB6003E9972 /* HCAllOf.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = HCAllOf.mm; sourceTree = "<group>"; };
A34CFDAB12DE9BB6003E9972 /* HCAnyOf.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HCAnyOf.h; sourceTree = "<group>"; };
@@ -400,9 +400,9 @@
A34CFCA112DE982B003E9972 /* External */ = {
isa = PBXGroup;
children = (
+ A34C00CD12DF5405003E9972 /* AssertEventually.h */,
+ A34C00CE12DF5405003E9972 /* AssertEventually.m */,
A34CFDA312DE9B8F003E9972 /* Hamcrest */,
- A34CFD9612DE99BF003E9972 /* AssertEventually.h */,
- A34CFD9712DE99BF003E9972 /* AssertEventually.m */,
A34CFCB712DE985C003E9972 /* Kiwi */,
);
name = External;
@@ -904,7 +904,6 @@
A34CFD6B12DE985C003E9972 /* NSObject+KiwiStubAdditions.m in Sources */,
A34CFD6C12DE985C003E9972 /* NSObject+KiwiVerifierAdditions.m in Sources */,
A34CFD6D12DE985C003E9972 /* NSValue+KiwiAdditions.m in Sources */,
- A34CFD9812DE99BF003E9972 /* AssertEventually.m in Sources */,
A34CFDFB12DE9BB6003E9972 /* HCAllOf.mm in Sources */,
A34CFDFC12DE9BB6003E9972 /* HCAnyOf.mm in Sources */,
A34CFDFD12DE9BB6003E9972 /* HCDescribedAs.mm in Sources */,
@@ -943,6 +942,7 @@
A34CFEAE12DEA453003E9972 /* MimicSpec.m in Sources */,
A34CFEC412DF34BD003E9972 /* MimicSpecWithWrapper.m in Sources */,
A34C001312DF4808003E9972 /* LRMimic.m in Sources */,
+ A34C00CF12DF5405003E9972 /* AssertEventually.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

0 comments on commit 2858b4a

Please sign in to comment.
Something went wrong with that request. Please try again.