Skip to content

Commit

Permalink
GH-83 Add spec for recursive relationship copying / deallocation
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima Zen committed May 4, 2017
1 parent 80ac66b commit 030fd2b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 10 deletions.
6 changes: 6 additions & 0 deletions FastEasyMapping.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@
AD90A4C51EBB4B93006751E9 /* FEMObjectRef.m in Sources */ = {isa = PBXBuildFile; fileRef = AD90A4BF1EBB4B93006751E9 /* FEMObjectRef.m */; };
AD90A4C61EBB4B93006751E9 /* FEMObjectRef.m in Sources */ = {isa = PBXBuildFile; fileRef = AD90A4BF1EBB4B93006751E9 /* FEMObjectRef.m */; };
AD90A4C71EBB4B93006751E9 /* FEMObjectRef.m in Sources */ = {isa = PBXBuildFile; fileRef = AD90A4BF1EBB4B93006751E9 /* FEMObjectRef.m */; };
AD90A4C91EBB4EB1006751E9 /* FEMRelationshipMappingSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = AD90A4C81EBB4EB1006751E9 /* FEMRelationshipMappingSpec.m */; };
AD90A4CA1EBB4EB1006751E9 /* FEMRelationshipMappingSpec.m in Sources */ = {isa = PBXBuildFile; fileRef = AD90A4C81EBB4EB1006751E9 /* FEMRelationshipMappingSpec.m */; };
AD9400641E8AA43500558F37 /* FEMTypes.h in Headers */ = {isa = PBXBuildFile; fileRef = AD9400631E8AA43500558F37 /* FEMTypes.h */; settings = {ATTRIBUTES = (Public, ); }; };
AD9400681E8AA7AD00558F37 /* FEMRelationship.h in Headers */ = {isa = PBXBuildFile; fileRef = AD9400671E8AA7AD00558F37 /* FEMRelationship.h */; settings = {ATTRIBUTES = (Public, ); }; };
AD94006A1E8AA7C000558F37 /* FEMObjectStore.h in Headers */ = {isa = PBXBuildFile; fileRef = AD9400691E8AA7C000558F37 /* FEMObjectStore.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -496,6 +498,7 @@
AD4FC9A01E9A80F2006C6C56 /* Child+Mapping.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "Child+Mapping.m"; sourceTree = "<group>"; };
AD90A4BE1EBB4B93006751E9 /* FEMObjectRef.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FEMObjectRef.h; sourceTree = "<group>"; };
AD90A4BF1EBB4B93006751E9 /* FEMObjectRef.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FEMObjectRef.m; sourceTree = "<group>"; };
AD90A4C81EBB4EB1006751E9 /* FEMRelationshipMappingSpec.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FEMRelationshipMappingSpec.m; sourceTree = "<group>"; };
AD9400631E8AA43500558F37 /* FEMTypes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FEMTypes.h; sourceTree = "<group>"; };
AD9400671E8AA7AD00558F37 /* FEMRelationship.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FEMRelationship.h; sourceTree = "<group>"; };
AD9400691E8AA7C000558F37 /* FEMObjectStore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FEMObjectStore.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -758,6 +761,7 @@
69ED70EFE263881EF2703FEE /* Specs */ = {
isa = PBXGroup;
children = (
AD90A4C81EBB4EB1006751E9 /* FEMRelationshipMappingSpec.m */,
69ED7C04E2BE02B24CFA1D7C /* FEMManagedObjectMappingSpec.m */,
69ED7EC086EF5CE3263C6D8D /* FEMObjectMappingSpec.m */,
69ED75B675560A0A804494CB /* FEMSerializerSpec.m */,
Expand Down Expand Up @@ -1559,6 +1563,7 @@
2C375F1B18CCD3FB00FCB8EA /* PlaneNative.m in Sources */,
2C375F1C18CCD3FB00FCB8EA /* Native.m in Sources */,
2C375F1D18CCD3FB00FCB8EA /* FingerNative.m in Sources */,
AD90A4C91EBB4EB1006751E9 /* FEMRelationshipMappingSpec.m in Sources */,
AD52DF871B0387C000E46915 /* FEMSerializerSpec.m in Sources */,
2C375F1E18CCD3FB00FCB8EA /* SeaplaneNative.m in Sources */,
2C375F1F18CCD3FB00FCB8EA /* AlienNative.m in Sources */,
Expand Down Expand Up @@ -1628,6 +1633,7 @@
ADCD43DD1E9640AB006C8106 /* FEMDeserializerSpec.m in Sources */,
ADCD43F11E9640C7006C8106 /* PlaneNative.m in Sources */,
ADCD43D81E9640AB006C8106 /* FEMManagedObjectMappingSpec.m in Sources */,
AD90A4CA1EBB4EB1006751E9 /* FEMRelationshipMappingSpec.m in Sources */,
ADCD43E41E9640C7006C8106 /* CoreData.xcdatamodel in Sources */,
ADCD43ED1E9640C7006C8106 /* Person.m in Sources */,
ADCD43F31E9640C7006C8106 /* UniqueObject.m in Sources */,
Expand Down
33 changes: 23 additions & 10 deletions FastEasyMapping/Source/Mapping/FEMRelationship.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ - (instancetype)initWithProperty:(NSString *)property keyPath:(NSString *)keyPat
#pragma mark - NSCopying

- (instancetype)copyWithZone:(NSZone *)zone {
FEMRelationship *relationship = [[FEMRelationship allocWithZone:zone] initWithProperty:self.property keyPath:self.keyPath mapping:[self.mapping copy]];
FEMMapping *mapping = self.isRecursive ? self.mapping : [self.mapping copy];

FEMRelationship *relationship = [[FEMRelationship allocWithZone:zone] initWithProperty:self.property keyPath:self.keyPath mapping:mapping];
relationship.assignmentPolicy = self.assignmentPolicy;
relationship.toMany = self.toMany;
relationship.weak = self.weak;
Expand All @@ -61,15 +63,26 @@ - (void)setMapping:(nonnull FEMMapping *)mapping forKeyPath:(nullable NSString *
#pragma mark - Description

- (NSString *)description {
return [NSString stringWithFormat:
@"<%@ %p>\n {\nproperty:%@ keyPath:%@ toMany:%@\nmapping:(%@)}\n",
NSStringFromClass(self.class),
(__bridge void *) self,
self.property,
self.keyPath,
@(self.toMany),
[self.mapping description]
];
if (self.isRecursive) {
return [NSString stringWithFormat:
@"<%@ %p>\n {\nproperty:%@ keyPath:%@ toMany:%@\nrecursive}\n",
NSStringFromClass(self.class),
(__bridge void *)self,
self.property,
self.keyPath,
@(self.toMany)
];
} else {
return [NSString stringWithFormat:
@"<%@ %p>\n {\nproperty:%@ keyPath:%@ toMany:%@\nmapping:%@}\n",
NSStringFromClass(self.class),
(__bridge void *)self,
self.property,
self.keyPath,
@(self.toMany),
[self.mapping description]
];
}
}

#pragma mark - Recursive Relationships Support
Expand Down
67 changes: 67 additions & 0 deletions FastEasyMappingTests/Specs/FEMRelationshipMappingSpec.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// For License please refer to LICENSE file in the root of FastEasyMapping project

#import <Kiwi/Kiwi.h>

#import "FEMMapping.h"
#import "FEMRelationship.h"

SPEC_BEGIN(FEMRelationshipMappingSpec)
describe(@"FEMRelationship", ^{
__block FEMMapping *mapping = nil;

beforeEach(^{
mapping = [[FEMMapping alloc] initWithObjectClass:[NSObject class]];
});

context(@"NSCopying", ^{
context(@"recursive", ^{
__block FEMRelationship *relationship = nil;

beforeEach(^{
relationship = [[FEMRelationship alloc] initWithProperty:@"property" mapping:mapping];
[mapping addRelationship:relationship];
});

it(@"should copy recursive relationship", ^{
FEMMapping *copy = [mapping copy];

FEMRelationship *relationshipCopy = [copy relationshipForProperty:@"property"];

[[relationshipCopy shouldNot] beNil];
[[theValue(relationshipCopy == relationship) should] beFalse];
[[theValue(relationshipCopy.isRecursive) should] beTrue];
[[relationshipCopy.mapping should] equal:copy];
});
});
});

describe(@"recursive support", ^{
__block FEMRelationship *relationship = nil;

beforeEach(^{
relationship = [[FEMRelationship alloc] initWithProperty:@"property" mapping:mapping];
});

it(@"should become recursive when added to the mapping", ^{
[[theValue(relationship.isRecursive) should] beFalse];

[mapping addRelationship:relationship];

[[theValue(relationship.isRecursive) should] beTrue];
});

it(@"should not create retain cycle", ^{
__block __weak FEMMapping *weakMapping = nil;
@autoreleasepool {
FEMMapping *strongMapping = [mapping copy];
FEMRelationship *rel = [[FEMRelationship alloc] initWithProperty:@"property" mapping:strongMapping];
[strongMapping addRelationship:rel];
weakMapping = strongMapping;
}

[[weakMapping should] beNil];
});
});
});

SPEC_END

0 comments on commit 030fd2b

Please sign in to comment.