KWExample.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // Licensed under the terms in License.txt
  3. //
  4. // Copyright 2010 Allen Ding. All rights reserved.
  5. //
  6. #import "KiwiConfiguration.h"
  7. #import "KWLet.h"
  8. #import "KWBlock.h"
  9. #import "KWVerifying.h"
  10. #import "KWExpectationType.h"
  11. #import "KWExampleNode.h"
  12. #import "KWExampleNodeVisitor.h"
  13. #import "KWReporting.h"
  14. #import "KWExampleDelegate.h"
  15. #import <XCTest/XCTestCase.h>
  16. @class KWCallSite;
  17. @class KWExampleSuite;
  18. @class KWContextNode;
  19. @interface KWExample : NSObject <KWExampleNodeVisitor, KWReporting>
  20. @property (nonatomic, strong, readonly) NSMutableArray *lastInContexts;
  21. @property (nonatomic, weak) KWExampleSuite *suite;
  22. @property (nonatomic, strong) id<KWVerifying> unresolvedVerifier;
  23. - (id)initWithExampleNode:(id<KWExampleNode>)node;
  24. #pragma mark - Adding Verifiers
  25. - (id)addVerifier:(id<KWVerifying>)aVerifier;
  26. - (id)addExistVerifierWithExpectationType:(KWExpectationType)anExpectationType callSite:(KWCallSite *)aCallSite;
  27. - (id)addMatchVerifierWithExpectationType:(KWExpectationType)anExpectationType callSite:(KWCallSite *)aCallSite;
  28. - (id)addAsyncVerifierWithExpectationType:(KWExpectationType)anExpectationType callSite:(KWCallSite *)aCallSite timeout:(NSTimeInterval)timeout shouldWait:(BOOL)shouldWait;
  29. #pragma mark - Report failure
  30. - (void)reportFailure:(KWFailure *)failure;
  31. #pragma mark - Running
  32. - (void)runWithDelegate:(XCTestCase<KWExampleDelegate> *)delegate;
  33. #pragma mark - Anonymous It Node Descriptions
  34. - (NSString *)generateDescriptionForAnonymousItNode;
  35. #pragma mark - Checking if last in context
  36. - (BOOL)isLastInContext:(KWContextNode *)context;
  37. #pragma mark - Full description with context
  38. - (NSString *)descriptionWithContext;
  39. #pragma mark - Format description as a valid selector
  40. @property (readonly) NSString *selectorName;
  41. @end
  42. #pragma mark - Building Example Groups
  43. void describe(NSString *aDescription, void (^block)(void));
  44. void context(NSString *aDescription, void (^block)(void));
  45. void registerMatchers(NSString *aNamespacePrefix);
  46. void beforeAll(void (^block)(void));
  47. void afterAll(void (^block)(void));
  48. void beforeEach(void (^block)(void));
  49. void afterEach(void (^block)(void));
  50. void let_(id *anObjectRef, const char *aSymbolName, id (^block)(void));
  51. void it(NSString *aDescription, void (^block)(void));
  52. void specify(void (^block)(void));
  53. void pending_(NSString *aDescription, void (^block)(void));
  54. void describeWithCallSite(KWCallSite *aCallSite, NSString *aDescription, void (^block)(void));
  55. void contextWithCallSite(KWCallSite *aCallSite, NSString *aDescription, void (^block)(void));
  56. void registerMatchersWithCallSite(KWCallSite *aCallSite, NSString *aNamespacePrefix);
  57. void beforeAllWithCallSite(KWCallSite *aCallSite, void (^block)(void));
  58. void afterAllWithCallSite(KWCallSite *aCallSite, void (^block)(void));
  59. void beforeEachWithCallSite(KWCallSite *aCallSite, void (^block)(void));
  60. void afterEachWithCallSite(KWCallSite *aCallSite, void (^block)(void));
  61. void letWithCallSite(KWCallSite *aCallSite, id *anObjectRef, NSString *aSymbolName, id (^block)(void));
  62. void itWithCallSite(KWCallSite *aCallSite, NSString *aDescription, void (^block)(void));
  63. void pendingWithCallSite(KWCallSite *aCallSite, NSString *aDescription, void (^block)(void));
  64. /**
  65. Declares a local helper variable that is re-initialised before each
  66. example with the return value of the provided block.
  67. You can declare multiple `let` blocks in a context (unlike `beforeEach`,
  68. which can only be used once per context):
  69. describe(@"multiple let blocks", ^{
  70. let(subject, ^{ return @"world"; });
  71. let(greeting, ^{
  72. return [NSString stringWithFormat:@"Hello, %@!", subject];
  73. });
  74. it(@"allows you to declare multiple variables", ^{
  75. [[greeting should] equal:@"Hello, world!"];
  76. });
  77. });
  78. You can also redefine a `let` variable inside a nested context. This
  79. allows for some very useful kinds of code reuse:
  80. describe(@"greetings in different contexts", ^{
  81. let(subject, nil); // no subject by default
  82. let(greeting, ^{
  83. // greeting references subject
  84. return [NSString stringWithFormat:@"Hello, %@!", subject];
  85. });
  86. context(@"greeting the world", ^{
  87. let(subject, ^{ return @"world"; }); // redefine subject
  88. specify(^{
  89. [[greeting should] equal:@"Hello, world!"];
  90. });
  91. });
  92. context(@"greeting Kiwi", ^{
  93. let(subject, ^{ return @"Kiwi"; }); // redefine subject
  94. specify(^{
  95. [[greeting should] equal:@"Hello, Kiwi!"];
  96. });
  97. });
  98. });
  99. @param name A name for the local variable
  100. @param block The block to evaluate
  101. @note `let` blocks are evaluated before each example, and also prior to
  102. evaluating the `beforeEach` block. You should not reference a `let`
  103. variable in a `beforeAll` block, as its value is undefined at this point.
  104. */
  105. void let(id name, id (^block)(void)); // coax Xcode into autocompleting
  106. #define let(var, ...) \
  107. __block __typeof__((__VA_ARGS__)()) var; \
  108. let_(KW_LET_REF(var), #var, __VA_ARGS__)
  109. #define PRAGMA(x) _Pragma (#x)
  110. #define PENDING(x) PRAGMA(message ( "Pending: " #x ))
  111. #define pending(title, args...) \
  112. PENDING(title) \
  113. pending_(title, ## args)
  114. #define xit(title, args...) \
  115. PENDING(title) \
  116. pending_(title, ## args)