/core/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. #import "GTMSenTestCase.h"
  19. #import "GTMNSEnumerator+Filter.h"
  20. @interface GTMNSEnumerator_FilterTest : GTMTestCase
  21. @end
  22. @implementation GTMNSEnumerator_FilterTest
  23. - (void)testEnumeratorByMakingEachObjectPerformSelector {
  24. // test w/ a set of strings
  25. NSSet *numbers = [NSSet setWithObjects: @"1", @"2", @"3", nil];
  26. NSEnumerator *e = [[numbers objectEnumerator]
  27. gtm_enumeratorByMakingEachObjectPerformSelector:@selector(stringByAppendingString:)
  28. withObject:@" "];
  29. NSMutableSet *trailingSpaces = [NSMutableSet set];
  30. id obj;
  31. while (nil != (obj = [e nextObject])) {
  32. [trailingSpaces addObject:obj];
  33. }
  34. NSSet *trailingSpacesGood = [NSSet setWithObjects: @"1 ", @"2 ", @"3 ", nil];
  35. STAssertEqualObjects(trailingSpaces, trailingSpacesGood, @"");
  36. // test an empty set
  37. NSSet *empty = [NSSet set];
  38. e = [[empty objectEnumerator]
  39. gtm_enumeratorByMakingEachObjectPerformSelector:@selector(stringByAppendingString:)
  40. withObject:@" "];
  41. STAssertNil([e nextObject],
  42. @"shouldn't have gotten anything from first advance of "
  43. @"enumerator");
  44. }
  45. - (void)testFilteredEnumeratorByMakingEachObjectPerformSelector {
  46. // test with a dict of strings
  47. NSDictionary *testDict = [NSDictionary dictionaryWithObjectsAndKeys:
  48. @"foo", @"1",
  49. @"bar", @"2",
  50. @"foobar", @"3",
  51. nil];
  52. // test those that have prefixes
  53. NSEnumerator *e = [[testDict objectEnumerator]
  54. gtm_filteredEnumeratorByMakingEachObjectPerformSelector:@selector(hasPrefix:)
  55. withObject:@"foo"];
  56. // since the dictionary iterates in any order, compare as sets
  57. NSSet *filteredValues = [NSSet setWithArray:[e allObjects]];
  58. NSSet *expectedValues = [NSSet setWithObjects:@"foo", @"foobar", nil];
  59. STAssertEqualObjects(filteredValues, expectedValues, @"");
  60. // test an empty set
  61. NSSet *empty = [NSSet set];
  62. e = [[empty objectEnumerator]
  63. gtm_filteredEnumeratorByMakingEachObjectPerformSelector:@selector(hasPrefix:)
  64. withObject:@"foo"];
  65. STAssertNil([e nextObject],
  66. @"shouldn't have gotten anything from first advance of "
  67. @"enumerator");
  68. // test an set that will filter out
  69. NSSet *filterAway = [NSSet setWithObjects:@"bar", @"baz", nil];
  70. e = [[filterAway objectEnumerator]
  71. gtm_filteredEnumeratorByMakingEachObjectPerformSelector:@selector(hasPrefix:)
  72. withObject:@"foo"];
  73. STAssertNil([e nextObject],
  74. @"shouldn't have gotten anything from first advance of "
  75. @"enumerator");
  76. }
  77. - (void)testEnumeratorByTargetPerformOnEachSelector {
  78. // test w/ a set of strings
  79. NSSet *numbers = [NSSet setWithObjects: @"1", @"2", @"3", nil];
  80. NSString *target = @"foo";
  81. NSEnumerator *e = [[numbers objectEnumerator]
  82. gtm_enumeratorByTarget:target
  83. performOnEachSelector:@selector(stringByAppendingString:)];
  84. // since the set iterates in any order, compare as sets
  85. NSSet *collectedValues = [NSSet setWithArray:[e allObjects]];
  86. NSSet *expectedValues = [NSSet setWithObjects:@"foo1", @"foo2", @"foo3", nil];
  87. STAssertEqualObjects(collectedValues, expectedValues, @"");
  88. // test an empty set
  89. NSSet *empty = [NSSet set];
  90. e = [[empty objectEnumerator]
  91. gtm_enumeratorByTarget:target
  92. performOnEachSelector:@selector(stringByAppendingString:)];
  93. STAssertNil([e nextObject],
  94. @"shouldn't have gotten anything from first advance of "
  95. @"enumerator");
  96. }
  97. - (id)prependString:(NSString*)pre toString:(NSString *)post {
  98. return [pre stringByAppendingString:post];
  99. }
  100. - (void)testEnumeratorByTargetPerformOnEachSelectorWithObject {
  101. // test w/ a set of strings
  102. NSSet *numbers = [NSSet setWithObjects: @"1", @"2", @"3", nil];
  103. NSEnumerator *e = [[numbers objectEnumerator]
  104. gtm_enumeratorByTarget:self
  105. performOnEachSelector:@selector(prependString:toString:)
  106. withObject:@"bar"];
  107. // since the set iterates in any order, compare as sets
  108. NSSet *collectedValues = [NSSet setWithArray:[e allObjects]];
  109. NSSet *expectedValues = [NSSet setWithObjects:@"1bar",
  110. @"2bar",
  111. @"3bar",
  112. nil];
  113. STAssertEqualObjects(collectedValues, expectedValues, @"");
  114. // test an empty set
  115. NSSet *empty = [NSSet set];
  116. e = [[empty objectEnumerator]
  117. gtm_enumeratorByTarget:self
  118. performOnEachSelector:@selector(prependString:toString:)
  119. withObject:@"bar"];
  120. STAssertNil([e nextObject],
  121. @"shouldn't have gotten anything from first advance of "
  122. @"enumerator");
  123. }
  124. - (void)testFilteredEnumeratorByTargetPerformOnEachSelector {
  125. // test w/ a set of strings
  126. NSSet *numbers = [NSSet setWithObjects:@"1", @"2", @"3", @"4", nil];
  127. NSSet *target = [NSSet setWithObjects:@"2", @"4", @"6", nil];
  128. NSEnumerator *e = [[numbers objectEnumerator]
  129. gtm_filteredEnumeratorByTarget:target
  130. performOnEachSelector:@selector(containsObject:)];
  131. // since the set iterates in any order, compare as sets
  132. NSSet *filteredValues = [NSSet setWithArray:[e allObjects]];
  133. NSSet *expectedValues = [NSSet setWithObjects:@"2", @"4", nil];
  134. STAssertEqualObjects(filteredValues, expectedValues, @"");
  135. // test an empty set
  136. NSSet *empty = [NSSet set];
  137. e = [[empty objectEnumerator]
  138. gtm_filteredEnumeratorByTarget:target
  139. performOnEachSelector:@selector(containsObject:)];
  140. STAssertNil([e nextObject],
  141. @"shouldn't have gotten anything from first advance of "
  142. @"enumerator");
  143. // test a set that will filter out
  144. NSSet *filterAway = [NSSet setWithObjects:@"bar", @"baz", nil];
  145. e = [[filterAway objectEnumerator]
  146. gtm_filteredEnumeratorByTarget:target
  147. performOnEachSelector:@selector(containsObject:)];
  148. STAssertNil([e nextObject],
  149. @"shouldn't have gotten anything from first advance of "
  150. @"enumerator");
  151. }
  152. - (BOOL)is:(id)a equalTo:(id)b {
  153. return [a isEqual:b];
  154. }
  155. - (void)testFilteredEnumeratorByTargetPerformOnEachSelectorWithObject {
  156. // test w/ a set of strings
  157. NSSet *numbers = [NSSet setWithObjects:@"1", @"2", @"3", @"4", nil];
  158. NSEnumerator *e = [[numbers objectEnumerator]
  159. gtm_filteredEnumeratorByTarget:self
  160. performOnEachSelector:@selector(is:equalTo:)
  161. withObject:@"2"];
  162. // since the set iterates in any order, compare as sets
  163. NSSet *filteredValues = [NSSet setWithArray:[e allObjects]];
  164. NSSet *expectedValues = [NSSet setWithObjects:@"2", nil];
  165. STAssertEqualObjects(filteredValues, expectedValues, @"");
  166. // test an empty set
  167. NSSet *empty = [NSSet set];
  168. e = [[empty objectEnumerator]
  169. gtm_filteredEnumeratorByTarget:self
  170. performOnEachSelector:@selector(is:equalTo:)
  171. withObject:@"2"];
  172. STAssertNil([e nextObject],
  173. @"shouldn't have gotten anything from first advance of "
  174. @"enumerator");
  175. // test a set that will filter out
  176. NSSet *filterAway = [NSSet setWithObjects:@"bar", @"baz", nil];
  177. e = [[filterAway objectEnumerator]
  178. gtm_filteredEnumeratorByTarget:self
  179. performOnEachSelector:@selector(is:equalTo:)
  180. withObject:@"2"];
  181. STAssertNil([e nextObject],
  182. @"shouldn't have gotten anything from first advance of "
  183. @"enumerator");
  184. }
  185. @end