/
SOCKitTests.m
executable file
·195 lines (152 loc) · 9.81 KB
/
SOCKitTests.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
//
// Copyright 2011 Jeff Verkoeyen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import <SenTestingKit/SenTestingKit.h>
#import <QuartzCore/QuartzCore.h>
#import "SOCKit.h"
typedef void (^SimpleBlock)(void);
@interface SOCTestObject : NSObject
- (id)initWithId:(NSInteger)ident floatValue:(CGFloat)flv doubleValue:(double)dv longLongValue:(long long)llv stringValue:(NSString *)string;
- (id)initWithId:(NSInteger)ident floatValue:(CGFloat)flv doubleValue:(double)dv longLongValue:(long long)llv stringValue:(NSString *)string userInfo:(id)userInfo;
@property (nonatomic, readwrite, assign) NSInteger ident;
@property (nonatomic, readwrite, assign) CGFloat flv;
@property (nonatomic, readwrite, assign) double dv;
@property (nonatomic, readwrite, assign) long long llv;
@property (nonatomic, readwrite, copy) NSString* string;
@end
@implementation SOCTestObject
@synthesize ident;
@synthesize flv;
@synthesize dv;
@synthesize llv;
@synthesize string;
- (void)dealloc {
[string release]; string = nil;
[super dealloc];
}
- (id)initWithId:(NSInteger)anIdent floatValue:(CGFloat)anFlv doubleValue:(double)aDv longLongValue:(long long)anLlv stringValue:(NSString *)aString {
if ((self = [super init])) {
self.ident = anIdent;
self.flv = anFlv;
self.dv = aDv;
self.llv = anLlv;
self.string = aString;
}
return self;
}
- (id)initWithId:(NSInteger)anIdent floatValue:(CGFloat)anFlv doubleValue:(double)aDv longLongValue:(long long)anLlv stringValue:(NSString *)aString userInfo:(id)userInfo {
return [self initWithId:anIdent floatValue:anFlv doubleValue:aDv longLongValue:anLlv stringValue:aString];
}
@end
@interface SOCKitTests : SenTestCase
@end
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation SOCKitTests
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)testEmptyCases {
STAssertTrue([SOCStringFromStringWithObject(nil, nil) isEqualToString:@""], @"Should be the same string.");
STAssertTrue([SOCStringFromStringWithObject(@"", nil) isEqualToString:@""], @"Should be the same string.");
STAssertTrue([SOCStringFromStringWithObject(@" ", nil) isEqualToString:@" "], @"Should be the same string.");
STAssertTrue([SOCStringFromStringWithObject(@"abcdef", nil) isEqualToString:@"abcdef"], @"Should be the same string.");
STAssertTrue([SOCStringFromStringWithObject(@"abcdef", [NSArray array]) isEqualToString:@"abcdef"], @"Should be the same string.");
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)testFailureCases {
STAssertThrows([SOCPattern patternWithString:@":dilly:isacat"], @"Parameters must be separated by strings.");
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)testSingleParameterCoding {
NSDictionary* obj = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:1337], @"leet",
[NSNumber numberWithInt:5000], @"five",
nil];
STAssertTrue([SOCStringFromStringWithObject(@":leet", obj) isEqualToString:@"1337"], @"Should be the same string.");
STAssertTrue([SOCStringFromStringWithObject(@":five", obj) isEqualToString:@"5000"], @"Should be the same string.");
STAssertTrue([SOCStringFromStringWithObject(@":six", obj) isEqualToString:@"(null)"], @"Should be the same string.");
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)testMultiParameterCoding {
NSDictionary* obj = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:1337], @"leet",
[NSNumber numberWithInt:5000], @"five",
nil];
STAssertTrue([SOCStringFromStringWithObject(@":leet/:five", obj) isEqualToString:@"1337/5000"], @"Should be the same string.");
STAssertTrue([SOCStringFromStringWithObject(@":five/:five", obj) isEqualToString:@"5000/5000"], @"Should be the same string.");
STAssertTrue([SOCStringFromStringWithObject(@":five/:five/:five/:five/:five/:five", obj) isEqualToString:@"5000/5000/5000/5000/5000/5000"], @"Should be the same string.");
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)testCollectionOperators {
NSDictionary* obj = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:1337], @"leet",
[NSNumber numberWithInt:5000], @"five",
nil];
STAssertTrue([SOCStringFromStringWithObject(@":@count", obj) isEqualToString:@"2"], @"Should be the same string.");
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)testOutboundParameters {
SOCPattern* pattern = [SOCPattern patternWithString:@"soc://:ident"];
STAssertTrue([pattern stringMatches:@"soc://3"], @"String should conform.");
STAssertTrue([pattern stringMatches:@"soc://33413413454353254235245235"], @"String should conform.");
STAssertFalse([pattern stringMatches:@""], @"String should not conform.");
STAssertFalse([pattern stringMatches:@"soc://"], @"String should not conform.");
STAssertTrue([pattern stringMatches:@"soc://joe"], @"String might conform.");
pattern = [SOCPattern patternWithString:@"soc://:ident/sandwich"];
STAssertTrue([pattern stringMatches:@"soc://3/sandwich"], @"String should conform.");
STAssertTrue([pattern stringMatches:@"soc://33413413454353254235245235/sandwich"], @"String should conform.");
STAssertFalse([pattern stringMatches:@""], @"String should not conform.");
STAssertFalse([pattern stringMatches:@"soc://"], @"String should not conform.");
STAssertFalse([pattern stringMatches:@"soc:///sandwich"], @"String should not conform.");
pattern = [SOCPattern patternWithString:@"soc://:ident/sandwich/:catName"];
STAssertTrue([pattern stringMatches:@"soc://3/sandwich/dilly"], @"String should conform.");
STAssertTrue([pattern stringMatches:@"soc://33413413454353254235245235/sandwich/dilly"], @"String should conform.");
STAssertFalse([pattern stringMatches:@""], @"String should not conform.");
STAssertFalse([pattern stringMatches:@"soc://"], @"String should not conform.");
STAssertFalse([pattern stringMatches:@"soc://33413413454353254235245235/sandwich/"], @"String should not conform.");
STAssertFalse([pattern stringMatches:@"soc:///sandwich/"], @"String should not conform.");
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)testPerformSelectorOnObjectWithSourceString {
SOCPattern* pattern = [SOCPattern patternWithString:@"soc://:ident/:flv/:dv/:llv/:string"];
SOCTestObject* testObject = [pattern performSelector:@selector(initWithId:floatValue:doubleValue:longLongValue:stringValue:userInfo:) onObject:[SOCTestObject class] sourceString:@"soc://3/3.5/6.14/13413143124321/dilly"];
STAssertEquals(testObject.ident, (NSInteger)3, @"Values should be equal.");
STAssertEquals(testObject.flv, (CGFloat)3.5, @"Values should be equal.");
STAssertEquals(testObject.dv, 6.14, @"Values should be equal.");
STAssertEquals(testObject.llv, (long long)13413143124321, @"Values should be equal.");
STAssertTrue([testObject.string isEqualToString:@"dilly"], @"Values should be equal.");
testObject = [pattern performSelector:@selector(initWithId:floatValue:doubleValue:longLongValue:stringValue:) onObject:[SOCTestObject class] sourceString:@"soc://3/3.5/6.14/13413143124321/dilly"];
STAssertEquals(testObject.ident, (NSInteger)3, @"Values should be equal.");
STAssertEquals(testObject.flv, (CGFloat)3.5, @"Values should be equal.");
STAssertEquals(testObject.dv, 6.14, @"Values should be equal.");
STAssertEquals(testObject.llv, (long long)13413143124321, @"Values should be equal.");
STAssertTrue([testObject.string isEqualToString:@"dilly"], @"Values should be equal.");
pattern = [SOCPattern patternWithString:@"soc://:ident"];
[pattern performSelector:@selector(setIdent:) onObject:testObject sourceString:@"soc://6"];
STAssertEquals(testObject.ident, (NSInteger)6, @"Values should be equal.");
[pattern performSelector:@selector(setLlv:) onObject:testObject sourceString:@"soc://6"];
STAssertEquals(testObject.llv, (long long)6, @"Values should be equal.");
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)testExtractParameterKeyValuesFromSourceString {
SOCPattern* pattern = [SOCPattern patternWithString:@"soc://:ident/:flv/:dv/:llv/:string"];
NSDictionary* kvs = [pattern extractParameterKeyValuesFromSourceString:@"soc://3/3.5/6.14/13413143124321/dilly"];
STAssertEquals([[kvs objectForKey:@"ident"] intValue], 3, @"Values should be equal.");
STAssertEquals([[kvs objectForKey:@"flv"] floatValue], 3.5f, @"Values should be equal.");
STAssertEquals([[kvs objectForKey:@"dv"] doubleValue], 6.14, @"Values should be equal.");
STAssertEquals([[kvs objectForKey:@"llv"] longLongValue], 13413143124321L, @"Values should be equal.");
STAssertTrue([[kvs objectForKey:@"string"] isEqualToString:@"dilly"], @"Values should be equal.");
}
@end