QuickSpec.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #import "QuickSpec.h"
  2. #import "QuickConfiguration.h"
  3. #if __has_include("Quick-Swift.h")
  4. #import "Quick-Swift.h"
  5. #else
  6. #import <Quick/Quick-Swift.h>
  7. #endif
  8. static QuickSpec *currentSpec = nil;
  9. @interface QuickSpec ()
  10. @property (nonatomic, strong) Example *example;
  11. @end
  12. @implementation QuickSpec
  13. #pragma mark - XCTestCase Overrides
  14. /**
  15. Invocations for each test method in the test case. QuickSpec overrides this method to define a
  16. new method for each example defined in +[QuickSpec spec].
  17. @return An array of invocations that execute the newly defined example methods.
  18. */
  19. + (NSArray *)testInvocations {
  20. NSArray *examples = [[World sharedWorld] examplesForSpecClass:[self class]];
  21. NSMutableArray *invocations = [NSMutableArray arrayWithCapacity:[examples count]];
  22. NSMutableSet<NSString*> *selectorNames = [NSMutableSet set];
  23. for (Example *example in examples) {
  24. SEL selector = [self addInstanceMethodForExample:example classSelectorNames:selectorNames];
  25. NSMethodSignature *signature = [self instanceMethodSignatureForSelector:selector];
  26. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
  27. invocation.selector = selector;
  28. [invocations addObject:invocation];
  29. }
  30. return invocations;
  31. }
  32. #pragma mark - Public Interface
  33. - (void)spec { }
  34. + (QuickSpec*) current {
  35. return currentSpec;
  36. }
  37. #pragma mark - Internal Methods
  38. /**
  39. Runs the `spec` method and builds the examples for this class.
  40. It's safe to call this method multiple times. If the examples for the class have been built, invocation
  41. of this method has no effect.
  42. */
  43. + (void)buildExamplesIfNeeded {
  44. [QuickConfiguration class];
  45. World *world = [World sharedWorld];
  46. if ([world isRootExampleGroupInitializedForSpecClass:[self class]]) {
  47. // The examples fot this subclass have been already built. Skipping.
  48. return;
  49. }
  50. ExampleGroup *rootExampleGroup = [world rootExampleGroupForSpecClass:[self class]];
  51. [world performWithCurrentExampleGroup:rootExampleGroup closure:^{
  52. QuickSpec *spec = [self new];
  53. @try {
  54. [spec spec];
  55. }
  56. @catch (NSException *exception) {
  57. [NSException raise:NSInternalInconsistencyException
  58. format:@"An exception occurred when building Quick's example groups.\n"
  59. @"Some possible reasons this might happen include:\n\n"
  60. @"- An 'expect(...).to' expectation was evaluated outside of "
  61. @"an 'it', 'context', or 'describe' block\n"
  62. @"- 'sharedExamples' was called twice with the same name\n"
  63. @"- 'itBehavesLike' was called with a name that is not registered as a shared example\n\n"
  64. @"Here's the original exception: '%@', reason: '%@', userInfo: '%@'",
  65. exception.name, exception.reason, exception.userInfo];
  66. }
  67. }];
  68. }
  69. /**
  70. QuickSpec uses this method to dynamically define a new instance method for the
  71. given example. The instance method runs the example, catching any exceptions.
  72. The exceptions are then reported as test failures.
  73. In order to report the correct file and line number, examples must raise exceptions
  74. containing following keys in their userInfo:
  75. - "SenTestFilenameKey": A String representing the file name
  76. - "SenTestLineNumberKey": An Int representing the line number
  77. These keys used to be used by SenTestingKit, and are still used by some testing tools
  78. in the wild. See: https://github.com/Quick/Quick/pull/41
  79. @return The selector of the newly defined instance method.
  80. */
  81. + (SEL)addInstanceMethodForExample:(Example *)example classSelectorNames:(NSMutableSet<NSString*> *)selectorNames {
  82. IMP implementation = imp_implementationWithBlock(^(QuickSpec *self){
  83. self.example = example;
  84. currentSpec = self;
  85. [example run];
  86. });
  87. const char *types = [[NSString stringWithFormat:@"%s%s%s", @encode(void), @encode(id), @encode(SEL)] UTF8String];
  88. NSString *originalName = [QCKObjCStringUtils c99ExtendedIdentifierFrom:example.name];
  89. NSString *selectorName = originalName;
  90. NSUInteger i = 2;
  91. while ([selectorNames containsObject:selectorName]) {
  92. selectorName = [NSString stringWithFormat:@"%@_%tu", originalName, i++];
  93. }
  94. [selectorNames addObject:selectorName];
  95. SEL selector = NSSelectorFromString(selectorName);
  96. class_addMethod(self, selector, implementation, types);
  97. return selector;
  98. }
  99. /**
  100. This method is used to record failures, whether they represent example
  101. expectations that were not met, or exceptions raised during test setup
  102. and teardown. By default, the failure will be reported as an
  103. XCTest failure, and the example will be highlighted in Xcode.
  104. */
  105. - (void)recordFailureWithDescription:(NSString *)description
  106. inFile:(NSString *)filePath
  107. atLine:(NSUInteger)lineNumber
  108. expected:(BOOL)expected {
  109. if (self.example.isSharedExample) {
  110. filePath = self.example.callsite.file;
  111. lineNumber = self.example.callsite.line;
  112. }
  113. [currentSpec.testRun recordFailureWithDescription:description
  114. inFile:filePath
  115. atLine:lineNumber
  116. expected:expected];
  117. }
  118. @end
  119. #pragma mark - Test Observation
  120. __attribute__((constructor))
  121. static void registerQuickTestObservation(void) {
  122. [[XCTestObservationCenter sharedTestObservationCenter] addTestObserver:[QuickTestObservation sharedInstance]];
  123. }