realm-js/tests/RealmJSTests.mm

173 lines
5.8 KiB
Plaintext
Raw Normal View History

2015-08-14 15:18:49 +00:00
////////////////////////////////////////////////////////////////////////////
2015-08-13 16:12:48 +00:00
//
2015-08-14 15:18:49 +00:00
// Copyright 2015 Realm Inc.
2015-08-13 16:12:48 +00:00
//
2015-08-14 15:18:49 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2015-08-13 16:12:48 +00:00
//
2015-08-14 15:18:49 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
2015-08-13 16:12:48 +00:00
#import <objc/runtime.h>
2015-08-13 16:12:48 +00:00
#import "RealmJSTests.h"
#import "RJSUtil.hpp"
#import "RJSRealm.hpp"
#import "RJSModuleLoader.h"
2015-08-13 16:12:48 +00:00
#import "shared_realm.hpp"
2015-08-13 16:12:48 +00:00
NSString *RealmPathForFile(NSString *fileName) {
#if TARGET_OS_IPHONE
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
#else
NSString *path = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES)[0];
path = [path stringByAppendingPathComponent:[[[NSBundle mainBundle] executablePath] lastPathComponent]];
#endif
return [path stringByAppendingPathComponent:fileName];
}
NSString *TestRealmPath() {
return RealmPathForFile(@"test.realm");
2015-08-13 16:12:48 +00:00
}
static void DeleteOrThrow(NSString *path) {
NSError *error;
if (![[NSFileManager defaultManager] removeItemAtPath:path error:&error]) {
if (error.code != NSFileNoSuchFileError) {
@throw [NSException exceptionWithName:@"RLMTestException"
reason:[@"Unable to delete realm: " stringByAppendingString:error.description]
userInfo:nil];
}
}
}
static void DeleteRealmFilesAtPath(NSString *path) {
DeleteOrThrow(path);
DeleteOrThrow([path stringByAppendingString:@".lock"]);
DeleteOrThrow([path stringByAppendingString:@".note"]);
}
@interface RealmJSTests ()
@property (nonatomic, strong) JSValue *testObject;
@end
@implementation RealmJSTests
2015-08-13 16:12:48 +00:00
- (instancetype)initWithJSTestObject:(JSValue *)testObject methodName:(NSString *)methodName {
self = [super initWithSelector:NSSelectorFromString(methodName)];
if (!self) {
return nil;
}
_testObject = testObject;
return self;
}
- (JSContext *)context {
return self.testObject.context;
2015-08-13 16:12:48 +00:00
}
- (void)setUp {
[super setUp];
NSString *defaultDir = [[NSString stringWithUTF8String:RJSDefaultPath().c_str()] stringByDeletingLastPathComponent];
2015-08-13 16:12:48 +00:00
[[NSFileManager defaultManager] createDirectoryAtPath:defaultDir withIntermediateDirectories:YES attributes:nil error:nil];
self.context.exception = nil;
2015-08-13 16:12:48 +00:00
}
- (void)tearDown {
realm::Realm::s_global_cache.invalidate_all();
realm::Realm::s_global_cache.clear();
2015-10-13 00:18:49 +00:00
// FIXME - find all realm files in the docs dir and delete them rather than hardcoding these
2015-10-13 00:01:51 +00:00
DeleteRealmFilesAtPath(RealmPathForFile(@"test.realm"));
DeleteRealmFilesAtPath(RealmPathForFile(@"test1.realm"));
DeleteRealmFilesAtPath(RealmPathForFile(@"test2.realm"));
2015-08-13 16:12:48 +00:00
DeleteRealmFilesAtPath(@(RJSDefaultPath().c_str()));
2015-08-13 16:12:48 +00:00
[super tearDown];
}
- (void)invokeTest {
@autoreleasepool {
[super invokeTest];
}
}
+ (XCTestSuite *)defaultTestSuite {
XCTestSuite *suite = [super defaultTestSuite];
JSContext *context = [[JSContext alloc] init];
RJSModuleLoader *moduleLoader = [[RJSModuleLoader alloc] initWithContext:context];
NSURL *scriptURL = [[NSBundle bundleForClass:self] URLForResource:@"index" withExtension:@"js"];
2015-08-13 16:12:48 +00:00
[RealmJS initializeContext:context.JSGlobalContextRef];
2015-08-13 16:12:48 +00:00
// Expose the global Realm object as a global 'realm' CommonJS module.
[moduleLoader addGlobalModuleObject:context[@"Realm"] forName:@"realm"];
NSError *error;
JSValue *testObjects = [moduleLoader loadModuleFromURL:scriptURL error:&error];
2015-08-13 16:12:48 +00:00
if (!testObjects) {
NSLog(@"index.js - %@", error);
2015-08-13 16:12:48 +00:00
exit(1);
}
for (NSString *testName in [testObjects toDictionary]) {
JSValue *testObject = testObjects[testName];
XCTestSuite *testSuite = [[XCTestSuite alloc] initWithName:testName];
Class testClass = objc_allocateClassPair(self, testName.UTF8String, 0);
for (NSString *methodName in [testObject toDictionary]) {
XCTestCase *testCase = [[testClass alloc] initWithJSTestObject:testObject methodName:methodName];
[testSuite addTest:testCase];
}
[suite addTest:testSuite];
2015-08-13 16:12:48 +00:00
}
return suite;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
NSMethodSignature *sig = [super methodSignatureForSelector:aSelector];
return sig ?: [NSMethodSignature signatureWithObjCTypes:"v@:"];
2015-08-13 16:12:48 +00:00
}
- (void)forwardInvocation:(NSInvocation *)anInvocation {
JSValue *testObject = self.testObject;
JSContext *context = testObject.context;
2015-08-13 16:12:48 +00:00
[testObject invokeMethod:NSStringFromSelector(anInvocation.selector) withArguments:nil];
2015-08-13 16:12:48 +00:00
JSValue *exception = context.exception;
2015-08-13 16:12:48 +00:00
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;
2015-08-13 16:12:48 +00:00
[self recordFailureWithDescription:message.description
inFile:sourceURL ? sourceURL.absoluteString : @(__FILE__)
atLine:sourceURL ? line : __LINE__
expected:YES];
}
2015-08-14 17:47:56 +00:00
}
2015-08-13 16:12:48 +00:00
@end