-
Notifications
You must be signed in to change notification settings - Fork 644
/
GBObjectiveCParser-ProtocolParsingTesting.m
189 lines (167 loc) · 8.55 KB
/
GBObjectiveCParser-ProtocolParsingTesting.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//
// GBObjectiveCParser-ProtocolParsingTesting.m
// appledoc
//
// Created by Tomaz Kragelj on 25.7.10.
// Copyright (C) 2010 Gentle Bytes. All rights reserved.
//
#import "GBStore.h"
#import "GBDataObjects.h"
#import "GBObjectiveCParser.h"
// Note that we're only testing protocol specific stuff here - i.e. all common parsing modules (adopted protocols, ivars, methods...) are tested separately to avoid repetition.
@interface GBObjectiveCParserProtocolsParsingTesting : GBObjectsAssertor
@end
@implementation GBObjectiveCParserProtocolsParsingTesting
#pragma mark Protocols common data parsing testing
- (void)testParseObjectsFromString_shouldRegisterProtocolDefinition {
// setup
GBObjectiveCParser *parser = [GBObjectiveCParser parserWithSettingsProvider:[GBTestObjectsRegistry mockSettingsProvider]];
GBStore *store = [[GBStore alloc] init];
// execute
[parser parseObjectsFromString:@"@protocol MyProtocol @end" sourceFile:@"filename.h" toStore:store];
// verify
NSArray *protocols = [store protocolsSortedByName];
assertThatInteger([protocols count], equalToInteger(1));
assertThat([[protocols objectAtIndex:0] nameOfProtocol], is(@"MyProtocol"));
}
- (void)testParseObjectsFromString_shouldRegisterProtocolSourceFileAndLineNumber {
// setup
GBObjectiveCParser *parser = [GBObjectiveCParser parserWithSettingsProvider:[GBTestObjectsRegistry mockSettingsProvider]];
GBStore *store = [[GBStore alloc] init];
// execute
[parser parseObjectsFromString:@"@protocol MyProtocol @end" sourceFile:@"filename.h" toStore:store];
// verify
NSSet *files = [[[store protocolsSortedByName] objectAtIndex:0] sourceInfos];
assertThatInteger([files count], equalToInteger(1));
assertThat([[files anyObject] filename], is(@"filename.h"));
assertThatInteger([[files anyObject] lineNumber], equalToInteger(1));
}
- (void)testParseObjectsFromString_shouldRegisterProtocolProperSourceLineNumber {
// setup
GBObjectiveCParser *parser = [GBObjectiveCParser parserWithSettingsProvider:[GBTestObjectsRegistry mockSettingsProvider]];
GBStore *store = [[GBStore alloc] init];
// execute
[parser parseObjectsFromString:@"\n// cmt\n\n#define DEBUG\n\n/// hello\n@protocol MyProtocol @end" sourceFile:@"filename.h" toStore:store];
// verify
NSSet *files = [[[store protocolsSortedByName] objectAtIndex:0] sourceInfos];
assertThatInteger([[files anyObject] lineNumber], equalToInteger(7));
}
- (void)testParseObjectsFromString_shouldRegisterAllProtocolDefinitions {
// setup
GBObjectiveCParser *parser = [GBObjectiveCParser parserWithSettingsProvider:[GBTestObjectsRegistry mockSettingsProvider]];
GBStore *store = [[GBStore alloc] init];
// execute
[parser parseObjectsFromString:@"@protocol MyProtocol1 @end @protocol MyProtocol2 @end" sourceFile:@"filename.h" toStore:store];
// verify
NSArray *protocols = [store protocolsSortedByName];
assertThatInteger([protocols count], equalToInteger(2));
assertThat([[protocols objectAtIndex:0] nameOfProtocol], is(@"MyProtocol1"));
assertThat([[protocols objectAtIndex:1] nameOfProtocol], is(@"MyProtocol2"));
}
#pragma mark Protocol comments parsing testing
- (void)testParseObjectsFromString_shouldRegisterProtocolDefinitionComment {
// setup
GBObjectiveCParser *parser = [GBObjectiveCParser parserWithSettingsProvider:[GBTestObjectsRegistry mockSettingsProvider]];
GBStore *store = [[GBStore alloc] init];
// execute
[parser parseObjectsFromString:@"/** Comment */ @protocol MyProtocol @end" sourceFile:@"filename.h" toStore:store];
// verify
GBProtocolData *protocol = [[store protocols] anyObject];
assertThat(protocol.comment.stringValue, is(@"Comment"));
}
- (void)testParseObjectsFromString_shouldRegisterProtocolDefinitionCommentSourceFileAndLineNumber {
// setup
GBObjectiveCParser *parser = [GBObjectiveCParser parserWithSettingsProvider:[GBTestObjectsRegistry mockSettingsProvider]];
GBStore *store = [[GBStore alloc] init];
// execute
[parser parseObjectsFromString:@"/// comment\n\n#define SOMETHING\n\n/** comment */ @protocol MyProtocol @end" sourceFile:@"filename.h" toStore:store];
// verify
GBClassData *protocol = [[store protocols] anyObject];
assertThat(protocol.comment.sourceInfo.filename, is(@"filename.h"));
assertThatInteger(protocol.comment.sourceInfo.lineNumber, equalToInteger(5));
}
- (void)testParseObjectsFromString_shouldRegisterProtocolDefinitionCommentForComplexDeclarations {
// setup
GBObjectiveCParser *parser = [GBObjectiveCParser parserWithSettingsProvider:[GBTestObjectsRegistry mockSettingsProvider]];
GBStore *store = [[GBStore alloc] init];
// execute
[parser parseObjectsFromString:
@"/** Comment */\n"
@"#ifdef SOMETHING\n"
@"@protocol MyProtocol\n"
@"#else\n"
@"@protocol MyProtocol1\n"
@"#endif\n"
@"@end" sourceFile:@"filename.h" toStore:store];
// verify
GBProtocolData *protocol = [store.protocols anyObject];
assertThat(protocol.nameOfProtocol, is(@"MyProtocol"));
assertThat(protocol.comment.stringValue, is(@"Comment"));
}
- (void)testParseObjectsFromString_shouldProperlyResetComments {
// setup
GBObjectiveCParser *parser = [GBObjectiveCParser parserWithSettingsProvider:[GBTestObjectsRegistry mockSettingsProvider]];
GBStore *store = [[GBStore alloc] init];
// execute
[parser parseObjectsFromString:@"/** Comment */ @protocol MyProtocol -(void)method; @end" sourceFile:@"filename.h" toStore:store];
// verify
GBProtocolData *protocol = [store.protocols anyObject];
GBMethodData *method = [protocol.methods.methods lastObject];
assertThat(method.comment, is(nil));
}
#pragma mark Protocol components parsing testing
- (void)testParseObjectsFromString_shouldRegisterAdoptedProtocols {
// setup
GBObjectiveCParser *parser = [GBObjectiveCParser parserWithSettingsProvider:[GBTestObjectsRegistry mockSettingsProvider]];
GBStore *store = [[GBStore alloc] init];
// execute
[parser parseObjectsFromString:@"@protocol MyProtocol <Protocol1, Protocol2> @end" sourceFile:@"filename.h" toStore:store];
// verify
GBProtocolData *protocol = [[store protocols] anyObject];
NSArray *protocols = [protocol.adoptedProtocols protocolsSortedByName];
assertThatInteger([protocols count], equalToInteger(2));
assertThat([[protocols objectAtIndex:0] nameOfProtocol], is(@"Protocol1"));
assertThat([[protocols objectAtIndex:1] nameOfProtocol], is(@"Protocol2"));
}
- (void)testParseObjectsFromString_shouldRegisterMethods {
// setup
GBObjectiveCParser *parser = [GBObjectiveCParser parserWithSettingsProvider:[GBTestObjectsRegistry mockSettingsProvider]];
GBStore *store = [[GBStore alloc] init];
// execute
[parser parseObjectsFromString:@"@protocol MyProtocol -(void)method1; -(void)method2; @end" sourceFile:@"filename.h" toStore:store];
// verify
GBProtocolData *protocol = [[store protocols] anyObject];
NSArray *methods = [protocol.methods methods];
assertThatInteger([methods count], equalToInteger(2));
assertThat([[methods objectAtIndex:0] methodSelector], is(@"method1"));
assertThat([[methods objectAtIndex:1] methodSelector], is(@"method2"));
}
#pragma mark Merging testing
- (void)testParseObjectsFromString_shouldMergeProtocolDefinitions {
// setup
GBObjectiveCParser *parser = [GBObjectiveCParser parserWithSettingsProvider:[GBTestObjectsRegistry mockSettingsProvider]];
GBStore *store = [[GBStore alloc] init];
// execute
[parser parseObjectsFromString:@"@protocol MyProtocol1 -(void)method1; @end" sourceFile:@"filename1.h" toStore:store];
[parser parseObjectsFromString:@"@protocol MyProtocol1 -(void)method2; @end" sourceFile:@"filename2.h" toStore:store];
// verify - simple testing here, details within GBModelBaseTesting!
assertThatInteger([[store protocols] count], equalToInteger(1));
GBProtocolData *protocol = [[store protocols] anyObject];
NSArray *methods = [protocol.methods methods];
assertThatInteger([methods count], equalToInteger(2));
assertThat([[methods objectAtIndex:0] methodSelector], is(@"method1"));
assertThat([[methods objectAtIndex:1] methodSelector], is(@"method2"));
}
#pragma mark Complex parsing testing
- (void)testParseObjectsFromString_shouldRegisterProtocolFromRealLifeInput {
// setup
GBObjectiveCParser *parser = [GBObjectiveCParser parserWithSettingsProvider:[GBTestObjectsRegistry mockSettingsProvider]];
GBStore *store = [[GBStore alloc] init];
// execute
[parser parseObjectsFromString:[GBRealLifeDataProvider headerWithClassCategoryAndProtocol] sourceFile:@"filename.h" toStore:store];
// verify - we're not going into details here, just checking that top-level objects were properly parsed!
assertThatInteger([[store protocols] count], equalToInteger(1));
GBProtocolData *protocol = [[store protocols] anyObject];
assertThat(protocol.nameOfProtocol, is(@"GBObserving"));
}
@end