Each test can now have a beforeEach and afterEach

This commit is contained in:
Scott Kyle 2015-10-14 14:58:42 -07:00
parent dc84ac63b6
commit 39b262b279
4 changed files with 120 additions and 47 deletions

View File

@ -8,12 +8,6 @@ var React = require('react-native');
var Realm = require('realm'); var Realm = require('realm');
var RealmTests = require('realm-tests'); var RealmTests = require('realm-tests');
for (var suiteName in RealmTests) {
for (var testName in RealmTests[suiteName]) {
RealmTests[suiteName][testName]();
}
}
var { var {
AppRegistry, AppRegistry,
StyleSheet, StyleSheet,
@ -21,8 +15,43 @@ var {
View, View,
} = React; } = React;
function runTests() {
let specialMethodNames = {'beforeEach': true, 'afterEach': true};
for (let suiteName in RealmTests) {
let testSuite = RealmTests[suiteName];
console.log('Starting suite:', suiteName);
for (let testName in testSuite) {
if (testName in specialMethodNames || typeof testSuite[testName] != 'function') {
continue;
}
console.log('-', testName);
if (testSuite.beforeEach) {
testSuite.beforeEach();
}
try {
testSuite[testName]();
}
finally {
if (testSuite.afterEach) {
testSuite.afterEach();
}
}
}
}
}
var ReactTests = React.createClass({ var ReactTests = React.createClass({
render: function() { componentDidMount() {
runTests();
},
render() {
return ( return (
<View style={styles.container}> <View style={styles.container}>
<Text style={styles.welcome}> <Text style={styles.welcome}>

View File

@ -14,7 +14,7 @@
00C302E91ABCBA2D00DB3ED1 /* libRCTNetwork.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 00C302DC1ABCB9D200DB3ED1 /* libRCTNetwork.a */; }; 00C302E91ABCBA2D00DB3ED1 /* libRCTNetwork.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 00C302DC1ABCB9D200DB3ED1 /* libRCTNetwork.a */; };
00C302EA1ABCBA2D00DB3ED1 /* libRCTVibration.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 00C302E41ABCB9EE00DB3ED1 /* libRCTVibration.a */; }; 00C302EA1ABCBA2D00DB3ED1 /* libRCTVibration.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 00C302E41ABCB9EE00DB3ED1 /* libRCTVibration.a */; };
00E356F31AD99517003FC87E /* ReactTestsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 00E356F21AD99517003FC87E /* ReactTestsTests.m */; }; 00E356F31AD99517003FC87E /* ReactTestsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 00E356F21AD99517003FC87E /* ReactTestsTests.m */; };
0277991C1BBF3BC600C96559 /* libRealmReact.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 0277991B1BBF3BB700C96559 /* libRealmReact.a */; }; 0277991C1BBF3BC600C96559 /* RealmReact.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0277991B1BBF3BB700C96559 /* RealmReact.framework */; };
133E29F31AD74F7200F7D852 /* libRCTLinking.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 78C398B91ACF4ADC00677621 /* libRCTLinking.a */; }; 133E29F31AD74F7200F7D852 /* libRCTLinking.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 78C398B91ACF4ADC00677621 /* libRCTLinking.a */; };
139105C61AF99C1200B5F7CC /* libRCTSettings.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 139105C11AF99BAD00B5F7CC /* libRCTSettings.a */; }; 139105C61AF99C1200B5F7CC /* libRCTSettings.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 139105C11AF99BAD00B5F7CC /* libRCTSettings.a */; };
139FDEF61B0652A700C62182 /* libRCTWebSocket.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 139FDEF41B06529B00C62182 /* libRCTWebSocket.a */; }; 139FDEF61B0652A700C62182 /* libRCTWebSocket.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 139FDEF41B06529B00C62182 /* libRCTWebSocket.a */; };
@ -24,6 +24,7 @@
13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; }; 13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; };
146834051AC3E58100842450 /* libReact.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 146834041AC3E56700842450 /* libReact.a */; }; 146834051AC3E58100842450 /* libReact.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 146834041AC3E56700842450 /* libReact.a */; };
832341BD1AAA6AB300B99B32 /* libRCTText.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 832341B51AAA6A8300B99B32 /* libRCTText.a */; }; 832341BD1AAA6AB300B99B32 /* libRCTText.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 832341B51AAA6A8300B99B32 /* libRCTText.a */; };
F6F405C71BCE566300A1E24F /* RealmReact.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 0277991B1BBF3BB700C96559 /* RealmReact.framework */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */ /* Begin PBXContainerItemProxy section */
@ -125,8 +126,29 @@
remoteGlobalIDString = 58B5119B1A9E6C1200147676; remoteGlobalIDString = 58B5119B1A9E6C1200147676;
remoteInfo = RCTText; remoteInfo = RCTText;
}; };
F6F405B71BCE55FB00A1E24F /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 027799061BBF3BB700C96559 /* RealmJS.xcodeproj */;
proxyType = 1;
remoteGlobalIDString = 02B29A151B7CF7C9008A7E6B;
remoteInfo = RealmReact;
};
/* End PBXContainerItemProxy section */ /* End PBXContainerItemProxy section */
/* Begin PBXCopyFilesBuildPhase section */
F6F405C61BCE565100A1E24F /* Embed Frameworks */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = "";
dstSubfolderSpec = 10;
files = (
F6F405C71BCE566300A1E24F /* RealmReact.framework in Embed Frameworks */,
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
008F07F21AC5B25A0029DE68 /* main.jsbundle */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = main.jsbundle; sourceTree = "<group>"; }; 008F07F21AC5B25A0029DE68 /* main.jsbundle */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = main.jsbundle; sourceTree = "<group>"; };
00C302A71ABCB8CE00DB3ED1 /* RCTActionSheet.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTActionSheet.xcodeproj; path = "../node_modules/react-native/Libraries/ActionSheetIOS/RCTActionSheet.xcodeproj"; sourceTree = "<group>"; }; 00C302A71ABCB8CE00DB3ED1 /* RCTActionSheet.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTActionSheet.xcodeproj; path = "../node_modules/react-native/Libraries/ActionSheetIOS/RCTActionSheet.xcodeproj"; sourceTree = "<group>"; };
@ -164,7 +186,7 @@
isa = PBXFrameworksBuildPhase; isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
0277991C1BBF3BC600C96559 /* libRealmReact.a in Frameworks */, 0277991C1BBF3BC600C96559 /* RealmReact.framework in Frameworks */,
146834051AC3E58100842450 /* libReact.a in Frameworks */, 146834051AC3E58100842450 /* libReact.a in Frameworks */,
00C302E51ABCBA2D00DB3ED1 /* libRCTActionSheet.a in Frameworks */, 00C302E51ABCBA2D00DB3ED1 /* libRCTActionSheet.a in Frameworks */,
00C302E71ABCBA2D00DB3ED1 /* libRCTGeolocation.a in Frameworks */, 00C302E71ABCBA2D00DB3ED1 /* libRCTGeolocation.a in Frameworks */,
@ -243,7 +265,7 @@
children = ( children = (
027799171BBF3BB700C96559 /* RealmJS.framework */, 027799171BBF3BB700C96559 /* RealmJS.framework */,
027799191BBF3BB700C96559 /* RealmJSTests.xctest */, 027799191BBF3BB700C96559 /* RealmJSTests.xctest */,
0277991B1BBF3BB700C96559 /* libRealmReact.a */, 0277991B1BBF3BB700C96559 /* RealmReact.framework */,
); );
name = Products; name = Products;
sourceTree = "<group>"; sourceTree = "<group>";
@ -370,10 +392,12 @@
13B07F871A680F5B00A75B9A /* Sources */, 13B07F871A680F5B00A75B9A /* Sources */,
13B07F8C1A680F5B00A75B9A /* Frameworks */, 13B07F8C1A680F5B00A75B9A /* Frameworks */,
13B07F8E1A680F5B00A75B9A /* Resources */, 13B07F8E1A680F5B00A75B9A /* Resources */,
F6F405C61BCE565100A1E24F /* Embed Frameworks */,
); );
buildRules = ( buildRules = (
); );
dependencies = ( dependencies = (
F6F405B81BCE55FB00A1E24F /* PBXTargetDependency */,
); );
name = ReactTests; name = ReactTests;
productName = "Hello World"; productName = "Hello World";
@ -510,10 +534,10 @@
remoteRef = 027799181BBF3BB700C96559 /* PBXContainerItemProxy */; remoteRef = 027799181BBF3BB700C96559 /* PBXContainerItemProxy */;
sourceTree = BUILT_PRODUCTS_DIR; sourceTree = BUILT_PRODUCTS_DIR;
}; };
0277991B1BBF3BB700C96559 /* libRealmReact.a */ = { 0277991B1BBF3BB700C96559 /* RealmReact.framework */ = {
isa = PBXReferenceProxy; isa = PBXReferenceProxy;
fileType = archive.ar; fileType = wrapper.framework;
path = libRealmReact.a; path = RealmReact.framework;
remoteRef = 0277991A1BBF3BB700C96559 /* PBXContainerItemProxy */; remoteRef = 0277991A1BBF3BB700C96559 /* PBXContainerItemProxy */;
sourceTree = BUILT_PRODUCTS_DIR; sourceTree = BUILT_PRODUCTS_DIR;
}; };
@ -616,6 +640,11 @@
target = 13B07F861A680F5B00A75B9A /* ReactTests */; target = 13B07F861A680F5B00A75B9A /* ReactTests */;
targetProxy = 00E356F41AD99517003FC87E /* PBXContainerItemProxy */; targetProxy = 00E356F41AD99517003FC87E /* PBXContainerItemProxy */;
}; };
F6F405B81BCE55FB00A1E24F /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
name = RealmReact;
targetProxy = F6F405B71BCE55FB00A1E24F /* PBXContainerItemProxy */;
};
/* End PBXTargetDependency section */ /* End PBXTargetDependency section */
/* Begin PBXVariantGroup section */ /* Begin PBXVariantGroup section */

View File

@ -86,10 +86,12 @@ static void DeleteRealmFilesAtPath(NSString *path) {
NSString *defaultDir = [[NSString stringWithUTF8String:RJSDefaultPath().c_str()] stringByDeletingLastPathComponent]; NSString *defaultDir = [[NSString stringWithUTF8String:RJSDefaultPath().c_str()] stringByDeletingLastPathComponent];
[[NSFileManager defaultManager] createDirectoryAtPath:defaultDir withIntermediateDirectories:YES attributes:nil error:nil]; [[NSFileManager defaultManager] createDirectoryAtPath:defaultDir withIntermediateDirectories:YES attributes:nil error:nil];
self.context.exception = nil; [self invokeMethod:@"beforeEach"];
} }
- (void)tearDown { - (void)tearDown {
[self invokeMethod:@"afterEach"];
realm::Realm::s_global_cache.invalidate_all(); realm::Realm::s_global_cache.invalidate_all();
realm::Realm::s_global_cache.clear(); realm::Realm::s_global_cache.clear();
@ -127,15 +129,25 @@ static void DeleteRealmFilesAtPath(NSString *path) {
exit(1); exit(1);
} }
NSSet *specialMethodNames = [NSSet setWithObjects:@"beforeEach", @"afterEach", nil];
for (NSString *testName in [testObjects toDictionary]) { for (NSString *testName in [testObjects toDictionary]) {
JSValue *testObject = testObjects[testName]; JSValue *testObject = testObjects[testName];
XCTestSuite *testSuite = [[XCTestSuite alloc] initWithName:testName]; XCTestSuite *testSuite = [[XCTestSuite alloc] initWithName:testName];
Class testClass = objc_allocateClassPair(self, testName.UTF8String, 0); Class testClass = objc_allocateClassPair(self, testName.UTF8String, 0);
for (NSString *methodName in [testObject toDictionary]) { for (NSString *methodName in [testObject toDictionary]) {
if ([specialMethodNames containsObject:methodName]) {
continue;
}
JSObjectRef jsMethod = JSValueToObject(context.JSGlobalContextRef, [testObject[methodName] JSValueRef], NULL);
if (jsMethod && JSObjectIsFunction(context.JSGlobalContextRef, jsMethod)) {
XCTestCase *testCase = [[testClass alloc] initWithJSTestObject:testObject methodName:methodName]; XCTestCase *testCase = [[testClass alloc] initWithJSTestObject:testObject methodName:methodName];
[testSuite addTest:testCase]; [testSuite addTest:testCase];
} }
}
[suite addTest:testSuite]; [suite addTest:testSuite];
} }
@ -149,13 +161,22 @@ static void DeleteRealmFilesAtPath(NSString *path) {
} }
- (void)forwardInvocation:(NSInvocation *)anInvocation { - (void)forwardInvocation:(NSInvocation *)anInvocation {
JSValue *testObject = self.testObject; [self invokeMethod:NSStringFromSelector(anInvocation.selector)];
JSContext *context = testObject.context; }
[testObject invokeMethod:NSStringFromSelector(anInvocation.selector) withArguments:nil]; - (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; JSValue *exception = context.exception;
if (exception) { if (exception) {
JSValue *message = [exception hasProperty:@"message"] ? exception[@"message"] : exception; JSValue *message = [exception hasProperty:@"message"] ? exception[@"message"] : exception;
NSString *source = [exception hasProperty:@"sourceURL"] ? [exception[@"sourceURL"] toString] : nil; NSString *source = [exception hasProperty:@"sourceURL"] ? [exception[@"sourceURL"] toString] : nil;

View File

@ -20,13 +20,12 @@
var Realm = require('realm'); var Realm = require('realm');
var TestObjectSchema = { exports.TestObject = {
name: 'TestObject', name: 'TestObject',
properties: [ properties: [
{name: 'doubleCol', type: Realm.Types.DOUBLE}, {name: 'doubleCol', type: Realm.Types.DOUBLE},
] ]
}; };
exports.TestObject = TestObjectSchema;
function PersonObject() {} function PersonObject() {}
PersonObject.prototype.schema = { PersonObject.prototype.schema = {
@ -41,7 +40,7 @@ PersonObject.prototype.description = function() {
}; };
exports.PersonObject = PersonObject; exports.PersonObject = PersonObject;
var BasicTypesObjectSchema = { exports.BasicTypes = {
name: 'BasicTypesObject', name: 'BasicTypesObject',
properties: [ properties: [
{name: 'boolCol', type: Realm.Types.BOOL}, {name: 'boolCol', type: Realm.Types.BOOL},
@ -53,9 +52,8 @@ var BasicTypesObjectSchema = {
{name: 'dataCol', type: Realm.Types.DATA}, {name: 'dataCol', type: Realm.Types.DATA},
] ]
}; };
exports.BasicTypes = BasicTypesObjectSchema;
var LinkTypesObjectSchema = { exports.LinkTypes = {
name: 'LinkTypesObject', name: 'LinkTypesObject',
properties: [ properties: [
{name: 'objectCol', type: 'TestObject'}, {name: 'objectCol', type: 'TestObject'},
@ -63,9 +61,8 @@ var LinkTypesObjectSchema = {
{name: 'arrayCol', type: Realm.Types.LIST, objectType: 'TestObject'}, {name: 'arrayCol', type: Realm.Types.LIST, objectType: 'TestObject'},
] ]
}; };
exports.LinkTypes = LinkTypesObjectSchema;
var IntPrimaryObjectSchema = { exports.IntPrimary = {
name: 'IntPrimaryObject', name: 'IntPrimaryObject',
primaryKey: 'primaryCol', primaryKey: 'primaryCol',
properties: [ properties: [
@ -73,9 +70,8 @@ var IntPrimaryObjectSchema = {
{name: 'valueCol', type: Realm.Types.STRING}, {name: 'valueCol', type: Realm.Types.STRING},
] ]
}; };
exports.IntPrimary = IntPrimaryObjectSchema;
var AllTypesObjectSchema = { exports.AllTypes = {
name: 'AllTypesObject', name: 'AllTypesObject',
primaryKey: 'primaryCol', primaryKey: 'primaryCol',
properties: [ properties: [
@ -91,9 +87,8 @@ var AllTypesObjectSchema = {
{name: 'arrayCol', type: Realm.Types.LIST, objectType: 'TestObject'}, {name: 'arrayCol', type: Realm.Types.LIST, objectType: 'TestObject'},
] ]
}; };
exports.AllTypes = AllTypesObjectSchema;
var DefaultValuesObjectSchema = { exports.DefaultValues = {
name: 'DefaultValuesObject', name: 'DefaultValuesObject',
properties: [ properties: [
{name: 'boolCol', type: Realm.Types.BOOL, default: true}, {name: 'boolCol', type: Realm.Types.BOOL, default: true},
@ -108,4 +103,3 @@ var DefaultValuesObjectSchema = {
{name: 'arrayCol', type: Realm.Types.LIST, objectType: 'TestObject', default: [[2]]}, {name: 'arrayCol', type: Realm.Types.LIST, objectType: 'TestObject', default: [[2]]},
] ]
}; };
exports.DefaultValues = DefaultValuesObjectSchema;