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