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 RealmTests = require('realm-tests');
for (var suiteName in RealmTests) {
for (var testName in RealmTests[suiteName]) {
RealmTests[suiteName][testName]();
}
}
var {
AppRegistry,
StyleSheet,
@ -21,23 +15,58 @@ var {
View,
} = 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({
render: function() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Text style={styles.instructions}>
To get started, edit index.ios.js
</Text>
<Text style={styles.instructions}>
Press Cmd+R to reload,{'\n'}
Cmd+D or shake for dev menu
</Text>
</View>
);
}
componentDidMount() {
runTests();
},
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Text style={styles.instructions}>
To get started, edit index.ios.js
</Text>
<Text style={styles.instructions}>
Press Cmd+R to reload,{'\n'}
Cmd+D or shake for dev menu
</Text>
</View>
);
}
});
var styles = StyleSheet.create({

View File

@ -14,7 +14,7 @@
00C302E91ABCBA2D00DB3ED1 /* libRCTNetwork.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 00C302DC1ABCB9D200DB3ED1 /* libRCTNetwork.a */; };
00C302EA1ABCBA2D00DB3ED1 /* libRCTVibration.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 00C302E41ABCB9EE00DB3ED1 /* libRCTVibration.a */; };
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 */; };
139105C61AF99C1200B5F7CC /* libRCTSettings.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 139105C11AF99BAD00B5F7CC /* libRCTSettings.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 */; };
146834051AC3E58100842450 /* libReact.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 146834041AC3E56700842450 /* libReact.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 */
/* Begin PBXContainerItemProxy section */
@ -125,8 +126,29 @@
remoteGlobalIDString = 58B5119B1A9E6C1200147676;
remoteInfo = RCTText;
};
F6F405B71BCE55FB00A1E24F /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 027799061BBF3BB700C96559 /* RealmJS.xcodeproj */;
proxyType = 1;
remoteGlobalIDString = 02B29A151B7CF7C9008A7E6B;
remoteInfo = RealmReact;
};
/* 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 */
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>"; };
@ -164,7 +186,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
0277991C1BBF3BC600C96559 /* libRealmReact.a in Frameworks */,
0277991C1BBF3BC600C96559 /* RealmReact.framework in Frameworks */,
146834051AC3E58100842450 /* libReact.a in Frameworks */,
00C302E51ABCBA2D00DB3ED1 /* libRCTActionSheet.a in Frameworks */,
00C302E71ABCBA2D00DB3ED1 /* libRCTGeolocation.a in Frameworks */,
@ -243,7 +265,7 @@
children = (
027799171BBF3BB700C96559 /* RealmJS.framework */,
027799191BBF3BB700C96559 /* RealmJSTests.xctest */,
0277991B1BBF3BB700C96559 /* libRealmReact.a */,
0277991B1BBF3BB700C96559 /* RealmReact.framework */,
);
name = Products;
sourceTree = "<group>";
@ -370,10 +392,12 @@
13B07F871A680F5B00A75B9A /* Sources */,
13B07F8C1A680F5B00A75B9A /* Frameworks */,
13B07F8E1A680F5B00A75B9A /* Resources */,
F6F405C61BCE565100A1E24F /* Embed Frameworks */,
);
buildRules = (
);
dependencies = (
F6F405B81BCE55FB00A1E24F /* PBXTargetDependency */,
);
name = ReactTests;
productName = "Hello World";
@ -510,10 +534,10 @@
remoteRef = 027799181BBF3BB700C96559 /* PBXContainerItemProxy */;
sourceTree = BUILT_PRODUCTS_DIR;
};
0277991B1BBF3BB700C96559 /* libRealmReact.a */ = {
0277991B1BBF3BB700C96559 /* RealmReact.framework */ = {
isa = PBXReferenceProxy;
fileType = archive.ar;
path = libRealmReact.a;
fileType = wrapper.framework;
path = RealmReact.framework;
remoteRef = 0277991A1BBF3BB700C96559 /* PBXContainerItemProxy */;
sourceTree = BUILT_PRODUCTS_DIR;
};
@ -616,6 +640,11 @@
target = 13B07F861A680F5B00A75B9A /* ReactTests */;
targetProxy = 00E356F41AD99517003FC87E /* PBXContainerItemProxy */;
};
F6F405B81BCE55FB00A1E24F /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
name = RealmReact;
targetProxy = F6F405B71BCE55FB00A1E24F /* PBXContainerItemProxy */;
};
/* End PBXTargetDependency section */
/* Begin PBXVariantGroup section */

View File

@ -86,10 +86,12 @@ static void DeleteRealmFilesAtPath(NSString *path) {
NSString *defaultDir = [[NSString stringWithUTF8String:RJSDefaultPath().c_str()] stringByDeletingLastPathComponent];
[[NSFileManager defaultManager] createDirectoryAtPath:defaultDir withIntermediateDirectories:YES attributes:nil error:nil];
self.context.exception = nil;
[self invokeMethod:@"beforeEach"];
}
- (void)tearDown {
[self invokeMethod:@"afterEach"];
realm::Realm::s_global_cache.invalidate_all();
realm::Realm::s_global_cache.clear();
@ -127,14 +129,24 @@ static void DeleteRealmFilesAtPath(NSString *path) {
exit(1);
}
NSSet *specialMethodNames = [NSSet setWithObjects:@"beforeEach", @"afterEach", nil];
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];
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];
[testSuite addTest:testCase];
}
}
[suite addTest:testSuite];
@ -149,13 +161,22 @@ static void DeleteRealmFilesAtPath(NSString *path) {
}
- (void)forwardInvocation:(NSInvocation *)anInvocation {
JSValue *testObject = self.testObject;
JSContext *context = testObject.context;
[self invokeMethod:NSStringFromSelector(anInvocation.selector)];
}
[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;
if (exception) {
JSValue *message = [exception hasProperty:@"message"] ? exception[@"message"] : exception;
NSString *source = [exception hasProperty:@"sourceURL"] ? [exception[@"sourceURL"] toString] : nil;

View File

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