-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathCSPropertyTests.m
More file actions
114 lines (91 loc) · 3.53 KB
/
CSPropertyTests.m
File metadata and controls
114 lines (91 loc) · 3.53 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
//
// MochaPropertyTests.m
// Mocha
//
// Created by Adam Fedor on 4/24/15.
// Copyright (c) 2015 Sunflower Softworks. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#import <XCTest/XCTest.h>
#import "Mocha.h"
#import <objc/runtime.h>
#import <CoreData/CoreData.h>
@interface Product : NSManagedObject
@property (nonatomic, retain) NSString * name;
@end
@implementation Product
@dynamic name;
@end
@interface MochaTestDynamicProperty : NSObject
@property NSString *testProperty;
@end
@implementation MochaTestDynamicProperty
@dynamic testProperty;
id dynamicMethodIMP(id self, SEL _cmd) {
return [self valueForUndefinedKey: NSStringFromSelector(_cmd)];
}
+ (BOOL)resolveInstanceMethod:(SEL)aSEL
{
if (aSEL == @selector(testProperty)) {
class_addMethod([self class], aSEL, (IMP) dynamicMethodIMP, "@@:");
return YES;
}
return [super resolveInstanceMethod:aSEL];
}
- (id)valueForUndefinedKey:(NSString *)key
{
return @"testValue";
}
@end
@interface MochaPropertyTests : XCTestCase
@end
@implementation MochaPropertyTests
{
Mocha *runtime;
NSURL *testModelURL;
NSManagedObjectContext *moContext;
}
- (void)setUp {
[super setUp];
testModelURL = [[NSBundle bundleForClass: [self class]] URLForResource:@"Model" withExtension:@"momd"];
runtime = [[Mocha alloc] init];
NSError *error = nil;
NSPersistentStoreCoordinator *coordinator = nil;
NSManagedObjectModel *model = nil;
moContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
model = [[NSManagedObjectModel alloc] initWithContentsOfURL: testModelURL];
coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: model];
[coordinator addPersistentStoreWithType: NSInMemoryStoreType
configuration: nil
URL: [NSURL fileURLWithPath: @"/tmp/testmodel"]
options: 0
error: &error];
[moContext setPersistentStoreCoordinator: coordinator];
}
- (void)tearDown {
[super tearDown];
}
- (void)testCallDynamicProperty_ShouldReturnSimpleObject
{
MochaTestDynamicProperty *dynamicObject = [[MochaTestDynamicProperty alloc] init];
[runtime evalString: @"function getTestProperty(object) { return object.testProperty() }"];
id result = [runtime callFunctionWithName:@"getTestProperty" withArgumentsInArray: @[dynamicObject]];
XCTAssertEqual(result, @"testValue", @"Property not correct");
}
#pragma mark Managed Object Properties
- (void)testCallSubclassedManagedObjectProperty_ShouldReturnSimpleObject
{
NSManagedObject *modelObject = [NSEntityDescription insertNewObjectForEntityForName: @"Product" inManagedObjectContext: moContext];
[modelObject setValue: @"testValue" forKey: @"name"];
[runtime evalString: @"function getTestProperty(object) { return object.name() }"];
id result = [runtime callFunctionWithName:@"getTestProperty" withArgumentsInArray: @[modelObject]];
XCTAssertEqual(result, @"testValue", @"Property not correct");
}
- (void)testSetSubclassedManagedObjectProperty_ShouldReturnSimpleObject
{
NSManagedObject *modelObject = [NSEntityDescription insertNewObjectForEntityForName: @"Product" inManagedObjectContext: moContext];
[runtime evalString: @"function setTestProperty(object) { object.name = \"testValue\" }"];
[runtime callFunctionWithName:@"setTestProperty" withArgumentsInArray: @[modelObject]];
XCTAssertTrue([[modelObject valueForKey: @"name"] isEqualToString: @"testValue"], @"Property not set");
}
@end