KWExample.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. //
  2. // Licensed under the terms in License.txt
  3. //
  4. // Copyright 2010 Allen Ding. All rights reserved.
  5. //
  6. #import "KWExample.h"
  7. #import "KWExampleSuiteBuilder.h"
  8. #import "KWContextNode.h"
  9. #import "KWMatcherFactory.h"
  10. #import "KWExistVerifier.h"
  11. #import "KWMatchVerifier.h"
  12. #import "KWAsyncVerifier.h"
  13. #import "KWFailure.h"
  14. #import "KWContextNode.h"
  15. #import "KWBeforeEachNode.h"
  16. #import "KWBeforeAllNode.h"
  17. #import "KWLetNode.h"
  18. #import "KWItNode.h"
  19. #import "KWAfterEachNode.h"
  20. #import "KWAfterAllNode.h"
  21. #import "KWPendingNode.h"
  22. #import "KWRegisterMatchersNode.h"
  23. #import "KWWorkarounds.h"
  24. #import "KWIntercept.h"
  25. #import "KWExampleNode.h"
  26. #import "KWExampleSuite.h"
  27. #import "KWCallSite.h"
  28. #import "KWSymbolicator.h"
  29. @interface KWExample ()
  30. @property (nonatomic, readonly) NSMutableArray *verifiers;
  31. @property (nonatomic, readonly) KWMatcherFactory *matcherFactory;
  32. @property (nonatomic, weak) XCTestCase<KWExampleDelegate> *delegate;
  33. @property (nonatomic, assign) BOOL didNotFinish;
  34. @property (nonatomic, strong) id<KWExampleNode> exampleNode;
  35. @property (nonatomic, assign) BOOL passed;
  36. - (void)reportResultForExampleNodeWithLabel:(NSString *)label;
  37. @end
  38. @implementation KWExample
  39. @synthesize selectorName = _selectorName;
  40. - (id)initWithExampleNode:(id<KWExampleNode>)node {
  41. self = [super init];
  42. if (self) {
  43. _exampleNode = node;
  44. _matcherFactory = [[KWMatcherFactory alloc] init];
  45. _verifiers = [[NSMutableArray alloc] init];
  46. _lastInContexts = [[NSMutableArray alloc] init];
  47. _passed = YES;
  48. }
  49. return self;
  50. }
  51. - (BOOL)isLastInContext:(KWContextNode *)context {
  52. for (KWContextNode *contextWhereItLast in self.lastInContexts) {
  53. if (context == contextWhereItLast) {
  54. return YES;
  55. }
  56. }
  57. return NO;
  58. }
  59. - (NSString *)description {
  60. return [NSString stringWithFormat:@"<KWExample: %@>", self.exampleNode.description];
  61. }
  62. #pragma mark - Message forwarding
  63. - (id)forwardingTargetForSelector:(SEL)aSelector {
  64. if ([self.delegate respondsToSelector:aSelector]) {
  65. return self.delegate;
  66. } else {
  67. return [super forwardingTargetForSelector:aSelector];
  68. }
  69. }
  70. - (BOOL)respondsToSelector:(SEL)aSelector {
  71. return [super respondsToSelector:aSelector] || [self.delegate respondsToSelector:aSelector];
  72. }
  73. #pragma mark - Adding Verifiers
  74. - (id)addVerifier:(id<KWVerifying>)aVerifier {
  75. if (![self.verifiers containsObject:aVerifier])
  76. [self.verifiers addObject:aVerifier];
  77. return aVerifier;
  78. }
  79. - (id)addExistVerifierWithExpectationType:(KWExpectationType)anExpectationType callSite:(KWCallSite *)aCallSite {
  80. id verifier = [KWExistVerifier existVerifierWithExpectationType:anExpectationType callSite:aCallSite reporter:self];
  81. [self addVerifier:verifier];
  82. return verifier;
  83. }
  84. - (id)addMatchVerifierWithExpectationType:(KWExpectationType)anExpectationType callSite:(KWCallSite *)aCallSite {
  85. if (self.unresolvedVerifier) {
  86. KWFailure *failure = [KWFailure failureWithCallSite:self.unresolvedVerifier.callSite format:@"expected subject not to be nil"];
  87. [self reportFailure:failure];
  88. }
  89. id<KWVerifying> verifier = [KWMatchVerifier matchVerifierWithExpectationType:anExpectationType callSite:aCallSite matcherFactory:self.matcherFactory reporter:self];
  90. [self addVerifier:verifier];
  91. self.unresolvedVerifier = verifier;
  92. return verifier;
  93. }
  94. - (id)addAsyncVerifierWithExpectationType:(KWExpectationType)anExpectationType callSite:(KWCallSite *)aCallSite timeout:(NSTimeInterval)timeout shouldWait:(BOOL)shouldWait {
  95. id verifier = [KWAsyncVerifier asyncVerifierWithExpectationType:anExpectationType callSite:aCallSite matcherFactory:self.matcherFactory reporter:self probeTimeout:timeout shouldWait: shouldWait];
  96. [self addVerifier:verifier];
  97. return verifier;
  98. }
  99. #pragma mark - Clear Verifiers
  100. - (void)clearVerifiers {
  101. [self.verifiers removeAllObjects];
  102. }
  103. #pragma mark - Running examples
  104. - (void)runWithDelegate:(XCTestCase<KWExampleDelegate> *)delegate {
  105. self.delegate = delegate;
  106. [self.matcherFactory registerMatcherClassesWithNamespacePrefix:@"KW"];
  107. [[KWExampleSuiteBuilder sharedExampleSuiteBuilder] setCurrentExample:self];
  108. [self.exampleNode acceptExampleNodeVisitor:self];
  109. [self clearVerifiers];
  110. }
  111. #pragma mark - Reporting failure
  112. - (NSString *)descriptionForExampleContext {
  113. NSMutableArray *parts = [NSMutableArray array];
  114. for (KWContextNode *context in [[self.exampleNode contextStack] reverseObjectEnumerator]) {
  115. if ([context description] != nil) {
  116. [parts addObject:[[context description] stringByAppendingString:@","]];
  117. }
  118. }
  119. return [parts componentsJoinedByString:@" "];
  120. }
  121. - (KWFailure *)outputReadyFailureWithFailure:(KWFailure *)aFailure {
  122. NSString *annotatedFailureMessage = [NSString stringWithFormat:@"'%@ %@' [FAILED], %@",
  123. [self descriptionForExampleContext], [self.exampleNode description],
  124. aFailure.message];
  125. #if TARGET_IPHONE_SIMULATOR
  126. // \uff1a is the unicode for a fill width colon, as opposed to a regular
  127. // colon character (':'). This escape is performed so that Xcode doesn't
  128. // truncate the error output in the build results window, which is running
  129. // build time specs.
  130. annotatedFailureMessage = [annotatedFailureMessage stringByReplacingOccurrencesOfString:@":" withString:@"\uff1a"];
  131. #endif // #if TARGET_IPHONE_SIMULATOR
  132. return [KWFailure failureWithCallSite:aFailure.callSite message:annotatedFailureMessage];
  133. }
  134. - (void)reportFailure:(KWFailure *)failure {
  135. self.passed = NO;
  136. [self.delegate example:self didFailWithFailure:[self outputReadyFailureWithFailure:failure]];
  137. }
  138. - (void)reportResultForExampleNodeWithLabel:(NSString *)label {
  139. NSLog(@"+ '%@ %@' [%@]", [self descriptionForExampleContext], [self.exampleNode description], label);
  140. }
  141. #pragma mark - Full description with context
  142. /** Pending cases will be marked yellow by XCode as not finished, because their description differs for -[SenTestCaseRun start] and -[SenTestCaseRun stop] methods
  143. */
  144. - (NSString *)pendingNotFinished {
  145. BOOL reportPending = self.didNotFinish;
  146. self.didNotFinish = YES;
  147. return reportPending ? @"(PENDING)" : @"";
  148. }
  149. - (NSString *)descriptionWithContext {
  150. NSString *descriptionWithContext = [NSString stringWithFormat:@"%@ %@",
  151. [self descriptionForExampleContext],
  152. [self.exampleNode description] ? [self.exampleNode description] : @""];
  153. BOOL isPending = [self.exampleNode isKindOfClass:[KWPendingNode class]];
  154. return isPending ? [descriptionWithContext stringByAppendingString:[self pendingNotFinished]] : descriptionWithContext;
  155. }
  156. - (NSString *)selectorName {
  157. if (_selectorName) {
  158. return _selectorName;
  159. }
  160. NSString *name = [self descriptionWithContext];
  161. // CamelCase the string
  162. NSArray *words = [name componentsSeparatedByString:@" "];
  163. name = @"";
  164. for (NSString *word in words) {
  165. if ([word length] < 1)
  166. {
  167. continue;
  168. }
  169. name = [name stringByAppendingString:[[word substringToIndex:1] uppercaseString]];
  170. name = [name stringByAppendingString:[word substringFromIndex:1]];
  171. }
  172. // Replace the commas with underscores to separate the levels of context
  173. name = [name stringByReplacingOccurrencesOfString:@"," withString:@"_"];
  174. // Strip out characters not legal in function names
  175. NSError *error = nil;
  176. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^a-zA-Z0-9_]*" options:0 error:&error];
  177. name = [regex stringByReplacingMatchesInString:name options:0 range:NSMakeRange(0, name.length) withTemplate:@""];
  178. // Ensure examples in the same suite have unique selector names
  179. if (self.suite) {
  180. name = [self.suite nextUniqueSelectorName:name];
  181. }
  182. return (_selectorName = name);
  183. }
  184. #pragma mark - Visiting Nodes
  185. - (void)visitRegisterMatchersNode:(KWRegisterMatchersNode *)aNode {
  186. [self.matcherFactory registerMatcherClassesWithNamespacePrefix:aNode.namespacePrefix];
  187. }
  188. - (void)visitBeforeAllNode:(KWBeforeAllNode *)aNode {
  189. if (aNode.block == nil)
  190. return;
  191. aNode.block();
  192. }
  193. - (void)visitAfterAllNode:(KWAfterAllNode *)aNode {
  194. if (aNode.block == nil)
  195. return;
  196. aNode.block();
  197. }
  198. - (void)visitBeforeEachNode:(KWBeforeEachNode *)aNode {
  199. if (aNode.block == nil)
  200. return;
  201. aNode.block();
  202. }
  203. - (void)visitAfterEachNode:(KWAfterEachNode *)aNode {
  204. if (aNode.block == nil)
  205. return;
  206. aNode.block();
  207. }
  208. - (void)visitLetNode:(KWLetNode *)aNode
  209. {
  210. [aNode evaluateTree];
  211. }
  212. - (void)visitItNode:(KWItNode *)aNode {
  213. if (aNode.block == nil || aNode != self.exampleNode)
  214. return;
  215. aNode.example = self;
  216. [aNode.context performExample:self withBlock:^{
  217. @try {
  218. aNode.block();
  219. #if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
  220. NSException *invocationException = KWGetAndClearExceptionFromAcrossInvocationBoundary();
  221. [invocationException raise];
  222. #endif // #if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
  223. // Finish verifying and clear
  224. for (id<KWVerifying> verifier in self.verifiers) {
  225. [verifier exampleWillEnd];
  226. }
  227. if (self.unresolvedVerifier) {
  228. KWFailure *failure = [KWFailure failureWithCallSite:self.unresolvedVerifier.callSite format:@"expected subject not to be nil"];
  229. [self reportFailure:failure];
  230. }
  231. } @catch (NSException *exception) {
  232. KWFailure *failure = [KWFailure failureWithCallSite:aNode.callSite format:@"%@ \"%@\" raised",
  233. [exception name],
  234. [exception reason]];
  235. [self reportFailure:failure];
  236. }
  237. if (self.passed) {
  238. [self reportResultForExampleNodeWithLabel:@"PASSED"];
  239. }
  240. // Always clear stubs and spies at the end of it blocks
  241. KWClearStubsAndSpies();
  242. }];
  243. }
  244. - (void)visitPendingNode:(KWPendingNode *)aNode {
  245. if (aNode != self.exampleNode)
  246. return;
  247. [self reportResultForExampleNodeWithLabel:@"PENDING"];
  248. }
  249. - (NSString *)generateDescriptionForAnonymousItNode {
  250. // anonymous specify blocks should only have one verifier, but use the first in any case
  251. return [(self.verifiers)[0] descriptionForAnonymousItNode];
  252. }
  253. @end
  254. #pragma mark - Looking up CallSites
  255. KWCallSite *callSiteWithAddress(long address);
  256. KWCallSite *callSiteAtAddressIfNecessary(long address);
  257. KWCallSite *callSiteAtAddressIfNecessary(long address){
  258. BOOL shouldLookup = [[KWExampleSuiteBuilder sharedExampleSuiteBuilder] isFocused] && ![[KWExampleSuiteBuilder sharedExampleSuiteBuilder] foundFocus];
  259. return shouldLookup ? [KWCallSite callSiteWithCallerAddress:address] : nil;
  260. }
  261. #pragma mark - Building Example Groups
  262. void describe(NSString *aDescription, void (^block)(void)) {
  263. KWCallSite *callSite = callSiteAtAddressIfNecessary(kwCallerAddress());
  264. describeWithCallSite(callSite, aDescription, block);
  265. }
  266. void context(NSString *aDescription, void (^block)(void)) {
  267. KWCallSite *callSite = callSiteAtAddressIfNecessary(kwCallerAddress());
  268. contextWithCallSite(callSite, aDescription, block);
  269. }
  270. void registerMatchers(NSString *aNamespacePrefix) {
  271. registerMatchersWithCallSite(nil, aNamespacePrefix);
  272. }
  273. void beforeAll(void (^block)(void)) {
  274. beforeAllWithCallSite(nil, block);
  275. }
  276. void afterAll(void (^block)(void)) {
  277. afterAllWithCallSite(nil, block);
  278. }
  279. void beforeEach(void (^block)(void)) {
  280. beforeEachWithCallSite(nil, block);
  281. }
  282. void afterEach(void (^block)(void)) {
  283. afterEachWithCallSite(nil, block);
  284. }
  285. void it(NSString *aDescription, void (^block)(void)) {
  286. KWCallSite *callSite = callSiteAtAddressIfNecessary(kwCallerAddress());
  287. itWithCallSite(callSite, aDescription, block);
  288. }
  289. void let_(__autoreleasing id *anObjectRef, const char *aSymbolName, id (^block)(void))
  290. {
  291. NSString *aDescription = [NSString stringWithUTF8String:aSymbolName];
  292. letWithCallSite(nil, anObjectRef, aDescription, block);
  293. }
  294. void specify(void (^block)(void))
  295. {
  296. itWithCallSite(nil, nil, block);
  297. }
  298. void pending_(NSString *aDescription, void (^ignoredBlock)(void)) {
  299. pendingWithCallSite(nil, aDescription, ignoredBlock);
  300. }
  301. void describeWithCallSite(KWCallSite *aCallSite, NSString *aDescription, void (^block)(void)) {
  302. contextWithCallSite(aCallSite, aDescription, block);
  303. }
  304. void contextWithCallSite(KWCallSite *aCallSite, NSString *aDescription, void (^block)(void)) {
  305. [[KWExampleSuiteBuilder sharedExampleSuiteBuilder] pushContextNodeWithCallSite:aCallSite description:aDescription];
  306. block();
  307. [[KWExampleSuiteBuilder sharedExampleSuiteBuilder] popContextNode];
  308. }
  309. void registerMatchersWithCallSite(KWCallSite *aCallSite, NSString *aNamespacePrefix) {
  310. [[KWExampleSuiteBuilder sharedExampleSuiteBuilder] setRegisterMatchersNodeWithCallSite:aCallSite namespacePrefix:aNamespacePrefix];
  311. }
  312. void beforeAllWithCallSite(KWCallSite *aCallSite, void (^block)(void)) {
  313. [[KWExampleSuiteBuilder sharedExampleSuiteBuilder] setBeforeAllNodeWithCallSite:aCallSite block:block];
  314. }
  315. void afterAllWithCallSite(KWCallSite *aCallSite, void (^block)(void)) {
  316. [[KWExampleSuiteBuilder sharedExampleSuiteBuilder] setAfterAllNodeWithCallSite:aCallSite block:block];
  317. }
  318. void beforeEachWithCallSite(KWCallSite *aCallSite, void (^block)(void)) {
  319. [[KWExampleSuiteBuilder sharedExampleSuiteBuilder] setBeforeEachNodeWithCallSite:aCallSite block:block];
  320. }
  321. void afterEachWithCallSite(KWCallSite *aCallSite, void (^block)(void)) {
  322. [[KWExampleSuiteBuilder sharedExampleSuiteBuilder] setAfterEachNodeWithCallSite:aCallSite block:block];
  323. }
  324. void letWithCallSite(KWCallSite *aCallSite, __autoreleasing id *anObjectRef, NSString *aSymbolName, id (^block)(void))
  325. {
  326. [[KWExampleSuiteBuilder sharedExampleSuiteBuilder] addLetNodeWithCallSite:aCallSite objectRef:anObjectRef symbolName:aSymbolName block:block];
  327. }
  328. void itWithCallSite(KWCallSite *aCallSite, NSString *aDescription, void (^block)(void)) {
  329. [[KWExampleSuiteBuilder sharedExampleSuiteBuilder] addItNodeWithCallSite:aCallSite description:aDescription block:block];
  330. }
  331. void pendingWithCallSite(KWCallSite *aCallSite, NSString *aDescription, void (^ignoredBlock)(void)) {
  332. [[KWExampleSuiteBuilder sharedExampleSuiteBuilder] addPendingNodeWithCallSite:aCallSite description:aDescription];
  333. }