PageRenderTime 85ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/src/Three20Core/UnitTests/CoreAdditionTests.m

https://github.com/GetMoPix/three20
Objective C | 574 lines | 362 code | 141 blank | 71 comment | 36 complexity | 8fee8cbb2ac6b1a0c79eb9e2c86defaf MD5 | raw file
  1. //
  2. // Copyright 2009-2011 Facebook
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // See: http://bit.ly/hS5nNh for unit test macros.
  17. // See Also: http://bit.ly/hgpqd2
  18. #import <SenTestingKit/SenTestingKit.h>
  19. // Core
  20. #import "Three20Core/TTCorePreprocessorMacros.h"
  21. #import "Three20Core/NSArrayAdditions.h"
  22. #import "Three20Core/NSDataAdditions.h"
  23. #import "Three20Core/NSMutableArrayAdditions.h"
  24. #import "Three20Core/NSMutableDictionaryAdditions.h"
  25. #import "Three20Core/NSStringAdditions.h"
  26. /**
  27. * Unit tests for the Core additions found within Three20. These tests are a part of
  28. * the comprehensive test suite for the Core functionality of the library.
  29. *
  30. * Notice:
  31. *
  32. * NSDateAdditions cannot be easily tested from a library unit test due to their dependence upon
  33. * TTLocalizedString. This is because the Three20.bundle file needs to be loaded for
  34. * TTLocalizedString to work, but the octest framework does not play well with bundles.
  35. * It tries to load the bundle from the simulator's /bin directory
  36. * which is not a place we can normally copy to from the Xcode project settings.
  37. */
  38. @interface CoreAdditionTests : SenTestCase {
  39. }
  40. @end
  41. ///////////////////////////////////////////////////////////////////////////////////////////////////
  42. ///////////////////////////////////////////////////////////////////////////////////////////////////
  43. ///////////////////////////////////////////////////////////////////////////////////////////////////
  44. @implementation CoreAdditionTests
  45. ///////////////////////////////////////////////////////////////////////////////////////////////////
  46. ///////////////////////////////////////////////////////////////////////////////////////////////////
  47. #pragma mark -
  48. #pragma mark NSDataAdditions
  49. ///////////////////////////////////////////////////////////////////////////////////////////////////
  50. - (void)testNSData_md5Hash {
  51. const char* bytes = "three20";
  52. NSData* data = [[NSData alloc] initWithBytes:bytes length:strlen(bytes)];
  53. STAssertTrue([[data md5Hash] isEqualToString:@"2804d14501153a0c8495afb3c1185012"],
  54. @"MD5 hashes don't match.");
  55. TT_RELEASE_SAFELY(data);
  56. }
  57. ///////////////////////////////////////////////////////////////////////////////////////////////////
  58. - (void)testNSData_sha1Hash {
  59. const char* bytes = "three20";
  60. NSData* data = [[NSData alloc] initWithBytes:bytes length:strlen(bytes)];
  61. STAssertTrue([[data sha1Hash] isEqualToString:@"ca264456199abfcc3023a880b6e924026ca57164"],
  62. @"SHA1 hashes don't match.");
  63. TT_RELEASE_SAFELY(data);
  64. }
  65. ///////////////////////////////////////////////////////////////////////////////////////////////////
  66. ///////////////////////////////////////////////////////////////////////////////////////////////////
  67. #pragma mark -
  68. #pragma mark NSStringAdditions
  69. ///////////////////////////////////////////////////////////////////////////////////////////////////
  70. - (void)testNSString_isWhitespace {
  71. // From the Apple docs:
  72. // Returns a character set containing only the whitespace characters space (U+0020) and tab
  73. // (U+0009) and the newline and nextline characters (U+000A–U+000D, U+0085).
  74. STAssertTrue([@"" isWhitespaceAndNewlines], @"Empty string should be whitespace.");
  75. STAssertTrue([@" " isWhitespaceAndNewlines], @"Space character should be whitespace.");
  76. STAssertTrue([@"\t" isWhitespaceAndNewlines], @"Tab character should be whitespace.");
  77. STAssertTrue([@"\n" isWhitespaceAndNewlines], @"Newline character should be whitespace.");
  78. STAssertTrue([@"\r" isWhitespaceAndNewlines], @"Carriage return character should be whitespace.");
  79. // Unicode whitespace
  80. for (int unicode = 0x000A; unicode <= 0x000D; ++unicode) {
  81. NSString* str = [NSString stringWithFormat:@"%C", unicode];
  82. STAssertTrue([str isWhitespaceAndNewlines],
  83. @"Unicode string #%X should be whitespace.", unicode);
  84. }
  85. NSString* str = [NSString stringWithFormat:@"%C", 0x0085];
  86. STAssertTrue([str isWhitespaceAndNewlines], @"Unicode string should be whitespace.");
  87. STAssertTrue([@" \t\r\n" isWhitespaceAndNewlines], @"Empty string should be whitespace.");
  88. STAssertTrue(![@"a" isWhitespaceAndNewlines], @"Text should not be whitespace.");
  89. STAssertTrue(![@" \r\n\ta\r\n " isWhitespaceAndNewlines], @"Text should not be whitespace.");
  90. }
  91. ///////////////////////////////////////////////////////////////////////////////////////////////////
  92. - (void)testNSString_isEmptyOrWhitespace {
  93. // From the Apple docs:
  94. // Returns a character set containing only the in-line whitespace characters space (U+0020)
  95. // and tab (U+0009).
  96. STAssertTrue([@"" isEmptyOrWhitespace], @"Empty string should be empty.");
  97. STAssertTrue([@" " isEmptyOrWhitespace], @"Space character should be whitespace.");
  98. STAssertTrue([@"\t" isEmptyOrWhitespace], @"Tab character should be whitespace.");
  99. STAssertTrue(![@"\n" isEmptyOrWhitespace], @"Newline character should not be whitespace.");
  100. STAssertTrue(![@"\r" isEmptyOrWhitespace],
  101. @"Carriage return character should not be whitespace.");
  102. // Unicode whitespace
  103. for (int unicode = 0x000A; unicode <= 0x000D; ++unicode) {
  104. NSString* str = [NSString stringWithFormat:@"%C", unicode];
  105. STAssertTrue(![str isEmptyOrWhitespace],
  106. @"Unicode string #%X should not be whitespace.", unicode);
  107. }
  108. NSString* str = [NSString stringWithFormat:@"%C", 0x0085];
  109. STAssertTrue(![str isEmptyOrWhitespace], @"Unicode string should not be whitespace.");
  110. STAssertTrue([@" \t" isEmptyOrWhitespace], @"Empty string should be whitespace.");
  111. STAssertTrue(![@"a" isEmptyOrWhitespace], @"Text should not be whitespace.");
  112. STAssertTrue(![@" \r\n\ta\r\n " isEmptyOrWhitespace], @"Text should not be whitespace.");
  113. }
  114. ///////////////////////////////////////////////////////////////////////////////////////////////////
  115. - (void)testNSString_stringByRemovingHTMLTags {
  116. STAssertTrue([[@"" stringByRemovingHTMLTags] isEqualToString:@""], @"Empty case failed");
  117. STAssertTrue([[@"&nbsp;" stringByRemovingHTMLTags] isEqualToString:@" "],
  118. @"Didn't translate nbsp entity");
  119. STAssertTrue([[@"&amp;" stringByRemovingHTMLTags] isEqualToString:@"&"],
  120. @"Didn't translate amp entity");
  121. STAssertTrue([[@"&quot;" stringByRemovingHTMLTags] isEqualToString:@"\""],
  122. @"Didn't translate quot entity");
  123. STAssertTrue([[@"&lt;" stringByRemovingHTMLTags] isEqualToString:@"<"],
  124. @"Didn't translate < entity");
  125. STAssertTrue([[@"&gt;" stringByRemovingHTMLTags] isEqualToString:@">"],
  126. @"Didn't translate > entity");
  127. STAssertTrue([[@"<html>" stringByRemovingHTMLTags] isEqualToString:@""], @"Failed to remove tag");
  128. STAssertTrue([[@"<html>three20</html>" stringByRemovingHTMLTags] isEqualToString:@"three20"],
  129. @"Failed to remove tag");
  130. STAssertTrue([[@"<span class=\"large\">three20</span>"
  131. stringByRemovingHTMLTags] isEqualToString:@"three20"], @"Failed to remove tag");
  132. }
  133. ///////////////////////////////////////////////////////////////////////////////////////////////////
  134. - (void)testNSString_queryDictionaryUsingEncoding {
  135. NSDictionary* query;
  136. query = [@"" queryDictionaryUsingEncoding:NSUTF8StringEncoding];
  137. STAssertTrue([query count] == 0, @"Query: %@", query);
  138. query = [@"q" queryDictionaryUsingEncoding:NSUTF8StringEncoding];
  139. STAssertTrue([query count] == 0, @"Query: %@", query);
  140. query = [@"q=" queryDictionaryUsingEncoding:NSUTF8StringEncoding];
  141. STAssertTrue([[query objectForKey:@"q"] isEqualToString:@""], @"Query: %@", query);
  142. query = [@"q=three20" queryDictionaryUsingEncoding:NSUTF8StringEncoding];
  143. STAssertTrue([[query objectForKey:@"q"] isEqualToString:@"three20"], @"Query: %@", query);
  144. query = [@"q=three20%20github" queryDictionaryUsingEncoding:NSUTF8StringEncoding];
  145. STAssertTrue([[query objectForKey:@"q"] isEqualToString:@"three20 github"], @"Query: %@", query);
  146. query = [@"q=three20&hl=en" queryDictionaryUsingEncoding:NSUTF8StringEncoding];
  147. STAssertTrue([[query objectForKey:@"q"] isEqualToString:@"three20"], @"Query: %@", query);
  148. STAssertTrue([[query objectForKey:@"hl"] isEqualToString:@"en"], @"Query: %@", query);
  149. query = [@"q=three20&hl=" queryDictionaryUsingEncoding:NSUTF8StringEncoding];
  150. STAssertTrue([[query objectForKey:@"q"] isEqualToString:@"three20"], @"Query: %@", query);
  151. STAssertTrue([[query objectForKey:@"hl"] isEqualToString:@""], @"Query: %@", query);
  152. query = [@"q=&&hl=" queryDictionaryUsingEncoding:NSUTF8StringEncoding];
  153. STAssertTrue([[query objectForKey:@"q"] isEqualToString:@""], @"Query: %@", query);
  154. STAssertTrue([[query objectForKey:@"hl"] isEqualToString:@""], @"Query: %@", query);
  155. query = [@"q=three20=repo&hl=en" queryDictionaryUsingEncoding:NSUTF8StringEncoding];
  156. STAssertNil([query objectForKey:@"q"], @"Query: %@", query);
  157. STAssertTrue([[query objectForKey:@"hl"] isEqualToString:@"en"], @"Query: %@", query);
  158. query = [@"&&" queryDictionaryUsingEncoding:NSUTF8StringEncoding];
  159. STAssertTrue([query count] == 0, @"Query: %@", query);
  160. }
  161. ///////////////////////////////////////////////////////////////////////////////////////////////////
  162. - (void)testNSString_queryContentsUsingEncoding {
  163. NSDictionary* query;
  164. query = [@"" queryContentsUsingEncoding:NSUTF8StringEncoding];
  165. STAssertTrue([query count] == 0, @"Query: %@", query);
  166. query = [@"q" queryContentsUsingEncoding:NSUTF8StringEncoding];
  167. STAssertTrue([[query objectForKey:@"q"] isEqual:[NSArray arrayWithObject:[NSNull null]]],
  168. @"Query: %@", query);
  169. query = [@"q=" queryContentsUsingEncoding:NSUTF8StringEncoding];
  170. STAssertTrue([[query objectForKey:@"q"] isEqual:[NSArray arrayWithObject:@""]],
  171. @"Query: %@", query);
  172. query = [@"q=three20" queryContentsUsingEncoding:NSUTF8StringEncoding];
  173. STAssertTrue([[query objectForKey:@"q"] isEqual:[NSArray arrayWithObject:@"three20"]],
  174. @"Query: %@", query);
  175. query = [@"q=three20%20github" queryContentsUsingEncoding:NSUTF8StringEncoding];
  176. STAssertTrue([[query objectForKey:@"q"] isEqual:[NSArray arrayWithObject:@"three20 github"]],
  177. @"Query: %@", query);
  178. query = [@"q=three20&hl=en" queryContentsUsingEncoding:NSUTF8StringEncoding];
  179. STAssertTrue([[query objectForKey:@"q"] isEqual:[NSArray arrayWithObject:@"three20"]],
  180. @"Query: %@", query);
  181. STAssertTrue([[query objectForKey:@"hl"] isEqual:[NSArray arrayWithObject:@"en"]],
  182. @"Query: %@", query);
  183. query = [@"q=three20&hl=" queryContentsUsingEncoding:NSUTF8StringEncoding];
  184. STAssertTrue([[query objectForKey:@"q"] isEqual:[NSArray arrayWithObject:@"three20"]],
  185. @"Query: %@", query);
  186. STAssertTrue([[query objectForKey:@"hl"] isEqual:[NSArray arrayWithObject:@""]],
  187. @"Query: %@", query);
  188. query = [@"q=&&hl=" queryContentsUsingEncoding:NSUTF8StringEncoding];
  189. STAssertTrue([[query objectForKey:@"q"] isEqual:[NSArray arrayWithObject:@""]],
  190. @"Query: %@", query);
  191. STAssertTrue([[query objectForKey:@"hl"] isEqual:[NSArray arrayWithObject:@""]],
  192. @"Query: %@", query);
  193. query = [@"q=three20=repo&hl=en" queryContentsUsingEncoding:NSUTF8StringEncoding];
  194. STAssertNil([query objectForKey:@"q"], @"Query: %@", query);
  195. STAssertTrue([[query objectForKey:@"hl"] isEqual:[NSArray arrayWithObject:@"en"]],
  196. @"Query: %@", query);
  197. query = [@"&&" queryContentsUsingEncoding:NSUTF8StringEncoding];
  198. STAssertTrue([query count] == 0, @"Query: %@", query);
  199. query = [@"q=foo&q=three20" queryContentsUsingEncoding:NSUTF8StringEncoding];
  200. NSArray* qArr = [NSArray arrayWithObjects:@"foo", @"three20", nil];
  201. STAssertTrue([[query objectForKey:@"q"] isEqual:qArr], @"Query: %@", query);
  202. query = [@"q=foo&q=three20&hl=en" queryContentsUsingEncoding:NSUTF8StringEncoding];
  203. qArr = [NSArray arrayWithObjects:@"foo", @"three20", nil];
  204. STAssertTrue([[query objectForKey:@"q"] isEqual:qArr], @"Query: %@", query);
  205. STAssertTrue([[query objectForKey:@"hl"] isEqual:[NSArray arrayWithObject:@"en"]],
  206. @"Query: %@", query);
  207. query = [@"q=foo&q=three20&hl=en&g" queryContentsUsingEncoding:NSUTF8StringEncoding];
  208. qArr = [NSArray arrayWithObjects:@"foo", @"three20", nil];
  209. STAssertTrue([[query objectForKey:@"q"] isEqual:qArr], @"Query: %@", query);
  210. STAssertTrue([[query objectForKey:@"hl"] isEqual:[NSArray arrayWithObject:@"en"]],
  211. @"Query: %@", query);
  212. STAssertTrue([[query objectForKey:@"g"] isEqual:[NSArray arrayWithObject:[NSNull null]]],
  213. @"Query: %@", query);
  214. query = [@"q&q=three20&hl=en&g" queryContentsUsingEncoding:NSUTF8StringEncoding];
  215. qArr = [NSArray arrayWithObjects:[NSNull null], @"three20", nil];
  216. STAssertTrue([[query objectForKey:@"q"] isEqual:qArr], @"Query: %@", query);
  217. STAssertTrue([[query objectForKey:@"hl"] isEqual:[NSArray arrayWithObject:@"en"]],
  218. @"Query: %@", query);
  219. }
  220. ///////////////////////////////////////////////////////////////////////////////////////////////////
  221. - (void)testNSString_stringByAddingQueryDictionary {
  222. NSString* baseUrl = @"http://google.com/search";
  223. STAssertTrue([[baseUrl stringByAddingQueryDictionary:nil] isEqualToString:
  224. [baseUrl stringByAppendingString:@"?"]], @"Empty dictionary fail.");
  225. STAssertTrue([[baseUrl stringByAddingQueryDictionary:[NSDictionary dictionary]] isEqualToString:
  226. [baseUrl stringByAppendingString:@"?"]], @"Empty dictionary fail.");
  227. STAssertTrue([[baseUrl stringByAddingQueryDictionary:[NSDictionary
  228. dictionaryWithObject:@"three20" forKey:@"q"]] isEqualToString:
  229. [baseUrl stringByAppendingString:@"?q=three20"]], @"Single parameter fail.");
  230. NSDictionary* query = [NSDictionary
  231. dictionaryWithObjectsAndKeys:
  232. @"three20", @"q",
  233. @"en", @"hl",
  234. nil];
  235. NSString* baseUrlWithQuery = [baseUrl stringByAddingQueryDictionary:query];
  236. STAssertTrue([baseUrlWithQuery isEqualToString:[baseUrl
  237. stringByAppendingString:@"?hl=en&q=three20"]]
  238. || [baseUrlWithQuery isEqualToString:[baseUrl
  239. stringByAppendingString:@"?q=three20&hl=en"]],
  240. @"Additional query parameters not correct. %@", [baseUrl stringByAddingQueryDictionary:query]);
  241. }
  242. ///////////////////////////////////////////////////////////////////////////////////////////////////
  243. - (void)testNSString_stringByAddingURLEncodedQueryDictionary {
  244. NSString* baseUrl = @"http://google.com/search";
  245. STAssertEqualObjects([baseUrl stringByAddingURLEncodedQueryDictionary:nil],
  246. [baseUrl stringByAppendingString:@"?"],
  247. @"Empty dictionary fail.");
  248. STAssertEqualObjects([baseUrl stringByAddingURLEncodedQueryDictionary:[NSDictionary dictionary]],
  249. [baseUrl stringByAppendingString:@"?"],
  250. @"Empty dictionary fail.");
  251. baseUrl = @"http://google.com/search?hl=foo";
  252. STAssertEqualObjects([baseUrl stringByAddingURLEncodedQueryDictionary:[NSDictionary
  253. dictionaryWithObject:@"Ö " forKey:@"Ü"]],
  254. [baseUrl stringByAppendingString:@"&%C3%9C=%C3%96%20"],
  255. @"Single parameter fail.");
  256. NSDictionary* query = [NSDictionary
  257. dictionaryWithObjectsAndKeys:
  258. @"%(", @"\u1234",
  259. @"§/", @"hl",
  260. nil];
  261. NSString* baseUrlWithQuery = [baseUrl stringByAddingURLEncodedQueryDictionary:query];
  262. STAssertTrue([baseUrlWithQuery isEqualToString:[baseUrl
  263. stringByAppendingString:@"&%E1%88%B4=%25%28&hl=%C2%A7%2F"]]
  264. || [baseUrlWithQuery isEqualToString:[baseUrl
  265. stringByAppendingString:@"&hl=%C2%A7%2F&%E1%88%B4=%25%28"]],
  266. @"Additional query parameters not correct. %@",
  267. [baseUrl stringByAddingQueryDictionary:query]);
  268. NSDictionary* malformedQueryDict = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:1]
  269. forKey:@""];
  270. STAssertNoThrowSpecificNamed([@"" stringByAddingURLEncodedQueryDictionary:malformedQueryDict],
  271. NSException, NSInvalidArgumentException,
  272. @"Doesn't thow expected exception");
  273. }
  274. ///////////////////////////////////////////////////////////////////////////////////////////////////
  275. - (void)testNSString_urlEncoded {
  276. NSString* reservedCharacters = @"!#$%&'()*+,/:;=?@[] ";
  277. STAssertEqualObjects([reservedCharacters
  278. urlEncoded],
  279. @"%21%23%24%25%26%27%28%29%2A%2B%2C%2F%3A%3B%3D%3F%40%5B%5D%20",
  280. @"incorrect url encoding");
  281. NSString* aLittleBitOfWhiteSpace = @"\r\n\t";
  282. STAssertEqualObjects([aLittleBitOfWhiteSpace
  283. urlEncoded],
  284. @"%0D%0A%09",
  285. @"incorrect url encoding");
  286. NSString* someHighCodeCharacters = @"äÄöÖüÜñàÀáÀîÎ";
  287. STAssertEqualObjects([someHighCodeCharacters
  288. urlEncoded],
  289. @"%C3%A4%C3%84%C3%B6%C3%96%C3%BC%C3%9C%C3%B1%"
  290. @"C3%A0%C3%80%C3%A1%C3%80%C3%AE%C3%8E",
  291. @"incorrect url encoding");
  292. NSString* someUnusualCharacters = @"Ƕဿᴞ🆒";
  293. STAssertEqualObjects([someUnusualCharacters
  294. urlEncoded],
  295. @"%C7%B6%E1%80%BF%E1%B4%9E%F0%9F%86%92",
  296. @"incorrect url encoding");
  297. }
  298. ///////////////////////////////////////////////////////////////////////////////////////////////////
  299. - (void)testNSString_versionStringCompare {
  300. STAssertTrue([@"3.0" versionStringCompare:@"3.0"] == NSOrderedSame, @"same version");
  301. STAssertTrue([@"3.0a2" versionStringCompare:@"3.0a2"] == NSOrderedSame, @"same version alpha");
  302. STAssertTrue([@"3.0" versionStringCompare:@"2.5"] == NSOrderedDescending, @"major no alpha");
  303. STAssertTrue([@"3.1" versionStringCompare:@"3.0"] == NSOrderedDescending, @"minor no alpha");
  304. STAssertTrue([@"3.0a1" versionStringCompare:@"3.0"] == NSOrderedAscending, @"alpha-no alpha");
  305. STAssertTrue([@"3.0a1" versionStringCompare:@"3.0a4"] == NSOrderedAscending, @"alpha diff");
  306. STAssertTrue([@"3.0a2" versionStringCompare:@"3.0a19"] == NSOrderedAscending, @"numeric alpha");
  307. STAssertTrue([@"3.0a" versionStringCompare:@"3.0a1"] == NSOrderedAscending, @"empty alpha");
  308. STAssertTrue([@"3.02" versionStringCompare:@"3.03"] == NSOrderedAscending, @"point diff");
  309. STAssertTrue([@"3.0.2" versionStringCompare:@"3.0.3"] == NSOrderedAscending, @"point diff");
  310. }
  311. ///////////////////////////////////////////////////////////////////////////////////////////////////
  312. ///////////////////////////////////////////////////////////////////////////////////////////////////
  313. #pragma mark -
  314. #pragma mark NSArrayAdditions
  315. ///////////////////////////////////////////////////////////////////////////////////////////////////
  316. - (void)testNSArray_perform {
  317. NSMutableArray* obj1 = [[NSMutableArray alloc] init];
  318. NSMutableArray* obj2 = [[NSMutableArray alloc] init];
  319. NSMutableArray* obj3 = [[NSMutableArray alloc] init];
  320. NSArray* arrayWithObjects = [[NSArray alloc] initWithObjects:obj1, obj2, obj3, nil];
  321. // Invalid selector
  322. [arrayWithObjects perform:@selector(three20)];
  323. // No parameters
  324. [arrayWithObjects perform:@selector(retain)];
  325. for (id obj in arrayWithObjects) {
  326. STAssertTrue([obj retainCount] == 3, @"Retain count wasn't modified, %d", [obj retainCount]);
  327. }
  328. [arrayWithObjects perform:@selector(release)];
  329. for (id obj in arrayWithObjects) {
  330. STAssertTrue([obj retainCount] == 2, @"Retain count wasn't modified, %d", [obj retainCount]);
  331. }
  332. // One parameter
  333. NSMutableArray* obj4 = [[NSMutableArray alloc] init];
  334. [arrayWithObjects perform:@selector(addObject:) withObject:obj4];
  335. for (id obj in arrayWithObjects) {
  336. STAssertTrue([obj count] == 1, @"The new object wasn't added, %d", [obj count]);
  337. }
  338. // Two parameters
  339. NSMutableArray* obj5 = [[NSMutableArray alloc] init];
  340. [arrayWithObjects perform:@selector(replaceObjectAtIndex:withObject:)
  341. withObject:0 withObject:obj5];
  342. for (id obj in arrayWithObjects) {
  343. STAssertTrue([obj count] == 1, @"The array should have the same count, %d", [obj count]);
  344. STAssertEquals([obj objectAtIndex:0], obj5, @"The new object should have been swapped");
  345. }
  346. TT_RELEASE_SAFELY(arrayWithObjects);
  347. TT_RELEASE_SAFELY(obj1);
  348. TT_RELEASE_SAFELY(obj2);
  349. TT_RELEASE_SAFELY(obj3);
  350. TT_RELEASE_SAFELY(obj4);
  351. TT_RELEASE_SAFELY(obj5);
  352. }
  353. ///////////////////////////////////////////////////////////////////////////////////////////////////
  354. - (void)testNSArray_makeObjectsPerformSelector {
  355. NSMutableArray* obj1 = [[NSMutableArray alloc] init];
  356. NSMutableArray* obj2 = [[NSMutableArray alloc] init];
  357. NSMutableArray* obj3 = [[NSMutableArray alloc] init];
  358. NSArray* arrayWithObjects = [[NSArray alloc] initWithObjects:obj1, obj2, obj3, nil];
  359. // Two parameters
  360. NSMutableArray* obj5 = [[NSMutableArray alloc] init];
  361. [arrayWithObjects makeObjectsPerformSelector:@selector(insertObject:atIndex:)
  362. withObject:obj5 withObject:0];
  363. for (id obj in arrayWithObjects) {
  364. STAssertTrue([obj count] == 1, @"The array should have the same count, %d", [obj count]);
  365. STAssertEquals([obj objectAtIndex:0], obj5, @"The new object should have been swapped");
  366. }
  367. TT_RELEASE_SAFELY(arrayWithObjects);
  368. TT_RELEASE_SAFELY(obj1);
  369. TT_RELEASE_SAFELY(obj2);
  370. TT_RELEASE_SAFELY(obj3);
  371. TT_RELEASE_SAFELY(obj5);
  372. }
  373. ///////////////////////////////////////////////////////////////////////////////////////////////////
  374. - (void)testNSArray_objectWithValue {
  375. NSArray* arrayWithObjects = [[NSArray alloc] initWithObjects:
  376. [NSDictionary dictionaryWithObject:@"three20" forKey:@"name"],
  377. [NSDictionary dictionaryWithObject:@"objc" forKey:@"name"],
  378. nil];
  379. STAssertNotNil([arrayWithObjects objectWithValue:@"three20" forKey:@"name"],
  380. @"Should have found an object");
  381. STAssertNil([arrayWithObjects objectWithValue:@"three20" forKey:@"no"],
  382. @"Should not have found an object");
  383. STAssertNil([arrayWithObjects objectWithValue:@"three20" forKey:nil],
  384. @"Should not have found an object");
  385. STAssertNil([arrayWithObjects objectWithValue:nil forKey:@"name"],
  386. @"Should not have found an object");
  387. STAssertNil([arrayWithObjects objectWithValue:nil forKey:nil],
  388. @"Should not have found an object");
  389. TT_RELEASE_SAFELY(arrayWithObjects);
  390. }
  391. ///////////////////////////////////////////////////////////////////////////////////////////////////
  392. - (void)testNSArray_objectWithClass {
  393. NSArray* arrayWithObjects = [[NSArray alloc] initWithObjects:
  394. [NSMutableDictionary dictionaryWithObject:@"three20" forKey:@"name"],
  395. [NSMutableDictionary dictionaryWithObject:@"objc" forKey:@"name"],
  396. nil];
  397. STAssertNotNil([arrayWithObjects objectWithClass:[NSDictionary class]],
  398. @"Should have found an object");
  399. STAssertNotNil([arrayWithObjects objectWithClass:[NSMutableDictionary class]],
  400. @"Should have found an object");
  401. STAssertNil([arrayWithObjects objectWithClass:[NSArray class]],
  402. @"Should not have found an object");
  403. STAssertNil([arrayWithObjects objectWithClass:nil],
  404. @"Should not have found an object");
  405. TT_RELEASE_SAFELY(arrayWithObjects);
  406. }
  407. ///////////////////////////////////////////////////////////////////////////////////////////////////
  408. ///////////////////////////////////////////////////////////////////////////////////////////////////
  409. #pragma mark -
  410. #pragma mark Non-empty strings for NSMutableArray and NSMutableDictionary
  411. ///////////////////////////////////////////////////////////////////////////////////////////////////
  412. - (void)testNSMutableArray_NonEmptyStrings {
  413. NSMutableArray* arrayOfStrings = [[NSMutableArray alloc] init];
  414. [arrayOfStrings addNonEmptyString:nil];
  415. STAssertTrue([arrayOfStrings count] == 0, @"nil shouldn't be added");
  416. [arrayOfStrings addNonEmptyString:@""];
  417. STAssertTrue([arrayOfStrings count] == 0, @"empty string shouldn't be added");
  418. [arrayOfStrings addNonEmptyString:@"three20"];
  419. STAssertTrue([arrayOfStrings count] == 1, @"string should have been added");
  420. TT_RELEASE_SAFELY(arrayOfStrings);
  421. }
  422. ///////////////////////////////////////////////////////////////////////////////////////////////////
  423. - (void)testNSMutableDictionary_NonEmptyStrings {
  424. NSMutableDictionary* dictionaryOfStrings = [[NSMutableDictionary alloc] init];
  425. [dictionaryOfStrings setNonEmptyString:nil forKey:@"name"];
  426. STAssertTrue([dictionaryOfStrings count] == 0, @"nil shouldn't be added");
  427. [dictionaryOfStrings setNonEmptyString:@"" forKey:@"name"];
  428. STAssertTrue([dictionaryOfStrings count] == 0, @"empty string shouldn't be added");
  429. [dictionaryOfStrings setNonEmptyString:@"three20" forKey:@"name"];
  430. STAssertTrue([dictionaryOfStrings count] == 1, @"string should have been added");
  431. TT_RELEASE_SAFELY(dictionaryOfStrings);
  432. }
  433. @end