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"
|
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
static NSString *s_testPrefix;
|
|
|
|
|
|
|
|
NSString *TestRealmPath() {
|
|
|
|
return RealmPathForFile([s_testPrefix stringByAppendingPathComponent:@"test.realm"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2015-09-28 23:05:57 +00:00
|
|
|
@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];
|
|
|
|
|
|
|
|
s_testPrefix = [[NSUUID UUID] UUIDString];
|
|
|
|
NSString *defaultDir = RealmPathForFile(s_testPrefix);
|
|
|
|
[[NSFileManager defaultManager] createDirectoryAtPath:defaultDir withIntermediateDirectories:YES attributes:nil error:nil];
|
|
|
|
RJSSetDefaultPath([defaultDir stringByAppendingPathComponent:@"default.realm"].UTF8String);
|
|
|
|
|
2015-09-28 23:05:57 +00:00
|
|
|
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 {
|
2015-09-28 23:05:57 +00:00
|
|
|
self.context = nil;
|
2015-08-13 16:12:48 +00:00
|
|
|
|
|
|
|
DeleteRealmFilesAtPath(TestRealmPath());
|
|
|
|
DeleteRealmFilesAtPath(@(RJSDefaultPath().c_str()));
|
|
|
|
|
|
|
|
[super tearDown];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)invokeTest {
|
|
|
|
@autoreleasepool {
|
|
|
|
[super invokeTest];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-28 23:05:57 +00:00
|
|
|
- (void)evaluateScriptWithName:(NSString *)name {
|
|
|
|
NSURL *url = [self.class scriptURLWithName:name];
|
|
|
|
NSString *script = [self.class loadScriptWithURL:url];
|
2015-08-13 16:12:48 +00:00
|
|
|
|
2015-09-28 23:05:57 +00:00
|
|
|
[self evaluateScript:script fromURL:url];
|
2015-08-13 16:12:48 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 23:05:57 +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
|
|
|
|
2015-09-28 23:05:57 +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
|
|
|
|
2015-09-28 23:05:57 +00:00
|
|
|
_XCTFailureHandler(self, YES, sourcePath ?: __FILE__, sourcePath ? line : __LINE__, @"JS", @"%@", message);
|
|
|
|
}
|
2015-08-13 16:12:48 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 23:05:57 +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;
|
|
|
|
}
|
|
|
|
|
2015-09-28 23:05:57 +00:00
|
|
|
return [JSValue valueWithJSValueRef:jsResult inContext:context];
|
|
|
|
}
|
2015-08-13 16:12:48 +00:00
|
|
|
|
2015-09-28 23:05:57 +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);
|
|
|
|
}
|
2015-09-28 23:05:57 +00:00
|
|
|
return url;
|
2015-08-13 16:12:48 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 23:05:57 +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) {
|
2015-09-28 23:05:57 +00:00
|
|
|
NSLog(@"Error reading JS file (%@): %@", url, error);
|
2015-08-13 16:12:48 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
2015-09-28 23:05:57 +00:00
|
|
|
+ (NSString *)jsSuiteName {
|
|
|
|
return nil;
|
|
|
|
}
|
2015-08-13 16:12:48 +00:00
|
|
|
|
2015-09-28 23:05:57 +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;
|
|
|
|
}
|
|
|
|
|
2015-09-28 23:05:57 +00:00
|
|
|
JSContext *context = [[JSContext alloc] init];
|
|
|
|
JSValue *exception;
|
2015-08-13 16:12:48 +00:00
|
|
|
|
2015-09-28 23:05:57 +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);
|
|
|
|
}
|
|
|
|
|
2015-09-28 23:05:57 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-09-28 23:05:57 +00:00
|
|
|
if (![suiteObject isObject]) {
|
|
|
|
NSLog(@"%@.js - JS test suite is not an object: %@", suiteName, suiteObject);
|
2015-08-13 16:12:48 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-09-28 23:05:57 +00:00
|
|
|
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 {
|
2015-09-28 23:05:57 +00:00
|
|
|
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
|
|
|
|
|