Advertisement
chunkyguy

array_of_dictionaries

Jun 8th, 2014
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (void)test
  2. {
  3.     NSArray *arr = @[@{@"name": @"a", @"website": @"http://a.com", @"isTrue": @(YES)},
  4.                      @{@"name": @"b", @"website": @"http://b.com", @"isTrue": @(NO)},
  5.                      @{@"name": @"c", @"website": @"http://c.com", @"isTrue": @(NO)}];
  6.    
  7.     // Both the cases should be same as we don't care what value 'isTrue' has
  8.     NSDictionary *testCaseFound1 = @{@"name": @"c", @"website": @"http://c.com", @"isTrue": @(YES)};
  9.     NSDictionary *testCaseFound2 = @{@"name": @"c", @"website": @"http://c.com", @"isTrue": @(NO)};
  10.  
  11.     NSDictionary *testCaseNotFound1 = @{@"name": @"d", @"website": @"http://d.com", @"isTrue": @(YES)};
  12.     NSDictionary *testCaseNotFound2 = @{@"name": @"d", @"website": @"http://d.com", @"isTrue": @(NO)};
  13.  
  14.  
  15.     /* Comment or uncomment this line to toggle between two logics */
  16.     //#define USE_OLD_LOGIC
  17.    
  18. #ifdef USE_OLD_LOGIC
  19.  
  20.     /* Old logic: must not work */
  21.     NSAssert([arr indexOfObject:testCaseFound1] == 2, @"Found logic 1 broken!");
  22.     NSAssert([arr indexOfObject:testCaseFound2] == 2, @"Found logic 2 broken!");
  23.     NSAssert([arr indexOfObject:testCaseNotFound1] == NSNotFound, @"Not found logic 1 broken!");
  24.     NSAssert([arr indexOfObject:testCaseNotFound2] == NSNotFound, @"Not found logic 2 broken!");
  25.  
  26. #else
  27.  
  28.     /* New logic: must work */
  29.     __block NSDictionary *testCase = nil; // will be set later
  30.     BOOL (^skipIsTruePredicate)(NSDictionary *, NSUInteger idx, BOOL *stop) = ^BOOL(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
  31.  
  32.         NSAssert(testCase, @"Invalid testCase");
  33.        
  34.         NSUInteger checks = 0;
  35.         for (NSString *key in [obj allKeys]) {
  36.            
  37.             if ([key isEqualToString:@"isTrue"]) {
  38.                 checks++;
  39.                 continue; // skip test
  40.             }
  41.            
  42.             if ([obj objectForKey:key] == [testCase objectForKey:key]) {
  43.                 checks++;
  44.             }
  45.         }
  46.        
  47.         return (checks == [[obj allKeys] count]);
  48.     };
  49.  
  50.     testCase = testCaseFound1;
  51.     NSAssert([arr indexOfObjectPassingTest:skipIsTruePredicate] == 2, @"Found logic 1 broken!");
  52.     testCase = testCaseFound2;
  53.     NSAssert([arr indexOfObjectPassingTest:skipIsTruePredicate] == 2, @"Found logic 2 broken!");
  54.     testCase = testCaseNotFound1;
  55.     NSAssert([arr indexOfObjectPassingTest:skipIsTruePredicate] == NSNotFound, @"Not found logic 1 broken!");
  56.     testCase = testCaseNotFound2;
  57.     NSAssert([arr indexOfObjectPassingTest:skipIsTruePredicate] == NSNotFound, @"Not found logic 2 broken!");
  58.    
  59. #endif
  60.    
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement