Skip to content

Commit

Permalink
Update unit tests with error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jspahrsummers committed Feb 20, 2013
1 parent e0c0017 commit e8d2dea
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion MantleTests/MTLJSONAdapterSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
MTLTestModel *model = [MTLTestModel modelWithDictionary:@{
@"name": @"foobar",
@"count": @5,
}];
} error:NULL];

MTLJSONAdapter *adapter = [[MTLJSONAdapter alloc] initWithModel:model];
expect(adapter).notTo.beNil();
Expand Down
4 changes: 3 additions & 1 deletion MantleTests/MTLModelNSCodingSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@
@"count": @5,
};

model = [[MTLTestModel alloc] initWithDictionary:values];
NSError *error = nil;
model = [[MTLTestModel alloc] initWithDictionary:values error:&error];
expect(model).notTo.beNil();
expect(error).to.beNil();

archiveAndUnarchiveModel = [^{
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:model];
Expand Down
21 changes: 15 additions & 6 deletions MantleTests/MTLModelSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
});

it(@"should initialize to default values with a nil dictionary", ^{
MTLTestModel *dictionaryModel = [[MTLTestModel alloc] initWithDictionary:nil];
NSError *error = nil;
MTLTestModel *dictionaryModel = [[MTLTestModel alloc] initWithDictionary:nil error:&error];
expect(dictionaryModel).notTo.beNil();
expect(error).to.beNil();

MTLTestModel *defaultModel = [[MTLTestModel alloc] init];
expect(dictionaryModel).to.equal(defaultModel);
Expand All @@ -61,8 +63,10 @@
@"weakModel": emptyModel,
};

model = [[MTLTestModel alloc] initWithDictionary:values];
NSError *error = nil;
model = [[MTLTestModel alloc] initWithDictionary:values error:&error];
expect(model).notTo.beNil();
expect(error).to.beNil();
});

it(@"should initialize with the given values", ^{
Expand All @@ -78,7 +82,7 @@
it(@"should compare equal to a matching model", ^{
expect(model).to.equal(model);

MTLTestModel *matchingModel = [[MTLTestModel alloc] initWithDictionary:values];
MTLTestModel *matchingModel = [[MTLTestModel alloc] initWithDictionary:values error:NULL];
expect(model).to.equal(matchingModel);
expect(model.hash).to.equal(matchingModel.hash);
expect(model.dictionaryValue).to.equal(matchingModel.dictionaryValue);
Expand All @@ -98,15 +102,20 @@
});

it(@"should fail to initialize if dictionary validation fails", ^{
MTLTestModel *model = [[MTLTestModel alloc] initWithDictionary:@{ @"name": @"this is too long a name" }];
NSError *error = nil;
MTLTestModel *model = [[MTLTestModel alloc] initWithDictionary:@{ @"name": @"this is too long a name" } error:&error];
expect(model).to.beNil();

expect(error).notTo.beNil();
expect(error.domain).to.equal(MTLTestModelErrorDomain);
expect(error.code).to.equal(MTLTestModelNameTooLong);
});

it(@"should merge two models together", ^{
MTLTestModel *target = [[MTLTestModel alloc] initWithDictionary:@{ @"name": @"foo", @"count": @(5) }];
MTLTestModel *target = [[MTLTestModel alloc] initWithDictionary:@{ @"name": @"foo", @"count": @(5) } error:NULL];
expect(target).notTo.beNil();

MTLTestModel *source = [[MTLTestModel alloc] initWithDictionary:@{ @"name": @"bar", @"count": @(3) }];
MTLTestModel *source = [[MTLTestModel alloc] initWithDictionary:@{ @"name": @"bar", @"count": @(3) } error:NULL];
expect(source).notTo.beNil();

[target mergeValuesForKeysFromModel:source];
Expand Down
3 changes: 3 additions & 0 deletions MantleTests/MTLTestModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
// Copyright (c) 2012 GitHub. All rights reserved.
//

extern NSString * const MTLTestModelErrorDomain;
extern const NSInteger MTLTestModelNameTooLong;

@interface MTLEmptyTestModel : MTLModel
@end

Expand Down
10 changes: 9 additions & 1 deletion MantleTests/MTLTestModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

#import "MTLTestModel.h"

NSString * const MTLTestModelErrorDomain = @"MTLTestModelErrorDomain";
const NSInteger MTLTestModelNameTooLong = 1;

static NSUInteger modelVersion = 1;

@implementation MTLEmptyTestModel
Expand All @@ -18,7 +21,12 @@ @implementation MTLTestModel
#pragma mark Properties

- (BOOL)validateName:(NSString **)name error:(NSError **)error {
return [*name length] < 10;
if ([*name length] < 10) return YES;
if (error != NULL) {
*error = [NSError errorWithDomain:MTLTestModelErrorDomain code:MTLTestModelNameTooLong userInfo:nil];
}

return NO;
}

- (NSString *)dynamicName {
Expand Down

0 comments on commit e8d2dea

Please sign in to comment.