This repository was archived by the owner on Aug 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMPWTagAction.m
More file actions
260 lines (212 loc) · 8.12 KB
/
MPWTagAction.m
File metadata and controls
260 lines (212 loc) · 8.12 KB
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
//
// MPWTagAction.m
// ObjectiveXML
//
// Created by Marcel Weiher on 7/12/12.
//
//
#import "MPWTagAction.h"
#import "mpwfoundation_imports.h"
@interface MPWTagAction(testingSupport)
+defaultElement:childen attributes:children parser:parser;
@end
@implementation MPWTagAction
objectAccessor(NSString, tagName, setTagName)
objectAccessor(NSString, mappedName, setMappedName)
objectAccessor(NSString, tagNamespace, setTagNamespace)
objectAccessor(NSString, namespacePrefix, setNamespacePrefix)
objectAccessor(MPWFastInvocation, tagAction, setTagAction)
objectAccessor(MPWFastInvocation, elementAction, setElementAction)
-initWithTagName:(NSString*)tag
{
self=[super init];
[self setTagName:tag];
[self setNamespacePrefix:@""];
return self;
}
+tagActionWithName:(NSString*)tag
{
return [[[self alloc] initWithTagName:tag] autorelease];
}
+undeclaredElementAction
{
return [self tagActionWithName:@"undeclared"];
}
-(NSString*)mappedOrNormalTagName
{
id result=[self mappedName];
if ( !result){
result=[self tagName];
}
return result;
}
-(SEL)elementSelector
{
return NSSelectorFromString( [[[self mappedOrNormalTagName] stringByAppendingString:namespacePrefix ] stringByAppendingString:@"Element:attributes:parser:"] );
}
-(SEL)tagSelector
{
return NSSelectorFromString( [[self mappedOrNormalTagName] stringByAppendingString:@"Tag:parser:"] );
}
-(SEL)defaultElementSelector
{
return @selector(defaultElement:attributes:parser:);
}
-(BOOL)targetRespondsToElementSelector:aTarget
{
return [aTarget respondsToSelector:[self elementSelector]];
}
-(SEL)elementSelectorForTarget:aTarget
{
return [self targetRespondsToElementSelector:aTarget] ? [self elementSelector]:[self defaultElementSelector];
}
-(MPWFastInvocation*)createElementInvocationWithTarget:primaryTarget backup:backupTarget
{
MPWFastInvocation *invocation=[[MPWFastInvocation new] autorelease];
id target=primaryTarget;
BOOL primaryHasSelector=[self targetRespondsToElementSelector:primaryTarget];
BOOL backupHasSelector=[self targetRespondsToElementSelector:backupTarget];
BOOL primaryHasDefault=[primaryTarget respondsToSelector:[self defaultElementSelector]];
if ( !primaryHasSelector) {
if ( backupHasSelector || !primaryHasDefault ) {
target=backupTarget;
}
}
[invocation setTarget:target];
[invocation setSelector:[self elementSelectorForTarget:target]];
[invocation setUseCaching:YES];
return invocation;
}
-(void)setElementInvocationForTarget:primaryTarget backup:backupTarget
{
[self setElementAction:[self createElementInvocationWithTarget:primaryTarget backup:backupTarget]];
}
-(void)setTagInvocationForTarget:primaryTarget
{
MPWFastInvocation *invocation=[[MPWFastInvocation new] autorelease];
[invocation setTarget:primaryTarget];
[invocation setSelector:[self tagSelector]];
[invocation setUseCaching:YES];
[self setTagAction:invocation];
}
-(void)dealloc
{
[tagName release];
[mappedName release];
[tagNamespace release];
[tagAction release];
[elementAction release];
[super dealloc];
}
@end
#import "DebugMacros.h"
@implementation MPWTagAction(testing)
+helloElement:childen attributes:children parser:parser
{
return [@"54" retain];
}
+defaultElement:childen attributes:children parser:parser
{
return [@"default" retain];
}
+(void)testBasicTagName
{
IDEXPECT([[self tagActionWithName:@"hello"] tagName],@"hello",@"tag name");
}
+(void)testElementSelectorName
{
id action=[self tagActionWithName:@"hello"];
IDEXPECT( NSStringFromSelector([action elementSelector]),@"helloElement:attributes:parser:", @"element selector for hello");
}
+(void)testTagSelectorName
{
id action=[self tagActionWithName:@"hello"];
IDEXPECT( NSStringFromSelector([action tagSelector]),@"helloTag:parser:", @"element selector for hello");
}
+(void)testMappedSelectorName
{
id action=[self tagActionWithName:@"hello"];
[action setMappedName:@"hi"];
IDEXPECT( NSStringFromSelector([action tagSelector]),@"hiTag:parser:", @"element selector for hello mapped to hi");
}
+(void)testDefaultSelector
{
id action=[self tagActionWithName:@"hello"];
IDEXPECT( NSStringFromSelector([action defaultElementSelector]),@"defaultElement:attributes:parser:", @"element selector for hello mapped to hi");
}
+(void)testTargetRespondsToElementSelector
{
id action1=[self tagActionWithName:@"hello"];
id action2=[self tagActionWithName:@"hi"];
EXPECTTRUE([action1 targetRespondsToElementSelector:self], @"responds to helloElemenet")
EXPECTFALSE([action2 targetRespondsToElementSelector:self], @"responds to hiElemenet")
}
+(void)testElementSelectorOrDefaultSelectorIfTargetDoesntRespond
{
id action1=[self tagActionWithName:@"hello"];
id action2=[self tagActionWithName:@"hi"];
IDEXPECT( NSStringFromSelector([action1 elementSelectorForTarget:self]),@"helloElement:attributes:parser:", @"exists, not defaulted");
IDEXPECT( NSStringFromSelector([action2 elementSelectorForTarget:self]),@"defaultElement:attributes:parser:", @"does not exist, defaulted");
}
+(void)testCreateElementInvocationWithBackupTargetAndDefaultSelector
{
id action1=[self tagActionWithName:@"hello"];
id action2=[self tagActionWithName:@"hi"];
MPWFastInvocation *i1=[action1 createElementInvocationWithTarget:self backup:action1];
IDEXPECT( NSStringFromSelector([i1 selector]),@"helloElement:attributes:parser:" , @"nondefaulted selector");
IDEXPECT([i1 target], self, @"original target");
MPWFastInvocation *i2=[action1 createElementInvocationWithTarget:action1 backup:self];
IDEXPECT( NSStringFromSelector([i2 selector]),@"helloElement:attributes:parser:" , @"nondefaulted selector because backup object handles exact element");
IDEXPECT([i2 target], self, @"backup target");
MPWFastInvocation *i3=[action2 createElementInvocationWithTarget:self backup:action1];
IDEXPECT( NSStringFromSelector([i3 selector]),@"defaultElement:attributes:parser:" , @"nondefaulted selector because backup object handles exact element");
IDEXPECT([i3 target], self, @"primary target with default selector");
MPWFastInvocation *i4=[action2 createElementInvocationWithTarget:action1 backup:self];
IDEXPECT( NSStringFromSelector([i4 selector]),@"defaultElement:attributes:parser:" , @"backup target, default selector");
IDEXPECT([i4 target], self, @"backup target, default selector");
}
+(void)testUndeclaredElementExistsAndHasElementSelector
{
id action=[self undeclaredElementAction];
IDEXPECT( NSStringFromSelector([action elementSelector]),@"undeclaredElement:attributes:parser:", @"undeclared elemenet selector");
}
+(void)testSetTagInvocation
{
id action=[self undeclaredElementAction];
EXPECTNIL([action tagAction], @"tag action before I set it up");
[action setTagInvocationForTarget:self];
IDEXPECT(NSStringFromSelector([[action tagAction] selector]), @"undeclaredTag:parser:", @"selector of tag action");
IDEXPECT([[action tagAction] target], self, @"target of tag action");
}
+(void)testSetElementInvocation
{
id action1=[self tagActionWithName:@"hello"];
[action1 setElementInvocationForTarget:self backup:action1];
MPWFastInvocation *i1=[action1 elementAction];
IDEXPECT( NSStringFromSelector([i1 selector]),@"helloElement:attributes:parser:" , @"nondefaulted selector");
IDEXPECT([i1 target], self, @"original target");
}
+(void)testPrefix
{
id action1=[self tagActionWithName:@"hello"];
[action1 setNamespacePrefix:@"Namespace"];
IDEXPECT( NSStringFromSelector([action1 elementSelector]),@"helloNamespaceElement:attributes:parser:" , @"prefixed selector");
}
+testSelectors
{
return @[
@"testBasicTagName",
@"testTagSelectorName",
@"testElementSelectorName",
@"testMappedSelectorName",
@"testDefaultSelector",
@"testTargetRespondsToElementSelector",
@"testElementSelectorOrDefaultSelectorIfTargetDoesntRespond",
@"testCreateElementInvocationWithBackupTargetAndDefaultSelector",
@"testUndeclaredElementExistsAndHasElementSelector",
@"testSetTagInvocation",
@"testSetElementInvocation",
@"testPrefix",
];
}
@end