From 39b262b2792cdd15bfb7bca8c4f337f7c47b5768 Mon Sep 17 00:00:00 2001 From: Scott Kyle Date: Wed, 14 Oct 2015 14:58:42 -0700 Subject: [PATCH] Each test can now have a beforeEach and afterEach --- tests/ReactTests/index.ios.js | 73 +++++++++++++------ .../ios/ReactTests.xcodeproj/project.pbxproj | 41 +++++++++-- tests/RealmJSTests.mm | 35 +++++++-- tests/schemas.js | 18 ++--- 4 files changed, 120 insertions(+), 47 deletions(-) diff --git a/tests/ReactTests/index.ios.js b/tests/ReactTests/index.ios.js index 3a1e1702..96beddf1 100644 --- a/tests/ReactTests/index.ios.js +++ b/tests/ReactTests/index.ios.js @@ -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 ( - - - Welcome to React Native! - - - To get started, edit index.ios.js - - - Press Cmd+R to reload,{'\n'} - Cmd+D or shake for dev menu - - - ); - } + componentDidMount() { + runTests(); + }, + + render() { + return ( + + + Welcome to React Native! + + + To get started, edit index.ios.js + + + Press Cmd+R to reload,{'\n'} + Cmd+D or shake for dev menu + + + ); + } }); var styles = StyleSheet.create({ diff --git a/tests/ReactTests/ios/ReactTests.xcodeproj/project.pbxproj b/tests/ReactTests/ios/ReactTests.xcodeproj/project.pbxproj index 676b58f2..4df22ab0 100644 --- a/tests/ReactTests/ios/ReactTests.xcodeproj/project.pbxproj +++ b/tests/ReactTests/ios/ReactTests.xcodeproj/project.pbxproj @@ -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 = ""; }; 00C302A71ABCB8CE00DB3ED1 /* RCTActionSheet.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTActionSheet.xcodeproj; path = "../node_modules/react-native/Libraries/ActionSheetIOS/RCTActionSheet.xcodeproj"; sourceTree = ""; }; @@ -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 = ""; @@ -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 */ diff --git a/tests/RealmJSTests.mm b/tests/RealmJSTests.mm index 9cec8089..55eae491 100644 --- a/tests/RealmJSTests.mm +++ b/tests/RealmJSTests.mm @@ -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; diff --git a/tests/schemas.js b/tests/schemas.js index af8280d5..407bf171 100644 --- a/tests/schemas.js +++ b/tests/schemas.js @@ -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;