KiwiMacros.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // Licensed under the terms in License.txt
  3. //
  4. // Copyright 2010 Allen Ding. All rights reserved.
  5. //
  6. #import "KiwiConfiguration.h"
  7. // This category is solely meant to coax Xcode into exposing the method names below during autocompletion.
  8. // There is no implementation and this class definition must come before the macro definitions below.
  9. @interface NSObject (KiwiVerifierMacroNames)
  10. - (void)should;
  11. - (void)shouldNot;
  12. - (void)shouldBeNil DEPRECATED_ATTRIBUTE;
  13. - (void)shouldNotBeNil DEPRECATED_ATTRIBUTE;
  14. - (void)shouldEventually;
  15. - (void)shouldNotEventually;
  16. - (void)shouldEventuallyBeforeTimingOutAfter;
  17. - (void)shouldNotEventuallyBeforeTimingOutAfter;
  18. - (void)shouldAfterWait;
  19. - (void)shouldNotAfterWait;
  20. - (void)shouldAfterWaitOf;
  21. - (void)shouldNotAfterWaitOf;
  22. @end
  23. #pragma mark - Support Macros
  24. #define KW_THIS_CALLSITE [KWCallSite callSiteWithFilename:@__FILE__ lineNumber:__LINE__]
  25. #define KW_ADD_EXIST_VERIFIER(expectationType) [KWSpec addExistVerifierWithExpectationType:expectationType callSite:KW_THIS_CALLSITE]
  26. #define KW_ADD_MATCH_VERIFIER(expectationType) [KWSpec addMatchVerifierWithExpectationType:expectationType callSite:KW_THIS_CALLSITE]
  27. #define KW_ADD_ASYNC_VERIFIER(expectationType, timeOut, wait) [KWSpec addAsyncVerifierWithExpectationType:expectationType callSite:KW_THIS_CALLSITE timeout:timeOut shouldWait:wait]
  28. #pragma mark - Keywords
  29. #ifndef KIWI_DISABLE_MATCHERS
  30. // Kiwi macros used in specs for verifying expectations.
  31. #define should attachToVerifier:KW_ADD_MATCH_VERIFIER(KWExpectationTypeShould)
  32. #define shouldNot attachToVerifier:KW_ADD_MATCH_VERIFIER(KWExpectationTypeShouldNot)
  33. #define shouldBeNil attachToVerifier:KW_ADD_EXIST_VERIFIER(KWExpectationTypeShouldNot)
  34. #define shouldNotBeNil attachToVerifier:KW_ADD_EXIST_VERIFIER(KWExpectationTypeShould)
  35. #define shouldEventually attachToVerifier:KW_ADD_ASYNC_VERIFIER(KWExpectationTypeShould, kKW_DEFAULT_PROBE_TIMEOUT, NO)
  36. #define shouldNotEventually attachToVerifier:KW_ADD_ASYNC_VERIFIER(KWExpectationTypeShouldNot, kKW_DEFAULT_PROBE_TIMEOUT, NO)
  37. #define shouldEventuallyBeforeTimingOutAfter(timeout) attachToVerifier:KW_ADD_ASYNC_VERIFIER(KWExpectationTypeShould, timeout, NO)
  38. #define shouldNotEventuallyBeforeTimingOutAfter(timeout) attachToVerifier:KW_ADD_ASYNC_VERIFIER(KWExpectationTypeShouldNot, timeout, NO)
  39. #define shouldAfterWait attachToVerifier:KW_ADD_ASYNC_VERIFIER(KWExpectationTypeShould, kKW_DEFAULT_PROBE_TIMEOUT, YES)
  40. #define shouldNotAfterWait attachToVerifier:KW_ADD_ASYNC_VERIFIER(KWExpectationTypeShouldNot, kKW_DEFAULT_PROBE_TIMEOUT, YES)
  41. #define shouldAfterWaitOf(timeout) attachToVerifier:KW_ADD_ASYNC_VERIFIER(KWExpectationTypeShould, timeout, YES)
  42. #define shouldNotAfterWaitOf(timeout) attachToVerifier:KW_ADD_ASYNC_VERIFIER(KWExpectationTypeShouldNot, timeout, YES)
  43. #define beNil beNil:[KWNilMatcher verifyNilSubject]
  44. #define beNonNil beNonNil:[KWNilMatcher verifyNonNilSubject]
  45. // used to wrap a pointer to an object that will change in the future (used with shouldEventually)
  46. #define expectFutureValue(futureValue) [KWFutureObject futureObjectWithBlock:^{ return futureValue; }]
  47. // `fail` triggers a failure report when called
  48. #define fail(message, ...) [[[KWExampleSuiteBuilder sharedExampleSuiteBuilder] currentExample] reportFailure:[KWFailure failureWithCallSite:KW_THIS_CALLSITE format:message, ##__VA_ARGS__]]
  49. // used for message patterns to allow matching any value
  50. #define kw_any() [KWAny any]
  51. #endif
  52. // If a gcc compatible compiler is available, use the statement and
  53. // declarations in expression extension to provide a convenient catch-all macro
  54. // to create KWValues.
  55. #if defined(__GNUC__)
  56. #define theValue(expr) \
  57. ({ \
  58. __typeof__(expr) kiwiReservedPrefix_lVar = expr; \
  59. [KWValue valueWithBytes:&kiwiReservedPrefix_lVar objCType:@encode(__typeof__(expr))]; \
  60. })
  61. #endif // #if defined(__GNUC__)
  62. // Allows for comparision of pointer values in expectations
  63. #define thePointerValue(expr) [NSValue valueWithPointer:(expr)]
  64. // Example group declarations.
  65. #define SPEC_BEGIN(name) \
  66. \
  67. @interface name : KWSpec \
  68. \
  69. @end \
  70. \
  71. @implementation name \
  72. \
  73. + (NSString *)file { return @__FILE__; } \
  74. \
  75. + (void)buildExampleGroups { \
  76. [super buildExampleGroups]; \
  77. \
  78. id _kw_test_case_class = self; \
  79. { \
  80. /* The shadow `self` must be declared inside a new scope to avoid compiler warnings. */ \
  81. /* The receiving class object delegates unrecognized selectors to the current example. */ \
  82. __unused name *self = _kw_test_case_class;
  83. #define SPEC_END \
  84. } \
  85. } \
  86. \
  87. @end
  88. // Test suite configuration declaration
  89. #define CONFIG_START \
  90. @interface KWSuiteConfiguration : KWSuiteConfigurationBase \
  91. \
  92. @end \
  93. \
  94. @implementation KWSuiteConfiguration \
  95. \
  96. - (void)configureSuite {
  97. #define CONFIG_END \
  98. } \
  99. \
  100. @end
  101. // Used to ensure that shared examples are registered before any
  102. // examples are evaluated. The name parameter is not used except
  103. // to define a category. Therefore, it must be unique.
  104. #define SHARED_EXAMPLES_BEGIN(name) \
  105. \
  106. @interface KWSharedExample (name) \
  107. \
  108. @end \
  109. \
  110. @implementation KWSharedExample (name) \
  111. \
  112. + (void)load { \
  113. #define SHARED_EXAMPLES_END \
  114. } \
  115. \
  116. @end \