Fix RKDictionaryByReverseMergingDictionaryWithDictionary method and add unit tests. Refs #938.

This commit is contained in:
Jawwad Ahmad
2012-09-18 15:17:51 -04:00
parent 8369770a2c
commit 261cafe18a
5 changed files with 185 additions and 4 deletions

View File

@@ -27,7 +27,7 @@
@param anotherDictionary A secondary dictionary to perform the reverse merging with.
@return A new `NSDicionary` object that is the product of the reverse merge.
*/
NSDictionary *RKDictionaryByReverseMergingDictionaryWithDictionary(NSDictionary *dictionary, NSDictionary *anotherDictionary);
NSDictionary *RKDictionaryByReverseMergingDictionaryWithDictionary(NSDictionary *dict1, NSDictionary *dict2);
/**
Return a new dictionary by stripping out any percent escapes (such as %20) from the given dictionary's key and values.

View File

@@ -11,8 +11,8 @@
NSDictionary * RKDictionaryByReverseMergingDictionaryWithDictionary(NSDictionary *dictionary, NSDictionary *anotherDictionary)
{
NSMutableDictionary *mergedDictionary = [NSMutableDictionary dictionaryWithDictionary:dictionary];
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if ([mergedDictionary objectForKey:key] && [obj isKindOfClass:[NSDictionary class]]) {
[anotherDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if ([[mergedDictionary objectForKey:key] isKindOfClass:[NSDictionary class]] && [obj isKindOfClass:[NSDictionary class]]) {
NSDictionary *newVal = RKDictionaryByReverseMergingDictionaryWithDictionary([mergedDictionary objectForKey:key], obj);
[mergedDictionary setObject:newVal forKey:key];
} else {

View File

@@ -600,6 +600,8 @@
49D2759E14C9EF1E0090845D /* ISO8601DateFormatter.h in Headers */ = {isa = PBXBuildFile; fileRef = 49D2759914C9EF1E0090845D /* ISO8601DateFormatter.h */; settings = {ATTRIBUTES = (Public, ); }; };
49D2759F14C9EF1E0090845D /* ISO8601DateFormatter.m in Sources */ = {isa = PBXBuildFile; fileRef = 49D2759A14C9EF1E0090845D /* ISO8601DateFormatter.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
49D275A114C9EF1E0090845D /* ISO8601DateFormatter.m in Sources */ = {isa = PBXBuildFile; fileRef = 49D2759A14C9EF1E0090845D /* ISO8601DateFormatter.m */; };
5C927E141608FFFD00DC8B07 /* RKDictionaryUtilitiesTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 5C927E131608FFFD00DC8B07 /* RKDictionaryUtilitiesTest.m */; };
5C927E151608FFFD00DC8B07 /* RKDictionaryUtilitiesTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 5C927E131608FFFD00DC8B07 /* RKDictionaryUtilitiesTest.m */; };
5CCC295615B7124A0045F0F5 /* RKMacros.h in Headers */ = {isa = PBXBuildFile; fileRef = 5CCC295515B7124A0045F0F5 /* RKMacros.h */; settings = {ATTRIBUTES = (Public, ); }; };
5CCC295715B7124A0045F0F5 /* RKMacros.h in Headers */ = {isa = PBXBuildFile; fileRef = 5CCC295515B7124A0045F0F5 /* RKMacros.h */; settings = {ATTRIBUTES = (Public, ); }; };
73D3907414CA1AE00093E3D6 /* parent.json in Resources */ = {isa = PBXBuildFile; fileRef = 73D3907114CA19F90093E3D6 /* parent.json */; };
@@ -1026,6 +1028,7 @@
49D2759A14C9EF1E0090845D /* ISO8601DateFormatter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ISO8601DateFormatter.m; path = iso8601parser/ISO8601DateFormatter.m; sourceTree = "<group>"; };
49D2759B14C9EF1E0090845D /* LICENSE.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = LICENSE.txt; path = iso8601parser/LICENSE.txt; sourceTree = "<group>"; };
49D2759C14C9EF1E0090845D /* README.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = README.txt; path = iso8601parser/README.txt; sourceTree = "<group>"; };
5C927E131608FFFD00DC8B07 /* RKDictionaryUtilitiesTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RKDictionaryUtilitiesTest.m; sourceTree = "<group>"; };
5CCC295515B7124A0045F0F5 /* RKMacros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RKMacros.h; sourceTree = "<group>"; };
7394DF3514CF157A00CE7BCE /* RKManagedObjectCaching.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RKManagedObjectCaching.h; sourceTree = "<group>"; };
7394DF3814CF168C00CE7BCE /* RKFetchRequestManagedObjectCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RKFetchRequestManagedObjectCache.h; sourceTree = "<group>"; };
@@ -1646,6 +1649,7 @@
251610511456F2330060A5C5 /* Support */ = {
isa = PBXGroup;
children = (
5C927E131608FFFD00DC8B07 /* RKDictionaryUtilitiesTest.m */,
252EFB0C14D98F76004863C8 /* RKMutableBlockDictionaryTest.m */,
251610521456F2330060A5C5 /* NSDictionary+RKRequestSerializationTest.m */,
251610531456F2330060A5C5 /* NSStringRestKitTest.m */,
@@ -2552,6 +2556,7 @@
25A763E115C72B9D00A9DF31 /* RKSearchTokenizerTest.m in Sources */,
25A763E515C7424500A9DF31 /* RKSearchIndexerTest.m in Sources */,
25C246A415C83B090032212E /* RKSearchTest.m in Sources */,
5C927E141608FFFD00DC8B07 /* RKDictionaryUtilitiesTest.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -2712,6 +2717,7 @@
25A763E215C72B9D00A9DF31 /* RKSearchTokenizerTest.m in Sources */,
25A763E615C7424500A9DF31 /* RKSearchIndexerTest.m in Sources */,
25C246A515C83B090032212E /* RKSearchTest.m in Sources */,
5C927E151608FFFD00DC8B07 /* RKDictionaryUtilitiesTest.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@@ -0,0 +1,176 @@
//
// RKDictionaryUtilitiesTest.m
// RestKit
//
// Created by Jawwad Ahmad on 9/18/12.
// Copyright (c) 2009-2012 RestKit. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "RKTestEnvironment.h"
#import "RKDictionaryUtilities.h"
@interface RKDictionaryUtilitiesTest : RKTestCase
@end
@implementation RKDictionaryUtilitiesTest
- (void)testMergedDictGetsNewKeyFromSubdict
{
NSDictionary *dict1 = @{
@"name" : @{
@"firstName" : @"Blake"
}
};
NSDictionary *dict2 = @{
@"name" : @{
@"lastName" : @"Watters"
}
};
NSDictionary *expectedMergedDict = @{
@"name" : @{
@"firstName" : @"Blake",
@"lastName" : @"Watters"
}
};
NSDictionary *actualMergedDict = RKDictionaryByReverseMergingDictionaryWithDictionary(dict1, dict2);
assertThat(actualMergedDict, equalTo(expectedMergedDict));
}
- (void)testMergedDictOverwritesOldKeyInSubdict
{
NSDictionary *dict1 = @{
@"name" : @{
@"firstName" : @"Blake",
@"lastName" : @"Unknown"
}
};
NSDictionary *dict2 = @{
@"name" : @{
@"lastName" : @"Watters"
}
};
NSDictionary *expectedMergedDict = @{
@"name" : @{
@"firstName" : @"Blake",
@"lastName" : @"Watters"
}
};
NSDictionary *actualMergedDict = RKDictionaryByReverseMergingDictionaryWithDictionary(dict1, dict2);
assertThat(actualMergedDict, equalTo(expectedMergedDict));
}
- (void)testMergedDictAddsNewDict
{
NSDictionary *dict1 = @{
@"name" : @{
@"firstName" : @"Blake",
}
};
NSDictionary *dict2 = @{
@"name" : @{
@"lastName" : @"Watters"
},
@"email" : @"blake@restkit.org"
};
NSDictionary *expectedMergedDict = @{
@"name" : @{
@"firstName" : @"Blake",
@"lastName" : @"Watters"
},
@"email" : @"blake@restkit.org"
};
NSDictionary *actualMergedDict = RKDictionaryByReverseMergingDictionaryWithDictionary(dict1, dict2);
assertThat(actualMergedDict, equalTo(expectedMergedDict));
}
- (void)testMergedDictAddsOverwritesOldNonDictValueWithNewSubDictValue
{
NSDictionary *dict1 = @{
@"name" : @{
@"firstName" : @"Blake",
},
@"email" : @"blake@restkit.org"
};
NSDictionary *dict2 = @{
@"name" : @{
@"lastName" : @"Watters"
},
@"email" : @{
@"RestKit" : @"blake@restkit.org",
@"Other" : @"blake@example.com"
}
};
NSDictionary *expectedMergedDict = @{
@"name" : @{
@"firstName" : @"Blake",
@"lastName" : @"Watters"
},
@"email" : @{
@"RestKit" : @"blake@restkit.org",
@"Other" : @"blake@example.com"
}
};
NSDictionary *actualMergedDict = RKDictionaryByReverseMergingDictionaryWithDictionary(dict1, dict2);
assertThat(actualMergedDict, equalTo(expectedMergedDict));
}
- (void)testMergedDictAddsOverwritesOldSubdictValueWithNonDictValue
{
NSDictionary *dict1 = @{
@"name" : @{
@"lastName" : @"Watters"
},
@"email" : @{
@"RestKit" : @"blake@restkit.org",
@"Other" : @"blake@example.com"
}
};
NSDictionary *dict2 = @{
@"name" : @{
@"firstName" : @"Blake",
},
@"email" : @"blake@restkit.org"
};
NSDictionary *expectedMergedDict = @{
@"name" : @{
@"firstName" : @"Blake",
@"lastName" : @"Watters"
},
@"email" : @"blake@restkit.org",
};
NSDictionary *actualMergedDict = RKDictionaryByReverseMergingDictionaryWithDictionary(dict1, dict2);
assertThat(actualMergedDict, equalTo(expectedMergedDict));
}
@end

View File

@@ -20,7 +20,6 @@
#include <objc/runtime.h>
#import "RKTestEnvironment.h"
#import "RKParserRegistry.h"
@implementation RKTestCase