59 lines
2.1 KiB
Objective-C
59 lines
2.1 KiB
Objective-C
// OCHamcrest by Jon Reid, https://qualitycoding.org/
|
|
// Copyright 2019 hamcrest.org. See LICENSE.txt
|
|
|
|
#import "HCAssertThat.h"
|
|
|
|
#import "HCRunloopRunner.h"
|
|
#import "HCStringDescription.h"
|
|
#import "HCMatcher.h"
|
|
#import "HCTestFailure.h"
|
|
#import "HCTestFailureReporter.h"
|
|
#import "HCTestFailureReporterChain.h"
|
|
|
|
static void reportMismatch(id testCase, id actual, id <HCMatcher> matcher,
|
|
char const *fileName, int lineNumber)
|
|
{
|
|
HCTestFailure *failure = [[HCTestFailure alloc] initWithTestCase:testCase
|
|
fileName:[NSString stringWithUTF8String:fileName]
|
|
lineNumber:(NSUInteger)lineNumber
|
|
reason:HCDescribeMismatch(matcher, actual)];
|
|
HCTestFailureReporter *chain = [HCTestFailureReporterChain reporterChain];
|
|
[chain handleFailure:failure];
|
|
}
|
|
|
|
void HC_assertThatWithLocation(id testCase, _Nullable id actual, id <HCMatcher> matcher,
|
|
const char *fileName, int lineNumber)
|
|
{
|
|
if (![matcher matches:actual])
|
|
reportMismatch(testCase, actual, matcher, fileName, lineNumber);
|
|
}
|
|
|
|
void HC_assertWithTimeoutAndLocation(id testCase, NSTimeInterval timeout,
|
|
HCFutureValue actualBlock, id <HCMatcher> matcher,
|
|
const char *fileName, int lineNumber)
|
|
{
|
|
__block BOOL match = [matcher matches:actualBlock()];
|
|
|
|
if (!match)
|
|
{
|
|
HCRunloopRunner *runner = [[HCRunloopRunner alloc] initWithFulfillmentBlock:^{
|
|
match = [matcher matches:actualBlock()];
|
|
return match;
|
|
}];
|
|
[runner runUntilFulfilledOrTimeout:timeout];
|
|
}
|
|
|
|
if (!match)
|
|
reportMismatch(testCase, actualBlock(), matcher, fileName, lineNumber);
|
|
}
|
|
|
|
NSString *HCDescribeMismatch(id <HCMatcher> matcher, id actual)
|
|
{
|
|
HCStringDescription *description = [HCStringDescription stringDescription];
|
|
[[[description appendText:@"Expected "]
|
|
appendDescriptionOf:matcher]
|
|
appendText:@", but "];
|
|
[matcher describeMismatchOf:actual to:description];
|
|
return description.description;
|
|
}
|