Skip to content

Commit

Permalink
Fixed some problems with recursive defaults. Added tests so it doesn'…
Browse files Browse the repository at this point in the history
…t happen again.
  • Loading branch information
bnickel committed Aug 2, 2013
1 parent 54ac2c0 commit 7ce3ae9
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 2 deletions.
54 changes: 54 additions & 0 deletions AMYServer Tests/AMYMocktailResponseTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// AMYMocktailResponseTests.m
// AMYServer
//
// Created by Brian Nickel on 8/01/13.
// Copyright (c) 2013 Brian Nickel. All rights reserved.
//

#import <KIFTestStepValidation.h>
#import <KIFTestCase.h>
#import "_AMYMocktailResponse.h"
#import <NSBundle-KIFAdditions.h>

@interface AMYMocktailResponseTests : SenTestCase
@end

@implementation AMYMocktailResponseTests

- (void)testDefaults
{
_AMYMocktailResponse *response = [_AMYMocktailResponse responseFromTail:@"xml" bundle:[NSBundle KIFTestBundle] error:NULL];
NSString *body = [[NSString alloc] initWithData:[response bodyWithValues:@{} error:NULL] encoding:NSUTF8StringEncoding];
STAssertTrue([body rangeOfString:@"<items description=\"abc\">"].location != NSNotFound, @"Should have found default.");
STAssertTrue([body rangeOfString:@"<item key=\"k\">v</item>"].location != NSNotFound, @"Should have found default.");
}

- (void)testThatSettingOneValueDoesNotWipeOutAnother
{
_AMYMocktailResponse *response = [_AMYMocktailResponse responseFromTail:@"xml" bundle:[NSBundle KIFTestBundle] error:NULL];
NSString *body = [[NSString alloc] initWithData:[response bodyWithValues:@{@"a":@{@"q":@"r"}} error:NULL] encoding:NSUTF8StringEncoding];
NSLog(@"Body: %@", body);
STAssertTrue([body rangeOfString:@"<items description=\"abc\">"].location != NSNotFound, @"Should have found default.");
STAssertTrue([body rangeOfString:@"<item key=\"k\">v</item>"].location != NSNotFound, @"Should have found default.");
}

- (void)testThatNewValuesOverrideOldOnes
{
_AMYMocktailResponse *response = [_AMYMocktailResponse responseFromTail:@"xml" bundle:[NSBundle KIFTestBundle] error:NULL];
NSString *body = [[NSString alloc] initWithData:[response bodyWithValues:@{@"a":@{@"b":@{@"c":@"d"}}} error:NULL] encoding:NSUTF8StringEncoding];
STAssertTrue([body rangeOfString:@"<items description=\"d\">"].location != NSNotFound, @"Should have overridden default for child.");
}

- (void)testThatItemsInAnArrayTreatTheFirstItemAsATemplate
{
_AMYMocktailResponse *response = [_AMYMocktailResponse responseFromTail:@"xml" bundle:[NSBundle KIFTestBundle] error:NULL];
NSString *body = [[NSString alloc] initWithData:[response bodyWithValues:@{@"items":@[@{@"key": @"key"}, @{@"value":@"value"}, @{}]} error:NULL] encoding:NSUTF8StringEncoding];
NSLog(@"Body: %@", body);
STAssertTrue([body rangeOfString:@"<item key=\"key\">v</item>"].location != NSNotFound, @"Should have found 'key' in position 1.");
STAssertTrue([body rangeOfString:@"<item key=\"k\">value</item>"].location != NSNotFound, @"Should have found 'value' in position 2.");
STAssertTrue([body rangeOfString:@"<item key=\"k\">v</item>"].location != NSNotFound, @"Should have found default in position 3.");
}


@end
10 changes: 10 additions & 0 deletions AMYServer Tests/xml.tail
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.*
.*
200
text/xml

<items description="{{a.b.c}}">
{{#items}}
<item key="{{key}}">{{value}}</item>
{{/items}}
</items>
4 changes: 4 additions & 0 deletions AMYServer Tests/xml.tail.defaults.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"a": { "b": { "c": "abc" } },
"items": [ { "key": "k", "value": "v" } ]
}
2 changes: 1 addition & 1 deletion AMYServer.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ Pod::Spec.new do |s|
s.requires_arc = true
s.frameworks = 'SenTestingKit'

s.dependency 'KIF-next', '~> 2.0.0pre3'
s.dependency 'KIF-next', '~> 2.0.0pre4'
s.dependency 'GRMustache', '~> 6.7'
end
12 changes: 12 additions & 0 deletions AMYServer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
5641C68D282B4F7C8E987DB7 /* libPods.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 7C65A889641443929FA2D0A6 /* libPods.a */; };
D8941B57AA6B462F808362B6 /* libPods-AMYServer Tests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 2CFA695D96E74AD0B14ECB01 /* libPods-AMYServer Tests.a */; };
EB1D09D417AA2E7B003D3333 /* invalid-header.tail in Resources */ = {isa = PBXBuildFile; fileRef = EB3F654317A9E5DC00469D18 /* invalid-header.tail */; };
EB23AA7317AB61F1002B6399 /* AMYMocktailResponseTests.m in Sources */ = {isa = PBXBuildFile; fileRef = EB23AA7217AB61F1002B6399 /* AMYMocktailResponseTests.m */; };
EB23AA7517AB6229002B6399 /* xml.tail in Resources */ = {isa = PBXBuildFile; fileRef = EB23AA7417AB6229002B6399 /* xml.tail */; };
EB23AA7717AB62D9002B6399 /* xml.tail.defaults.json in Resources */ = {isa = PBXBuildFile; fileRef = EB23AA7617AB62D9002B6399 /* xml.tail.defaults.json */; };
EB283E3B17A6317000883DEC /* _AMYMocktailResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = EB283E3A17A6317000883DEC /* _AMYMocktailResponse.m */; };
EB283E3C17A6317000883DEC /* _AMYMocktailResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = EB283E3A17A6317000883DEC /* _AMYMocktailResponse.m */; };
EB3F653A17A9B10B00469D18 /* successful-login.tail.defaults.json in Resources */ = {isa = PBXBuildFile; fileRef = EB3F653917A9AFD700469D18 /* successful-login.tail.defaults.json */; };
Expand Down Expand Up @@ -73,6 +76,9 @@
2CFA695D96E74AD0B14ECB01 /* libPods-AMYServer Tests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-AMYServer Tests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
660DAD8CDF4041B6ADB992C7 /* Pods.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.xcconfig; path = Pods/Pods.xcconfig; sourceTree = SOURCE_ROOT; };
7C65A889641443929FA2D0A6 /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; };
EB23AA7217AB61F1002B6399 /* AMYMocktailResponseTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AMYMocktailResponseTests.m; sourceTree = "<group>"; };
EB23AA7417AB6229002B6399 /* xml.tail */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = xml.tail; sourceTree = "<group>"; };
EB23AA7617AB62D9002B6399 /* xml.tail.defaults.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = xml.tail.defaults.json; sourceTree = "<group>"; };
EB283E3917A6317000883DEC /* _AMYMocktailResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = _AMYMocktailResponse.h; sourceTree = "<group>"; };
EB283E3A17A6317000883DEC /* _AMYMocktailResponse.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = _AMYMocktailResponse.m; sourceTree = "<group>"; };
EB3F653917A9AFD700469D18 /* successful-login.tail.defaults.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "successful-login.tail.defaults.json"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -244,13 +250,16 @@
EB971442179BAD210005E31E /* ExampleServer.h */,
EB971443179BAD210005E31E /* ExampleServer.m */,
EB97143C17966E740005E31E /* AMYServerTests.m */,
EB23AA7217AB61F1002B6399 /* AMYMocktailResponseTests.m */,
EBAE488317A46C680005EE19 /* successful-login.tail */,
EB3F653917A9AFD700469D18 /* successful-login.tail.defaults.json */,
EB3F653B17A9B16200469D18 /* bad-mustache.tail */,
EB3F653D17A9E4AE00469D18 /* not-enough-lines.tail */,
EB3F654317A9E5DC00469D18 /* invalid-header.tail */,
EB3F653F17A9E4D400469D18 /* bad-json.tail */,
EB3F654117A9E4E300469D18 /* bad-json.tail.defaults.json */,
EB23AA7417AB6229002B6399 /* xml.tail */,
EB23AA7617AB62D9002B6399 /* xml.tail.defaults.json */,
);
path = "AMYServer Tests";
sourceTree = "<group>";
Expand Down Expand Up @@ -373,11 +382,13 @@
EB1D09D417AA2E7B003D3333 /* invalid-header.tail in Resources */,
EB3F653C17A9B16200469D18 /* bad-mustache.tail in Resources */,
EB97142B179657770005E31E /* InfoPlist.strings in Resources */,
EB23AA7717AB62D9002B6399 /* xml.tail.defaults.json in Resources */,
EB3F653E17A9E4AE00469D18 /* not-enough-lines.tail in Resources */,
EB3F653A17A9B10B00469D18 /* successful-login.tail.defaults.json in Resources */,
EBAE488417A46C680005EE19 /* successful-login.tail in Resources */,
EB3F654017A9E4D400469D18 /* bad-json.tail in Resources */,
EB3F654217A9E4E300469D18 /* bad-json.tail.defaults.json in Resources */,
EB23AA7517AB6229002B6399 /* xml.tail in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -489,6 +500,7 @@
EB97143D17966E740005E31E /* AMYServerTests.m in Sources */,
EB97143F1796706D0005E31E /* AMYServer.m in Sources */,
EB9714401796706D0005E31E /* AMYRequest.m in Sources */,
EB23AA7317AB61F1002B6399 /* AMYMocktailResponseTests.m in Sources */,
EB9714411796706D0005E31E /* _AMYURLProtocol.m in Sources */,
EB971444179BAD210005E31E /* ExampleServer.m in Sources */,
);
Expand Down
3 changes: 2 additions & 1 deletion AMYServer/_AMYMocktailResponse.m
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,13 @@ - (NSDictionary *)defaultValuesWithError:(NSError **)error;
if (!destObj) {
result[key] = obj;
} else if ([obj isKindOfClass:[NSDictionary class]]) {
result[key] = _mocktailMergedDictionary(destObj, src);
result[key] = _mocktailMergedDictionary(destObj, obj);
} else if ([obj isKindOfClass:[NSArray class]] && [(NSArray *)obj count]) {
NSMutableArray *items = @[].mutableCopy;
for (NSDictionary *item in destObj) {
[items addObject:_mocktailMergedDictionary(item, obj[0])];
}
result[key] = items;
}
}];

Expand Down

0 comments on commit 7ce3ae9

Please sign in to comment.