PageRenderTime 67ms CodeModel.GetById 1ms app.highlight 62ms RepoModel.GetById 2ms app.codeStats 0ms

/core/externals/update-engine/externals/google-toolbox-for-mac/Foundation/GTMNSEnumerator+FilterTest.m

http://macfuse.googlecode.com/
Objective C | 208 lines | 147 code | 23 blank | 38 comment | 2 complexity | 39a7efd84ab3632160a451132bf59e67 MD5 | raw file
  1//
  2//  GTMNSEnumerator+FilterTest.m
  3//
  4//  Copyright 2007-2008 Google Inc.
  5//
  6//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7//  use this file except in compliance with the License.  You may obtain a copy
  8//  of the License at
  9// 
 10//  http://www.apache.org/licenses/LICENSE-2.0
 11// 
 12//  Unless required by applicable law or agreed to in writing, software
 13//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 14//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 15//  License for the specific language governing permissions and limitations under
 16//  the License.
 17//
 18
 19#import "GTMSenTestCase.h"
 20#import "GTMNSEnumerator+Filter.h"
 21
 22@interface GTMNSEnumerator_FilterTest : GTMTestCase
 23@end
 24
 25@implementation GTMNSEnumerator_FilterTest
 26
 27- (void)testEnumeratorByMakingEachObjectPerformSelector {
 28  // test w/ a set of strings
 29  NSSet *numbers = [NSSet setWithObjects: @"1", @"2", @"3", nil];
 30  NSEnumerator *e = [[numbers objectEnumerator]
 31    gtm_enumeratorByMakingEachObjectPerformSelector:@selector(stringByAppendingString:) 
 32                                         withObject:@" "];
 33  NSMutableSet *trailingSpaces = [NSMutableSet set];
 34  id obj;
 35  while (nil != (obj = [e nextObject])) {
 36    [trailingSpaces addObject:obj];
 37  }
 38  NSSet *trailingSpacesGood = [NSSet setWithObjects: @"1 ", @"2 ", @"3 ", nil];
 39  STAssertEqualObjects(trailingSpaces, trailingSpacesGood, @"");
 40
 41  // test an empty set
 42  NSSet *empty = [NSSet set];
 43  e = [[empty objectEnumerator]
 44    gtm_enumeratorByMakingEachObjectPerformSelector:@selector(stringByAppendingString:) 
 45                                         withObject:@" "];
 46  STAssertNil([e nextObject],
 47              @"shouldn't have gotten anything from first advance of "
 48              @"enumerator");
 49}
 50
 51- (void)testFilteredEnumeratorByMakingEachObjectPerformSelector {
 52  // test with a dict of strings
 53  NSDictionary *testDict = [NSDictionary dictionaryWithObjectsAndKeys:
 54                           @"foo", @"1",
 55                           @"bar", @"2",
 56                           @"foobar", @"3",
 57                           nil];
 58  // test those that have prefixes
 59  NSEnumerator *e = [[testDict objectEnumerator]
 60    gtm_filteredEnumeratorByMakingEachObjectPerformSelector:@selector(hasPrefix:) 
 61                                                 withObject:@"foo"];
 62  // since the dictionary iterates in any order, compare as sets
 63  NSSet *filteredValues = [NSSet setWithArray:[e allObjects]];
 64  NSSet *expectedValues = [NSSet setWithObjects:@"foo", @"foobar", nil];
 65  STAssertEqualObjects(filteredValues, expectedValues, @"");
 66
 67  // test an empty set
 68  NSSet *empty = [NSSet set];
 69  e = [[empty objectEnumerator]
 70    gtm_filteredEnumeratorByMakingEachObjectPerformSelector:@selector(hasPrefix:) 
 71                                                 withObject:@"foo"];
 72  STAssertNil([e nextObject],
 73              @"shouldn't have gotten anything from first advance of "
 74              @"enumerator");
 75  
 76  // test an set that will filter out
 77  NSSet *filterAway = [NSSet setWithObjects:@"bar", @"baz", nil];
 78  e = [[filterAway objectEnumerator]
 79    gtm_filteredEnumeratorByMakingEachObjectPerformSelector:@selector(hasPrefix:) 
 80                                                 withObject:@"foo"];
 81  STAssertNil([e nextObject],
 82              @"shouldn't have gotten anything from first advance of " 
 83              @"enumerator");
 84}
 85
 86- (void)testEnumeratorByTargetPerformOnEachSelector {
 87  // test w/ a set of strings
 88  NSSet *numbers = [NSSet setWithObjects: @"1", @"2", @"3", nil];
 89  NSString *target = @"foo";
 90  NSEnumerator *e = [[numbers objectEnumerator]
 91    gtm_enumeratorByTarget:target
 92     performOnEachSelector:@selector(stringByAppendingString:)];
 93  // since the set iterates in any order, compare as sets
 94  NSSet *collectedValues = [NSSet setWithArray:[e allObjects]];
 95  NSSet *expectedValues = [NSSet setWithObjects:@"foo1", @"foo2", @"foo3", nil];
 96  STAssertEqualObjects(collectedValues, expectedValues, @"");
 97  
 98  // test an empty set
 99  NSSet *empty = [NSSet set];
100  e = [[empty objectEnumerator]
101    gtm_enumeratorByTarget:target
102     performOnEachSelector:@selector(stringByAppendingString:)];
103  STAssertNil([e nextObject],
104              @"shouldn't have gotten anything from first advance of "
105              @"enumerator");
106}
107
108- (id)prependString:(NSString*)pre toString:(NSString *)post {
109  return [pre stringByAppendingString:post];
110}
111
112- (void)testEnumeratorByTargetPerformOnEachSelectorWithObject {
113  // test w/ a set of strings
114  NSSet *numbers = [NSSet setWithObjects: @"1", @"2", @"3", nil];
115  NSEnumerator *e = [[numbers objectEnumerator]
116                     gtm_enumeratorByTarget:self
117                     performOnEachSelector:@selector(prependString:toString:)
118                                 withObject:@"bar"];
119  // since the set iterates in any order, compare as sets
120  NSSet *collectedValues = [NSSet setWithArray:[e allObjects]];
121  NSSet *expectedValues = [NSSet setWithObjects:@"1bar", 
122                           @"2bar",
123                           @"3bar", 
124                           nil];
125  STAssertEqualObjects(collectedValues, expectedValues, @"");
126  
127  // test an empty set
128  NSSet *empty = [NSSet set];
129  e = [[empty objectEnumerator]
130       gtm_enumeratorByTarget:self
131        performOnEachSelector:@selector(prependString:toString:)
132                   withObject:@"bar"];
133  STAssertNil([e nextObject],
134              @"shouldn't have gotten anything from first advance of "
135              @"enumerator");
136}
137
138
139- (void)testFilteredEnumeratorByTargetPerformOnEachSelector {
140  // test w/ a set of strings
141  NSSet *numbers = [NSSet setWithObjects:@"1", @"2", @"3", @"4", nil];
142  NSSet *target = [NSSet setWithObjects:@"2", @"4", @"6", nil];
143  NSEnumerator *e = [[numbers objectEnumerator]
144    gtm_filteredEnumeratorByTarget:target
145             performOnEachSelector:@selector(containsObject:)];
146  // since the set iterates in any order, compare as sets
147  NSSet *filteredValues = [NSSet setWithArray:[e allObjects]];
148  NSSet *expectedValues = [NSSet setWithObjects:@"2", @"4", nil];
149  STAssertEqualObjects(filteredValues, expectedValues, @"");
150
151  // test an empty set
152  NSSet *empty = [NSSet set];
153  e = [[empty objectEnumerator]
154    gtm_filteredEnumeratorByTarget:target
155             performOnEachSelector:@selector(containsObject:)];
156  STAssertNil([e nextObject],
157              @"shouldn't have gotten anything from first advance of "
158              @"enumerator");
159
160  // test a set that will filter out
161  NSSet *filterAway = [NSSet setWithObjects:@"bar", @"baz", nil];
162  e = [[filterAway objectEnumerator]
163    gtm_filteredEnumeratorByTarget:target
164             performOnEachSelector:@selector(containsObject:)];
165  STAssertNil([e nextObject],
166              @"shouldn't have gotten anything from first advance of "
167              @"enumerator");
168}
169
170- (BOOL)is:(id)a equalTo:(id)b {
171  return [a isEqual:b];
172}
173
174- (void)testFilteredEnumeratorByTargetPerformOnEachSelectorWithObject {
175  // test w/ a set of strings
176  NSSet *numbers = [NSSet setWithObjects:@"1", @"2", @"3", @"4", nil];
177  NSEnumerator *e = [[numbers objectEnumerator]
178                     gtm_filteredEnumeratorByTarget:self
179                     performOnEachSelector:@selector(is:equalTo:)
180                     withObject:@"2"];
181  // since the set iterates in any order, compare as sets
182  NSSet *filteredValues = [NSSet setWithArray:[e allObjects]];
183  NSSet *expectedValues = [NSSet setWithObjects:@"2", nil];
184  STAssertEqualObjects(filteredValues, expectedValues, @"");
185  
186  // test an empty set
187  NSSet *empty = [NSSet set];
188  e = [[empty objectEnumerator]
189       gtm_filteredEnumeratorByTarget:self
190       performOnEachSelector:@selector(is:equalTo:)
191       withObject:@"2"];
192  STAssertNil([e nextObject],
193              @"shouldn't have gotten anything from first advance of "
194              @"enumerator");
195  
196  // test a set that will filter out
197  NSSet *filterAway = [NSSet setWithObjects:@"bar", @"baz", nil];
198  e = [[filterAway objectEnumerator]
199       gtm_filteredEnumeratorByTarget:self
200       performOnEachSelector:@selector(is:equalTo:)
201       withObject:@"2"];
202  STAssertNil([e nextObject],
203              @"shouldn't have gotten anything from first advance of "
204              @"enumerator");
205}
206
207
208@end