Repository: realm/realm-swift
File: Realm/Tests/ObjectTests.m (Objective-C)

Code before:
    XCTAssertEqual(count(NSLessThanPredicateOperatorType), (NSUInteger)0,
                   @"< operator in date comparison.");
    XCTAssertEqual(count(NSLessThanOrEqualToPredicateOperatorType), (NSUInteger)0,
                   @"<= operator in date comparison.");
    XCTAssertEqual(count(NSGreaterThanPredicateOperatorType), (NSUInteger)0,
                   @"> operator in date comparison.");
    XCTAssertEqual(count(NSGreaterThanOrEqualToPredicateOperatorType), (NSUInteger)0,
                   @">= operator in date comparison.");
    XCTAssertEqual(count(NSEqualToPredicateOperatorType), (NSUInteger)0,
                   @"= or == operator in date comparison.");
    XCTAssertEqual(count(NSNotEqualToPredicateOperatorType), (NSUInteger)0,
                   @"!= or <> operator in date comparison.");

    // Invalid operators.
    @try {
        count(NSBeginsWithPredicateOperatorType);
        XCTFail("Invalid operator in date comparison.");
    }
    @catch (NSException *exception){
        XCTAssertEqualObjects(exception.name,
                              @"filterWithPredicate:orderedBy: - Invalid operator type",
                              @"Invalid operator in date comparison.");
    }
}

- (void)testBinaryComparisonInPredicate
{
    NSExpression *binary = [NSExpression expressionForConstantValue:[[NSData alloc] init]];

    NSUInteger (^count)(OP_TYPE) = ^(OP_TYPE type) {
        NSPredicate * pred = [RLMPredicateGen comparisonWithKeyPath: @"binaryCol"
                                                         expression: binary
                                                               type: type];
        return [BinaryObject objectsWithPredicate: pred].count;
    };

    XCTAssertEqual(count(NSBeginsWithPredicateOperatorType), (NSUInteger)0,
                   @"BEGINSWITH operator in binary comparison.");
    XCTAssertEqual(count(NSEndsWithPredicateOperatorType), (NSUInteger)0,
                   @"ENDSWITH operator in binary comparison.");
    XCTAssertEqual(count(NSContainsPredicateOperatorType), (NSUInteger)0,
                   @"CONTAINS operator in binary comparison.");
    XCTAssertEqual(count(NSEqualToPredicateOperatorType), (NSUInteger)0,
                   @"= or == operator in binary comparison.");
    XCTAssertEqual(count(NSNotEqualToPredicateOperatorType), (NSUInteger)0,
                   @"!= or <> operator in binary comparison.");

    // Invalid operators.
    @try {
        count(NSLessThanPredicateOperatorType);
        XCTFail("Invalid operator in binary comparison.");

Review comment (suggestion):
Having an array of test cases and looping through would make these easier to read.

Also, use XCTAssertThrowsSpecificNamed instead of the try/catch


Code after:
    XCTAssertEqual(count(NSBeginsWithPredicateOperatorType, 0),
                   (NSUInteger)0, @"Case-sensitive BEGINSWITH operator in string comparison.");
    XCTAssertEqual(count(NSBeginsWithPredicateOperatorType, NSCaseInsensitivePredicateOption),
                   (NSUInteger)1, @"Case-insensitive BEGINSWITH operator in string comparison.");

    XCTAssertEqual(count(NSEndsWithPredicateOperatorType, 0),
                   (NSUInteger)0, @"Case-sensitive ENDSWITH operator in string comparison.");
    XCTAssertEqual(count(NSEndsWithPredicateOperatorType, NSCaseInsensitivePredicateOption),
                   (NSUInteger)1, @"Case-insensitive ENDSWITH operator in string comparison.");

    XCTAssertEqual(count(NSContainsPredicateOperatorType, 0),
                   (NSUInteger)0, @"Case-sensitive CONTAINS operator in string comparison.");
    XCTAssertEqual(count(NSContainsPredicateOperatorType, NSCaseInsensitivePredicateOption),
                   (NSUInteger)1, @"Case-insensitive CONTAINS operator in string comparison.");

    XCTAssertEqual(count(NSEqualToPredicateOperatorType, 0),
                   (NSUInteger)0, @"Case-sensitive = or == operator in string comparison.");
    XCTAssertEqual(count(NSEqualToPredicateOperatorType, NSCaseInsensitivePredicateOption),
                   (NSUInteger)1, @"Case-insensitive = or == operator in string comparison.");

    XCTAssertEqual(count(NSNotEqualToPredicateOperatorType, 0),
                   (NSUInteger)1, @"Case-sensitive != or <> operator in string comparison.");
    XCTAssertEqual(count(NSNotEqualToPredicateOperatorType, NSCaseInsensitivePredicateOption),
                   (NSUInteger)0, @"Case-insensitive != or <> operator in string comparison.");

    // Unsupported (but valid) modifiers.
    XCTAssertThrowsSpecificNamed(count(NSBeginsWithPredicateOperatorType, NSDiacriticInsensitivePredicateOption), NSException, @"filterWithPredicate:orderedBy: - Invalid predicate option", @"Diachritic insensitivity is not supported.");

    // Unsupported (but valid) operators.
    XCTAssertThrowsSpecificNamed(count(NSLikePredicateOperatorType, 0), NSException, @"filterWithPredicate:orderedBy: - Invalid operator type", @"LIKE not supported for string comparison.");
    XCTAssertThrowsSpecificNamed(count(NSMatchesPredicateOperatorType, 0), NSException, @"filterWithPredicate:orderedBy: - Invalid operator type", @"MATCHES not supported in string comparison.");

    // Invalid operators.
    XCTAssertThrowsSpecificNamed(count(NSLessThanPredicateOperatorType, 0), NSException, @"filterWithPredicate:orderedBy: - Invalid operator type", @"Invalid operator in string comparison.");
}

- (void)testDateComparisonInPredicate
{
    NSExpression *now = [NSExpression expressionForConstantValue:[[NSDate alloc] init]];

    NSUInteger (^count)(OP_TYPE) = ^(OP_TYPE type) {
        NSPredicate * pred = [RLMPredicateUtil comparisonWithKeyPath: @"dateCol"
                                                         expression: now
                                                               type: type];
        return [DateObject objectsWithPredicate: pred].count;
    };

    XCTAssertEqual(count(NSLessThanPredicateOperatorType), (NSUInteger)0,
                   @"< operator in date comparison.");
    XCTAssertEqual(count(NSLessThanOrEqualToPredicateOperatorType), (NSUInteger)0,
                   @"<= operator in date comparison.");