Skip to content
This repository
Robert Böhnke
file 211 lines (173 sloc) 6.216 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
//
// MTLJSONAdapterSpec.m
// Mantle
//
// Created by Justin Spahr-Summers on 2013-02-13.
// Copyright (c) 2013 GitHub. All rights reserved.
//

#import "MTLTestModel.h"

SpecBegin(MTLJSONAdapter)

it(@"should initialize from JSON", ^{
NSDictionary *values = @{
@"username": NSNull.null,
@"count": @"5",
};

NSError *error = nil;
MTLJSONAdapter *adapter = [[MTLJSONAdapter alloc] initWithJSONDictionary:values modelClass:MTLTestModel.class error:&error];
expect(adapter).notTo.beNil();
expect(error).to.beNil();

MTLTestModel *model = (id)adapter.model;
expect(model).notTo.beNil();
expect(model.name).to.beNil();
expect(model.count).to.equal(5);

NSDictionary *JSONDictionary = @{
@"username": NSNull.null,
@"count": @"5",
@"nested": @{ @"name": NSNull.null },
};

expect(adapter.JSONDictionary).to.equal(JSONDictionary);
});

it(@"should initialize from a model", ^{
MTLTestModel *model = [MTLTestModel modelWithDictionary:@{
@"name": @"foobar",
@"count": @5,
} error:NULL];

MTLJSONAdapter *adapter = [[MTLJSONAdapter alloc] initWithModel:model];
expect(adapter).notTo.beNil();
expect(adapter.model).to.beIdenticalTo(model);

NSDictionary *JSONDictionary = @{
@"username": @"foobar",
@"count": @"5",
@"nested": @{ @"name": NSNull.null },
};

expect(adapter.JSONDictionary).to.equal(JSONDictionary);
});

it(@"should initialize nested key paths from JSON", ^{
NSDictionary *values = @{
@"username": @"foo",
@"nested": @{ @"name": @"bar" },
@"count": @"0"
};

NSError *error = nil;
MTLTestModel *model = [MTLJSONAdapter modelOfClass:MTLTestModel.class fromJSONDictionary:values error:&error];
expect(model).notTo.beNil();
expect(error).to.beNil();

expect(model.name).to.equal(@"foo");
expect(model.count).to.equal(0);
expect(model.nestedName).to.equal(@"bar");

expect([MTLJSONAdapter JSONDictionaryFromModel:model]).to.equal(values);
});

it(@"should return nil and error with an invalid key path from JSON",^{
NSDictionary *values = @{
@"username": @"foo",
@"nested": @"bar",
@"count": @"0"
};

NSError *error = nil;
MTLTestModel *model = [MTLJSONAdapter modelOfClass:MTLTestModel.class fromJSONDictionary:values error:&error];
expect(model).beNil();
expect(error).notTo.beNil();
expect(error.domain).to.equal(MTLJSONAdapterErrorDomain);
expect(error.code).to.equal(MTLJSONAdapterErrorInvalidJSONDictionary);
});

it(@"should support key paths across arrays", ^{
NSDictionary *values = @{
@"users": @[
@{
@"name": @"foo"
},
@{
@"name": @"bar"
},
@{
@"name": @"baz"
}
]
};

NSError *error = nil;
MTLArrayTestModel *model = [MTLJSONAdapter modelOfClass:MTLArrayTestModel.class fromJSONDictionary:values error:&error];
expect(model).notTo.beNil();
expect(error).to.beNil();

expect(model.names).to.equal((@[ @"foo", @"bar", @"baz" ]));
});

it(@"should initialize without returning any error when using a JSON dictionary which Null.null as value",^{
NSDictionary *values = @{
@"username": @"foo",
@"nested": NSNull.null,
@"count": @"0"
};

NSError *error = nil;
MTLTestModel *model = [MTLJSONAdapter modelOfClass:MTLTestModel.class fromJSONDictionary:values error:&error];
expect(model).notTo.beNil();
expect(error).to.beNil();

expect(model.name).to.equal(@"foo");
expect(model.count).to.equal(0);
expect(model.nestedName).to.beNil();
});

it(@"should return nil and an error with a nil JSON dictionary", ^{
NSError *error = nil;
MTLJSONAdapter *adapter = [[MTLJSONAdapter alloc] initWithJSONDictionary:nil modelClass:MTLTestModel.class error:&error];
expect(adapter).to.beNil();
expect(error).notTo.beNil();
expect(error.domain).to.equal(MTLJSONAdapterErrorDomain);
expect(error.code).to.equal(MTLJSONAdapterErrorInvalidJSONDictionary);
});

it(@"should return nil and an error with a wrong data type as dictionary", ^{
NSError *error = nil;
id wrongDictionary = @"";
MTLJSONAdapter *adapter = [[MTLJSONAdapter alloc] initWithJSONDictionary:wrongDictionary modelClass:MTLTestModel.class error:&error];
expect(adapter).to.beNil();
expect(error).notTo.beNil();
expect(error.domain).to.equal(MTLJSONAdapterErrorDomain);
expect(error.code).to.equal(MTLJSONAdapterErrorInvalidJSONDictionary);
});

it(@"should ignore unrecognized JSON keys", ^{
NSDictionary *values = @{
@"foobar": @"foo",
@"count": @"2",
@"_": NSNull.null,
@"username": @"buzz",
@"nested": @{ @"name": @"bar", @"stuffToIgnore": @5, @"moreNonsense": NSNull.null },
};

NSError *error = nil;
MTLTestModel *model = [MTLJSONAdapter modelOfClass:MTLTestModel.class fromJSONDictionary:values error:&error];
expect(model).notTo.beNil();
expect(error).to.beNil();

expect(model.name).to.equal(@"buzz");
expect(model.count).to.equal(2);
expect(model.nestedName).to.equal(@"bar");
});

it(@"should fail to initialize if JSON dictionary validation fails", ^{
NSDictionary *values = @{
@"username": @"this is too long a name",
};

NSError *error = nil;
MTLTestModel *model = [MTLJSONAdapter modelOfClass:MTLTestModel.class fromJSONDictionary:values error:&error];
expect(model).to.beNil();
expect(error.domain).to.equal(MTLTestModelErrorDomain);
expect(error.code).to.equal(MTLTestModelNameTooLong);
});

it(@"should parse a different model class", ^{
NSDictionary *values = @{
@"username": @"foo",
@"nested": @{ @"name": @"bar" },
@"count": @"0"
};

NSError *error = nil;
MTLTestModel *model = [MTLJSONAdapter modelOfClass:MTLSubstitutingTestModel.class fromJSONDictionary:values error:&error];
expect(model).to.beKindOf(MTLTestModel.class);
expect(error).to.beNil();

expect(model.name).to.equal(@"foo");
expect(model.count).to.equal(0);
expect(model.nestedName).to.equal(@"bar");

expect([MTLJSONAdapter JSONDictionaryFromModel:model]).to.equal(values);
});

it(@"should return an error when no suitable model class is found", ^{
NSError *error = nil;
MTLTestModel *model = [MTLJSONAdapter modelOfClass:MTLSubstitutingTestModel.class fromJSONDictionary:@{} error:&error];
expect(model).to.beNil();

expect(error).notTo.beNil();
expect(error.domain).to.equal(MTLJSONAdapterErrorDomain);
expect(error.code).to.equal(MTLJSONAdapterErrorNoClassFound);
});

SpecEnd
Something went wrong with that request. Please try again.