realm-js/tests/RealmJSTests.mm

262 lines
8.2 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 "RealmJSTests.h"
#import "RJSUtil.hpp"
#import "RJSRealm.hpp"
#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"]);
}
static JSClassRef s_globalClass;
@interface RealmJSTests ()
@property (nonatomic, strong) JSContext *context;
@end
@implementation RealmJSTests
2015-08-13 16:12:48 +00:00
+ (void)initialize {
JSClassDefinition globalDefinition = kJSClassDefinitionEmpty;
globalDefinition.attributes = kJSClassAttributeNoAutomaticPrototype;
s_globalClass = JSClassCreate(&globalDefinition);
}
- (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];
JSGlobalContextRef ctx = JSGlobalContextCreateInGroup(NULL, s_globalClass);
self.context = [JSContext contextWithJSGlobalContextRef:ctx];
[RealmJS initializeContext:ctx];
[self evaluateScriptWithName:@"TestCase"];
[self evaluateScriptWithName:@"TestObjects"];
[self evaluateScriptWithName:self.class.jsSuiteName];
2015-08-13 16:12:48 +00:00
}
- (void)tearDown {
self.context = nil;
2015-08-13 16:12:48 +00:00
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];
}
}
- (void)evaluateScriptWithName:(NSString *)name {
NSURL *url = [self.class scriptURLWithName:name];
NSString *script = [self.class loadScriptWithURL:url];
2015-08-13 16:12:48 +00:00
[self evaluateScript:script fromURL:url];
2015-08-13 16:12:48 +00:00
}
- (void)evaluateScript:(NSString *)script fromURL:(NSURL *)url {
JSValue *exception;
[self.class evaluateScript:script fromURL:url inContext:self.context exception:&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] : 0;
NSURL *sourceURL = source ? [NSURL URLWithString:source.lastPathComponent relativeToURL:[NSURL URLWithString:@(__FILE__)]] : nil;
const char *sourcePath = sourceURL.absoluteString.UTF8String;
2015-08-13 16:12:48 +00:00
_XCTFailureHandler(self, YES, sourcePath ?: __FILE__, sourcePath ? line : __LINE__, @"JS", @"%@", message);
}
2015-08-13 16:12:48 +00:00
}
+ (JSValue *)evaluateScript:(NSString *)script fromURL:(NSURL *)url inContext:(JSContext *)context exception:(JSValue **)exception {
JSStringRef jsScript = JSStringCreateWithUTF8CString(script.UTF8String);
JSStringRef jsURL = url ? JSStringCreateWithUTF8CString(url.absoluteString.UTF8String) : NULL;
JSValueRef jsException = NULL;
JSValueRef jsResult = JSEvaluateScript(context.JSGlobalContextRef, jsScript, NULL, jsURL, 1, &jsException);
JSStringRelease(jsScript);
if (jsURL) {
JSStringRelease(jsURL);
}
if (jsException) {
*exception = [JSValue valueWithJSValueRef:jsException inContext:context];
2015-08-13 16:12:48 +00:00
return NULL;
}
return [JSValue valueWithJSValueRef:jsResult inContext:context];
}
2015-08-13 16:12:48 +00:00
+ (NSURL *)scriptURLWithName:(NSString *)name {
NSURL *url = [[NSBundle bundleForClass:self] URLForResource:name withExtension:@"js"];
if (!url) {
NSLog(@"JS file does not exist: %@", url);
2015-08-13 16:12:48 +00:00
exit(1);
}
return url;
2015-08-13 16:12:48 +00:00
}
+ (NSString *)loadScriptWithURL:(NSURL *)url {
NSError *error;
NSString *script = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
2015-08-13 16:12:48 +00:00
if (!script) {
NSLog(@"Error reading JS file (%@): %@", url, error);
2015-08-13 16:12:48 +00:00
exit(1);
}
return script;
}
+ (NSString *)jsSuiteName {
return nil;
}
2015-08-13 16:12:48 +00:00
+ (NSString *)jsSuiteScript {
NSString *name = [self jsSuiteName];
return name ? [self loadScriptWithURL:[self scriptURLWithName:name]] : nil;
}
+ (XCTestSuite *)defaultTestSuite {
XCTestSuite *suite = [super defaultTestSuite];
NSString *suiteName = [self jsSuiteName];
NSURL *scriptURL = suiteName ? [self scriptURLWithName:suiteName] : nil;
NSString *script = scriptURL ? [self loadScriptWithURL:scriptURL] : nil;
2015-08-13 16:12:48 +00:00
if (!script) {
return suite;
}
JSContext *context = [[JSContext alloc] init];
JSValue *exception;
2015-08-13 16:12:48 +00:00
[self evaluateScript:script fromURL:scriptURL inContext:context exception:&exception];
if (exception) {
NSLog(@"%@.js - %@", suiteName, exception);
2015-08-13 16:12:48 +00:00
exit(1);
}
JSValue *suiteObject = [self evaluateScript:suiteName fromURL:nil inContext:context exception:&exception];
if (exception) {
NSLog(@"%@.js - %@", suiteName, exception);
2015-08-13 16:12:48 +00:00
exit(1);
}
if (![suiteObject isObject]) {
NSLog(@"%@.js - JS test suite is not an object: %@", suiteName, suiteObject);
2015-08-13 16:12:48 +00:00
exit(1);
}
for (NSString *testName in [suiteObject toDictionary]) {
[suite addTest:[self testCaseWithSelector:NSSelectorFromString(testName)]];
2015-08-13 16:12:48 +00:00
}
return suite;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
NSMethodSignature *sig = [super methodSignatureForSelector:aSelector];
if (sig) {
return sig;
}
return [NSMethodSignature signatureWithObjCTypes:"v@:"];
}
- (void)forwardInvocation:(NSInvocation *)anInvocation {
NSString *script = [NSString stringWithFormat:@"%@.%@();", [self.class jsSuiteName], NSStringFromSelector(anInvocation.selector)];
[self evaluateScript:script fromURL:nil];
2015-08-13 16:12:48 +00:00
}
@end
@interface RJSResultsTests : RealmJSTests
@end
@implementation RJSResultsTests
+ (NSString *)jsSuiteName {
return @"ResultsTests";
}
@end
@interface RJSObjectTests : RealmJSTests
@end
@implementation RJSObjectTests
+ (NSString *)jsSuiteName {
return @"ObjectTests";
}
@end
2015-08-14 17:47:56 +00:00
@interface RJSArrayTests : RealmJSTests
@end
@implementation RJSArrayTests
+ (NSString *)jsSuiteName {
return @"ArrayTests";
}
@end
2015-08-13 16:12:48 +00:00
@interface RJSRealmTests : RealmJSTests
@end
@implementation RJSRealmTests
+ (NSString *)jsSuiteName {
return @"RealmTests";
}
@end