forked from tidev/titanium-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKrollCoverage.m
291 lines (252 loc) · 8.42 KB
/
KrollCoverage.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2011 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#ifdef KROLL_COVERAGE
#import "KrollCoverage.h"
#import "TiUtils.h"
#import "TiModule.h"
#import "SBJSON.h"
@implementation KrollCoverageObject
@synthesize componentType, componentName;
static NSMutableDictionary *coverageCount = nil;
static NSArray* submodules = nil;
+(void)incrementCoverage:(NSString*)componentType_ componentName:(NSString*)componentName_
apiName:(NSString*)apiName_ coverageType:(NSString*)coverageType_ apiType:(NSString*)apiType_
{
if (coverageCount == nil)
{
coverageCount = [[NSMutableDictionary alloc] init];
}
NSMutableDictionary *components = [coverageCount objectForKey:componentType_];
if (components == nil)
{
components = [[NSMutableDictionary alloc] init];
[coverageCount setObject:components forKey:componentType_];
}
NSMutableDictionary *component = [components objectForKey:componentName_];
if (component == nil)
{
component = [[NSMutableDictionary alloc] init];
[components setObject:component forKey:componentName_];
}
NSMutableDictionary *coverage = [component objectForKey:apiName_];
if (coverage == nil)
{
coverage = [[NSMutableDictionary alloc] init];
[component setObject:coverage forKey:apiName_];
}
[coverage setObject:apiType_ forKey:@"_type"];
int count = [TiUtils intValue:coverageType_ properties:coverage def:0];
count++;
[coverage setObject:[NSNumber numberWithInt:count] forKey:coverageType_];
}
+(void)incrementTopLevelFunctionCall:(NSString*)componentName_ name:(NSString*)apiName
{
[KrollCoverageObject incrementCoverage:COMPONENT_TYPE_OTHER componentName:componentName_ apiName:apiName
coverageType:COVERAGE_TYPE_CALL apiType:API_TYPE_FUNCTION];
}
+(NSDictionary*)dumpCoverage
{
return coverageCount;
}
+(void)releaseCoverage
{
[coverageCount removeAllObjects];
[coverageCount release];
coverageCount = nil;
}
-(id)initWithTarget:(id)target_ context:(KrollContext*)context_
{
if (self = [super initWithTarget:target_ context:context_])
{
componentType = COMPONENT_TYPE_PROXIES;
NSString *className = [NSString stringWithCString:class_getName([target_ class]) encoding:NSUTF8StringEncoding];
if ([target_ isKindOfClass:[TiModule class]])
{
componentType = COMPONENT_TYPE_MODULES;
[self setComponentName:[[NSString stringWithFormat:@"Ti%@.",@"tanium"] stringByAppendingString:[className stringByReplacingOccurrencesOfString:@"Module" withString:@""]]];
}
else
{
NSString *apiName = [className stringByReplacingOccurrencesOfString:@"Proxy" withString:@""];
// Because of the substitution performed as part of the build, have to split 'Ti''tanium' into two strings
NSString *parentName = [NSString stringWithFormat:@"Ti%@",@"tanium"];
if ([apiName hasPrefix:@"Ti"])
{
apiName = [apiName substringFromIndex:2];
}
// Slight hack.. we can guess the module by hard coding any all-caps module (UI/API/XML)
// Otherwise, split on the next capital letter (is there a better way?)
if ([apiName hasPrefix:@"UI"])
{
parentName = @"UI";
apiName = [apiName substringFromIndex:2];
}
else if ([apiName hasPrefix:@"API"])
{
parentName = @"API";
apiName = [apiName substringFromIndex:3];
}
else if ([apiName hasPrefix:@"XML"])
{
parentName = @"XML";
apiName = [apiName substringFromIndex:3];
}
else if ([apiName isEqual:@"TopTiModule"])
{
apiName = @"";
}
else if ([apiName length] > 1)
{
NSCharacterSet *upperChars = [NSCharacterSet uppercaseLetterCharacterSet];
int i = 1, length = [apiName length];
// Find the first upper case char after the first char
for (; i < length; i++)
{
if ([upperChars characterIsMember:[apiName characterAtIndex:i]])
{
break;
}
}
if (i < length - 1)
{
parentName = [apiName substringToIndex:i];
apiName = [apiName substringFromIndex:i];
}
// Determine if we're in a submodule
if (submodules == nil) {
submodules = [[NSArray alloc] initWithObjects:@"iOS",@"iPhone",@"iPad",@"Socket",@"Properties",nil];
}
for (NSString* submodule in submodules) {
if ([apiName isEqualToString:submodule]) {
break;
}
NSRange pos = [apiName rangeOfString:submodule];
if (pos.location != NSNotFound) {
apiName = [NSString stringWithFormat:@"%@.%@",submodule,[apiName substringFromIndex:(pos.location+pos.length)]];
break;
}
}
}
// Note: This doesn't properly detect things like submodules right now
if ([apiName length] > 0)
{
[self setComponentName:[NSString stringWithFormat:@"%@.%@", parentName, apiName]];
}
else
{
[self setComponentName:parentName];
}
}
}
return self;
}
-(id)initWithTarget:(id)target_ context:(KrollContext*)context_ componentName:(NSString*)componentName_
{
if (self = [super initWithTarget:target_ context:context_])
{
componentType = COMPONENT_TYPE_PROXIES;
if ([target_ isKindOfClass:[TiModule class]])
{
componentType = COMPONENT_TYPE_MODULES;
}
[self setComponentName:componentName_];
}
return self;
}
-(void)increment:(NSString*)apiName coverageType:(NSString*)coverageType apiType:(NSString*)apiType;
{
[KrollCoverageObject incrementCoverage:componentType componentName:componentName
apiName:apiName coverageType:coverageType apiType:apiType];
}
-(NSString*)coverageName
{
return componentName;
}
-(NSString*)coverageType
{
return componentType;
}
@end
@implementation KrollCoverageMethod
@synthesize parentType, parentName;
-(id)initWithTarget:(id)target_ context:(KrollContext *)context_ parent:(id)parent_
{
if (self = [super initWithTarget:target_ context:context_])
{
parent = [parent_ retain];
}
return self;
}
-(id)initWithTarget:(id)target_ selector:(SEL)selector_
argcount:(int)argcount_ type:(KrollMethodType)type_ name:(id)name_
context:(KrollContext*)context_ parent:(id<KrollCoverage>)parent_
{
if (self = [super initWithTarget:target_ selector:selector_ argcount:argcount_ type:type_ name:name_ context:context_])
{
parent = [parent_ retain];
}
return self;
}
-(void)dealloc
{
RELEASE_TO_NIL(parent);
[super dealloc];
}
-(id)call:(NSArray*)args
{
NSString *coverageType;
NSString *apiType;
NSString *apiName = [super name];
switch ([super type])
{
//case KrollMethodGetter:
case KrollMethodPropertyGetter:
{
coverageType = COVERAGE_TYPE_GET;
apiType = API_TYPE_PROPERTY;
NSString *propertyKey = [super propertyKey];
NSString *upperProperty = [[[propertyKey substringToIndex:1] uppercaseString] stringByAppendingString:[propertyKey substringFromIndex:1]];
apiName = [NSString stringWithFormat:@"get%@",upperProperty];
} break;
//case KrollMethodSetter:
case KrollMethodPropertySetter:
{
coverageType = COVERAGE_TYPE_SET;
apiType = API_TYPE_PROPERTY;
NSString *propertyKey = [super propertyKey];
NSString *upperProperty = [[[propertyKey substringToIndex:1] uppercaseString] stringByAppendingString:[propertyKey substringFromIndex:1]];
apiName = [NSString stringWithFormat:@"set%@",upperProperty];
} break;
default:
{
coverageType = COVERAGE_TYPE_CALL;
apiType = API_TYPE_FUNCTION;
if (apiName == nil)
{
apiName = [NSString stringWithCString:sel_getName([self selector]) encoding:NSUTF8StringEncoding];
apiName = [apiName substringToIndex:[apiName length]-1];
}
}
}
[KrollCoverageObject incrementCoverage:[self coverageType] componentName:[self coverageName]
apiName:apiName coverageType:coverageType apiType:apiType];
return [super call:args];
}
-(void)increment:(NSString*)apiName coverageType:(NSString*)coverageType apiType:(NSString*)apiType
{
[parent increment:apiName coverageType:coverageType apiType:apiType];
}
-(NSString*)coverageName
{
return [parent coverageName];
}
-(NSString*)coverageType
{
return [parent coverageType];
}
@end
#endif