PageRenderTime 71ms CodeModel.GetById 12ms app.highlight 55ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 16#define typeof __typeof__ // fixes http://www.brethorsting.com/blog/2006/02/stupid-issue-with-ocunit.html
 17
 18#import <SenTestingKit/SenTestingKit.h>
 19
 20#import "GDataUtilities.h"
 21
 22@interface GDataUtilitiesTest : SenTestCase
 23@end
 24
 25@implementation GDataUtilitiesTest
 26
 27- (void)testControlsFiltering {
 28  
 29  NSString *input;
 30  NSString *output;
 31  
 32  input = nil;
 33  output = [GDataUtilities stringWithControlsFilteredForString:input];
 34  STAssertNil(output, @"nil test");
 35  
 36  input = @"";
 37  output = [GDataUtilities stringWithControlsFilteredForString:input];
 38  STAssertEqualObjects(output, input, @"empty string");
 39  
 40  input = @"Fred & Wilma";
 41  output = [GDataUtilities stringWithControlsFilteredForString:input];
 42  STAssertEqualObjects(output, input, @"plain string");
 43  
 44  input = [NSString stringWithFormat:@"Nuts%CBolts", (unichar) 0x0B]; // 0xB: vertical tab
 45  output = [GDataUtilities stringWithControlsFilteredForString:input];
 46  STAssertEqualObjects(output, @"NutsBolts", @"vt failure");
 47  
 48  // filter a string containing all chars from 0x01 to 0x7F
 49  NSMutableString *allCharStr = [NSMutableString string];
 50  for (int idx = 1; idx <= 127; idx++) {
 51    [allCharStr appendFormat:@"%c", idx];
 52  }
 53  input = allCharStr;
 54  output = [GDataUtilities stringWithControlsFilteredForString:input];
 55  NSString *expected = @"\t\n\r !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLM"
 56    "NOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
 57  STAssertEqualObjects(output, expected, @"all-chars failure");
 58}
 59
 60- (void)testPercentEncodingUTF8 {
 61  
 62  NSString *input;
 63  NSString *output;
 64  
 65  input = nil;
 66  output = [GDataUtilities stringByPercentEncodingUTF8ForString:input];
 67  STAssertNil(output, @"nil test");
 68  
 69  input = @"";
 70  output = [GDataUtilities stringByPercentEncodingUTF8ForString:input];
 71  STAssertEqualObjects(output, input, @"empty string");
 72    
 73  input = @"Fred & Wilma";
 74  output = [GDataUtilities stringByPercentEncodingUTF8ForString:input];
 75  STAssertEqualObjects(output, input, @"plain string");
 76  
 77  input = [NSString stringWithFormat:@"The Beach at S%Cte", (unichar) 0x00E8];
 78  output = [GDataUtilities stringByPercentEncodingUTF8ForString:input];
 79  STAssertEqualObjects(output, @"The Beach at S%C3%A8te", @"8-bit failure");
 80
 81  input = @"\ttab\tline1\rline2%percent\nline3";
 82  output = [GDataUtilities stringByPercentEncodingUTF8ForString:input];
 83  STAssertEqualObjects(output, @"%09tab%09line1%0Dline2%25percent%0Aline3", 
 84                       @"control char");
 85
 86  input = [NSString stringWithFormat:@"photo%C.jpg", (unichar) 0x53C3];
 87  output = [GDataUtilities stringByPercentEncodingUTF8ForString:input];
 88  STAssertEqualObjects(output, @"photo%E5%8F%83.jpg", @"cjk failure");
 89}
 90
 91- (void)testURLEncodingForURI {
 92  NSString *input;
 93  NSString *output;
 94  NSString *expected;
 95
 96  input = nil;
 97  output = [GDataUtilities stringByURLEncodingForURI:input];
 98  STAssertNil(output, @"nil test");
 99
100  input = @"";
101  output = [GDataUtilities stringByURLEncodingForURI:input];
102  STAssertEqualObjects(output, input, @"empty string");
103
104  input = @"abcdef";
105  output = [GDataUtilities stringByURLEncodingForURI:input];
106  expected = @"abcdef";
107  STAssertEqualObjects(output, expected, @"plain string");
108
109  input = @"abc def";
110  output = [GDataUtilities stringByURLEncodingForURI:input];
111  expected = @"abc%20def";
112  STAssertEqualObjects(output, expected, @"plain string with space");
113
114  input = @"abc!*'();:@&=+$,/?%#[]def";
115  output = [GDataUtilities stringByURLEncodingForURI:input];
116  expected = @"abc%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%25%23%5B%5Ddef";
117  STAssertEqualObjects(output, expected, @"all chars to escape");
118}
119
120- (void)testURLEncodingForStringParameter {
121  NSString *input;
122  NSString *output;
123  NSString *expected;
124
125  input = nil;
126  output = [GDataUtilities stringByURLEncodingStringParameter:input];
127  STAssertNil(output, @"nil test");
128
129  input = @"";
130  output = [GDataUtilities stringByURLEncodingStringParameter:input];
131  STAssertEqualObjects(output, input, @"empty string");
132
133  input = @"abcdef";
134  output = [GDataUtilities stringByURLEncodingStringParameter:input];
135  expected = @"abcdef";
136  STAssertEqualObjects(output, expected, @"plain string");
137
138  input = @"abc def";
139  output = [GDataUtilities stringByURLEncodingStringParameter:input];
140  expected = @"abc+def";
141  STAssertEqualObjects(output, expected, @"plain string with space");
142
143  input = @"abc!*'();:@&=+$,/?%#[]def";
144  output = [GDataUtilities stringByURLEncodingStringParameter:input];
145  expected = @"abc%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%25%23%5B%5Ddef";
146  STAssertEqualObjects(output, expected, @"all chars to escape");
147}
148
149#pragma mark -
150
151- (void)doTestEqualAndDistinctElementsInArray:(NSArray *)testArray
152                                 andArrayCopy:(NSArray *)copyArray {
153
154  NSUInteger numItems = [testArray count];
155
156  // test that we got an equal copy
157  STAssertEqualObjects(copyArray, testArray,
158                       @"Array copy failed (%lu items)",
159                       (unsigned long) numItems);
160
161  // check that the objects in the copy are actual copies, not retains
162  // of the original objects
163  NSEnumerator *enumTest = [testArray objectEnumerator];
164  NSEnumerator *enumCopy = [copyArray objectEnumerator];
165
166  id objTest = [enumTest nextObject];
167  id objCopy = [enumCopy nextObject];
168
169  while (objTest) {
170    STAssertTrue(objTest != objCopy,
171                  @"array copy is reusing original object (%lu items)",
172                 (unsigned long) numItems);
173
174    objTest = [enumTest nextObject];
175    objCopy = [enumCopy nextObject];
176  }
177}
178
179- (void)doArrayCopyTestsForArray:(NSArray *)testArray {
180
181  NSUInteger numItems = [testArray count];
182
183  // immutable copy
184  id copyArray = [GDataUtilities arrayWithCopiesOfObjectsInArray:testArray];
185
186  [self doTestEqualAndDistinctElementsInArray:testArray
187                                 andArrayCopy:copyArray];
188
189  // mutable copy
190  copyArray = [GDataUtilities mutableArrayWithCopiesOfObjectsInArray:testArray];
191
192  [self doTestEqualAndDistinctElementsInArray:testArray
193                                 andArrayCopy:copyArray];
194
195  // test that copy is mutable (isKindOfClass: will fail on the class
196  // cluster, so brute-force it)
197  @try {
198    [copyArray addObject:@"foo"];
199  }
200  @catch(NSException *exc) {
201    STFail(@"Array mutableCopy not mutable (%lu items)",
202           (unsigned long) numItems);
203  }
204}
205
206- (void)doTestEqualAndDistinctElementsInDictionary:(NSDictionary *)testDict
207                                 andDictionaryCopy:(NSDictionary *)copyDict {
208
209  NSUInteger numItems = [testDict count];
210
211  // test that we got an equal copy
212  STAssertEqualObjects(copyDict, testDict,
213                       @"Dict copy failed (%lu items)",
214                       (unsigned long) numItems);
215
216  // check that the objects in the copy are actual copies, not retains
217  // of the original objects
218  NSEnumerator *enumTestKeys = [testDict keyEnumerator];
219
220  id testKey = [enumTestKeys nextObject];
221  while (testKey) {
222    id objTest = [testDict objectForKey:testKey];
223    id objCopy = [copyDict objectForKey:testKey];
224
225    STAssertTrue(objTest != objCopy,
226                  @"dict copy is reusing original object (%lu items)",
227                 (unsigned long) numItems);
228
229    // if the elements are arrays, apply the array comparison too
230    if ([objTest isKindOfClass:[NSArray class]]) {
231
232     [self doTestEqualAndDistinctElementsInArray:objTest
233                                    andArrayCopy:objCopy];
234    }
235
236    testKey = [enumTestKeys nextObject];
237  }
238}
239
240
241- (void)doDictionaryCopyTestsForDictionary:(NSDictionary *)testDict {
242
243  NSUInteger numItems = [testDict count];
244
245  // immutable copy
246  id copyDict = [GDataUtilities dictionaryWithCopiesOfObjectsInDictionary:testDict];
247
248  [self doTestEqualAndDistinctElementsInDictionary:testDict
249                                 andDictionaryCopy:copyDict];
250
251  // mutable copy
252  copyDict = [GDataUtilities mutableDictionaryWithCopiesOfObjectsInDictionary:testDict];
253
254  [self doTestEqualAndDistinctElementsInDictionary:testDict
255                                 andDictionaryCopy:copyDict];
256
257  // test that copy is mutable (isKindOfClass: will fail on the class
258  // cluster, so brute-force it)
259  @try {
260     [(NSMutableDictionary *)copyDict setObject:@"foo" forKey:@"bar"];
261  }
262  @catch(NSException *exc) {
263    STFail(@"Dict mutableCopy not mutable (%lu items)", (unsigned long) numItems);
264  }
265}
266
267
268- (void)doDictionaryOfArraysCopyTestsForDictionary:(NSDictionary *)testDict {
269
270  NSUInteger numItems = [testDict count];
271
272  // immutable copy
273  id copyDict = [GDataUtilities dictionaryWithCopiesOfArraysInDictionary:testDict];
274
275  [self doTestEqualAndDistinctElementsInDictionary:testDict
276                                 andDictionaryCopy:copyDict];
277
278  // mutable copy
279  copyDict = [GDataUtilities mutableDictionaryWithCopiesOfArraysInDictionary:testDict];
280
281  [self doTestEqualAndDistinctElementsInDictionary:testDict
282                                 andDictionaryCopy:copyDict];
283
284  // test that copy is mutable
285  @try {
286    [(NSMutableDictionary *)copyDict setObject:@"foo" forKey:@"bar"];
287  }
288  @catch(NSException *exc) {
289    STFail(@"Dict of arrays mutableCopy not mutable (%lu items)",
290           (unsigned long) numItems);
291  }
292}
293
294
295- (void)testCollectionCopying {
296
297  // test array copies
298
299  // mutable strings, when copied, will have unique pointers rather than
300  // just increased refCounts, so we can ensure we made a copy of the
301  // strings
302  NSMutableString *str1 = [NSMutableString stringWithString:@"one"];
303  NSMutableString *str2 = [NSMutableString stringWithString:@"two"];
304  NSMutableString *str3 = [NSMutableString stringWithString:@"three"];
305
306  NSArray *zeroArray = [NSArray array];
307  NSArray *oneArray = [NSArray arrayWithObject:str1];
308  NSArray *twoArray = [NSArray arrayWithObjects:str2, str3, nil];
309
310  [self doArrayCopyTestsForArray:zeroArray];
311  [self doArrayCopyTestsForArray:oneArray];
312  [self doArrayCopyTestsForArray:twoArray];
313
314
315  // test dictionary copies
316  NSMutableDictionary *zeroDict = [NSMutableDictionary dictionary];
317  NSMutableDictionary *oneDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
318                                  str1, @"1", nil];
319  NSMutableDictionary *twoDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
320                                  str2, @"2", str3, @"3", nil];
321  [self doDictionaryCopyTestsForDictionary:zeroDict];
322  [self doDictionaryCopyTestsForDictionary:oneDict];
323  [self doDictionaryCopyTestsForDictionary:twoDict];
324
325  // test dictionary-of-arrays copies
326  NSMutableDictionary *zeroArrayDict = [NSMutableDictionary dictionary];
327  NSMutableDictionary *oneArrayDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
328                                       oneArray, @"1a", nil];
329  NSMutableDictionary *twoArrayDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
330                                       oneArray, @"1aa", twoArray, @"2aa", nil];
331
332  [self doDictionaryOfArraysCopyTestsForDictionary:zeroArrayDict];
333  [self doDictionaryOfArraysCopyTestsForDictionary:oneArrayDict];
334  [self doDictionaryOfArraysCopyTestsForDictionary:twoArrayDict];
335}
336
337@end