diff --git a/RealmJS.xcodeproj/project.pbxproj b/RealmJS.xcodeproj/project.pbxproj index aa78e2fa..a9c15f96 100644 --- a/RealmJS.xcodeproj/project.pbxproj +++ b/RealmJS.xcodeproj/project.pbxproj @@ -63,6 +63,7 @@ F64E1EF11BC3510E00E0E150 /* util.js in Resources */ = {isa = PBXBuildFile; fileRef = F64E1EF01BC3510E00E0E150 /* util.js */; settings = {ASSET_TAGS = (); }; }; F68A278C1BC2722A0063D40A /* RJSModuleLoader.m in Sources */ = {isa = PBXBuildFile; fileRef = F68A278B1BC2722A0063D40A /* RJSModuleLoader.m */; settings = {ASSET_TAGS = (); }; }; F68A278E1BC30F0A0063D40A /* index.js in Resources */ = {isa = PBXBuildFile; fileRef = F68A278D1BC30F0A0063D40A /* index.js */; settings = {ASSET_TAGS = (); }; }; + F6F405F81BCF0C1A00A1E24F /* base-test.js in Resources */ = {isa = PBXBuildFile; fileRef = F6F405F71BCF0C1A00A1E24F /* base-test.js */; settings = {ASSET_TAGS = (); }; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -199,6 +200,7 @@ F68A278A1BC2722A0063D40A /* RJSModuleLoader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RJSModuleLoader.h; path = tests/RJSModuleLoader.h; sourceTree = SOURCE_ROOT; }; F68A278B1BC2722A0063D40A /* RJSModuleLoader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = RJSModuleLoader.m; path = tests/RJSModuleLoader.m; sourceTree = SOURCE_ROOT; }; F68A278D1BC30F0A0063D40A /* index.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; name = index.js; path = tests/index.js; sourceTree = SOURCE_ROOT; }; + F6F405F71BCF0C1A00A1E24F /* base-test.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; name = "base-test.js"; path = "tests/base-test.js"; sourceTree = SOURCE_ROOT; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -325,6 +327,7 @@ 0270BC781B7D020100010E03 /* Info.plist */, F68A278D1BC30F0A0063D40A /* index.js */, 0270BC7E1B7D020100010E03 /* asserts.js */, + F6F405F71BCF0C1A00A1E24F /* base-test.js */, 0270BC7F1B7D020100010E03 /* schemas.js */, F64E1EF01BC3510E00E0E150 /* util.js */, 02D456D91B7E59A500EE1299 /* ArrayTests.js */, @@ -546,6 +549,7 @@ 0270BC811B7D020100010E03 /* ObjectTests.js in Resources */, 02D456DA1B7E59A500EE1299 /* ArrayTests.js in Resources */, 0270BC861B7D020100010E03 /* schemas.js in Resources */, + F6F405F81BCF0C1A00A1E24F /* base-test.js in Resources */, F68A278E1BC30F0A0063D40A /* index.js in Resources */, 0270BC831B7D020100010E03 /* RealmTests.js in Resources */, 0270BC841B7D020100010E03 /* ResultsTests.js in Resources */, diff --git a/tests/ArrayTests.js b/tests/ArrayTests.js index 24590821..4fb02a6c 100644 --- a/tests/ArrayTests.js +++ b/tests/ArrayTests.js @@ -19,10 +19,11 @@ 'use strict'; var Realm = require('realm'); +var BaseTest = require('./base-test'); var TestCase = require('./asserts'); var schemas = require('./schemas'); -module.exports = { +module.exports = BaseTest.extend({ testArrayLength: function() { var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]}); realm.write(function() { @@ -282,4 +283,4 @@ module.exports = { obj.arrayCol.splice(0, 0, obj.objectCol); }, 'can only splice in a write transaction'); }, -}; +}); diff --git a/tests/ObjectTests.js b/tests/ObjectTests.js index b8aa97c9..5d045e44 100644 --- a/tests/ObjectTests.js +++ b/tests/ObjectTests.js @@ -19,10 +19,11 @@ 'use strict'; var Realm = require('realm'); +var BaseTest = require('./base-test'); var TestCase = require('./asserts'); var schemas = require('./schemas'); -module.exports = { +module.exports = BaseTest.extend({ testBasicTypesPropertyGetters: function() { var basicTypesValues = [true, 1, 1.1, 1.11, 'string', new Date(1), 'DATA']; var realm = new Realm({schema: [schemas.BasicTypes]}); @@ -147,4 +148,4 @@ module.exports = { TestCase.assertEqual(obj.arrayCol[1].doubleCol, 1); TestCase.assertEqual(obj.arrayCol[2].doubleCol, 2); }, -}; +}); diff --git a/tests/RealmJSTests.mm b/tests/RealmJSTests.mm index 55eae491..345bd54d 100644 --- a/tests/RealmJSTests.mm +++ b/tests/RealmJSTests.mm @@ -92,15 +92,6 @@ static void DeleteRealmFilesAtPath(NSString *path) { - (void)tearDown { [self invokeMethod:@"afterEach"]; - realm::Realm::s_global_cache.invalidate_all(); - realm::Realm::s_global_cache.clear(); - - // FIXME - find all realm files in the docs dir and delete them rather than hardcoding these - DeleteRealmFilesAtPath(RealmPathForFile(@"test.realm")); - DeleteRealmFilesAtPath(RealmPathForFile(@"test1.realm")); - DeleteRealmFilesAtPath(RealmPathForFile(@"test2.realm")); - DeleteRealmFilesAtPath(@(RJSDefaultPath().c_str())); - [super tearDown]; } @@ -116,6 +107,10 @@ static void DeleteRealmFilesAtPath(NSString *path) { RJSModuleLoader *moduleLoader = [[RJSModuleLoader alloc] initWithContext:context]; NSURL *scriptURL = [[NSBundle bundleForClass:self] URLForResource:@"index" withExtension:@"js"]; + context[@"cleanupTestRealms"] = ^{ + [self cleanupTestRealms]; + }; + [RealmJS initializeContext:context.JSGlobalContextRef]; // Expose the global Realm object as a global 'realm' CommonJS module. @@ -155,6 +150,17 @@ static void DeleteRealmFilesAtPath(NSString *path) { return suite; } ++ (void)cleanupTestRealms { + realm::Realm::s_global_cache.invalidate_all(); + realm::Realm::s_global_cache.clear(); + + // FIXME - find all realm files in the docs dir and delete them rather than hardcoding these + DeleteRealmFilesAtPath(RealmPathForFile(@"test.realm")); + DeleteRealmFilesAtPath(RealmPathForFile(@"test1.realm")); + DeleteRealmFilesAtPath(RealmPathForFile(@"test2.realm")); + DeleteRealmFilesAtPath(@(RJSDefaultPath().c_str())); +} + - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { NSMethodSignature *sig = [super methodSignatureForSelector:aSelector]; return sig ?: [NSMethodSignature signatureWithObjCTypes:"v@:"]; diff --git a/tests/RealmTests.js b/tests/RealmTests.js index 22c5662d..b75360f2 100644 --- a/tests/RealmTests.js +++ b/tests/RealmTests.js @@ -19,11 +19,12 @@ 'use strict'; var Realm = require('realm'); +var BaseTest = require('./base-test'); var TestCase = require('./asserts'); var schemas = require('./schemas'); var util = require('./util'); -module.exports = { +module.exports = BaseTest.extend({ testRealmConstructorPath: function() { TestCase.assertThrows(function() { new Realm('/invalidpath'); }); TestCase.assertThrows(function() { new Realm(util.realmPathForFile('test1.realm'), 'invalidArgument'); }); @@ -291,4 +292,4 @@ module.exports = { TestCase.assertEqual(notificationCount, 1); TestCase.assertEqual(notificationName, 'DidChangeNotification'); }, -}; +}); diff --git a/tests/ResultsTests.js b/tests/ResultsTests.js index 82033fb3..c3180c24 100644 --- a/tests/ResultsTests.js +++ b/tests/ResultsTests.js @@ -19,10 +19,11 @@ 'use strict'; var Realm = require('realm'); +var BaseTest = require('./base-test'); var TestCase = require('./asserts'); var schemas = require('./schemas'); -module.exports = { +module.exports = BaseTest.extend({ testResultsLength: function() { var realm = new Realm({schema: [schemas.TestObject]}); var objects = realm.objects('TestObject'); @@ -103,4 +104,4 @@ module.exports = { TestCase.assertEqual(objects[3].doubleCol, 1); TestCase.assertEqual(objects[4].doubleCol, 0); }, -}; +}); diff --git a/tests/base-test.js b/tests/base-test.js new file mode 100644 index 00000000..a7ad8aaa --- /dev/null +++ b/tests/base-test.js @@ -0,0 +1,36 @@ +//////////////////////////////////////////////////////////////////////////// +// +// Copyright 2015 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. +// +//////////////////////////////////////////////////////////////////////////// + +'use strict'; + +var util = require('./util'); + +var prototype = exports.prototype = {}; + +exports.extend = function(object) { + object.__proto__ = prototype; + return object; +}; + +Object.defineProperties(prototype, { + afterEach: { + value: function() { + util.cleanupTestRealms(); + } + } +}); diff --git a/tests/util.js b/tests/util.js index 6b1b1bb7..3008d62a 100644 --- a/tests/util.js +++ b/tests/util.js @@ -20,7 +20,13 @@ var Realm = require('realm'); +var global = (typeof global != 'undefined') ? global : this; + exports.realmPathForFile = function(str) { var path = Realm.defaultPath; return path.substring(0, path.lastIndexOf("/") + 1) + str; }; + +exports.cleanupTestRealms = function() { + global.cleanupTestRealms(); +};