2016-02-18 19:59:34 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright 2016 Realm Inc.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
// 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-10-14 22:46:46 +00:00
|
|
|
|
2015-10-15 18:34:24 +00:00
|
|
|
#import "RealmJSTests.h"
|
|
|
|
#import "RJSModuleLoader.h"
|
|
|
|
|
2016-04-29 19:58:25 +00:00
|
|
|
#include "jsc_init.h"
|
2015-11-24 06:05:06 +00:00
|
|
|
|
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];
|
2016-10-04 22:02:51 +00:00
|
|
|
|
|
|
|
// We need a JS context from a UIWebView so it has setTimeout, Promise, etc.
|
|
|
|
UIWebView *webView = [[UIWebView alloc] init];
|
|
|
|
JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
|
2015-10-15 18:34:24 +00:00
|
|
|
RJSModuleLoader *moduleLoader = [[RJSModuleLoader alloc] initWithContext:context];
|
2016-02-28 19:52:38 +00:00
|
|
|
NSURL *realmURL = [[NSBundle bundleForClass:self] URLForResource:@"index" withExtension:@"js" subdirectory:@"lib"];
|
|
|
|
NSURL *scriptURL = [[NSBundle bundleForClass:self] URLForResource:@"index" withExtension:@"js" subdirectory:@"js"];
|
|
|
|
NSError *error;
|
|
|
|
|
2016-10-04 22:02:51 +00:00
|
|
|
// The ES6 global Promise constructor was added in iOS 8.
|
|
|
|
if (![context[@"Promise"] isObject]) {
|
|
|
|
JSValue *promiseModule = [moduleLoader loadGlobalModule:@"es6-promise" relativeToURL:scriptURL error:&error];
|
|
|
|
NSAssert(promiseModule, @"%@", error);
|
|
|
|
|
|
|
|
context[@"Promise"] = promiseModule[@"Promise"];
|
|
|
|
}
|
|
|
|
|
2016-02-28 19:52:38 +00:00
|
|
|
// Create Realm constructor in the JS context.
|
|
|
|
RJSInitializeInContext(context.JSGlobalContextRef);
|
|
|
|
|
|
|
|
// Load the Realm module so additional functionality is exposed on Realm objects.
|
|
|
|
JSValue *realmConstructor = [moduleLoader loadModuleFromURL:realmURL error:&error];
|
|
|
|
NSAssert(realmConstructor, @"%@", error);
|
2015-10-15 18:34:24 +00:00
|
|
|
|
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
|
|
|
|
2016-02-15 22:22:40 +00:00
|
|
|
JSValue *testObject = [moduleLoader loadModuleFromURL:scriptURL error:&error];
|
|
|
|
NSAssert(testObject, @"%@", error);
|
2015-10-15 18:34:24 +00:00
|
|
|
|
2016-02-15 22:22:40 +00:00
|
|
|
NSDictionary *testCaseNames = [[testObject invokeMethod:@"getTestNames" withArguments:nil] toDictionary];
|
|
|
|
NSAssert(testCaseNames.count, @"No test names were provided by the JS");
|
2015-10-15 18:34:24 +00:00
|
|
|
|
|
|
|
for (XCTestSuite *testSuite in [self testSuitesFromDictionary:testCaseNames]) {
|
|
|
|
for (RealmJSCoreTests *test in testSuite.tests) {
|
2016-02-15 22:22:40 +00:00
|
|
|
test.testObject = testObject;
|
2015-10-15 18:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[suite addTest:testSuite];
|
|
|
|
}
|
|
|
|
|
|
|
|
return suite;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (JSContext *)context {
|
|
|
|
return self.testObject.context;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)invokeMethod:(NSString *)method {
|
|
|
|
JSValue *testObject = self.testObject;
|
|
|
|
JSContext *context = testObject.context;
|
|
|
|
context.exception = nil;
|
|
|
|
|
2016-10-04 22:02:51 +00:00
|
|
|
JSValue *promise = [testObject invokeMethod:@"runTest" withArguments:@[NSStringFromClass(self.class), method]];
|
2015-10-15 18:34:24 +00:00
|
|
|
|
2016-10-04 22:02:51 +00:00
|
|
|
if (context.exception) {
|
|
|
|
[self recordException:context.exception];
|
|
|
|
return;
|
|
|
|
}
|
2015-12-09 23:43:48 +00:00
|
|
|
|
2016-10-04 22:02:51 +00:00
|
|
|
if ([promise isObject]) {
|
|
|
|
XCTestExpectation *expectation = [self expectationWithDescription:@"Promise resolved or rejected"];
|
|
|
|
|
|
|
|
JSValue *onFulfilled = [JSValue valueWithObject:^() {
|
|
|
|
[expectation fulfill];
|
|
|
|
} inContext:context];
|
2015-10-15 18:34:24 +00:00
|
|
|
|
2016-10-04 22:02:51 +00:00
|
|
|
JSValue *onRejected = [JSValue valueWithObject:^(JSValue *error) {
|
|
|
|
[self recordException:error];
|
|
|
|
[expectation fulfill];
|
|
|
|
} inContext:context];
|
|
|
|
|
|
|
|
[promise invokeMethod:@"then" withArguments:@[onFulfilled, onRejected]];
|
|
|
|
|
|
|
|
[self waitForExpectationsWithTimeout:5.0 handler:NULL];
|
2015-10-15 18:34:24 +00:00
|
|
|
}
|
2015-10-14 22:46:46 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 22:02:51 +00:00
|
|
|
- (void)recordException:(JSValue *)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 = nil;
|
|
|
|
|
|
|
|
if (source) {
|
|
|
|
NSString *path = [NSString pathWithComponents:@[[@(__FILE__) stringByDeletingLastPathComponent], @"..", @"js", source.lastPathComponent]];
|
|
|
|
sourceURL = [NSURL URLWithString:path];
|
|
|
|
}
|
|
|
|
|
|
|
|
[self recordFailureWithDescription:message.description
|
|
|
|
inFile:sourceURL ? sourceURL.absoluteString : @(__FILE__)
|
|
|
|
atLine:sourceURL ? line : __LINE__
|
|
|
|
expected:YES];
|
|
|
|
}
|
|
|
|
|
2015-10-14 22:46:46 +00:00
|
|
|
@end
|