Skip to content

Commit

Permalink
Put async examples in a root context
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeredpath committed Sep 8, 2011
1 parent 23f059a commit b788bf7
Showing 1 changed file with 67 additions and 65 deletions.
132 changes: 67 additions & 65 deletions Examples/ExampleAsyncSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,78 +16,80 @@

const float nanosecondToSeconds = 1e9;

it(@"should verify asynchronous expectations on a variable that starts as nil that succeed in time", ^{
__block NSString *fetchedData = nil;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * nanosecondToSeconds), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
fetchedData = @"expected response data";
context(@"Asynchronous specs", ^{
it(@"should verify asynchronous expectations on a variable that starts as nil that succeed in time", ^{
__block NSString *fetchedData = nil;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * nanosecondToSeconds), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
fetchedData = @"expected response data";
});

// this will block until the matcher is satisfied or it times out (default: 1s)
[[theObject(&fetchedData) shouldEventually] equal:@"expected response data"];
});

// this will block until the matcher is satisfied or it times out (default: 1s)
[[theObject(&fetchedData) shouldEventually] equal:@"expected response data"];
});

it(@"should verify asynchronous expectations on a variable that starts as nil that succeed with an explicit time", ^{
__block NSString *fetchedData = nil;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * nanosecondToSeconds), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
fetchedData = @"expected response data";
it(@"should verify asynchronous expectations on a variable that starts as nil that succeed with an explicit time", ^{
__block NSString *fetchedData = nil;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * nanosecondToSeconds), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
fetchedData = @"expected response data";
});
// this will block until the matcher is satisfied or it times out (default: 1s)
[[theObject(&fetchedData) shouldEventuallyBeforeTimingOutAfter(2.0)] equal:@"expected response data"];
});

// this will block until the matcher is satisfied or it times out (default: 1s)
[[theObject(&fetchedData) shouldEventuallyBeforeTimingOutAfter(2.0)] equal:@"expected response data"];
});

it(@"should verify asynchronous expectations on the return value of a block", ^{
__block NSString *fetchedData = nil;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * nanosecondToSeconds), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
fetchedData = @"expected response data";

it(@"should verify asynchronous expectations on the return value of a block", ^{
__block NSString *fetchedData = nil;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * nanosecondToSeconds), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
fetchedData = @"expected response data";
});

[[expectFutureValue([fetchedData uppercaseString]) shouldEventually] equal:@"EXPECTED RESPONSE DATA"];
});

[[expectFutureValue([fetchedData uppercaseString]) shouldEventually] equal:@"EXPECTED RESPONSE DATA"];
});

it(@"should verify asynchronous mock expectations on an existing object set before the asynchronous call", ^{
__block id mock = [KWMock mockForClass:[NSString class]];

[[[mock shouldEventually] receive] uppercaseString];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * nanosecondToSeconds), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
[mock uppercaseString];

it(@"should verify asynchronous mock expectations on an existing object set before the asynchronous call", ^{
__block id mock = [KWMock mockForClass:[NSString class]];

[[[mock shouldEventually] receive] uppercaseString];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * nanosecondToSeconds), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
[mock uppercaseString];
});
});
});

it(@"should verify asynchronous mock expectations on an existing object set after the asynchronous call", ^{
__block id mock = [KWMock mockForClass:[NSString class]];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * nanosecondToSeconds), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
[mock uppercaseString];

it(@"should verify asynchronous mock expectations on an existing object set after the asynchronous call", ^{
__block id mock = [KWMock mockForClass:[NSString class]];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * nanosecondToSeconds), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
[mock uppercaseString];
});

[[[mock shouldEventually] receive] uppercaseString];
});

[[[mock shouldEventually] receive] uppercaseString];
});

it(@"should verify asynchronous expectations on a variable that starts as nil and becomes not-nil", ^{
__block NSString *fetchedData = nil;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * nanosecondToSeconds), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
fetchedData = @"expected response data";

it(@"should verify asynchronous expectations on a variable that starts as nil and becomes not-nil", ^{
__block NSString *fetchedData = nil;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * nanosecondToSeconds), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
fetchedData = @"expected response data";
});

// this will block until the matcher is satisfied or it times out (default: 1s)
[[theObject(&fetchedData) shouldEventually] beNonNil];
});

// this will block until the matcher is satisfied or it times out (default: 1s)
[[theObject(&fetchedData) shouldEventually] beNonNil];
});

it(@"should verify asynchronous expectations on a variable that starts as non-nil and becomes nil", ^{
__block NSString *fetchedData = @"not nil";

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * nanosecondToSeconds), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
fetchedData = nil;
it(@"should verify asynchronous expectations on a variable that starts as non-nil and becomes nil", ^{
__block NSString *fetchedData = @"not nil";
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * nanosecondToSeconds), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
fetchedData = nil;
});
// this will block until the matcher is satisfied or it times out (default: 1s)
[[theObject(&fetchedData) shouldEventually] beNil];
});

// this will block until the matcher is satisfied or it times out (default: 1s)
[[theObject(&fetchedData) shouldEventually] beNil];
});

SPEC_END
Expand Down

0 comments on commit b788bf7

Please sign in to comment.