/core/externals/update-engine/externals/gdata-objectivec-client/Source/Tests/GDataUtilitiesTest.m

http://macfuse.googlecode.com/ · Objective C · 337 lines · 215 code · 83 blank · 39 comment · 6 complexity · 643a264e0a285901d0536326a91e31c9 MD5 · raw file

  1. /* Copyright (c) 2008 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #define typeof __typeof__ // fixes http://www.brethorsting.com/blog/2006/02/stupid-issue-with-ocunit.html
  16. #import <SenTestingKit/SenTestingKit.h>
  17. #import "GDataUtilities.h"
  18. @interface GDataUtilitiesTest : SenTestCase
  19. @end
  20. @implementation GDataUtilitiesTest
  21. - (void)testControlsFiltering {
  22. NSString *input;
  23. NSString *output;
  24. input = nil;
  25. output = [GDataUtilities stringWithControlsFilteredForString:input];
  26. STAssertNil(output, @"nil test");
  27. input = @"";
  28. output = [GDataUtilities stringWithControlsFilteredForString:input];
  29. STAssertEqualObjects(output, input, @"empty string");
  30. input = @"Fred & Wilma";
  31. output = [GDataUtilities stringWithControlsFilteredForString:input];
  32. STAssertEqualObjects(output, input, @"plain string");
  33. input = [NSString stringWithFormat:@"Nuts%CBolts", (unichar) 0x0B]; // 0xB: vertical tab
  34. output = [GDataUtilities stringWithControlsFilteredForString:input];
  35. STAssertEqualObjects(output, @"NutsBolts", @"vt failure");
  36. // filter a string containing all chars from 0x01 to 0x7F
  37. NSMutableString *allCharStr = [NSMutableString string];
  38. for (int idx = 1; idx <= 127; idx++) {
  39. [allCharStr appendFormat:@"%c", idx];
  40. }
  41. input = allCharStr;
  42. output = [GDataUtilities stringWithControlsFilteredForString:input];
  43. NSString *expected = @"\t\n\r !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLM"
  44. "NOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
  45. STAssertEqualObjects(output, expected, @"all-chars failure");
  46. }
  47. - (void)testPercentEncodingUTF8 {
  48. NSString *input;
  49. NSString *output;
  50. input = nil;
  51. output = [GDataUtilities stringByPercentEncodingUTF8ForString:input];
  52. STAssertNil(output, @"nil test");
  53. input = @"";
  54. output = [GDataUtilities stringByPercentEncodingUTF8ForString:input];
  55. STAssertEqualObjects(output, input, @"empty string");
  56. input = @"Fred & Wilma";
  57. output = [GDataUtilities stringByPercentEncodingUTF8ForString:input];
  58. STAssertEqualObjects(output, input, @"plain string");
  59. input = [NSString stringWithFormat:@"The Beach at S%Cte", (unichar) 0x00E8];
  60. output = [GDataUtilities stringByPercentEncodingUTF8ForString:input];
  61. STAssertEqualObjects(output, @"The Beach at S%C3%A8te", @"8-bit failure");
  62. input = @"\ttab\tline1\rline2%percent\nline3";
  63. output = [GDataUtilities stringByPercentEncodingUTF8ForString:input];
  64. STAssertEqualObjects(output, @"%09tab%09line1%0Dline2%25percent%0Aline3",
  65. @"control char");
  66. input = [NSString stringWithFormat:@"photo%C.jpg", (unichar) 0x53C3];
  67. output = [GDataUtilities stringByPercentEncodingUTF8ForString:input];
  68. STAssertEqualObjects(output, @"photo%E5%8F%83.jpg", @"cjk failure");
  69. }
  70. - (void)testURLEncodingForURI {
  71. NSString *input;
  72. NSString *output;
  73. NSString *expected;
  74. input = nil;
  75. output = [GDataUtilities stringByURLEncodingForURI:input];
  76. STAssertNil(output, @"nil test");
  77. input = @"";
  78. output = [GDataUtilities stringByURLEncodingForURI:input];
  79. STAssertEqualObjects(output, input, @"empty string");
  80. input = @"abcdef";
  81. output = [GDataUtilities stringByURLEncodingForURI:input];
  82. expected = @"abcdef";
  83. STAssertEqualObjects(output, expected, @"plain string");
  84. input = @"abc def";
  85. output = [GDataUtilities stringByURLEncodingForURI:input];
  86. expected = @"abc%20def";
  87. STAssertEqualObjects(output, expected, @"plain string with space");
  88. input = @"abc!*'();:@&=+$,/?%#[]def";
  89. output = [GDataUtilities stringByURLEncodingForURI:input];
  90. expected = @"abc%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%25%23%5B%5Ddef";
  91. STAssertEqualObjects(output, expected, @"all chars to escape");
  92. }
  93. - (void)testURLEncodingForStringParameter {
  94. NSString *input;
  95. NSString *output;
  96. NSString *expected;
  97. input = nil;
  98. output = [GDataUtilities stringByURLEncodingStringParameter:input];
  99. STAssertNil(output, @"nil test");
  100. input = @"";
  101. output = [GDataUtilities stringByURLEncodingStringParameter:input];
  102. STAssertEqualObjects(output, input, @"empty string");
  103. input = @"abcdef";
  104. output = [GDataUtilities stringByURLEncodingStringParameter:input];
  105. expected = @"abcdef";
  106. STAssertEqualObjects(output, expected, @"plain string");
  107. input = @"abc def";
  108. output = [GDataUtilities stringByURLEncodingStringParameter:input];
  109. expected = @"abc+def";
  110. STAssertEqualObjects(output, expected, @"plain string with space");
  111. input = @"abc!*'();:@&=+$,/?%#[]def";
  112. output = [GDataUtilities stringByURLEncodingStringParameter:input];
  113. expected = @"abc%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%25%23%5B%5Ddef";
  114. STAssertEqualObjects(output, expected, @"all chars to escape");
  115. }
  116. #pragma mark -
  117. - (void)doTestEqualAndDistinctElementsInArray:(NSArray *)testArray
  118. andArrayCopy:(NSArray *)copyArray {
  119. NSUInteger numItems = [testArray count];
  120. // test that we got an equal copy
  121. STAssertEqualObjects(copyArray, testArray,
  122. @"Array copy failed (%lu items)",
  123. (unsigned long) numItems);
  124. // check that the objects in the copy are actual copies, not retains
  125. // of the original objects
  126. NSEnumerator *enumTest = [testArray objectEnumerator];
  127. NSEnumerator *enumCopy = [copyArray objectEnumerator];
  128. id objTest = [enumTest nextObject];
  129. id objCopy = [enumCopy nextObject];
  130. while (objTest) {
  131. STAssertTrue(objTest != objCopy,
  132. @"array copy is reusing original object (%lu items)",
  133. (unsigned long) numItems);
  134. objTest = [enumTest nextObject];
  135. objCopy = [enumCopy nextObject];
  136. }
  137. }
  138. - (void)doArrayCopyTestsForArray:(NSArray *)testArray {
  139. NSUInteger numItems = [testArray count];
  140. // immutable copy
  141. id copyArray = [GDataUtilities arrayWithCopiesOfObjectsInArray:testArray];
  142. [self doTestEqualAndDistinctElementsInArray:testArray
  143. andArrayCopy:copyArray];
  144. // mutable copy
  145. copyArray = [GDataUtilities mutableArrayWithCopiesOfObjectsInArray:testArray];
  146. [self doTestEqualAndDistinctElementsInArray:testArray
  147. andArrayCopy:copyArray];
  148. // test that copy is mutable (isKindOfClass: will fail on the class
  149. // cluster, so brute-force it)
  150. @try {
  151. [copyArray addObject:@"foo"];
  152. }
  153. @catch(NSException *exc) {
  154. STFail(@"Array mutableCopy not mutable (%lu items)",
  155. (unsigned long) numItems);
  156. }
  157. }
  158. - (void)doTestEqualAndDistinctElementsInDictionary:(NSDictionary *)testDict
  159. andDictionaryCopy:(NSDictionary *)copyDict {
  160. NSUInteger numItems = [testDict count];
  161. // test that we got an equal copy
  162. STAssertEqualObjects(copyDict, testDict,
  163. @"Dict copy failed (%lu items)",
  164. (unsigned long) numItems);
  165. // check that the objects in the copy are actual copies, not retains
  166. // of the original objects
  167. NSEnumerator *enumTestKeys = [testDict keyEnumerator];
  168. id testKey = [enumTestKeys nextObject];
  169. while (testKey) {
  170. id objTest = [testDict objectForKey:testKey];
  171. id objCopy = [copyDict objectForKey:testKey];
  172. STAssertTrue(objTest != objCopy,
  173. @"dict copy is reusing original object (%lu items)",
  174. (unsigned long) numItems);
  175. // if the elements are arrays, apply the array comparison too
  176. if ([objTest isKindOfClass:[NSArray class]]) {
  177. [self doTestEqualAndDistinctElementsInArray:objTest
  178. andArrayCopy:objCopy];
  179. }
  180. testKey = [enumTestKeys nextObject];
  181. }
  182. }
  183. - (void)doDictionaryCopyTestsForDictionary:(NSDictionary *)testDict {
  184. NSUInteger numItems = [testDict count];
  185. // immutable copy
  186. id copyDict = [GDataUtilities dictionaryWithCopiesOfObjectsInDictionary:testDict];
  187. [self doTestEqualAndDistinctElementsInDictionary:testDict
  188. andDictionaryCopy:copyDict];
  189. // mutable copy
  190. copyDict = [GDataUtilities mutableDictionaryWithCopiesOfObjectsInDictionary:testDict];
  191. [self doTestEqualAndDistinctElementsInDictionary:testDict
  192. andDictionaryCopy:copyDict];
  193. // test that copy is mutable (isKindOfClass: will fail on the class
  194. // cluster, so brute-force it)
  195. @try {
  196. [(NSMutableDictionary *)copyDict setObject:@"foo" forKey:@"bar"];
  197. }
  198. @catch(NSException *exc) {
  199. STFail(@"Dict mutableCopy not mutable (%lu items)", (unsigned long) numItems);
  200. }
  201. }
  202. - (void)doDictionaryOfArraysCopyTestsForDictionary:(NSDictionary *)testDict {
  203. NSUInteger numItems = [testDict count];
  204. // immutable copy
  205. id copyDict = [GDataUtilities dictionaryWithCopiesOfArraysInDictionary:testDict];
  206. [self doTestEqualAndDistinctElementsInDictionary:testDict
  207. andDictionaryCopy:copyDict];
  208. // mutable copy
  209. copyDict = [GDataUtilities mutableDictionaryWithCopiesOfArraysInDictionary:testDict];
  210. [self doTestEqualAndDistinctElementsInDictionary:testDict
  211. andDictionaryCopy:copyDict];
  212. // test that copy is mutable
  213. @try {
  214. [(NSMutableDictionary *)copyDict setObject:@"foo" forKey:@"bar"];
  215. }
  216. @catch(NSException *exc) {
  217. STFail(@"Dict of arrays mutableCopy not mutable (%lu items)",
  218. (unsigned long) numItems);
  219. }
  220. }
  221. - (void)testCollectionCopying {
  222. // test array copies
  223. // mutable strings, when copied, will have unique pointers rather than
  224. // just increased refCounts, so we can ensure we made a copy of the
  225. // strings
  226. NSMutableString *str1 = [NSMutableString stringWithString:@"one"];
  227. NSMutableString *str2 = [NSMutableString stringWithString:@"two"];
  228. NSMutableString *str3 = [NSMutableString stringWithString:@"three"];
  229. NSArray *zeroArray = [NSArray array];
  230. NSArray *oneArray = [NSArray arrayWithObject:str1];
  231. NSArray *twoArray = [NSArray arrayWithObjects:str2, str3, nil];
  232. [self doArrayCopyTestsForArray:zeroArray];
  233. [self doArrayCopyTestsForArray:oneArray];
  234. [self doArrayCopyTestsForArray:twoArray];
  235. // test dictionary copies
  236. NSMutableDictionary *zeroDict = [NSMutableDictionary dictionary];
  237. NSMutableDictionary *oneDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  238. str1, @"1", nil];
  239. NSMutableDictionary *twoDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  240. str2, @"2", str3, @"3", nil];
  241. [self doDictionaryCopyTestsForDictionary:zeroDict];
  242. [self doDictionaryCopyTestsForDictionary:oneDict];
  243. [self doDictionaryCopyTestsForDictionary:twoDict];
  244. // test dictionary-of-arrays copies
  245. NSMutableDictionary *zeroArrayDict = [NSMutableDictionary dictionary];
  246. NSMutableDictionary *oneArrayDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  247. oneArray, @"1a", nil];
  248. NSMutableDictionary *twoArrayDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  249. oneArray, @"1aa", twoArray, @"2aa", nil];
  250. [self doDictionaryOfArraysCopyTestsForDictionary:zeroArrayDict];
  251. [self doDictionaryOfArraysCopyTestsForDictionary:oneArrayDict];
  252. [self doDictionaryOfArraysCopyTestsForDictionary:twoArrayDict];
  253. }
  254. @end