KWSpec.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // Licensed under the terms in License.txt
  3. //
  4. // Copyright 2010 Allen Ding. All rights reserved.
  5. //
  6. #import "KWSpec.h"
  7. #import "KWCallSite.h"
  8. #import "KWExample.h"
  9. #import "KWExampleSuiteBuilder.h"
  10. #import "KWFailure.h"
  11. #import "KWExampleSuite.h"
  12. #import <objc/runtime.h>
  13. @interface KWSpec()
  14. @property (nonatomic, strong) KWExample *currentExample;
  15. @end
  16. @implementation KWSpec
  17. /* Methods are only implemented by sub-classes */
  18. + (NSString *)file { return nil; }
  19. + (void)buildExampleGroups {}
  20. - (NSString *)name {
  21. return [self description];
  22. }
  23. /* Use camel case to make method friendly names from example description. */
  24. - (NSString *)description {
  25. KWExample *currentExample = self.currentExample ?: self.invocation.kw_example;
  26. return [NSString stringWithFormat:@"-[%@ %@]", NSStringFromClass([self class]), currentExample.selectorName];
  27. }
  28. #pragma mark - Getting Invocations
  29. /* Called by the XCTest to get an array of invocations that
  30. should be run on instances of test cases. */
  31. + (NSArray *)testInvocations {
  32. SEL buildExampleGroups = @selector(buildExampleGroups);
  33. // Only return invocation if the receiver is a concrete spec that has overridden -buildExampleGroups.
  34. if ([self methodForSelector:buildExampleGroups] == [KWSpec methodForSelector:buildExampleGroups])
  35. return @[];
  36. KWExampleSuite *exampleSuite = [[KWExampleSuiteBuilder sharedExampleSuiteBuilder] buildExampleSuite:^{
  37. [self buildExampleGroups];
  38. }];
  39. NSMutableArray *invocations = [NSMutableArray new];
  40. for (KWExample *example in exampleSuite) {
  41. SEL selector = [self addInstanceMethodForExample:example];
  42. NSInvocation *invocation = [self invocationForExample:example selector:selector];
  43. [invocations addObject:invocation];
  44. }
  45. return invocations;
  46. }
  47. + (SEL)addInstanceMethodForExample:(KWExample *)example {
  48. Method method = class_getInstanceMethod(self, @selector(runExample));
  49. SEL selector = NSSelectorFromString(example.selectorName);
  50. IMP implementation = method_getImplementation(method);
  51. const char *types = method_getTypeEncoding(method);
  52. class_addMethod(self, selector, implementation, types);
  53. return selector;
  54. }
  55. + (NSInvocation *)invocationForExample:(KWExample *)example selector:(SEL)selector {
  56. NSMethodSignature *signature = [NSMethodSignature signatureWithObjCTypes:"v@:"];
  57. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
  58. invocation.kw_example = example;
  59. invocation.selector = selector;
  60. return invocation;
  61. }
  62. #pragma mark - Message forwarding
  63. // Inside `SPEC_BEGIN`/`SPEC_END`, `self` references the spec class object but
  64. // ultimately needs to instead behave like an _instance_ of `KWSpec`. The current
  65. // example has a reference to the running test case via its delegate, and so
  66. // forwarding messages there enables messages sent to `self` from within an
  67. // example to behave just like the test case instance.
  68. //
  69. // This is useful, for example, because the `XCTAssert*` macros require a `self`
  70. // that is a kind of `XCTestCase`.
  71. + (id)forwardingTargetForSelector:(SEL)aSelector {
  72. KWExample *example = [[KWExampleSuiteBuilder sharedExampleSuiteBuilder] currentExample];
  73. if ([example respondsToSelector:aSelector]) {
  74. return example;
  75. } else {
  76. return [super forwardingTargetForSelector:aSelector];
  77. }
  78. }
  79. + (BOOL)respondsToSelector:(SEL)aSelector {
  80. if ([super respondsToSelector:aSelector]) {
  81. return YES;
  82. }
  83. KWExample *example = [[KWExampleSuiteBuilder sharedExampleSuiteBuilder] currentExample];
  84. return [example respondsToSelector:aSelector];
  85. }
  86. #pragma mark - Running Specs
  87. - (void)runExample {
  88. self.currentExample = self.invocation.kw_example;
  89. @try {
  90. [self.currentExample runWithDelegate:self];
  91. } @catch (NSException *exception) {
  92. [self recordFailureWithDescription:exception.description inFile:@"" atLine:0 expected:NO];
  93. }
  94. self.invocation.kw_example = nil;
  95. }
  96. #pragma mark - KWExampleGroupDelegate methods
  97. - (void)example:(KWExample *)example didFailWithFailure:(KWFailure *)failure {
  98. [self recordFailureWithDescription:failure.message
  99. inFile:failure.callSite.filename
  100. atLine:failure.callSite.lineNumber
  101. expected:NO];
  102. }
  103. #pragma mark - Verification proxies
  104. + (id)addVerifier:(id<KWVerifying>)aVerifier {
  105. return [[[KWExampleSuiteBuilder sharedExampleSuiteBuilder] currentExample] addVerifier:aVerifier];
  106. }
  107. + (id)addExistVerifierWithExpectationType:(KWExpectationType)anExpectationType callSite:(KWCallSite *)aCallSite {
  108. return [[[KWExampleSuiteBuilder sharedExampleSuiteBuilder] currentExample] addExistVerifierWithExpectationType:anExpectationType callSite:aCallSite];
  109. }
  110. + (id)addMatchVerifierWithExpectationType:(KWExpectationType)anExpectationType callSite:(KWCallSite *)aCallSite {
  111. return [[[KWExampleSuiteBuilder sharedExampleSuiteBuilder] currentExample] addMatchVerifierWithExpectationType:anExpectationType callSite:aCallSite];
  112. }
  113. + (id)addAsyncVerifierWithExpectationType:(KWExpectationType)anExpectationType callSite:(KWCallSite *)aCallSite timeout:(NSTimeInterval)timeout shouldWait:(BOOL)shouldWait {
  114. return [[[KWExampleSuiteBuilder sharedExampleSuiteBuilder] currentExample] addAsyncVerifierWithExpectationType:anExpectationType callSite:aCallSite timeout:timeout shouldWait: shouldWait];
  115. }
  116. @end