/
UISpecCommandReceiver.m
67 lines (52 loc) · 1.92 KB
/
UISpecCommandReceiver.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
//
// UISpecCommandReceiver.m
// UICatalog
//
// Created by phodgson on 5/23/10.
// Copyright 2010 ThoughtWorks. See NOTICE file for details.
//
#import "UISpecCommandReceiver.h"
#import "UIQuery.h"
@implementation UISpecCommandReceiver
- (NSArray *)performSelector:(NSString *)selectorString onViewsIn:(UIQuery *)query{
NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:[[query views]count]];
for (NSObject *view in [query views]) {
NSString *value = [NSString stringWithFormat:@"%@", [view performSelector:NSSelectorFromString(selectorString)] ];
[resultArray addObject:value];
}
return resultArray;
}
- (NSString*)evaluateCommandInParts:(NSArray *)commandParts {
NSString *commandType = [ (NSString *)[commandParts objectAtIndex:0] lowercaseString];
NSString *queryString = (NSString *)[commandParts objectAtIndex:1];
UIQuery *query;
query = $( [NSMutableString stringWithString:queryString] );
if( [@"perform" isEqual:commandType] )
return @"DONE";
else if( [@"get" isEqual:commandType] )
{
NSString *selectorString = (NSString *)[commandParts objectAtIndex:2];
NSArray *values = [self performSelector:selectorString onViewsIn:query];
return [values componentsJoinedByString:@"\n"];
}else if ([@"inspect" isEqual:commandType]) {
[query inspect];
return @"INSPECTED";
}else{
return [NSString stringWithFormat:@"Unrecognized command type %@",commandType];
}
}
- (NSString*)runCommandStep:(NSData*)commandAsData {
NSString *command = [[NSString alloc] initWithData:commandAsData encoding:NSASCIIStringEncoding];
NSLog( @"Received UISpec Command: %@",command);
NSArray *commandParts = [command componentsSeparatedByString:@"\n"];
@try {
NSString *result = [self evaluateCommandInParts:commandParts];
NSLog( @"Returning:\n%@",result);
return result;
}
@catch (NSException * e) {
NSLog( @"Exception!: %@", e );
return [NSString stringWithFormat: @"Exception! %@", e];
}
}
@end