PageRenderTime 82ms CodeModel.GetById 22ms app.highlight 58ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/google-toolbox-for-mac/Foundation/GTMNSArray+MergeTest.m

http://macfuse.googlecode.com/
Objective C | 219 lines | 168 code | 30 blank | 21 comment | 0 complexity | a5a7a92809b03f53c3b022e569bd7ec0 MD5 | raw file
  1//
  2//  GTMNSArray+MergeTest.m
  3//
  4//  Copyright 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
 16//  under the License.
 17//
 18
 19#import "GTMSenTestCase.h"
 20#import "GTMNSArray+Merge.h"
 21
 22@interface GTMNSArray_MergeTest : GTMTestCase
 23@end
 24
 25
 26@interface NSString (GTMStringMergingTestAdditions)
 27
 28- (NSString *)mergeString:(NSString *)stringB;
 29
 30@end
 31
 32
 33@implementation GTMNSArray_MergeTest
 34
 35- (void)testMergingTwoEmptyArrays {
 36  NSArray *emptyArrayA = [NSArray array];
 37  NSArray *emptyArrayB = [NSArray array];
 38  NSArray *mergedArray = [emptyArrayA gtm_mergeArray:emptyArrayB
 39                                       mergeSelector:nil];
 40  STAssertNil(mergedArray,
 41              @"merge of two empty arrays with no merger should render nil");
 42}
 43
 44- (void)testMergingTwoEmptyArraysWithMerger {
 45  NSArray *emptyArrayA = [NSArray array];
 46  NSArray *emptyArrayB = [NSArray array];
 47  NSArray *mergedArray
 48    = [emptyArrayA gtm_mergeArray:emptyArrayB
 49                    mergeSelector:@selector(mergeString:)];
 50  STAssertNil(mergedArray,
 51              @"merge of two empty arrays with merger should render nil");
 52}
 53
 54- (void)testMergingEmptyWithNilArray {
 55  NSArray *emptyArrayA = [NSArray array];
 56  NSArray *nilArrayB = nil;
 57  NSArray *mergedArray = [emptyArrayA gtm_mergeArray:nilArrayB
 58                                       mergeSelector:nil];
 59  STAssertNil(mergedArray,
 60              @"merge of empty with nil array with no merger should render nil");
 61}
 62
 63- (void)testMergingEmptyWithNilArrayWithMerger {
 64  NSArray *emptyArrayA = [NSArray array];
 65  NSArray *nilArrayB = nil;
 66  NSArray *mergedArray
 67    = [emptyArrayA gtm_mergeArray:nilArrayB
 68                    mergeSelector:@selector(mergeString:)];
 69  STAssertNil(mergedArray,
 70              @"merge of empty with nil array with merger should render nil");
 71}
 72
 73- (void)testMergingTwoOneItemArraysThatDontMatch {
 74  NSArray *arrayA = [NSArray arrayWithObject:@"abc.def"];
 75  NSArray *arrayB = [NSArray arrayWithObject:@"abc.ghi"];
 76  NSArray *mergedArray = [arrayA gtm_mergeArray:arrayB
 77                                  mergeSelector:nil];
 78  STAssertNotNil(mergedArray,
 79                 @"merge of two non empty arrays with no merger should render "
 80                 @"an array");
 81  STAssertEquals([mergedArray count], (NSUInteger)2,
 82                 @"merged array should have two items");
 83  STAssertEqualObjects([mergedArray objectAtIndex:0], @"abc.def", nil);
 84  STAssertEqualObjects([mergedArray objectAtIndex:1], @"abc.ghi", nil);
 85}
 86
 87- (void)testMergingTwoOneItemArraysThatDontMatchWithMerger {
 88  NSArray *arrayA = [NSArray arrayWithObject:@"abc.def"];
 89  NSArray *arrayB = [NSArray arrayWithObject:@"abc.ghi"];
 90  NSArray *mergedArray = [arrayA gtm_mergeArray:arrayB
 91                                  mergeSelector:@selector(mergeString:)];
 92  STAssertNotNil(mergedArray,
 93                 @"merge of two non empty arrays with merger should render "
 94                 @"an array");
 95  STAssertEquals([mergedArray count], (NSUInteger)2,
 96                 @"merged array should have two items");
 97  STAssertEqualObjects([mergedArray objectAtIndex:0], @"abc.def", nil);
 98  STAssertEqualObjects([mergedArray objectAtIndex:1], @"abc.ghi", nil);
 99}
100
101- (void)testMergingTwoOneItemArraysThatMatch {
102  NSArray *arrayA = [NSArray arrayWithObject:@"abc.def"];
103  NSArray *arrayB = [NSArray arrayWithObject:@"abc.def"];
104  NSArray *mergedArray = [arrayA gtm_mergeArray:arrayB
105                                  mergeSelector:nil];
106  STAssertNotNil(mergedArray,
107                 @"merge of two matching arrays with no merger should render "
108                 @"an array");
109  STAssertEquals([mergedArray count], (NSUInteger)2,
110                 @"merged array with no merger should have two items");
111  STAssertEqualObjects([mergedArray objectAtIndex:0], @"abc.def", nil);
112  STAssertEqualObjects([mergedArray objectAtIndex:1], @"abc.def", nil);
113}
114
115- (void)testMergingTwoOneItemArraysThatMatchWithMerger {
116  NSArray *arrayA = [NSArray arrayWithObject:@"abc.def"];
117  NSArray *arrayB = [NSArray arrayWithObject:@"abc.def"];
118  NSArray *mergedArray = [arrayA gtm_mergeArray:arrayB
119                                  mergeSelector:@selector(mergeString:)];
120  STAssertNotNil(mergedArray,
121                 @"merge of two matching arrays with merger should render "
122                 @"an array");
123  STAssertEquals([mergedArray count], (NSUInteger)1,
124                 @"merged array with merger should have one items");
125  STAssertEqualObjects([mergedArray objectAtIndex:0], @"abc.def", nil);
126}
127
128- (void)testMergingMultipleItemArray {
129  NSArray *arrayA = [NSArray arrayWithObjects:
130                     @"Kansas",
131                     @"Arkansas",
132                     @"Wisconson",
133                     @"South Carolina",
134                     nil];
135  NSArray *arrayB = [NSArray arrayWithObjects:
136                     @"South Carolina",
137                     @"Quebec",
138                     @"British Columbia",
139                     @"Arkansas",
140                     @"South Hamptom",
141                     nil];
142  NSArray *mergedArray = [arrayA gtm_mergeArray:arrayB
143                                  mergeSelector:nil];
144  STAssertNotNil(mergedArray,
145                 @"merge of two non empty arrays with no merger should render "
146                 @"an array");
147  STAssertEquals([mergedArray count], (NSUInteger)9,
148                 @"merged array should have 9 items");
149}
150
151- (void)testMergingMultipleItemArrayWithMerger {
152  NSArray *arrayA = [NSArray arrayWithObjects:
153                     @"Kansas",
154                     @"Arkansas",
155                     @"Wisconson",
156                     @"South Carolina",
157                     nil];
158  NSArray *arrayB = [NSArray arrayWithObjects:
159                     @"South Carolina",
160                     @"Quebec",
161                     @"British Columbia",
162                     @"Arkansas",
163                     @"South Hamptom",
164                     nil];
165  NSArray *mergedArray = [arrayA gtm_mergeArray:arrayB
166                                  mergeSelector:@selector(mergeString:)];
167  STAssertNotNil(mergedArray,
168                 @"merge of two non empty arrays with merger should render "
169                 @"an array");
170  STAssertEquals([mergedArray count], (NSUInteger)7,
171                 @"merged array should have 7 items");
172}
173
174- (void)testMergeWithEmptyArrays {
175  NSArray *arrayA = [NSArray arrayWithObjects:@"xyz", @"abc", @"mno", nil];
176  NSArray *arrayB = [NSArray array];
177  NSArray *expected = [NSArray arrayWithObjects:@"abc", @"mno", @"xyz", nil];
178  STAssertNotNil(arrayA, nil);
179  STAssertNotNil(arrayB, nil);
180  STAssertNotNil(expected, nil);
181  NSArray *mergedArray;
182
183  // no merger
184  mergedArray = [arrayA gtm_mergeArray:arrayB
185                         mergeSelector:nil];
186  STAssertNotNil(mergedArray, nil);
187  STAssertEqualObjects(mergedArray, expected, nil);
188
189  // w/ merger
190  mergedArray = [arrayA gtm_mergeArray:arrayB
191                         mergeSelector:@selector(mergeString:)];
192  STAssertNotNil(mergedArray, nil);
193  STAssertEqualObjects(mergedArray, expected, nil);
194
195  // no merger and array args reversed
196  mergedArray = [arrayB gtm_mergeArray:arrayA
197                         mergeSelector:nil];
198  STAssertNotNil(mergedArray, nil);
199  STAssertEqualObjects(mergedArray, expected, nil);
200
201  // w/ merger and array args reversed
202  mergedArray = [arrayB gtm_mergeArray:arrayA
203                         mergeSelector:@selector(mergeString:)];
204  STAssertNotNil(mergedArray, nil);
205  STAssertEqualObjects(mergedArray, expected, nil);
206
207}
208
209@end
210
211
212@implementation NSString (GTMStringMergingTestAdditions)
213
214- (NSString *)mergeString:(NSString *)stringB {
215  return stringB;
216}
217
218@end
219