ronantakizawa/codereview-bench huggingface dataset:
Repository: realm/realm-swift
File: Realm/Tests/RLMTestCase.m (Objective-C)

Code before:
NSString *RLMTestRealmPath() {
    return RLMRealmPathForFile(@"test.realm");
}

NSString *RLMTestRealmPathLock() {
    return RLMRealmPathForFile(@"test.realm.lock");
}

@implementation RLMTestCase

- (void)setUp {
    // This method is run before every test method
    [super setUp];
    [[NSFileManager defaultManager] removeItemAtPath:RLMDefaultRealmPath() error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:[RLMDefaultRealmPath() stringByAppendingString:@".lock"] error:nil];

    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPath() error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPathLock() error:nil];
}

+ (void)tearDown {
    // This method is run after all tests in a test method have run
    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPath() error:nil];
    [super tearDown];
}

- (RLMRealm *)realmWithTestPath {
    return [RLMRealm realmWithPath:RLMTestRealmPath() readOnly:NO error:nil];
}

@end


Review comment (suggestion):
Shouldn't we just use the RLMTestRealmPath instead?
Like this (see RealmTests.m)
RLMRealm *realm = [self realmWithTestPath];


Code after:
    return [documentsDirectory stringByAppendingPathComponent:fileName];
#else
    return fileName;
#endif
}

NSString *RLMDefaultRealmPath() {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:@"default.realm"];
}

NSString *RLMTestRealmPath() {
    return RLMRealmPathForFile(@"test.realm");
}

NSString *RLMTestRealmPathLock() {
    return RLMRealmPathForFile(@"test.realm.lock");
}

@implementation RLMTestCase

- (void)setUp {
    // This method is run before every test method
    [super setUp];
    [[NSFileManager defaultManager] removeItemAtPath:RLMDefaultRealmPath() error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:[RLMDefaultRealmPath() stringByAppendingString:@".lock"] error:nil];

    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPath() error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPathLock() error:nil];
}

+ (void)tearDown {
    // This method is run after all tests in a test method have run
    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPath() error:nil];
    [super tearDown];
}

- (RLMRealm *)realmWithTestPath {
    return [RLMRealm realmWithPath:RLMTestRealmPath() readOnly:NO error:nil];
}

@end
Repository: realm/realm-swift
File: Realm/Tests/RLMTestCase.m (Objective-C)

Code before:
NSString *RLMTestRealmPath() {
    return RLMRealmPathForFile(@"test.realm");
}

NSString *RLMTestRealmPathLock() {
    return RLMRealmPathForFile(@"test.realm.lock");
}

@implementation RLMTestCase

- (void)setUp {
    // This method is run before every test method
    [super setUp];
    [[NSFileManager defaultManager] removeItemAtPath:RLMDefaultRealmPath() error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:[RLMDefaultRealmPath() stringByAppendingString:@".lock"] error:nil];

    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPath() error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPathLock() error:nil];
}

+ (void)tearDown {
    // This method is run after all tests in a test method have run
    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPath() error:nil];
    [super tearDown];
}

- (RLMRealm *)realmWithTestPath {
    return [RLMRealm realmWithPath:RLMTestRealmPath() readOnly:NO error:nil];
}

@end


Review comment (suggestion):
It seems like both [RLMRealm defaultRealm] and [self realmWithTestPath] are used in the test cases.
@alazier  which one do you prefer, that we use?
I think we should streamline and settle on one of those two.


Code after:
    return [documentsDirectory stringByAppendingPathComponent:fileName];
#else
    return fileName;
#endif
}

NSString *RLMDefaultRealmPath() {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:@"default.realm"];
}

NSString *RLMTestRealmPath() {
    return RLMRealmPathForFile(@"test.realm");
}

NSString *RLMTestRealmPathLock() {
    return RLMRealmPathForFile(@"test.realm.lock");
}

@implementation RLMTestCase

- (void)setUp {
    // This method is run before every test method
    [super setUp];
    [[NSFileManager defaultManager] removeItemAtPath:RLMDefaultRealmPath() error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:[RLMDefaultRealmPath() stringByAppendingString:@".lock"] error:nil];

    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPath() error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPathLock() error:nil];
}

+ (void)tearDown {
    // This method is run after all tests in a test method have run
    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPath() error:nil];
    [super tearDown];
}

- (RLMRealm *)realmWithTestPath {
    return [RLMRealm realmWithPath:RLMTestRealmPath() readOnly:NO error:nil];
}

@end
Repository: realm/realm-swift
File: Realm/Tests/RLMTestCase.m (Objective-C)

Code before:
NSString *RLMTestRealmPath() {
    return RLMRealmPathForFile(@"test.realm");
}

NSString *RLMTestRealmPathLock() {
    return RLMRealmPathForFile(@"test.realm.lock");
}

@implementation RLMTestCase

- (void)setUp {
    // This method is run before every test method
    [super setUp];
    [[NSFileManager defaultManager] removeItemAtPath:RLMDefaultRealmPath() error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:[RLMDefaultRealmPath() stringByAppendingString:@".lock"] error:nil];

    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPath() error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPathLock() error:nil];
}

+ (void)tearDown {
    // This method is run after all tests in a test method have run
    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPath() error:nil];
    [super tearDown];
}

- (RLMRealm *)realmWithTestPath {
    return [RLMRealm realmWithPath:RLMTestRealmPath() readOnly:NO error:nil];
}

@end


Review comment (suggestion):
Yes, but not through the class methods, ie 

```
[MyClass allObjects]
```

only works on the defaultRealm.

The method which does this on any realm also requires you to pass in a nil query, so I'm thinking we should add the following method:

```
[realm allObjects:MyClass.class]
```


Code after:
    return [documentsDirectory stringByAppendingPathComponent:fileName];
#else
    return fileName;
#endif
}

NSString *RLMDefaultRealmPath() {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:@"default.realm"];
}

NSString *RLMTestRealmPath() {
    return RLMRealmPathForFile(@"test.realm");
}

NSString *RLMTestRealmPathLock() {
    return RLMRealmPathForFile(@"test.realm.lock");
}

@implementation RLMTestCase

- (void)setUp {
    // This method is run before every test method
    [super setUp];
    [[NSFileManager defaultManager] removeItemAtPath:RLMDefaultRealmPath() error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:[RLMDefaultRealmPath() stringByAppendingString:@".lock"] error:nil];

    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPath() error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPathLock() error:nil];
}

+ (void)tearDown {
    // This method is run after all tests in a test method have run
    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPath() error:nil];
    [super tearDown];
}

- (RLMRealm *)realmWithTestPath {
    return [RLMRealm realmWithPath:RLMTestRealmPath() readOnly:NO error:nil];
}

@end
Repository: realm/realm-swift
File: Realm/Tests/RLMTestCase.m (Objective-C)

Code before:
NSString *RLMTestRealmPath() {
    return RLMRealmPathForFile(@"test.realm");
}

NSString *RLMTestRealmPathLock() {
    return RLMRealmPathForFile(@"test.realm.lock");
}

@implementation RLMTestCase

- (void)setUp {
    // This method is run before every test method
    [super setUp];
    [[NSFileManager defaultManager] removeItemAtPath:RLMDefaultRealmPath() error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:[RLMDefaultRealmPath() stringByAppendingString:@".lock"] error:nil];

    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPath() error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPathLock() error:nil];
}

+ (void)tearDown {
    // This method is run after all tests in a test method have run
    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPath() error:nil];
    [super tearDown];
}

- (RLMRealm *)realmWithTestPath {
    return [RLMRealm realmWithPath:RLMTestRealmPath() readOnly:NO error:nil];
}

@end


Review comment (suggestion):
One other thing we might want to consider is passing a className as a string rather than a class. When you use a class as a method argument, the autocomplete puts in a really long attribute/flag and it gets really annoying. Problem with strings as that you can pass in an invalid classname, so we would want to add a static method 

```
+ (NSString *)className
```

to RLMObject and suggest that people call that to get the classname when using these methods


Code after:
    return [documentsDirectory stringByAppendingPathComponent:fileName];
#else
    return fileName;
#endif
}

NSString *RLMDefaultRealmPath() {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:@"default.realm"];
}

NSString *RLMTestRealmPath() {
    return RLMRealmPathForFile(@"test.realm");
}

NSString *RLMTestRealmPathLock() {
    return RLMRealmPathForFile(@"test.realm.lock");
}

@implementation RLMTestCase

- (void)setUp {
    // This method is run before every test method
    [super setUp];
    [[NSFileManager defaultManager] removeItemAtPath:RLMDefaultRealmPath() error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:[RLMDefaultRealmPath() stringByAppendingString:@".lock"] error:nil];

    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPath() error:nil];
    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPathLock() error:nil];
}

+ (void)tearDown {
    // This method is run after all tests in a test method have run
    [[NSFileManager defaultManager] removeItemAtPath:RLMTestRealmPath() error:nil];
    [super tearDown];
}

- (RLMRealm *)realmWithTestPath {
    return [RLMRealm realmWithPath:RLMTestRealmPath() readOnly:NO error:nil];
}

@end
Repository: realm/realm-swift
File: Realm/RLMRealm.mm (Objective-C++)

Code before:

- (void)dealloc
{
    [_updateTimer invalidate];
    _updateTimer = nil;
    
    if (self.transactionMode == RLMTransactionModeWrite) {
        [self commitWriteTransaction];
    }
    [self endReadTransaction];
}

- (void)refresh {
    try {
        // no-op if writing
        if (self.transactionMode == RLMTransactionModeWrite) {
            return;
        }
        
        // advance transaction if database has changed
        if (_sharedGroup->has_changed()) { // Throws
            [self endReadTransaction];
            [self beginReadTransaction];
            [self updateAllObjects];
            
            // send notification that someone else changed the realm
            [self sendNotifications];
        }
    }
    catch (exception &ex) {
        throw_objc_exception(ex);
    }
}

- (void)registerAccessor:(id<RLMAccessor>)accessor {
    [_objects setObject:accessor forKey:accessor];
}

- (void)updateAllObjects {
    try {
        // get the group
        tightdb::Group *group = self.group;
        BOOL writable = (self.transactionMode == RLMTransactionModeWrite);

        // refresh all outstanding objects
        for (id<RLMAccessor> obj in _objects.objectEnumerator.allObjects) {
            TableRef tableRef = group->get_table(obj.backingTableIndex); // Throws
            obj.backingTable = tableRef.get();
            obj.writable = writable;
        }
    }

Review comment (suggestion):
In the case a user forgot to close the transaction, it is more likely they would want to commit. Forgetting to rollback seems less likely. In the long term as we move towards implicit write transactions we will definitely want to commit.


Code after:
       @throw [NSException exceptionWithName:@"RLMException" reason:@"Can't commit a non-existing writetransaction" userInfo:nil];
    }
}

- (void)rollbackWriteTransaction {
    if (self.transactionMode == RLMTransactionModeWrite) {
        try {
            _sharedGroup->rollback();
            _writeGroup = NULL;
            
            [self beginReadTransaction];
        }
        catch (std::exception& ex) {
            throw_objc_exception(ex);
        }
    } else {
        @throw [NSException exceptionWithName:@"RLMException" reason:@"Can't roll-back a non-existing writetransaction" userInfo:nil];
    }
}

- (void)dealloc
{
    [_updateTimer invalidate];
    _updateTimer = nil;
    
    if (self.transactionMode == RLMTransactionModeWrite) {
        [self commitWriteTransaction];
    }
    [self endReadTransaction];
}

- (void)refresh {
    try {
        // no-op if writing
        if (self.transactionMode == RLMTransactionModeWrite) {
            return;
        }
        
        // advance transaction if database has changed
        if (_sharedGroup->has_changed()) { // Throws
            [self endReadTransaction];
            [self beginReadTransaction];
            [self updateAllObjects];
            
            // send notification that someone else changed the realm
            [self sendNotifications];
        }
    }
    catch (exception &ex) {
        throw_objc_exception(ex);
    }
Repository: realm/realm-swift
File: build.sh (Shell)

Code before:

xcrealm() {
    xc "-project Realm.xcodeproj $1"
}

######################################
# Input Validation
######################################

if [ "$#" -eq 0 -o "$#" -gt 2 ]; then
    usage
    exit 1
fi

######################################
# Variables
######################################

# Xcode sets this variable - set to current directory if running standalone
if [ -z "$SRCROOT" ]; then
    SRCROOT="$(pwd)"
fi

# You can override the version of the core library
# Otherwise, use the default value
if [ -z "$REALM_CORE_VERSION" ]; then
    REALM_CORE_VERSION=latest
fi

COMMAND="$1"
XCMODE="$2"
: ${XCMODE:=xcodebuild} # must be one of: xcodebuild (default), xcpretty, xctool


case "$COMMAND" in

    ######################################
    # Clean
    ######################################
    "clean")
        xcrealm "-scheme iOS -configuration Debug -sdk iphonesimulator clean" || exit 1
        xcrealm "-scheme iOS -configuration Release -sdk iphonesimulator clean" || exit 1
        xcrealm "-scheme OSX -configuration Debug clean" || exit 1
        xcrealm "-scheme OSX -configuration Release clean" || exit 1
        exit 0
	;;

    ######################################
    # Download Core Library
    ######################################
    "download-core")

Review comment (suggestion):
I would like to have sensible defaults (`REALM_CORE_VERSION`, `SRCROOT`, etc.). Xcode can set the version, and it would be possible to override it at the command line:

```
REALM_CORE_VERSION=0.42.0 sh build.sh download-core
```


Code after:

xcrealm() {
    xc "-project Realm.xcodeproj $1"
}

######################################
# Input Validation
######################################

if [ "$#" -eq 0 -o "$#" -gt 2 ]; then
    usage
    exit 1
fi

######################################
# Variables
######################################

# Xcode sets this variable - set to current directory if running standalone
if [ -z "$SRCROOT" ]; then
    SRCROOT="$(pwd)"
fi

# You can override the version of the core library
# Otherwise, use the default value
if [ -z "$REALM_CORE_VERSION" ]; then
    REALM_CORE_VERSION=latest
fi

COMMAND="$1"
XCMODE="$2"
: ${XCMODE:=xcodebuild} # must be one of: xcodebuild (default), xcpretty, xctool


case "$COMMAND" in

    ######################################
    # Clean
    ######################################
    "clean")
        xcrealm "-scheme iOS -configuration Debug -sdk iphonesimulator clean" || exit 1
        xcrealm "-scheme iOS -configuration Release -sdk iphonesimulator clean" || exit 1
        xcrealm "-scheme OSX -configuration Debug clean" || exit 1
        xcrealm "-scheme OSX -configuration Release clean" || exit 1
        exit 0
        ;;

    ######################################
    # Download Core Library
    ######################################
    "download-core")
Repository: realm/realm-swift
File: build.sh (Shell)

Code before:
    "build")
        sh build.sh ios "$XCMODE" || exit 1
        sh build.sh osx "$XCMODE" || exit 1
        exit 0
        ;;

    "ios")
        xcrealm "-scheme iOS"
        exit 0
        ;;

    "osx")
        xcrealm "-scheme OSX"
        exit 0
        ;;

    "docs")
        sh scripts/build-docs.sh || exit 1
        exit 0
        ;;

    ######################################
    # Testing
    ######################################
    "test")
        xcrealm "-scheme iOS -configuration Release -sdk iphonesimulator build test"
        xcrealm "-scheme OSX -configuration Release build test"
        exit 0
        ;;

    "test-debug")
        xcrealm "-scheme iOS -configuration Debug -sdk iphonesimulator build test"
        xcrealm "-scheme OSX -configuration Debug build test"
        exit 0
        ;;

    "test-all")
        sh build.sh test || exit 1
        sh build.sh test-debug || exit 1
        exit 0
        ;;

    "test-ios")
        xcrealm "-scheme iOS -sdk iphonesimulator test"
        exit 0
        ;;

    "test-osx")
        xcrealm "-scheme OSX test"
        exit 0
        ;;

Review comment (suggestion):
That's beyond the scope of this PR. JP decided not to publish those targets and only use them internally.


Code after:
    # Building
    ######################################
    "build")
        sh build.sh ios "$XCMODE" || exit 1
        sh build.sh osx "$XCMODE" || exit 1
        exit 0
        ;;

    "build-debug")
        sh build.sh ios-debug "$XCMODE" || exit 1
        sh build.sh osx-debug "$XCMODE" || exit 1
        exit 0
        ;;

    "ios")
        xcrealm "-scheme iOS -configuration Release"
        exit 0
        ;;

    "osx")
        xcrealm "-scheme OSX -configuration Release"
        exit 0
        ;;

    "ios-debug")
        xcrealm "-scheme iOS -configuration Debug"
        exit 0
        ;;

    "osx-debug")
        xcrealm "-scheme OSX -configuration Debug"
        exit 0
        ;;

    "docs")
        sh scripts/build-docs.sh || exit 1
        exit 0
        ;;

    ######################################
    # Testing
    ######################################
    "test")
        xcrealm "-scheme iOS -configuration Release -sdk iphonesimulator build test"
        xcrealm "-scheme OSX -configuration Release build test"
        exit 0
        ;;

    "test-debug")
        xcrealm "-scheme iOS -configuration Debug -sdk iphonesimulator build test"
        xcrealm "-scheme OSX -configuration Debug build test"
Repository: realm/realm-swift
File: build.sh (Shell)

Code before:
        xcrealm "-scheme OSX -configuration Debug"
        exit 0
        ;;

    "docs")
        sh scripts/build-docs.sh || exit 1
        exit 0
        ;;

    ######################################
    # Testing
    ######################################
    "test")
        sh build.sh test-ios "$XCMODE"
        sh build.sh test-osx "$XCMODE"
        exit 0
        ;;

    "test-debug")
        xcrealm "-scheme iOS -configuration Debug -sdk iphonesimulator build test"
        xcrealm "-scheme OSX -configuration Debug build test"
        exit 0
        ;;

    "test-all")
        sudo xcode-select -s /Applications/Xcode.app/Contents/Developer
        sh build.sh test "$XCMODE" || exit 1
        sh build.sh test-debug "$XCMODE" || exit 1
        sudo xcode-select -s /Applications/Xcode6-Beta2.app/Contents/Developer
        sh build.sh test "$XCMODE" || exit 1
        sh build.sh test-debug "$XCMODE" || exit 1
        exit 0
        ;;

    "test-ios")
        xcrealm "-scheme iOS -configuration Release -sdk iphonesimulator test"
        exit 0
        ;;

    "test-osx")
        xcrealm "-scheme OSX -configuration Release test"
        exit 0
        ;;

    "test-cover")
        echo "Not yet implemented"
        exit 0
        ;;

    "verify")
        sh build.sh docs || exit 1

Review comment (suggestion):
I can see that `sudo` is required but I find it a bit odd to type my password for running tests. I guess that once XCode 6 is released, we can revert this change.


Code after:
        xcrealm "-scheme OSX -configuration Debug"
        exit 0
        ;;

    "docs")
        sh scripts/build-docs.sh || exit 1
        exit 0
        ;;

    ######################################
    # Testing
    ######################################
    "test")
        sh build.sh test-ios "$XCMODE"
        sh build.sh test-osx "$XCMODE"
        exit 0
        ;;

    "test-debug")
        xcrealm "-scheme iOS -configuration Debug -sdk iphonesimulator build test"
        xcrealm "-scheme OSX -configuration Debug build test"
        exit 0
        ;;

    "test-all")
        sudo xcode-select -s /Applications/Xcode.app/Contents/Developer || exit 1
        sh build.sh test "$XCMODE" || exit 1
        sh build.sh test-debug "$XCMODE" || exit 1
        sudo xcode-select -s /Applications/Xcode6-Beta2.app/Contents/Developer || exit 1
        fail=0
        (
            sh build.sh test "$XCMODE" || exit 1
            sh build.sh test-debug "$XCMODE" || exit 1
        ) || fail=1
        sudo xcode-select -s /Applications/Xcode.app/Contents/Developer || exit 1
        exit $fail
        ;;

    "test-ios")
        xcrealm "-scheme iOS -configuration Release -sdk iphonesimulator test"
        exit 0
        ;;

    "test-osx")
        xcrealm "-scheme OSX -configuration Release test"
        exit 0
        ;;

    "test-cover")
        echo "Not yet implemented"
        exit 0