2015-10-27 20:59:15 +00:00
|
|
|
/* Copyright 2015 Realm Inc - All Rights Reserved
|
|
|
|
* Proprietary and Confidential
|
|
|
|
*/
|
2015-10-14 22:46:46 +00:00
|
|
|
|
2015-10-15 18:34:24 +00:00
|
|
|
#import "RealmJSTests.h"
|
|
|
|
#import "RJSModuleLoader.h"
|
|
|
|
|
2015-11-24 06:05:06 +00:00
|
|
|
#import <RealmJS/RealmJS.h>
|
|
|
|
|
2015-10-15 18:34:24 +00:00
|
|
|
@interface RealmJSCoreTests : RealmJSTests
|
|
|
|
|
|
|
|
@property (nonatomic, strong) JSValue *testObject;
|
|
|
|
|
|
|
|
@end
|
2015-10-14 22:46:46 +00:00
|
|
|
|
|
|
|
@implementation RealmJSCoreTests
|
|
|
|
|
2015-10-15 18:34:24 +00:00
|
|
|
+ (XCTestSuite *)defaultTestSuite {
|
|
|
|
XCTestSuite *suite = [super defaultTestSuite];
|
|
|
|
JSContext *context = [[JSContext alloc] init];
|
2015-10-29 06:18:11 +00:00
|
|
|
JSValue *realmConstructor = [JSValue valueWithJSValueRef:RJSConstructorCreate(context.JSGlobalContextRef) inContext:context];
|
2015-10-15 18:34:24 +00:00
|
|
|
RJSModuleLoader *moduleLoader = [[RJSModuleLoader alloc] initWithContext:context];
|
|
|
|
NSURL *scriptURL = [[NSBundle bundleForClass:self] URLForResource:@"index" withExtension:@"js"];
|
|
|
|
|
2015-10-29 06:18:11 +00:00
|
|
|
// Expose the Realm constructor as a global 'realm' CommonJS module.
|
|
|
|
[moduleLoader addGlobalModuleObject:realmConstructor forName:@"realm"];
|
2015-10-15 18:34:24 +00:00
|
|
|
|
|
|
|
NSError *error;
|
|
|
|
JSValue *testObjects = [moduleLoader loadModuleFromURL:scriptURL error:&error];
|
|
|
|
|
|
|
|
if (!testObjects) {
|
|
|
|
NSLog(@"%@", error);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
NSDictionary *testCaseNames = [[testObjects invokeMethod:@"getTestNames" withArguments:nil] toDictionary];
|
|
|
|
|
|
|
|
if (!testCaseNames.count) {
|
|
|
|
NSLog(@"No test case names from getTestNames() JS method!");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (XCTestSuite *testSuite in [self testSuitesFromDictionary:testCaseNames]) {
|
|
|
|
for (RealmJSCoreTests *test in testSuite.tests) {
|
|
|
|
test.testObject = testObjects[testSuite.name];
|
|
|
|
}
|
|
|
|
|
|
|
|
[suite addTest:testSuite];
|
|
|
|
}
|
|
|
|
|
|
|
|
return suite;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (JSContext *)context {
|
|
|
|
return self.testObject.context;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)invokeMethod:(NSString *)method {
|
|
|
|
JSValue *testObject = self.testObject;
|
|
|
|
|
|
|
|
if (![testObject hasProperty:method]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSContext *context = testObject.context;
|
|
|
|
context.exception = nil;
|
|
|
|
|
|
|
|
[testObject invokeMethod:method withArguments:nil];
|
|
|
|
|
|
|
|
JSValue *exception = context.exception;
|
|
|
|
if (exception) {
|
|
|
|
JSValue *message = [exception hasProperty:@"message"] ? exception[@"message"] : exception;
|
|
|
|
NSString *source = [exception hasProperty:@"sourceURL"] ? [exception[@"sourceURL"] toString] : nil;
|
|
|
|
NSUInteger line = [exception hasProperty:@"line"] ? [exception[@"line"] toUInt32] - 1 : 0;
|
|
|
|
NSURL *sourceURL = source ? [NSURL URLWithString:source.lastPathComponent relativeToURL:[NSURL URLWithString:@(__FILE__)]] : nil;
|
|
|
|
|
|
|
|
[self recordFailureWithDescription:message.description
|
|
|
|
inFile:sourceURL ? sourceURL.absoluteString : @(__FILE__)
|
|
|
|
atLine:sourceURL ? line : __LINE__
|
|
|
|
expected:YES];
|
|
|
|
}
|
2015-10-14 22:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|