Skip to content

Commit 50b3310

Browse files
committed
Merge pull request #78 from JosephEarl/feature/better-task-description
Better -(NSString*)description for BFTask
2 parents 8721616 + 49736ba commit 50b3310

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

Bolts/Common/BFTask.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,4 +377,29 @@ - (void)waitUntilFinished {
377377
[self.condition unlock];
378378
}
379379

380+
#pragma mark - NSObject
381+
382+
- (NSString *)description {
383+
// Acquire the data from the locked properties
384+
BOOL isCompleted;
385+
BOOL isCancelled;
386+
BOOL isFaulted;
387+
388+
@synchronized (self.lock) {
389+
isCompleted = self.completed;
390+
isCancelled = self.cancelled;
391+
isFaulted = self.faulted;
392+
}
393+
394+
// Description string includes status information and, if available, the
395+
// result sisnce in some ways this is what a promise actually "is".
396+
return [NSString stringWithFormat:@"<%@: %p; completed = %@; cancelled = %@; faulted = %@;%@>",
397+
NSStringFromClass([self class]),
398+
self,
399+
isCompleted ? @"YES" : @"NO",
400+
isCancelled ? @"YES" : @"NO",
401+
isFaulted ? @"YES" : @"NO",
402+
isCompleted ? [NSString stringWithFormat:@" result:%@", _result] : @""];
403+
}
404+
380405
@end

BoltsTests/TaskTests.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,4 +534,13 @@ - (void)testExecuteOnOperationQueue {
534534
[task waitUntilFinished];
535535
}
536536

537+
- (void)testDescription {
538+
BFTask *task = [BFTask taskWithResult:nil];
539+
NSString *expected = [NSString stringWithFormat:@"<BFTask: %p; completed = YES; cancelled = NO; faulted = NO; result:(null)>", task];
540+
541+
NSString *description = task.description;
542+
543+
XCTAssertTrue([expected isEqualToString:description]);
544+
}
545+
537546
@end

0 commit comments

Comments
 (0)