This commit is contained in:
giuseppenuc committed 2016-02-24 16:56:39 +01:00
1 parent d281d765ea
commit 5b074a5176
1261 files changed
+199158 -7303

No files matched your search

@@ -0,0 +1,23 @@
//
// OCHamcrest - FakeWithCount.h
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
#import <Foundation/Foundation.h>
@interface FakeWithCount : NSObject
{
NSUInteger count;
}
@property (readonly, nonatomic) NSUInteger count;
+ (id)fakeWithCount:(NSUInteger)fakeCount;
- (id)initWithCount:(NSUInteger)fakeCount;
@end
@@ -0,0 +1,36 @@
//
// OCHamcrest - FakeWithCount.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
#import "FakeWithCount.h"
@implementation FakeWithCount
@synthesize count;
+ (id)fakeWithCount:(NSUInteger)fakeCount
{
return [[self alloc] initWithCount:fakeCount];
}
- (id)initWithCount:(NSUInteger)fakeCount
{
self = [super init];
if (self)
count = fakeCount;
return self;
}
- (NSString *)description
{
return @"FakeWithCount";
}
@end
@@ -0,0 +1,17 @@
//
// OCHamcrest - FakeWithoutCount.h
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
#import <Foundation/Foundation.h>
@interface FakeWithoutCount : NSObject
+ (id)fake;
@end
@@ -0,0 +1,25 @@
//
// OCHamcrest - FakeWithoutCount.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
#import "FakeWithoutCount.h"
@implementation FakeWithoutCount
+ (id)fake
{
return [[self alloc] init];
}
- (NSString *)description
{
return @"FakeWithoutCount";
}
@end
@@ -0,0 +1,128 @@
//
// OCHamcrest - HasCountTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCHasCount.h>
// Collaborators
#import <OCHamcrest/HCIsEqualToNumber.h>
#import <OCHamcrest/HCOrderingComparison.h>
// Test support
#import "AbstractMatcherTest.h"
#import "FakeWithCount.h"
#import "FakeWithoutCount.h"
@interface HasCountTest : AbstractMatcherTest
@end
@implementation HasCountTest
- (id<HCMatcher>)createMatcher
{
return hasCount(equalToUnsignedInteger(42));
}
- (void)testConvertsCountToNSNumberAndPassesToNestedMatcher
{
FakeWithCount *fakeWithCount = [FakeWithCount fakeWithCount:5];
assertMatches(@"same number", hasCount(equalToUnsignedInteger(5)), fakeWithCount);
assertDoesNotMatch(@"different number", hasCount(equalToUnsignedInteger(6)), fakeWithCount);
}
- (void)testHasReadableDescription
{
assertDescription(@"a collection with count of a value greater than <5>",
hasCount(greaterThan(@(5))));
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
assertNoMismatchDescription(hasCountOf(2), ([NSSet setWithObjects:@"a", @"b", nil]));
}
- (void)testMismatchDescriptionForItemWithWrongCount
{
assertMismatchDescription(@"was count of <42> with <FakeWithCount>",
hasCount(equalToUnsignedInteger(1)),
[FakeWithCount fakeWithCount:42]);
}
- (void)testMismatchDescriptionForItemWithoutCount
{
assertMismatchDescription(@"was <FakeWithoutCount>",
hasCount(equalToUnsignedInteger(1)), [FakeWithoutCount fake]);
}
- (void)testDescribesMismatchForItemWithWrongCount
{
assertDescribeMismatch(@"was count of <42> with <FakeWithCount>",
hasCount(equalToUnsignedInteger(1)), [FakeWithCount fakeWithCount:42]);
}
- (void)testDescribesMismatchForItemWithoutCount
{
assertDescribeMismatch(@"was <FakeWithoutCount>",
hasCount(equalToUnsignedInteger(1)), [FakeWithoutCount fake]);
}
@end
#pragma mark -
@interface HasCountOfTest : AbstractMatcherTest
@end
@implementation HasCountOfTest
- (id<HCMatcher>)createMatcher
{
return hasCountOf(42);
}
- (void)testHasCountOfIsShortcutForEqualToUnsignedInteger
{
FakeWithCount *fakeWithCount = [FakeWithCount fakeWithCount:5];
assertMatches(@"same number", hasCountOf(5), fakeWithCount);
assertDoesNotMatch(@"different number", hasCountOf(6), fakeWithCount);
}
- (void)testHasReadableDescription
{
assertDescription(@"a collection with count of <5>", hasCountOf(5));
}
- (void)testMismatchDescriptionForItemWithWrongCount
{
assertMismatchDescription(@"was count of <42> with <FakeWithCount>",
hasCountOf(1), [FakeWithCount fakeWithCount:42]);
}
- (void)testMismatchDescriptionForItemWithoutCount
{
assertMismatchDescription(@"was <FakeWithoutCount>", hasCountOf(1), [FakeWithoutCount fake]);
}
- (void)testDescribesMismatchForItemWithWrongCount
{
assertDescribeMismatch(@"was count of <42> with <FakeWithCount>",
hasCountOf(1), [FakeWithCount fakeWithCount:42]);
}
- (void)testDescribesMismatchForItemWithoutCount
{
assertDescribeMismatch(@"was <FakeWithoutCount>", hasCountOf(1), [FakeWithoutCount fake]);
}
@end
@@ -0,0 +1,114 @@
//
// OCHamcrest - IsCollectionContainingInAnyOrderTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCIsCollectionContainingInAnyOrder.h>
// Collaborators
#import <OCHamcrest/HCIsEqual.h>
// Test support
#import "AbstractMatcherTest.h"
@interface IsCollectionContainingInAnyOrderTest : AbstractMatcherTest
@end
@implementation IsCollectionContainingInAnyOrderTest
- (id<HCMatcher>)createMatcher
{
return containsInAnyOrder(equalTo(@"irrelevant"), nil);
}
- (void)testMatchingSingleItemCollection
{
assertMatches(@"Single item collection",
(containsInAnyOrder(equalTo(@"a"), nil)), ([NSArray arrayWithObjects:@"a", nil]));
}
- (void)testMatchesCollectionInOfOrder
{
assertMatches(@"In order",
(containsInAnyOrder(equalTo(@"a"), equalTo(@"b"), nil)),
([NSArray arrayWithObjects:@"a", @"b", nil]));
}
- (void)testMatchesCollectionOutOfOrder
{
assertMatches(@"Out of order",
(containsInAnyOrder(equalTo(@"a"), equalTo(@"b"), nil)),
([NSArray arrayWithObjects:@"b", @"a", nil]));
}
- (void)testProvidesConvenientShortcutForMatchingWithEqualTo
{
assertMatches(@"Values automatically wrapped with equalTo",
(containsInAnyOrder(@"a", @"b", nil)),
([NSArray arrayWithObjects:@"b", @"a", nil]));
}
- (void)testDoesNotMatchEmptyCollection
{
assertMismatchDescription(@"no item matches: \"a\", \"b\" in []",
(containsInAnyOrder(@"a", @"b", nil)), @[]);
}
- (void)testDoesNotMatchNil
{
assertDoesNotMatch(@"Should not match nil", containsInAnyOrder(@"a", nil), nil);
}
- (void)testDoesNotMatchIfOneOfMultipleItemsMismatch
{
assertMismatchDescription(@"not matched: \"d\"",
(containsInAnyOrder(@"a", @"b", @"c", nil)),
([NSArray arrayWithObjects:@"a", @"b", @"d", nil]));
}
- (void)testDoesNotMatchWithMoreElementsThanExpected
{
assertMismatchDescription(@"not matched: \"b\"",
(containsInAnyOrder(@"a", @"c", nil)),
([NSArray arrayWithObjects:@"a", @"b", @"c", nil]));
}
- (void)testDoesNotMatchWithFewerElementsThanExpected
{
assertMismatchDescription(@"no item matches: \"d\" in [\"a\", \"b\", \"c\"]",
(containsInAnyOrder(@"a", @"b", @"c", @"d", nil)),
([NSArray arrayWithObjects:@"a", @"b", @"c", nil]));
}
- (void)testDoesNotMatchObjectWithoutEnumerator
{
assertDoesNotMatch(@"no enumerator",
containsInAnyOrder(@"a", nil), [[NSObject alloc] init]);
}
- (void)testHasAReadableDescription
{
assertDescription(@"a collection over [\"a\", \"b\"] in any order",
containsInAnyOrder(@"a", @"b", nil));
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"not matched: \"c\"",
(containsInAnyOrder(@"a", @"b", nil)),
([NSArray arrayWithObjects:@"a", @"c", nil]));
}
- (void)testDescribeMismatchOfNonCollection
{
assertDescribeMismatch(@"was nil", (containsInAnyOrder(@"a", @"b", nil)), nil);
}
@end
@@ -0,0 +1,111 @@
//
// OCHamcrest - IsCollectionContainingInOrderTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCIsCollectionContainingInOrder.h>
// Collaborators
#import <OCHamcrest/HCIsEqual.h>
// Test support
#import "AbstractMatcherTest.h"
@interface IsCollectionContainingInOrderTest : AbstractMatcherTest
@end
@implementation IsCollectionContainingInOrderTest
- (id<HCMatcher>)createMatcher
{
return contains(equalTo(@"irrelevant"), nil);
}
- (void)testMatchingSingleItemCollection
{
assertMatches(@"Single item collection",
(contains(equalTo(@"a"), nil)), ([NSArray arrayWithObjects:@"a", nil]));
}
- (void)testMatchingMultipleItemSequence
{
assertMatches(@"Multiple item sequence",
(contains(equalTo(@"a"), equalTo(@"b"), equalTo(@"c"), nil)),
([NSArray arrayWithObjects:@"a", @"b", @"c", nil]));
}
- (void)testProvidesConvenientShortcutForMatchingWithEqualTo
{
assertMatches(@"Values automatically wrapped with equalTo",
(contains(@"a", @"b", @"c", nil)),
([NSArray arrayWithObjects:@"a", @"b", @"c", nil]));
}
- (void)testDoesNotMatchWithMoreElementsThanExpected
{
assertMismatchDescription(@"not matched: \"d\"",
(contains(@"a", @"b", @"c", nil)),
([NSArray arrayWithObjects:@"a", @"b", @"c", @"d", nil]));
}
- (void)testDoesNotMatchWithFewerElementsThanExpected
{
assertMismatchDescription(@"no item matched: \"c\"",
(contains(@"a", @"b", @"c", nil)),
([NSArray arrayWithObjects:@"a", @"b", nil]));
}
- (void)testDoesNotMatchIfSingleItemMismatches
{
assertMismatchDescription(@"item 0: was \"c\"",
(contains(@"d", nil)), @[@"c"]);
}
- (void)testDoesNotMatchIfOneOfMultipleItemsMismatch
{
assertMismatchDescription(@"item 2: was \"d\"",
(contains(@"a", @"b", @"c", nil)),
([NSArray arrayWithObjects:@"a", @"b", @"d", nil]));
}
- (void)testDoesNotMatchNil
{
assertDoesNotMatch(@"Should not match nil", contains(@"a", nil), nil);
}
- (void)testDoesNotMatchEmptyCollection
{
assertMismatchDescription(@"no item matched: \"d\"", (contains(@"d", nil)), @[]);
}
- (void)testDoesNotMatchObjectWithoutEnumerator
{
assertDoesNotMatch(@"should not match object without enumerator",
contains(@"a", nil), [[NSObject alloc] init]);
}
- (void)testHasAReadableDescription
{
assertDescription(@"a collection containing [\"a\", \"b\"]", contains(@"a", @"b", nil));
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"item 1: was \"c\"",
(contains(@"a", @"b", nil)),
([NSArray arrayWithObjects:@"a", @"c", nil]));
}
- (void)testDescribeMismatchOfNonCollection
{
assertDescribeMismatch(@"was nil", (contains(@"a", @"b", nil)), nil);
}
@end
@@ -0,0 +1,162 @@
//
// OCHamcrest - IsCollectionContainingTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCIsCollectionContaining.h>
// Collaborators
#import <OCHamcrest/HCIsEqual.h>
// Test support
#import "AbstractMatcherTest.h"
@interface IsCollectionContainingTest : AbstractMatcherTest
@end
@implementation IsCollectionContainingTest
- (id<HCMatcher>)createMatcher
{
return hasItem(equalTo(@"irrelevant"));
}
- (void)testMatchesACollectionThatContainsAnElementMatchingTheGivenMatcher
{
assertMatches(@"list contains 'a'",
hasItem(equalTo(@"a")), ([NSArray arrayWithObjects:@"a", @"b", @"c", nil]));
}
- (void)testNoMatchIfCollectionDoesNotContainAnElementMatchingTheGivenMatcher
{
assertDoesNotMatch(@"list without 'a'",
hasItem(equalTo(@"a")), ([NSArray arrayWithObjects:@"b", @"c", nil]));
assertDoesNotMatch(@"empty", hasItem(equalTo(@"a")), @[]);
}
- (void)testDoesNotMatchNil
{
assertDoesNotMatch(@"should not match nil", hasItem(equalTo(@"a")), nil);
}
- (void)testProvidesConvenientShortcutForMatchingWithEqualTo
{
assertMatches(@"list contains 'a'",
hasItem(@"a"), ([NSSet setWithObjects:@"a", @"b", @"c", nil]));
assertDoesNotMatch(@"list without 'a'",
hasItem(@"a"), ([NSSet setWithObjects:@"b", @"c", nil]));
}
- (void)testDoesNotMatchObjectWithoutEnumerator
{
assertDoesNotMatch(@"should not match object without enumerator",
hasItem(@"a"), [[NSObject alloc] init]);
}
- (void)testMatcherCreationRequiresNonNilArgument
{
STAssertThrows(hasItem(nil), @"Should require non-nil argument");
}
- (void)testHasAReadableDescription
{
assertDescription(@"a collection containing \"a\"", hasItem(@"a"));
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
assertNoMismatchDescription(hasItem(@"a"), ([NSSet setWithObjects:@"a", @"b", nil]));
}
- (void)testMismatchDescriptionShowsActualArgument
{
assertMismatchDescription(@"was \"bad\"", hasItem(@"a"), @"bad");
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"was \"bad\"", hasItem(@"a"), @"bad");
}
@end
#pragma mark -
@interface IsCollectionContainingItemsTest : AbstractMatcherTest
@end
@implementation IsCollectionContainingItemsTest
- (void)testShouldMatchCollectionContainingAllItems
{
assertMatches(@"contains all items",
(hasItems(equalTo(@"a"), equalTo(@"b"), equalTo(@"c"), nil)),
([NSArray arrayWithObjects:@"a", @"b", @"c", nil]));
}
- (void)testProvidesConvenientShortcutForMatchingWithEqualTo
{
assertMatches(@"contains all items",
(hasItems(@"a", @"b", @"c", nil)),
([NSArray arrayWithObjects:@"a", @"b", @"c", nil]));
}
- (void)testShouldMatchCollectionContainingAllItemsInDifferentOrder
{
assertMatches(@"all items in different order",
(hasItems(@"a", @"b", @"c", nil)),
([NSArray arrayWithObjects:@"c", @"b", @"a", nil]));
}
- (void)testShouldMatchCollectionContainingAllItemsPlusExtras
{
assertMatches(@"all items plus extras",
(hasItems(@"a", @"b", @"c", nil)),
([NSArray arrayWithObjects:@"e", @"c", @"b", @"a", @"d", nil]));
}
- (void)testNoMatchIfCollectionDoesNotSatisfyAllMatchers
{
assertDoesNotMatch(@"missing 'a'",
(hasItems(equalTo(@"a"), equalTo(@"b"), equalTo(@"c"), nil)),
([NSArray arrayWithObjects:@"e", @"c", @"b", @"d", nil]));
}
- (void)testMatcherCreationRequiresNonNilArgument
{
STAssertThrows(hasItems(nil), @"Should require non-nil list");
}
- (void)testHasAReadableDescription
{
assertDescription(@"(a collection containing \"a\" and a collection containing \"b\")",
hasItems(@"a", @"b", nil));
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
assertNoMismatchDescription(hasItems(@"a", @"b", nil),
([NSSet setWithObjects:@"a", @"b", nil]));
}
- (void)testMismatchDescriptionShowsFirstUnmetMatcherAndActualArgument
{
assertMismatchDescription(@"a collection containing \"a\" was \"bad\"",
hasItems(@"a", @"b", nil), @"bad");
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"a collection containing \"a\" was \"bad\"",
hasItems(@"a", @"b", nil), @"bad");
}
@end
@@ -0,0 +1,94 @@
//
// OCHamcrest - IsCollectionOnlyContainingTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCIsCollectionOnlyContaining.h>
// Collaborators
#import <OCHamcrest/HCIsEqual.h>
#import <OCHamcrest/HCOrderingComparison.h>
// Test support
#import "AbstractMatcherTest.h"
@interface IsCollectionOnlyContainingTest : AbstractMatcherTest
@end
@implementation IsCollectionOnlyContainingTest
- (id<HCMatcher>)createMatcher
{
return onlyContains(equalTo(@"irrelevant"), nil);
}
- (void)testMatchesSingletonCollection
{
assertMatches(@"singleton collection",
onlyContains(equalTo(@"a"), nil),
([NSSet setWithObject:@"a"]));
}
- (void)testMatchesAllItemsWithOneMatcher
{
assertMatches(@"one matcher",
onlyContains(lessThan(@"d"), nil),
([NSArray arrayWithObjects:@"a", @"b", @"c", nil]));
}
- (void)testMatchesAllItemsWithMultipleMatchers
{
assertMatches(@"multiple matcher",
onlyContains(lessThan(@"d"), equalTo(@"hi"), nil),
([NSArray arrayWithObjects:@"a", @"hi", @"b", @"c", nil]));
}
- (void)testProvidesConvenientShortcutForMatchingWithEqualTo
{
assertMatches(@"Values automatically wrapped with equal_to",
onlyContains(lessThan(@"d"), @"hi", nil),
([NSArray arrayWithObjects:@"a", @"hi", @"b", @"c", nil]));
}
- (void)testDoesNotMatchCollectionWithMismatchingItem
{
assertDoesNotMatch(@"d is not less than d",
onlyContains(lessThan(@"d"), nil),
([NSArray arrayWithObjects:@"b", @"c", @"d", nil]));
}
- (void)testDoesNotMatchEmptyCollection
{
assertDoesNotMatch(@"empty collection", onlyContains(equalTo(@"foo"), nil), (@[]));
}
- (void)testMatcherCreationRequiresNonNilArgument
{
STAssertThrows(onlyContains(nil), @"Should require non-nil list");
}
- (void)testHasAReadableDescription
{
assertDescription(@"a collection containing items matching (\"a\" or \"b\")",
onlyContains(@"a", @"b", nil));
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"was \"bad\"", (onlyContains(@"a", @"b", nil)), @"bad");
}
- (void)testDescribeMismatchOfNonCollection
{
assertDescribeMismatch(@"was nil", (onlyContains(@"a", @"b", nil)), nil);
}
@end
@@ -0,0 +1,135 @@
//
// OCHamcrest - IsDictionaryContainingEntriesTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCIsDictionaryContainingEntries.h>
// Collaborators
#import <OCHamcrest/HCIsAnything.h>
#import <OCHamcrest/HCIsEqual.h>
// Test support
#import "AbstractMatcherTest.h"
@interface IsDictionaryContainingEntriesTest : AbstractMatcherTest
@end
@implementation IsDictionaryContainingEntriesTest
- (id<HCMatcher>)createMatcher
{
return hasEntries(@"irrelevant", @"irrelevant", nil);
}
- (void)testMatcherCreationRequiresEvenNumberOfArgs
{
STAssertThrows(hasEntries(@"a", nil), @"Should require pairs of arguments");
}
- (void)testDoesNotMatchNonDictionary
{
id object = [[NSObject alloc] init];
assertDoesNotMatch(@"not dictionary", hasEntries(@"a", equalTo(@"1"), nil), object);
}
- (void)testMatchesDictionaryContainingSingleKeyWithMatchingValue
{
NSDictionary *dict = @{@"a": @"1",
@"b": @"2"};
assertMatches(@"has a:1", hasEntries(@"a", equalTo(@"1"), nil), dict);
assertMatches(@"has b:2", hasEntries(@"b", equalTo(@"2"), nil), dict);
assertDoesNotMatch(@"no b:3", hasEntries(@"b", equalTo(@"3"), nil), dict);
assertDoesNotMatch(@"no c:2", hasEntries(@"c", equalTo(@"2"), nil), dict);
}
- (void)testMatchesDictionaryContainingMultipleKeysWithMatchingValues
{
NSDictionary *dict = @{@"a": @"1",
@"b": @"2",
@"c": @"3"};
assertMatches(@"has a & b", hasEntries(@"a", equalTo(@"1"), @"b", equalTo(@"2"), nil), dict);
assertMatches(@"has c & a", hasEntries(@"c", equalTo(@"3"), @"a", equalTo(@"1"), nil), dict);
assertDoesNotMatch(@"no d:3", hasEntries(@"d", equalTo(@"3"), nil), dict);
}
- (void)testProvidesConvenientShortcutForMatchingWithEqualTo
{
NSDictionary *dict = @{@"a": @"1",
@"b": @"2",
@"c": @"3"};
assertMatches(@"has a & b", hasEntries(@"a", @"1", @"b", @"2", nil), dict);
assertMatches(@"has c & a", hasEntries(@"c", @"3", @"a", @"1", nil), dict);
assertDoesNotMatch(@"no d:3", hasEntries(@"d", @"3", nil), dict);
}
- (void)testShouldNotMatchNil
{
assertDoesNotMatch(@"nil", hasEntries(@"a", @"1", nil), nil);
}
- (void)testMatcherCreationRequiresNonNilArguments
{
STAssertThrows(hasEntries(nil, @"value", nil), @"Should require non-nil argument");
STAssertThrows(hasEntries(@"key", nil, nil), @"Should require non-nil argument");
}
- (void)testHasReadableDescription
{
assertDescription(@"a dictionary containing { \"a\" = \"1\"; \"b\" = \"2\"; }",
hasEntries(@"a", @"1", @"b", @"2", nil));
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
NSDictionary *dict = @{@"a": @"1"};
assertNoMismatchDescription(hasEntries(@"a", @"1", nil), dict);
}
- (void)testMismatchDescriptionOfNonDictionaryShowsActualArgument
{
assertMismatchDescription(@"was \"bad\"", hasEntries(@"a", @"1", nil), @"bad");
}
- (void)testMismatchDescriptionOfDictionaryWithoutKey
{
NSDictionary *dict = @{@"a": @"1", @"c": @"3"};
assertMismatchDescription(@"no \"b\" key in <{\n a = 1;\n c = 3;\n}>",
hasEntries(@"a", @"1", @"b", @"2", nil), dict);
}
- (void)testMismatchDescriptionOfDictionaryWithNonMatchingValue
{
NSDictionary *dict = @{@"a": @"2"};
assertMismatchDescription(@"value for \"a\" was \"2\"", hasEntries(@"a", @"1", nil), dict);
}
- (void)testDescribeMismatchOfNonDictionaryShowsActualArgument
{
assertDescribeMismatch(@"was \"bad\"", hasEntries(@"a", @"1", nil), @"bad");
}
- (void)testDescribeMismatchOfDictionaryWithoutKey
{
NSDictionary *dict = @{@"a": @"1", @"c": @"3"};
assertDescribeMismatch(@"no \"b\" key in <{\n a = 1;\n c = 3;\n}>",
hasEntries(@"a", @"1", @"b", @"2", nil), dict);
}
- (void)testDescribeMismatchOfDictionaryWithNonMatchingValue
{
NSDictionary *dict = @{@"a": @"2"};
assertDescribeMismatch(@"value for \"a\" was \"2\"", hasEntries(@"a", @"1", nil), dict);
}
@end
@@ -0,0 +1,97 @@
//
// OCHamcrest - IsDictionaryContainingKeyTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCIsDictionaryContainingKey.h>
// Collaborators
#import <OCHamcrest/HCIsEqual.h>
// Test support
#import "AbstractMatcherTest.h"
@interface IsDictionaryContainingKeyTest : AbstractMatcherTest
@end
@implementation IsDictionaryContainingKeyTest
- (id<HCMatcher>)createMatcher
{
return hasKey(@"irrelevant");
}
- (void)testMatchesSingletonDictionaryContainingKey
{
NSDictionary *dict = @{@"a": @"1"};
assertMatches(@"Matches single key", hasKey(equalTo(@"a")), dict);
}
- (void)testMatchesDictionaryContainingKey
{
NSDictionary *dict = @{@"a": @"1",
@"b": @"2",
@"c": @"3"};
assertMatches(@"Matches a", hasKey(equalTo(@"a")), dict);
assertMatches(@"Matches c", hasKey(equalTo(@"c")), dict);
}
- (void)testProvidesConvenientShortcutForMatchingWithEqualTo
{
NSDictionary *dict = @{@"a": @"1",
@"b": @"2",
@"c": @"3"};
assertMatches(@"Matches c", hasKey(@"c"), dict);
}
- (void)testDoesNotMatchEmptyDictionary
{
assertDoesNotMatch(@"empty", hasKey(@"Foo"), @{});
}
- (void)testDoesNotMatchDictionaryMissingKey
{
NSDictionary *dict = @{@"a": @"1",
@"b": @"2",
@"c": @"3"};
assertDoesNotMatch(@"no matching key", hasKey(@"d"), dict);
}
- (void)testMatcherCreationRequiresNonNilArgument
{
STAssertThrows(hasKey(nil), @"Should require non-nil argument");
}
- (void)testHasReadableDescription
{
assertDescription(@"a dictionary containing key \"a\"", hasKey(@"a"));
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
NSDictionary *dict = @{@"a": @"1"};
assertNoMismatchDescription(hasKey(@"a"), dict);
}
- (void)testMismatchDescriptionShowsActualArgument
{
assertMismatchDescription(@"was \"bad\"", hasKey(@"a"), @"bad");
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"was \"bad\"", hasKey(@"a"), @"bad");
}
@end
@@ -0,0 +1,84 @@
//
// OCHamcrest - IsDictionaryContainingTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCIsDictionaryContaining.h>
// Collaborators
#import <OCHamcrest/HCIsAnything.h>
#import <OCHamcrest/HCIsEqual.h>
// Test support
#import "AbstractMatcherTest.h"
@interface IsDictionaryContainingTest : AbstractMatcherTest
@end
@implementation IsDictionaryContainingTest
- (id<HCMatcher>)createMatcher
{
return hasEntry(@"irrelevant", @"irrelevant");
}
- (void)testMatchesDictionaryContainingMatchingKeyAndValue
{
NSDictionary *dict = @{@"a": @"1",
@"b": @"2"};
assertMatches(@"has a:1", hasEntry(equalTo(@"a"), equalTo(@"1")), dict);
assertMatches(@"has b:2", hasEntry(equalTo(@"b"), equalTo(@"2")), dict);
assertDoesNotMatch(@"no c:3", hasEntry(equalTo(@"c"), equalTo(@"3")), dict);
}
- (void)testProvidesConvenientShortcutForMatchingWithEqualTo
{
NSDictionary *dict = @{@"a": @"1",
@"b": @"2"};
assertMatches(@"has a:1", hasEntry(@"a", equalTo(@"1")), dict);
assertMatches(@"has b:2", hasEntry(equalTo(@"b"), @"2"), dict);
assertDoesNotMatch(@"no c:3", hasEntry(@"c", @"3"), dict);
}
- (void)testShouldNotMatchNil
{
assertDoesNotMatch(@"nil", hasEntry(anything(), anything()), nil);
}
- (void)testMatcherCreationRequiresNonNilArguments
{
STAssertThrows(hasEntry(nil, @"value"), @"Should require non-nil argument");
STAssertThrows(hasEntry(@"key", nil), @"Should require non-nil argument");
}
- (void)testHasReadableDescription
{
assertDescription(@"a dictionary containing { \"a\" = \"1\"; }", hasEntry(@"a", @"1"));
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
NSDictionary *dict = @{@"a": @"1"};
assertNoMismatchDescription(hasEntry(@"a", @"1"), dict);
}
- (void)testMismatchDescriptionShowsActualArgument
{
assertMismatchDescription(@"was \"bad\"", hasEntry(@"a", @"1"), @"bad");
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"was \"bad\"", hasEntry(@"a", @"1"), @"bad");
}
@end
@@ -0,0 +1,97 @@
//
// OCHamcrest - IsDictionaryContainingValueTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCIsDictionaryContainingValue.h>
// Collaborators
#import <OCHamcrest/HCIsEqual.h>
// Test support
#import "AbstractMatcherTest.h"
@interface IsDictionaryContainingValueTest : AbstractMatcherTest
@end
@implementation IsDictionaryContainingValueTest
- (id<HCMatcher>)createMatcher
{
return hasValue(@"irrelevant");
}
- (void)testMatchesSingletonDictionaryContainingValue
{
NSDictionary *dict = @{@"a": @"1"};
assertMatches(@"same single key", hasValue(equalTo(@"1")), dict);
}
- (void)testMatchesDictionaryContainingValue
{
NSDictionary *dict = @{@"a": @"1",
@"b": @"2",
@"c": @"3"};
assertMatches(@"Matches 1", hasValue(equalTo(@"1")), dict);
assertMatches(@"Matches 3", hasValue(equalTo(@"3")), dict);
}
- (void)testProvidesConvenientShortcutForMatchingWithEqualTo
{
NSDictionary *dict = @{@"a": @"1",
@"b": @"2",
@"c": @"3"};
assertMatches(@"Matches 3", hasValue(@"3"), dict);
}
- (void)testDoesNotMatchEmptyDictionary
{
assertDoesNotMatch(@"Empty dictionary", hasValue(@"Foo"), @{});
}
- (void)testDoesNotMatchDictionaryMissingValue
{
NSDictionary *dict = @{@"a": @"1",
@"b": @"2",
@"c": @"3"};
assertDoesNotMatch(@"no matching value", hasValue(@"4"), dict);
}
- (void)testMatcherCreationRequiresNonNilArgument
{
STAssertThrows(hasValue(nil), @"Should require non-nil argument");
}
- (void)testHasReadableDescription
{
assertDescription(@"a dictionary containing value \"a\"", hasValue(@"a"));
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
NSDictionary *dict = @{@"a": @"1"};
assertNoMismatchDescription(hasValue(@"1"), dict);
}
- (void)testMismatchDescriptionShowsActualArgument
{
assertMismatchDescription(@"was \"bad\"", hasValue(@"1"), @"bad");
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"was \"bad\"", hasValue(@"1"), @"bad");
}
@end
@@ -0,0 +1,61 @@
//
// OCHamcrest - IsEmptyCollectionTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCIsEmptyCollection.h>
// Test support
#import "AbstractMatcherTest.h"
#import "FakeWithCount.h"
#import "FakeWithoutCount.h"
@interface IsEmptyCollectionTest : AbstractMatcherTest
@end
@implementation IsEmptyCollectionTest
- (id<HCMatcher>)createMatcher
{
return empty();
}
- (void)testMatchesEmptyCollection
{
assertMatches(@"empty", empty(), [FakeWithCount fakeWithCount:0]);
}
- (void)testDoesNotMatchesNonEmptyCollection
{
assertDoesNotMatch(@"non-empty", empty(), [FakeWithCount fakeWithCount:1]);
}
- (void)testDoesNotMatchItemWithoutCount
{
assertDoesNotMatch(@"no count", empty(), [FakeWithoutCount fake]);
}
- (void)testHasReadableDescription
{
assertDescription(@"empty collection", empty());
}
- (void)testMismatchDescriptionShowsActualArgument
{
assertMismatchDescription(@"was \"bad\"", empty(), @"bad");
}
- (void)testDescribesMismatch
{
assertDescribeMismatch(@"was <FakeWithCount>", empty(), [FakeWithCount fakeWithCount:1]);
}
@end
@@ -0,0 +1,69 @@
//
// OCHamcrest - IsInTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCIsIn.h>
// Test support
#import "AbstractMatcherTest.h"
@interface IsInTest : AbstractMatcherTest
@end
@implementation IsInTest
- (id<HCMatcher>)createMatcher
{
NSArray *collection = @[@"a", @"b", @"c"];
return isIn(collection);
}
- (void)testReturnsTrueIfArgumentIsInCollection
{
NSArray *collection = @[@"a", @"b", @"c"];
id<HCMatcher> matcher = isIn(collection);
assertMatches(@"has a", matcher, @"a");
assertMatches(@"has b", matcher, @"b");
assertMatches(@"has c", matcher, @"c");
assertDoesNotMatch(@"no d", matcher, @"d");
}
- (void)testMatcherCreationRequiresObjectWithContainsObjectMethod
{
id object = [[NSObject alloc] init];
STAssertThrows(isIn(object), @"object does not have -containsObject: method");
}
- (void)testMatcherCreationRequiresNonNilArgument
{
STAssertThrows(isIn(nil), @"Should require non-nil argument");
}
- (void)testHasReadableDescription
{
id<HCMatcher> matcher = isIn(@[@"a", @"b", @"c"]);
assertDescription(@"one of {\"a\", \"b\", \"c\"}", matcher);
}
- (void)testMismatchDescriptionShowsActualArgument
{
assertMismatchDescription(@"was \"bad\"", isIn(@[@"a"]), @"bad");
}
- (void)testDescribesMismatch
{
assertDescribeMismatch(@"was \"bad\"", isIn(@[@"a"]), @"bad");
}
@end
@@ -0,0 +1,51 @@
//
// OCHamcrest - AbstractMatcherTest.h
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
#import <SenTestingKit/SenTestingKit.h>
@protocol HCMatcher;
@interface AbstractMatcherTest : SenTestCase
- (void)assertTrue:(BOOL)condition message:(NSString *)message
inFile:(const char *)fileName atLine:(int)lineNumber;
- (void)assertFalse:(BOOL)condition message:(NSString *)message
inFile:(const char *)fileName atLine:(int)lineNumber;
- (void)assertMatcher:(id<HCMatcher>)matcher hasTheDescription:(NSString *)expected
inFile:(const char *)fileName atLine:(int)lineNumber;
- (void)assertMatcher:(id<HCMatcher>)matcher hasNoMismatchDescriptionFor:(id)arg
inFile:(const char *)fileName atLine:(int)lineNumber;
- (void)assertMatcher:(id<HCMatcher>)matcher matching:(id)arg yieldsMismatchDescription:(NSString *)expected
inFile:(const char *)fileName atLine:(int)lineNumber;
- (void)assertMatcher:(id<HCMatcher>)matcher matching:(id)arg describesMismatch:(NSString *)expected
inFile:(const char *)fileName atLine:(int)lineNumber;
- (id<HCMatcher>)createMatcher;
@end
#define assertMatches(aMessage, matcher, arg) \
[self assertTrue:[matcher matches:arg] message:aMessage inFile:__FILE__ atLine:__LINE__]
#define assertDoesNotMatch(aMessage, matcher, arg) \
[self assertFalse:[matcher matches:arg] message:aMessage inFile:__FILE__ atLine:__LINE__]
#define assertDescription(expected, matcher) \
[self assertMatcher:matcher hasTheDescription:expected inFile:__FILE__ atLine:__LINE__]
#define assertNoMismatchDescription(matcher, arg) \
[self assertMatcher:matcher hasNoMismatchDescriptionFor:arg inFile:__FILE__ atLine:__LINE__]
#define assertMismatchDescription(expected, matcher, arg) \
[self assertMatcher:matcher matching:arg yieldsMismatchDescription:expected inFile:__FILE__ atLine:__LINE__]
#define assertDescribeMismatch(expected, matcher, arg) \
[self assertMatcher:matcher matching:arg describesMismatch:expected inFile:__FILE__ atLine:__LINE__]
@@ -0,0 +1,133 @@
//
// OCHamcrest - AbstractMatcherTest.h
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
#import "AbstractMatcherTest.h"
#import <OCHamcrest/HCMatcher.h>
#import <OCHamcrest/HCStringDescription.h>
@implementation AbstractMatcherTest
- (void)assertTrue:(BOOL)condition message:(NSString *)message
inFile:(const char *)fileName atLine:(int)lineNumber
{
if (!condition)
{
[self failWithException:[NSException failureInFile:@(fileName)
atLine:lineNumber
withDescription:message]];
}
}
- (void)assertFalse:(BOOL)condition message:(NSString *)message
inFile:(const char *)fileName atLine:(int)lineNumber
{
if (condition)
{
[self failWithException:[NSException failureInFile:@(fileName)
atLine:lineNumber
withDescription:message]];
}
}
- (void)assertMatcher:(id<HCMatcher>)matcher hasTheDescription:(NSString *)expected
inFile:(const char *)fileName atLine:(int)lineNumber
{
HCStringDescription *description = [HCStringDescription stringDescription];
[description appendDescriptionOf:matcher];
NSString *actual = [description description];
if (![actual isEqualToString:expected])
{
[self failWithException:
[NSException failureInEqualityBetweenObject:actual
andObject:expected
inFile:@(fileName)
atLine:lineNumber
withDescription:@"Expected description"]];
}
}
- (void)assertMatcher:(id<HCMatcher>)matcher hasNoMismatchDescriptionFor:(id)arg
inFile:(const char *)fileName atLine:(int)lineNumber
{
HCStringDescription *description = [HCStringDescription stringDescription];
BOOL result = [matcher matches:arg];
if (!result)
{
[self failWithException:[NSException failureInFile:@(fileName)
atLine:lineNumber
withDescription:@"Precondition: Matcher should match item"]];
}
if ([[description description] length] != 0)
{
[self failWithException:[NSException failureInFile:@(fileName)
atLine:lineNumber
withDescription:@"Expected no mismatch description"]];
}
}
- (void)assertMatcher:(id<HCMatcher>)matcher matching:(id)arg yieldsMismatchDescription:(NSString *)expected
inFile:(const char *)fileName atLine:(int)lineNumber
{
HCStringDescription *description = [HCStringDescription stringDescription];
BOOL result = [matcher matches:arg describingMismatchTo:description];
if (result)
{
[self failWithException:[NSException failureInFile:@(fileName)
atLine:lineNumber
withDescription:@"Precondition: Matcher should not match item"]];
}
NSString *actual = [description description];
if (![actual isEqualToString:expected])
{
[self failWithException:
[NSException failureInEqualityBetweenObject:actual
andObject:expected
inFile:@(fileName)
atLine:lineNumber
withDescription:@"Expected mismatch description"]];
}
}
- (void)assertMatcher:(id<HCMatcher>)matcher matching:(id)arg describesMismatch:(NSString *)expected
inFile:(const char *)fileName atLine:(int)lineNumber
{
HCStringDescription *description = [HCStringDescription stringDescription];
[matcher describeMismatchOf:arg to:description];
NSString *actual = [description description];
if (![actual isEqualToString:expected])
{
[self failWithException:
[NSException failureInEqualityBetweenObject:actual
andObject:expected
inFile:@(fileName)
atLine:lineNumber
withDescription:@"Expected mismatch description"]];
}
}
- (id<HCMatcher>)createMatcher
{
return nil; // Override in subclass
}
- (void)testIsNilSafe
{
// Should not crash or throw exception.
[[self createMatcher] matches:nil];
}
- (void)testCopesWithUnknownTypes
{
// Should not crash or throw exception.
[[self createMatcher] matches:[[NSObject alloc] init]];
}
@end
@@ -0,0 +1,131 @@
//
// OCHamcrest - AssertThatTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Module under test
#define HC_SHORTHAND
#import <OCHamcrest/HCAssertThat.h>
// Collaborators
#import <OCHamcrest/HCIsEqual.h>
// Test support
#import <SenTestingKit/SenTestingKit.h>
@interface MockTestRun : SenTestCaseRun
{
// Can't use original attributes because they're declared @private.
unsigned int myFailureCount;
unsigned int myUnexpectedExceptionCount;
}
@end
@implementation MockTestRun
- (void)addException:(NSException *) anException
{
if ([[anException name] isEqualToString:SenTestFailureException])
++myFailureCount;
else
++myUnexpectedExceptionCount;
}
- (unsigned int)failureCount
{
return myFailureCount;
}
- (unsigned int)unexpectedExceptionCount
{
return myUnexpectedExceptionCount;
}
- (void)stop
{
// Override to avoid posting notification.
}
@end
#pragma mark -
@interface QuietTest : SenTestCase
@end
@implementation QuietTest
- (Class)testRunClass
{
return [MockTestRun class];
}
- (void)twoFailingAssertions
{
assertThat(@"1", equalTo(@"2"));
assertThat(@"3", equalTo(@"4"));
}
@end
#pragma mark -
@interface AssertThatTest : SenTestCase
@end
@implementation AssertThatTest
- (void)testShouldBeSilentOnSuccessfulMatch
{
assertThat(@"foo", equalTo(@"foo"));
}
- (void)testAssertionErrorShouldDescribeExpectedAndActual
{
NSString *expected = @"EXPECTED";
NSString *actual = @"ACTUAL";
NSString *expectedMessage = @"Expected \"EXPECTED\", but was \"ACTUAL\"";
@try
{
[self raiseAfterFailure];
assertThat(actual, equalTo(expected));
}
@catch (NSException* exception)
{
STAssertTrue([[exception reason] rangeOfString:expectedMessage].location != NSNotFound, nil);
return;
}
STFail(@"should have failed");
}
- (void)testAssertionRecordingAllErrors
{
QuietTest *testCase = [QuietTest testCaseWithSelector:@selector(twoFailingAssertions)];
[testCase continueAfterFailure]; // Default behavior of OCUnit
MockTestRun *testRun = (MockTestRun *)[testCase run];
STAssertEquals([testRun failureCount], 2U, nil);
STAssertEquals([testRun unexpectedExceptionCount], 0U, nil);
}
- (void)testAssertionStoppingAtFirstError
{
QuietTest *testCase = [QuietTest testCaseWithSelector:@selector(twoFailingAssertions)];
[testCase raiseAfterFailure]; // Change behavior
MockTestRun *testRun = (MockTestRun *)[testCase run];
STAssertEquals([testRun failureCount], 1U, nil);
STAssertEquals([testRun unexpectedExceptionCount], 0U, nil);
}
@end
@@ -0,0 +1,86 @@
//
// OCHamcrest - BaseMatcherTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#import <OCHamcrest/HCBaseMatcher.h>
// Collaborators
#import <OCHamcrest/HCDescription.h>
// Test support
#import "AbstractMatcherTest.h"
@interface TestingBaseMatcher : HCBaseMatcher
@end
@implementation TestingBaseMatcher
- (void)describeTo:(id<HCDescription>)description
{
[description appendText:@"SOME DESCRIPTION"];
}
@end
@interface BaseMatcherTest : AbstractMatcherTest
@end
@implementation BaseMatcherTest
- (void)testDescriptionShouldDescribeMatcher
{
TestingBaseMatcher *matcher = [[TestingBaseMatcher alloc] init];
STAssertEqualObjects([matcher description], @"SOME DESCRIPTION", nil);
}
@end
#pragma mark -
@interface IncompleteBaseMatcher : HCBaseMatcher
@end
@implementation IncompleteBaseMatcher
@end
@interface IncompleteMatcherTest : AbstractMatcherTest
@end
@implementation IncompleteMatcherTest
{
IncompleteBaseMatcher *matcher;
}
- (void)setUp
{
[super setUp];
matcher = [[IncompleteBaseMatcher alloc] init];
}
- (void)tearDown
{
matcher = nil;
[super tearDown];
}
- (void)testSubclassShouldBeRequiredToDefineMatchesMethod
{
STAssertThrows([matcher matches:nil], nil);
}
- (void)testSubclassShouldBeRequiredToDefineDescribeToMethod
{
STAssertThrows([matcher describeTo:nil], nil);
}
@end
@@ -0,0 +1,152 @@
//
// OCHamcrest - InvocationMatcherTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCInvocationMatcher.h>
// Collaborators
#import <OCHamcrest/HCDescription.h>
#import <OCHamcrest/HCIsEqual.h>
// Test support
#import "AbstractMatcherTest.h"
@interface Match : HCIsEqual
@end
@implementation Match
+ (Match *)matches:(id)arg
{
return [[Match alloc] initEqualTo:arg];
}
- (void)describeMismatchOf:(id)item to:(id<HCDescription>)description
{
[description appendText:@"MISMATCH"];
}
@end
#pragma mark -
@interface Thingy : NSObject
@end
@implementation Thingy
{
NSString *result;
}
- (id)initWithResult:(NSString *)aResult
{
self = [super init];
if (self)
result = aResult;
return self;
}
- (NSString *)description
{
return @"Thingy";
}
- (NSString *)result
{
return result;
}
+ (Thingy*) thingyWithResult:(NSString *)result
{
return [[Thingy alloc] initWithResult:result];
}
@end
#pragma mark -
@interface ShouldNotMatch : NSObject
@end
@implementation ShouldNotMatch
- (NSString *)description
{
return @"ShouldNotMatch";
}
@end
#pragma mark -
@interface InvocationMatcherTest : AbstractMatcherTest
@end
@implementation InvocationMatcherTest
{
HCInvocationMatcher *resultMatcher;
}
- (void)setUp
{
[super setUp];
NSInvocation *invocation = [HCInvocationMatcher invocationForSelector:@selector(result)
onClass:[Thingy class]];
resultMatcher = [[HCInvocationMatcher alloc] initWithInvocation:invocation
matching:[Match matches:@"bar"]];
}
- (void)tearDown
{
resultMatcher = nil;
[super tearDown];
}
- (void)testMatchesFeature
{
assertMatches(@"invoke on Thingy", resultMatcher, [Thingy thingyWithResult:@"bar"]);
assertDescription(@"an object with result \"bar\"", resultMatcher);
}
- (void)testMismatchWithDefaultLongDescription
{
assertMismatchDescription(@"<Thingy> result MISMATCH", resultMatcher,
[Thingy thingyWithResult:@"foo"]);
}
- (void)testMismatchWithShortDescription
{
[resultMatcher setShortMismatchDescription:YES];
assertMismatchDescription(@"MISMATCH", resultMatcher,
[Thingy thingyWithResult:@"foo"]);
}
- (void)testDoesNotMatchNil
{
assertMismatchDescription(@"was nil", resultMatcher, nil);
}
- (void)testDoesNotMatchObjectWithoutMethod
{
assertDoesNotMatch(@"was <ShouldNotMatch>", resultMatcher, [[ShouldNotMatch alloc] init]);
}
- (void)testObjectWithoutMethodShortDescriptionIsSameAsLongForm
{
[resultMatcher setShortMismatchDescription:YES];
assertDoesNotMatch(@"was <ShouldNotMatch>", resultMatcher, [[ShouldNotMatch alloc] init]);
}
@end
@@ -0,0 +1,195 @@
//
// OCHamcrest - StringDescriptionTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#import <OCHamcrest/HCStringDescription.h>
// Collaborators
#import <OCHamcrest/HCSelfDescribing.h>
// Test support
#import <SenTestingKit/SenTestingKit.h>
@interface FakeSelfDescribing : NSObject <HCSelfDescribing>
@end
@implementation FakeSelfDescribing
- (void)describeTo:(id<HCDescription>)description
{
[description appendText:@"DESCRIPTION"];
}
@end
#pragma mark -
@interface ObjectDescriptionWithLessThan : NSObject
@end
@implementation ObjectDescriptionWithLessThan
- (NSString *)description
{
return @"< is less than";
}
@end
#pragma mark -
@interface ObjectWithNilDescription : NSObject
@end
@implementation ObjectWithNilDescription
- (NSString *)description
{
return nil;
}
@end
#pragma mark -
@interface StringDescriptionTest : SenTestCase
@end
@implementation StringDescriptionTest
{
HCStringDescription *description;
}
- (void)setUp
{
[super setUp];
description = [[HCStringDescription alloc] init];
}
- (void)tearDown
{
description = nil;
[super tearDown];
}
- (void)testDescribesNil
{
[description appendDescriptionOf:nil];
STAssertEqualObjects([description description], @"nil", nil);
}
- (void)testLetsSelfDescribingObjectDescribeItself
{
[description appendDescriptionOf:[[FakeSelfDescribing alloc] init]];
STAssertEqualObjects([description description], @"DESCRIPTION", nil);
}
- (void)testDescribesStringInQuotes
{
[description appendDescriptionOf:@"FOO"];
STAssertEqualObjects([description description], @"\"FOO\"", nil);
}
- (void)testDescriptionOfStringWithQuotesShouldExpandToCSyntax
{
[description appendDescriptionOf:@"a\"b"];
STAssertEqualObjects([description description], @"\"a\\\"b\"", nil);
}
- (void)testDescriptionOfStringWithNewlineShouldExpandToCSyntax
{
[description appendDescriptionOf:@"a\nb"];
STAssertEqualObjects([description description], @"\"a\\nb\"", nil);
}
- (void)testDescriptionOfStringWithCarriageReturnShouldExpandToCSyntax
{
[description appendDescriptionOf:@"a\rb"];
STAssertEqualObjects([description description], @"\"a\\rb\"", nil);
}
- (void)testDescriptionOfStringWithTabShouldExpandToCSyntax
{
[description appendDescriptionOf:@"a\tb"];
STAssertEqualObjects([description description], @"\"a\\tb\"", nil);
}
- (void)testWrapsNonSelfDescribingObjectInAngleBrackets
{
[description appendDescriptionOf:@42];
STAssertEqualObjects([description description], @"<42>", nil);
}
- (void)testShouldNotAddAngleBracketsIfObjectDescriptionAlreadyHasThem
{
[description appendDescriptionOf:[[NSObject alloc] init]];
NSPredicate *expected = [NSPredicate predicateWithFormat:
@"SELF MATCHES '<NSObject: 0x[0-9a-fA-F]+>'"];
STAssertTrue([expected evaluateWithObject:[description description]], nil);
}
- (void)testWrapsNonSelfDescribingObjectInAngleBracketsIfItDoesNotEndInClosingBracket
{
ObjectDescriptionWithLessThan *lessThanDescription = [[ObjectDescriptionWithLessThan alloc] init];
[description appendDescriptionOf:lessThanDescription];
STAssertEqualObjects([description description], @"<< is less than>", nil);
}
- (void)testCanDescribeObjectWithNilDescription
{
[description appendDescriptionOf:[[ObjectWithNilDescription alloc] init]];
NSPredicate *expected = [NSPredicate predicateWithFormat:
@"SELF MATCHES '<ObjectWithNilDescription: 0x[0-9a-fA-F]+>'"];
STAssertTrue([expected evaluateWithObject:[description description]], nil);
}
- (void)testAppendListWithEmptyListShouldHaveStartAndEndOnly
{
[description appendList:@[]
start:@"["
separator:@","
end:@"]"];
STAssertEqualObjects([description description], @"[]", nil);
}
- (void)testAppendListWithOneItemShouldHaveStartItemAndEnd
{
[description appendList:@[@"a"]
start:@"["
separator:@","
end:@"]"];
STAssertEqualObjects([description description], @"[\"a\"]", nil);
}
- (void)testAppendListWithTwoItemsShouldHaveItemsWithSeparator
{
[description appendList:@[@"a", @"b"]
start:@"["
separator:@","
end:@"]"];
STAssertEqualObjects([description description], @"[\"a\",\"b\"]", nil);
}
@end
@@ -0,0 +1,107 @@
//
// OCHamcrest - DescribedAsTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCDescribedAs.h>
// Collaborators
#import <OCHamcrest/HCIsAnything.h>
// Test support
#import "AbstractMatcherTest.h"
#import "NeverMatch.h"
@interface DescribedAsTest : AbstractMatcherTest
@end
@implementation DescribedAsTest
- (id<HCMatcher>)createMatcher
{
return describedAs(@"irrelevant", anything(), nil);
}
- (void)testOverridesDescriptionOfNestedMatcherInitializerArgument
{
id<HCMatcher> m1 = describedAs(@"m1 description", anything(), nil);
id<HCMatcher> m2 = describedAs(@"m2 description", [NeverMatch neverMatch], nil);
assertDescription(@"m1 description", m1);
assertDescription(@"m2 description", m2);
}
- (void)testAppendsValuesToDescription
{
id<HCMatcher> m = describedAs(@"value 1 = %0, value 2 = %1",
anything(),
@33,
@97,
nil);
assertDescription(@"value 1 = <33>, value 2 = <97>", m);
}
- (void)testHandlesSubstitutionAtBeginning
{
id<HCMatcher> m = describedAs(@"%0ok",
anything(),
@33,
nil);
assertDescription(@"<33>ok", m);
}
- (void)testHandlesSubstitutionAtEnd
{
id<HCMatcher> m = describedAs(@"ok%0",
anything(),
@33,
nil);
assertDescription(@"ok<33>", m);
}
- (void)testDoesNotProcessPercentFollowedByNonDigit
{
id<HCMatcher> m = describedAs(@"With 33% remaining", anything(), nil);
assertDescription(@"With 33% remaining", m);
}
- (void)testDelegatesMatchingToNestedMatcher
{
id<HCMatcher> m1 = describedAs(@"m1 description", anything(), nil);
id<HCMatcher> m2 = describedAs(@"m2 description", [NeverMatch neverMatch], nil);
STAssertTrue([m1 matches:@"hi"], @"");
STAssertFalse([m2 matches:@"hi"], @"");
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
assertNoMismatchDescription(describedAs(@"irrelevant", anything(), nil), @"hi");
}
- (void)testDelegatesMismatchDescriptionToNestedMatcher
{
assertMismatchDescription([NeverMatch mismatchDescription],
describedAs(@"irrelevant", [NeverMatch neverMatch], nil),
@"hi");
}
- (void)testDelegatesDescribeMismatchToNestedMatcher
{
assertDescribeMismatch([NeverMatch mismatchDescription],
describedAs(@"irrelevant", [NeverMatch neverMatch], nil),
@"hi");
}
@end
@@ -0,0 +1,73 @@
//
// OCHamcrest - IsTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCIs.h>
// Collaborators
#import <OCHamcrest/HCIsEqual.h>
// Test support
#import "AbstractMatcherTest.h"
#import "NeverMatch.h"
@interface IsTest : AbstractMatcherTest
@end
@implementation IsTest
- (id<HCMatcher>)createMatcher
{
return is(@"something");
}
- (void)testDelegatesMatchingToNestedMatcher
{
assertMatches(@"should match", is(equalTo(@"A")), @"A");
assertMatches(@"should match", is(equalTo(@"B")), @"B");
assertDoesNotMatch(@"should not match", is(equalTo(@"A")), @"B");
assertDoesNotMatch(@"should not match", is(equalTo(@"B")), @"A");
}
- (void)testDescriptionShouldPassThrough
{
assertDescription(@"\"A\"", is(equalTo(@"A")));
}
- (void)testProvidesConvenientShortcutForIsEqualTo
{
assertMatches(@"should match", is(@"A"), @"A");
assertMatches(@"should match", is(@"B"), @"B");
assertDoesNotMatch(@"should not match", is(@"A"), @"B");
assertDoesNotMatch(@"should not match", is(@"B"), @"A");
assertDescription(@"\"A\"", is(@"A"));
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
assertNoMismatchDescription(is(@"A"), @"A");
}
- (void)testDelegatesMismatchDescriptionToNestedMatcher
{
assertMismatchDescription([NeverMatch mismatchDescription],
is([NeverMatch neverMatch]),
@"hi");
}
- (void)testDelegatesDescribeMismatchToNestedMatcher
{
assertDescribeMismatch([NeverMatch mismatchDescription],
is([NeverMatch neverMatch]),
@"hi");
}
@end
@@ -0,0 +1,20 @@
//
// OCHamcrest - NeverMatch.h
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
#import <OCHamcrest/HCBaseMatcher.h>
@interface NeverMatch : HCBaseMatcher
+ (id)neverMatch;
+ (NSString *)mismatchDescription;
- (BOOL)matches:(id)item;
- (void)describeMismatchOf:(id)item to:(id<HCDescription>)mismatchDescription;
@end
@@ -0,0 +1,37 @@
//
// OCHamcrest - NeverMatch.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
#import "NeverMatch.h"
#import "HCDescription.h"
@implementation NeverMatch
+ (id)neverMatch
{
return [[self alloc] init];
}
+ (NSString *)mismatchDescription
{
return @"NEVERMATCH";
}
- (BOOL)matches:(id)item
{
return NO;
}
- (void)describeMismatchOf:(id)item to:(id<HCDescription>)mismatchDescription
{
[mismatchDescription appendText:[NeverMatch mismatchDescription]];
}
@end
@@ -0,0 +1,103 @@
//
// OCHamcrest - AllOfTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCAllOf.h>
// Collaborators
#import <OCHamcrest/HCIsEqual.h>
// Test support
#import "AbstractMatcherTest.h"
@interface AllOfTest : AbstractMatcherTest
@end
@implementation AllOfTest
- (id<HCMatcher>)createMatcher
{
return allOf(equalTo(@"irrelevant"), equalTo(@"irrelevant"), nil);
}
- (void)testMatchesIfArgumentSatisfiesBothOfTwoOtherMatchers
{
assertMatches(@"both matchers", allOf(equalTo(@"good"), equalTo(@"good"), nil), @"good");
}
- (void)testProvidesConvenientShortcutForMatchingWithEqualTo
{
assertMatches(@"both matchers", allOf(@"good", @"good", nil), @"good");
}
- (void)testNoMatchIfArgumentFailsToSatisfyEitherOfTwoOtherMatchers
{
assertDoesNotMatch(@"first matcher", allOf(equalTo(@"bad"), equalTo(@"good"), nil), @"good");
assertDoesNotMatch(@"second matcher", allOf(equalTo(@"good"), equalTo(@"bad"), nil), @"good");
assertDoesNotMatch(@"either matcher", allOf(equalTo(@"bad"), equalTo(@"bad"), nil), @"good");
}
- (void)testMatchesIfArgumentSatisfiesAllOfManyOtherMatchers
{
assertMatches(@"all matchers",
allOf(equalTo(@"good"),
equalTo(@"good"),
equalTo(@"good"),
equalTo(@"good"),
equalTo(@"good"),
nil),
@"good");
}
- (void)testNoMatchIfArgumentFailsToSatisfyAllOfManyOtherMatchers
{
assertDoesNotMatch(@"matcher in the middle",
allOf(equalTo(@"good"),
equalTo(@"good"),
equalTo(@"bad"),
equalTo(@"good"),
equalTo(@"good"),
nil),
@"good");
}
- (void)testHasAReadableDescription
{
assertDescription(@"(\"good\" and \"bad\" and \"ugly\")",
allOf(equalTo(@"good"), equalTo(@"bad"), equalTo(@"ugly"), nil));
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
assertNoMismatchDescription(allOf(equalTo(@"good"), equalTo(@"good"), nil),
@"good");
}
- (void)testMismatchDescriptionDescribesFirstFailingMatch
{
assertMismatchDescription(@"\"good\" was \"bad\"",
allOf(equalTo(@"bad"), equalTo(@"good"), nil),
@"bad");
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"\"good\" was \"bad\"",
allOf(equalTo(@"bad"), equalTo(@"good"), nil),
@"bad");
}
- (void)testMatcherCreationRequiresNonNilArgument
{
STAssertThrows(allOf(nil), @"Should require non-nil list");
}
@end
@@ -0,0 +1,105 @@
//
// OCHamcrest - AnyOfTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCAnyOf.h>
// Collaborators
#import <OCHamcrest/HCIsEqual.h>
// Test support
#import "AbstractMatcherTest.h"
@interface AnyOfTest : AbstractMatcherTest
@end
@implementation AnyOfTest
- (id<HCMatcher>)createMatcher
{
return anyOf(equalTo(@"irrelevant"), nil);
}
- (void)testMatchesIfArgumentSatisfiesEitherOrBothOfTwoOtherMatchers
{
assertMatches(@"first matcher", anyOf(equalTo(@"good"), equalTo(@"bad"), nil), @"good");
assertMatches(@"second matcher", anyOf(equalTo(@"bad"), equalTo(@"good"), nil), @"good");
assertMatches(@"both matchers", anyOf(equalTo(@"good"), equalTo(@"good"), nil), @"good");
}
- (void)testProvidesConvenientShortcutForMatchingWithEqualTo
{
assertMatches(@"first matcher", anyOf(@"good", @"bad", nil), @"good");
assertMatches(@"second matcher", anyOf(@"bad", @"good", nil), @"good");
assertMatches(@"both matchers", anyOf(@"good", @"good", nil), @"good");
}
- (void)testNoMatchIfArgumentFailsToSatisfyEitherOfTwoOtherMatchers
{
assertDoesNotMatch(@"first matcher", anyOf(equalTo(@"bad"), equalTo(@"bad"), nil), @"good");
}
- (void)testMatchesIfArgumentSatisfiesAnyOfManyOtherMatchers
{
assertMatches(@"matcher in the middle",
anyOf(equalTo(@"bad"),
equalTo(@"bad"),
equalTo(@"good"),
equalTo(@"bad"),
equalTo(@"bad"),
nil),
@"good");
}
- (void)testNoMatchIfArgumentFailsToSatisfyAnyOfManyOtherMatchers
{
assertDoesNotMatch(@"all matchers",
anyOf(equalTo(@"bad"),
equalTo(@"bad"),
equalTo(@"bad"),
equalTo(@"bad"),
equalTo(@"bad"),
nil),
@"good");
}
- (void)testHasAReadableDescription
{
assertDescription(@"(\"good\" or \"bad\" or \"ugly\")",
anyOf(equalTo(@"good"), equalTo(@"bad"), equalTo(@"ugly"), nil));
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
assertNoMismatchDescription(anyOf(equalTo(@"good"), equalTo(@"good"), nil),
@"good");
}
- (void)testMismatchDescriptionDescribesFirstFailingMatch
{
assertMismatchDescription(@"was \"ugly\"",
anyOf(equalTo(@"bad"), equalTo(@"good"), nil),
@"ugly");
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"was \"ugly\"",
anyOf(equalTo(@"bad"), equalTo(@"good"), nil),
@"ugly");
}
- (void)testMatcherCreationRequiresNonNilArgument
{
STAssertThrows(anyOf(nil), @"Should require non-nil list");
}
@end
@@ -0,0 +1,51 @@
//
// OCHamcrest - IsAnythingTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCIsAnything.h>
// Test support
#import "AbstractMatcherTest.h"
@interface IsAnythingTest : AbstractMatcherTest
@end
@implementation IsAnythingTest
- (id<HCMatcher>)createMatcher
{
return anything();
}
- (void)testAlwaysEvaluatesToTrue
{
assertMatches(@"nil", anything(), nil);
assertMatches(@"object", anything(), [[NSObject alloc] init]);
assertMatches(@"string", anything(), @"hi");
}
- (void)testHasUsefulDefaultDescription
{
assertDescription(@"ANYTHING", anything());
}
- (void)testCanOverrideDescription
{
NSString *description = @"DESCRIPTION";
assertDescription(description, anythingWithDescription(description));
}
- (void)testMatchAlwaysSucceedsSoShouldNotGenerateMismatchDescription
{
assertNoMismatchDescription(anything(), @"hi");
}
@end
@@ -0,0 +1,63 @@
//
// OCHamcrest - IsNotTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCIsNot.h>
// Collaborators
#import <OCHamcrest/HCIsEqual.h>
// Test support
#import "AbstractMatcherTest.h"
@interface IsNotTest : AbstractMatcherTest
@end
@implementation IsNotTest
- (id<HCMatcher>)createMatcher
{
return isNot(@"something");
}
- (void)testEvaluatesToTheTheLogicalNegationOfAnotherMatcher
{
assertMatches(@"invert mismatch", isNot(equalTo(@"A")), @"B");
assertDoesNotMatch(@"invert match", isNot(equalTo(@"A")), @"A");
}
- (void)testProvidesConvenientShortcutForNotEqualTo
{
assertMatches(@"invert mismatch", isNot(@"A"), @"B");
assertDoesNotMatch(@"invert match", isNot(@"A"), @"A");
}
- (void)testHasAReadableDescription
{
assertDescription(@"not \"A\"", isNot(@"A"));
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
assertNoMismatchDescription(isNot(@"A"), @"B");
}
- (void)testMismatchDescriptionShowsActualArgument
{
assertMismatchDescription(@"was \"A\"", isNot(@"A"), @"A");
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"was \"A\"", isNot(@"A"), @"A");
}
@end
@@ -0,0 +1,81 @@
//
// OCHamcrest - IsCloseToTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCIsCloseTo.h>
// Test support
#import "AbstractMatcherTest.h"
@interface IsCloseToTest : AbstractMatcherTest
@end
@implementation IsCloseToTest
- (id<HCMatcher>)createMatcher
{
double irrelevant = 0.1;
return closeTo(irrelevant, irrelevant);
}
- (void)testEvaluatesToTrueIfArgumentIsEqualToADoubleValueWithinSomeError
{
id<HCMatcher> matcher = closeTo(1.0, 0.5);
assertMatches(@"equal", matcher, @1.0);
assertMatches(@"less but within delta", matcher, @0.5);
assertMatches(@"greater but within delta", matcher, @1.5);
assertDoesNotMatch(@"too small", matcher, @0.4);
assertDoesNotMatch(@"too big", matcher, @1.6);
}
- (void)testFailsIfMatchingAgainstNonNumber
{
id<HCMatcher> matcher = closeTo(1.0, 0.5);
assertDoesNotMatch(@"not a number", matcher, @"a");
assertDoesNotMatch(@"not a number", matcher, nil);
}
- (void)testHasAReadableDescription
{
assertDescription(@"a numeric value within <0.5> of <1>", closeTo(1.0, 0.5));
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
assertNoMismatchDescription(closeTo(1.0, 0.5), (@1.0));
}
- (void)testMismatchDescriptionShowsActualDeltaIfArgumentIsNumeric
{
assertMismatchDescription(@"<1.7> differed by <0.7>",
(closeTo(1.0, 0.5)), @1.7);
}
- (void)testMismatchDescriptionShowsActualArgumentIfNotNumeric
{
assertMismatchDescription(@"was \"bad\"", (closeTo(1.0, 0.5)), @"bad");
}
- (void)testDescribeMismatchShowsActualDeltaIfArgumentIsNumeric
{
assertDescribeMismatch(@"<1.7> differed by <0.7>",
(closeTo(1.0, 0.5)), @1.7);
}
- (void)testDescribeMismatchShowsActualArgumentIfNotNumeric
{
assertDescribeMismatch(@"was \"bad\"", (closeTo(1.0, 0.5)), @"bad");
}
@end
@@ -0,0 +1,390 @@
//
// OCHamcrest - IsEqualToNumberTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Module under test
#define HC_SHORTHAND
#import <OCHamcrest/HCIsEqualToNumber.h>
// Test support
#import "AbstractMatcherTest.h"
@interface IsEqualToBoolTest : AbstractMatcherTest
@end
@implementation IsEqualToBoolTest
- (id<HCMatcher>)createMatcher
{
BOOL irrelevant = NO;
return equalToBool(irrelevant);
}
- (void)testComparesWithNSNumber
{
assertMatches(@"Large BOOL", equalToBool(YES), [NSNumber numberWithBool:YES]);
assertMatches(@"Small BOOL", equalToBool(NO), [NSNumber numberWithBool:NO]);
}
- (void)testMismatchesDifferentNumber
{
assertDoesNotMatch(@"Different", equalToBool(YES), [NSNumber numberWithBool:NO]);
}
@end
@interface IsEqualToCharTest : AbstractMatcherTest
@end
@implementation IsEqualToCharTest
- (id<HCMatcher>)createMatcher
{
char irrelevant = 0;
return equalToChar(irrelevant);
}
- (void)testComparesWithNSNumber
{
assertMatches(@"Large char", equalToChar(CHAR_MAX), [NSNumber numberWithChar:CHAR_MAX]);
assertMatches(@"Small char", equalToChar(CHAR_MIN), [NSNumber numberWithChar:CHAR_MIN]);
}
- (void)testMismatchesDifferentNumber
{
assertDoesNotMatch(@"Different", equalToChar(CHAR_MAX), [NSNumber numberWithChar:CHAR_MIN]);
}
@end
@interface IsEqualToDoubleTest : AbstractMatcherTest
@end
@implementation IsEqualToDoubleTest
- (id<HCMatcher>)createMatcher
{
double irrelevant = 0;
return equalToDouble(irrelevant);
}
- (void)testComparesWithNSNumber
{
assertMatches(@"Large double", equalToDouble(DBL_MAX), [NSNumber numberWithDouble:DBL_MAX]);
assertMatches(@"Small double", equalToDouble(DBL_MIN), [NSNumber numberWithDouble:DBL_MIN]);
}
- (void)testMismatchesDifferentNumber
{
assertDoesNotMatch(@"Different", equalToDouble(DBL_MAX), [NSNumber numberWithDouble:DBL_MIN]);
}
@end
@interface IsEqualToFloatTest : AbstractMatcherTest
@end
@implementation IsEqualToFloatTest
- (id<HCMatcher>)createMatcher
{
float irrelevant = 0;
return equalToFloat(irrelevant);
}
- (void)testComparesWithNSNumber
{
assertMatches(@"Large float", equalToFloat(FLT_MAX), [NSNumber numberWithFloat:FLT_MAX]);
assertMatches(@"Small float", equalToFloat(FLT_MIN), [NSNumber numberWithFloat:FLT_MIN]);
}
- (void)testMismatchesDifferentNumber
{
assertDoesNotMatch(@"Different", equalToFloat(FLT_MAX), [NSNumber numberWithFloat:FLT_MIN]);
}
@end
@interface IsEqualToIntTest : AbstractMatcherTest
@end
@implementation IsEqualToIntTest
- (id<HCMatcher>)createMatcher
{
int irrelevant = 0;
return equalToInt(irrelevant);
}
- (void)testComparesWithNSNumber
{
assertMatches(@"Large int", equalToInt(INT_MAX), [NSNumber numberWithInt:INT_MAX]);
assertMatches(@"Small int", equalToInt(INT_MIN), [NSNumber numberWithInt:INT_MIN]);
}
- (void)testMismatchesDifferentNumber
{
assertDoesNotMatch(@"Different", equalToInt(INT_MAX), [NSNumber numberWithInt:INT_MIN]);
}
@end
@interface IsEqualToLongTest : AbstractMatcherTest
@end
@implementation IsEqualToLongTest
- (id<HCMatcher>)createMatcher
{
long irrelevant = 0;
return equalToLong(irrelevant);
}
- (void)testComparesWithNSNumber
{
assertMatches(@"Large long", equalToLong(LONG_MAX), [NSNumber numberWithLong:LONG_MAX]);
assertMatches(@"Small long", equalToLong(LONG_MIN), [NSNumber numberWithLong:LONG_MIN]);
}
- (void)testMismatchesDifferentNumber
{
assertDoesNotMatch(@"Different", equalToLong(LONG_MAX), [NSNumber numberWithLong:LONG_MIN]);
}
@end
@interface IsEqualToLongLongTest : AbstractMatcherTest
@end
@implementation IsEqualToLongLongTest
- (id<HCMatcher>)createMatcher
{
long long irrelevant = 0;
return equalToLongLong(irrelevant);
}
- (void)testComparesWithNSNumber
{
assertMatches(@"Large long long", equalToLongLong(LLONG_MAX), [NSNumber numberWithLongLong:LLONG_MAX]);
assertMatches(@"Small long long", equalToLongLong(LLONG_MIN), [NSNumber numberWithLongLong:LLONG_MIN]);
}
- (void)testMismatchesDifferentNumber
{
assertDoesNotMatch(@"Different", equalToLongLong(LLONG_MAX), [NSNumber numberWithLongLong:LLONG_MIN]);
}
@end
@interface IsEqualToShortTest : AbstractMatcherTest
@end
@implementation IsEqualToShortTest
- (id<HCMatcher>)createMatcher
{
short irrelevant = 0;
return equalToShort(irrelevant);
}
- (void)testComparesWithNSNumber
{
assertMatches(@"Large short", equalToShort(SHRT_MAX), [NSNumber numberWithShort:SHRT_MAX]);
assertMatches(@"Small short", equalToShort(SHRT_MIN), [NSNumber numberWithShort:SHRT_MIN]);
}
- (void)testMismatchesDifferentNumber
{
assertDoesNotMatch(@"Different", equalToShort(SHRT_MAX), [NSNumber numberWithShort:SHRT_MIN]);
}
@end
@interface IsEqualToUnsignedCharTest : AbstractMatcherTest
@end
@implementation IsEqualToUnsignedCharTest
- (id<HCMatcher>)createMatcher
{
unsigned char irrelevant = 0;
return equalToUnsignedChar(irrelevant);
}
- (void)testComparesWithNSNumber
{
assertMatches(@"Large unsigned char", equalToUnsignedChar(UCHAR_MAX), [NSNumber numberWithUnsignedChar:UCHAR_MAX]);
assertMatches(@"Small unsigned char", equalToUnsignedChar(0), [NSNumber numberWithUnsignedChar:0]);
}
- (void)testMismatchesDifferentNumber
{
assertDoesNotMatch(@"Different", equalToUnsignedChar(CHAR_MAX), [NSNumber numberWithUnsignedChar:0]);
}
@end
@interface IsEqualToUnsignedIntTest : AbstractMatcherTest
@end
@implementation IsEqualToUnsignedIntTest
- (id<HCMatcher>)createMatcher
{
unsigned int irrelevant = 0;
return equalToUnsignedInt(irrelevant);
}
- (void)testComparesWithNSNumber
{
assertMatches(@"Large unsigned int", equalToUnsignedInt(UINT_MAX), [NSNumber numberWithUnsignedInt:UINT_MAX]);
assertMatches(@"Small unsigned int", equalToUnsignedInt(0), [NSNumber numberWithUnsignedInt:0]);
}
- (void)testMismatchesDifferentNumber
{
assertDoesNotMatch(@"Different", equalToUnsignedInt(INT_MAX), [NSNumber numberWithUnsignedInt:0]);
}
@end
@interface IsEqualToUnsignedLongTest : AbstractMatcherTest
@end
@implementation IsEqualToUnsignedLongTest
- (id<HCMatcher>)createMatcher
{
unsigned long irrelevant = 0;
return equalToUnsignedLong(irrelevant);
}
- (void)testComparesWithNSNumber
{
assertMatches(@"Large unsigned long", equalToUnsignedLong(ULONG_MAX), [NSNumber numberWithUnsignedLong:ULONG_MAX]);
assertMatches(@"Small unsigned long", equalToUnsignedLong(0), [NSNumber numberWithUnsignedLong:0]);
}
- (void)testMismatchesDifferentNumber
{
assertDoesNotMatch(@"Different", equalToUnsignedLong(LONG_MAX), [NSNumber numberWithUnsignedLong:0]);
}
@end
@interface IsEqualToUnsignedLongLongTest : AbstractMatcherTest
@end
@implementation IsEqualToUnsignedLongLongTest
- (id<HCMatcher>)createMatcher
{
unsigned long long irrelevant = 0;
return equalToUnsignedLongLong(irrelevant);
}
- (void)testComparesWithNSNumber
{
assertMatches(@"Large unsigned long long", equalToUnsignedLongLong(ULLONG_MAX), [NSNumber numberWithUnsignedLongLong:ULLONG_MAX]);
assertMatches(@"Small unsigned long long", equalToUnsignedLongLong(0), [NSNumber numberWithUnsignedLongLong:0]);
}
- (void)testMismatchesDifferentNumber
{
assertDoesNotMatch(@"Different", equalToUnsignedLongLong(ULLONG_MAX), [NSNumber numberWithUnsignedLongLong:0]);
}
@end
@interface IsEqualToUnsignedShortTest : AbstractMatcherTest
@end
@implementation IsEqualToUnsignedShortTest
- (id<HCMatcher>)createMatcher
{
unsigned short irrelevant = 0;
return equalToUnsignedShort(irrelevant);
}
- (void)testComparesWithNSNumber
{
assertMatches(@"Large unsigned short", equalToUnsignedShort(USHRT_MAX), [NSNumber numberWithUnsignedShort:USHRT_MAX]);
assertMatches(@"Small unsigned short", equalToUnsignedShort(0), [NSNumber numberWithUnsignedShort:0]);
}
- (void)testMismatchesDifferentNumber
{
assertDoesNotMatch(@"Different", equalToUnsignedShort(USHRT_MAX), [NSNumber numberWithUnsignedShort:0]);
}
@end
@interface IsEqualToIntegerTest : AbstractMatcherTest
@end
@implementation IsEqualToIntegerTest
- (id<HCMatcher>)createMatcher
{
NSInteger irrelevant = 0;
return equalToInteger(irrelevant);
}
- (void)testComparesWithNSNumber
{
assertMatches(@"Large NSInteger", equalToInteger(INT_MAX), [NSNumber numberWithInteger:INT_MAX]);
assertMatches(@"Small NSInteger", equalToInteger(INT_MIN), [NSNumber numberWithInteger:INT_MIN]);
}
- (void)testMismatchesDifferentNumber
{
assertDoesNotMatch(@"Different", equalToInteger(INT_MAX), [NSNumber numberWithInteger:INT_MIN]);
}
@end
@interface IsEqualToUnsignedIntegerTest : AbstractMatcherTest
@end
@implementation IsEqualToUnsignedIntegerTest
- (id<HCMatcher>)createMatcher
{
NSUInteger irrelevant = 0;
return equalToUnsignedInteger(irrelevant);
}
- (void)testComparesWithNSNumber
{
assertMatches(@"Large NSUInteger", equalToUnsignedInteger(UINT_MAX), [NSNumber numberWithUnsignedInteger:UINT_MAX]);
assertMatches(@"Small NSUInteger", equalToUnsignedInteger(0), [NSNumber numberWithUnsignedInteger:0]);
}
- (void)testMismatchesDifferentNumber
{
assertDoesNotMatch(@"Different", equalToUnsignedInteger(INT_MAX), [NSNumber numberWithUnsignedInteger:0]);
}
@end
@@ -0,0 +1,317 @@
//
// OCHamcrest - NumberAssertTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Module under test
#define HC_SHORTHAND
#import <OCHamcrest/HCNumberAssert.h>
// Collaborators
#import <OCHamcrest/HCIsEqual.h>
// Test support
#import <SenTestingKit/SenTestingKit.h>
@interface NumberAssertTest : SenTestCase
@end
@implementation NumberAssertTest
- (void)setUp
{
[super setUp];
[self raiseAfterFailure];
}
- (void)testSuccess_withBool
{
assertThatBool(YES, equalTo([NSNumber numberWithBool:YES]));
}
- (void)testFailure_withBool
{
@try
{
assertThatBool(YES, equalTo([NSNumber numberWithBool:NO]));
}
@catch (NSException* exception)
{
STAssertEqualObjects([exception reason], @"Expected <0>, but was <1>", nil);
return;
}
STFail(@"should have failed");
}
- (void)testSuccess_withChar
{
assertThatChar('A', equalTo(@'A'));
}
- (void)testFailure_withChar
{
@try
{
assertThatChar('B', equalTo(@'A'));
}
@catch (NSException* exception)
{
STAssertEqualObjects([exception reason], @"Expected <65>, but was <66>", nil);
return;
}
STFail(@"should have failed");
}
- (void)testSuccess_withDouble
{
assertThatDouble(1.5, equalTo(@1.5));
}
- (void)testFailure_withDouble
{
@try
{
assertThatDouble(2.5, equalTo(@1.5));
}
@catch (NSException* exception)
{
STAssertEqualObjects([exception reason], @"Expected <1.5>, but was <2.5>", nil);
return;
}
STFail(@"should have failed");
}
- (void)testSuccess_withFloat
{
assertThatFloat(1.5f, equalTo(@1.5f));
}
- (void)testFailure_withFloat
{
@try
{
assertThatFloat(2.5f, equalTo(@1.5f));
}
@catch (NSException* exception)
{
STAssertEqualObjects([exception reason], @"Expected <1.5>, but was <2.5>", nil);
return;
}
STFail(@"should have failed");
}
- (void)testSuccess_withInt
{
assertThatInt(1, equalTo(@1));
}
- (void)testFailure_withInt
{
@try
{
assertThatInt(2, equalTo(@1));
}
@catch (NSException* exception)
{
STAssertEqualObjects([exception reason], @"Expected <1>, but was <2>", nil);
return;
}
STFail(@"should have failed");
}
- (void)testSuccess_withLong
{
assertThatLong(1L, equalTo(@1L));
}
- (void)testFailure_withLong
{
@try
{
assertThatLong(2L, equalTo(@1L));
}
@catch (NSException* exception)
{
STAssertEqualObjects([exception reason], @"Expected <1>, but was <2>", nil);
return;
}
STFail(@"should have failed");
}
- (void)testSuccess_withLongLong
{
assertThatLongLong(1LL, equalTo(@1LL));
}
- (void)testFailure_withLongLong
{
@try
{
assertThatLongLong(2LL, equalTo(@1LL));
}
@catch (NSException* exception)
{
STAssertEqualObjects([exception reason], @"Expected <1>, but was <2>", nil);
return;
}
STFail(@"should have failed");
}
- (void)testSuccess_withShort
{
assertThatShort(1, equalTo([NSNumber numberWithShort:1]));
}
- (void)testFailure_withShort
{
@try
{
assertThatShort(2, equalTo([NSNumber numberWithShort:1]));
}
@catch (NSException* exception)
{
STAssertEqualObjects([exception reason], @"Expected <1>, but was <2>", nil);
return;
}
STFail(@"should have failed");
}
- (void)testSuccess_withUnsignedChar
{
assertThatUnsignedChar('A', equalTo([NSNumber numberWithUnsignedChar:'A']));
}
- (void)testFailure_withUnsignedChar
{
@try
{
assertThatUnsignedChar('B', equalTo([NSNumber numberWithUnsignedChar:'A']));
}
@catch (NSException* exception)
{
STAssertEqualObjects([exception reason], @"Expected <65>, but was <66>", nil);
return;
}
STFail(@"should have failed");
}
- (void)testSuccess_withUnsignedInt
{
assertThatUnsignedInt(1U, equalTo(@1U));
}
- (void)testFailure_withUnsignedInt
{
@try
{
assertThatUnsignedInt(2U, equalTo(@1U));
}
@catch (NSException* exception)
{
STAssertEqualObjects([exception reason], @"Expected <1>, but was <2>", nil);
return;
}
STFail(@"should have failed");
}
- (void)testSuccess_withUnsignedLong
{
assertThatUnsignedLong(1UL, equalTo(@1UL));
}
- (void)testFailure_withUnsignedLong
{
@try
{
assertThatUnsignedLong(2UL, equalTo(@1UL));
}
@catch (NSException* exception)
{
STAssertEqualObjects([exception reason], @"Expected <1>, but was <2>", nil);
return;
}
STFail(@"should have failed");
}
- (void)testSuccess_withUnsignedLongLong
{
assertThatUnsignedLongLong(1ULL, equalTo(@1ULL));
}
- (void)testFailure_withUnsignedLongLong
{
@try
{
assertThatUnsignedLongLong(2ULL, equalTo(@1ULL));
}
@catch (NSException* exception)
{
STAssertEqualObjects(@"Expected <1>, but was <2>", [exception reason], nil);
return;
}
STFail(@"should have failed");
}
- (void)testSuccess_withUnsignedShort
{
assertThatUnsignedShort(1U, equalTo([NSNumber numberWithUnsignedShort:1U]));
}
- (void)testFailure_withUnsignedShort
{
@try
{
assertThatUnsignedShort(2U, equalTo([NSNumber numberWithUnsignedShort:1U]));
}
@catch (NSException* exception)
{
STAssertEqualObjects([exception reason], @"Expected <1>, but was <2>", nil);
return;
}
STFail(@"should have failed");
}
- (void)testSuccess_withInteger
{
assertThatInteger(1, equalTo(@1));
}
- (void)testFailure_withInteger
{
@try
{
assertThatInteger(2, equalTo(@1));
}
@catch (NSException* exception)
{
STAssertEqualObjects([exception reason], @"Expected <1>, but was <2>", nil);
return;
}
STFail(@"should have failed");
}
- (void)testSuccess_withUnsignedInteger
{
assertThatUnsignedInteger(1, equalTo([NSNumber numberWithUnsignedInteger:1]));
}
- (void)testFailure_withUnsignedInteger
{
@try
{
assertThatUnsignedInteger(2, equalTo([NSNumber numberWithUnsignedInteger:1]));
}
@catch (NSException* exception)
{
STAssertEqualObjects([exception reason], @"Expected <1>, but was <2>", nil);
return;
}
STFail(@"should have failed");
}
@end
@@ -0,0 +1,111 @@
//
// OCHamcrest - OrderingComparisonTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCOrderingComparison.h>
// Test support
#import "AbstractMatcherTest.h"
@interface OrderingComparisonTest : AbstractMatcherTest
@end
@implementation OrderingComparisonTest
- (id<HCMatcher>)createMatcher
{
return nil; // Ignore the inherited tests; they don't work well for this class.
}
- (void)testComparesObjectsForGreaterThan
{
assertMatches(@"match", greaterThan(@1), @2);
assertDoesNotMatch(@"no match", greaterThan(@1), @1);
}
- (void)testComparesObjectsForLessThan
{
assertMatches(@"match", lessThan(@1), @0);
assertDoesNotMatch(@"no match", lessThan(@1), @1);
}
- (void)testComparesObjectsForGreaterThanOrEqualTo
{
assertMatches(@"match", greaterThanOrEqualTo(@1), @2);
assertMatches(@"match", greaterThanOrEqualTo(@1), @1);
assertDoesNotMatch(@"no match", greaterThanOrEqualTo(@1), @0);
}
- (void)testComparesObjectsForLessThanOrEqualTo
{
assertMatches(@"match", lessThanOrEqualTo(@1), @0);
assertMatches(@"match", lessThanOrEqualTo(@1), @1);
assertDoesNotMatch(@"no match", lessThanOrEqualTo(@1), @2);
}
- (void)testDoesNotMatchNil
{
assertDoesNotMatch(@"nil argument", greaterThan(@1), nil);
}
- (void)testSupportsDifferentTypesOfComparableObjects
{
assertMatches(@"strings", greaterThan(@"bb"), @"cc");
assertMatches(@"dates", lessThan([NSDate date]), [NSDate distantPast]);
}
- (void)testMatcherCreationRequiresObjectWithCompareMethod
{
id object = [[NSObject alloc] init];
STAssertThrows(greaterThan(object), @"object does not have -compare: method");
}
- (void)testHasAReadableDescription
{
id one = @1;
assertDescription(@"a value greater than <1>", greaterThan(one));
assertDescription(@"a value greater than or equal to <1>", greaterThanOrEqualTo(one));
assertDescription(@"a value less than <1>", lessThan(one));
assertDescription(@"a value less than or equal to <1>", lessThanOrEqualTo(one));
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
id one = @1;
assertNoMismatchDescription(greaterThan(one), @2);
assertNoMismatchDescription(lessThan(one), @0);
assertNoMismatchDescription(greaterThanOrEqualTo(one), @1);
assertNoMismatchDescription(lessThanOrEqualTo(one), @1);
}
- (void)testMismatchDescription
{
id one = @1;
assertMismatchDescription(@"was <0>", greaterThan(one), @0);
assertMismatchDescription(@"was <2>", lessThan(one), @2);
assertMismatchDescription(@"was <0>", greaterThanOrEqualTo(one), @0);
assertMismatchDescription(@"was <2>", lessThanOrEqualTo(one), @2);
}
- (void)testDescribeMismatch
{
id one = @1;
assertDescribeMismatch(@"was <0>", greaterThan(one), @0);
assertDescribeMismatch(@"was <2>", lessThan(one), @2);
assertDescribeMismatch(@"was <0>", greaterThanOrEqualTo(one), @0);
assertDescribeMismatch(@"was <2>", lessThanOrEqualTo(one), @2);
}
@end
@@ -0,0 +1,78 @@
//
// OCHamcrest - ConformsToProtocolTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Todd Farrell
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCConformsToProtocol.h>
// Test support
#import "AbstractMatcherTest.h"
@protocol TestProtocol
@end
@interface TestClass : NSObject <TestProtocol>
@end
@implementation TestClass
+ (TestClass *)testClass
{
return [[TestClass alloc] init];
}
@end
#pragma mark -
@interface ConformsToProtocolTest : AbstractMatcherTest
@end
@implementation ConformsToProtocolTest
- (id<HCMatcher>)createMatcher
{
return conformsTo(@protocol(TestProtocol));
}
- (void)testEvaluatesToTrueIfArgumentConformsToASpecificProtocol
{
TestClass *instance = [TestClass testClass];
assertMatches(@"conforms to protocol", conformsTo(@protocol(TestProtocol)), instance);
assertDoesNotMatch(@"does not conform to protocol", conformsTo(@protocol(TestProtocol)), @"hi");
assertDoesNotMatch(@"nil", conformsTo(@protocol(TestProtocol)), nil);
}
- (void)testMatcherCreationRequiresNonNilArgument
{
STAssertThrows(conformsTo(nil), @"Should require non-nil argument");
}
- (void)testHasAReadableDescription
{
assertDescription(@"an object that conforms to TestProtocol", conformsTo(@protocol(TestProtocol)));
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
assertNoMismatchDescription(conformsTo(@protocol(NSObject)), @"hi");
}
- (void)testMismatchDescriptionShowsActualArgument
{
assertMismatchDescription(@"was \"bad\"", conformsTo(@protocol(TestProtocol)), @"bad");
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"was \"bad\"", conformsTo(@protocol(TestProtocol)), @"bad");
}
@end
@@ -0,0 +1,84 @@
//
// OCHamcrest - HasDescriptionTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCHasDescription.h>
// Collaborators
#import <OCHamcrest/HCIsEqual.h>
// Test support
#import "AbstractMatcherTest.h"
static NSString *fakeDescription = @"DESCRIPTION";
@interface FakeWithDescription : NSObject
@end
@implementation FakeWithDescription
+ (id)fake { return [[self alloc] init]; }
- (NSString *)description { return fakeDescription; }
@end
#pragma mark -
@interface HasDescriptionTest : AbstractMatcherTest
@end
@implementation HasDescriptionTest
- (id<HCMatcher>)createMatcher
{
return hasDescription(equalTo(@"irrelevant"));
}
- (void)testPassesResultOfDescriptionToNestedMatcher
{
FakeWithDescription* fake = [FakeWithDescription fake];
assertMatches(@"equal", hasDescription(equalTo(fakeDescription)), fake);
assertDoesNotMatch(@"unequal", hasDescription(equalTo(@"foo")), fake);
}
- (void)testProvidesConvenientShortcutForDescriptionEqualTo
{
FakeWithDescription* fake = [FakeWithDescription fake];
assertMatches(@"equal", hasDescription(fakeDescription), fake);
assertDoesNotMatch(@"unequal", hasDescription(@"foo"), fake);
}
- (void)testMismatchDoesNotRepeatTheDescription
{
FakeWithDescription* fake = [FakeWithDescription fake];
assertMismatchDescription(@"was \"DESCRIPTION\"", hasDescription(@"foo"), fake);
}
- (void)testHasReadableDescription
{
assertDescription(@"an object with description \"foo\"", hasDescription(@"foo"));
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
assertNoMismatchDescription(hasDescription(@"DESCRIPTION"), [FakeWithDescription fake]);
}
- (void)testMismatchDescriptionShowsActualArgument
{
assertMismatchDescription(@"was \"bad\"", hasDescription(@"foo"), @"bad");
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"was \"bad\"", hasDescription(@"foo"), @"bad");
}
@end
@@ -0,0 +1,300 @@
//
// OCHamcrest - HasPropertyTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Justin Shacklette
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCHasProperty.h>
// Collaborators
#import <OCHamcrest/HCIsEqual.h>
#import <OCHamcrest/HCIsEqualToNumber.h>
#import <OCHamcrest/HCIsNil.h>
// Test support
#import "AbstractMatcherTest.h"
@interface Person : NSObject
@property (copy, nonatomic) NSString *name;
- (NSNumber *)shoeSize;
@end
@implementation Person
{
NSNumber *_shoeSize;
}
- (id) initWithName:(NSString *)name shoeSize:(int)shoeSize
{
self = [super init];
if (self)
{
_name = name;
_shoeSize = [[NSNumber alloc] initWithInt:shoeSize];
}
return self;
}
- (NSNumber *)shoeSize
{
return _shoeSize;
}
@end
#pragma mark -
@interface HasPropertyTest : AbstractMatcherTest
@end
@implementation HasPropertyTest
{
Person *joe;
Person *nobody;
}
- (void)setUp
{
[super setUp];
joe = [[Person alloc] initWithName:@"Joe" shoeSize:13];
nobody = [[Person alloc] initWithName:nil shoeSize:0];
}
- (void)tearDown
{
joe = nil;
nobody = nil;
[super tearDown];
}
- (id<HCMatcher>)createMatcher
{
return hasProperty(@"irrelevant", @"irrelevant");
}
- (void)testCanMatchStringPropertyValues
{
assertMatches(@"equal string property values", hasProperty(@"name", @"Joe"), joe);
assertDoesNotMatch(@"unequal string property values", hasProperty(@"name", @"Bob"), joe);
assertDoesNotMatch(@"unequal string property values", hasProperty(@"name", nil), joe);
}
- (void)testCanMatchStringPropertyValuesWithMatchers
{
assertMatches(@"equal string property values", hasProperty(@"name", equalTo(@"Joe")), joe);
assertDoesNotMatch(@"unequal string property values", hasProperty(@"name", equalTo(@"Bob")), joe);
assertDoesNotMatch(@"unequal string property values", hasProperty(@"name", nilValue()), joe);
}
- (void)testCanMatchNumberPropertyValues
{
assertMatches(@"equal int property values", hasProperty(@"shoeSize", equalToInt(13)), joe);
assertDoesNotMatch(@"unequal int property values", hasProperty(@"shoeSize", equalToInt(3)), joe);
assertDoesNotMatch(@"unequal int property values", hasProperty(@"shoeSize", equalToInt(-3)), joe);
}
- (void)testNilPropertyValues
{
assertMatches(@"equal nil property values", hasProperty(@"name", nilValue()), nobody);
assertDoesNotMatch(@"unequal nil property values", hasProperty(@"name", @"Bob"), nobody);
}
- (void)testMatcherCreationRequiresNonNilPropertyName
{
STAssertThrows(hasProperty(nil, nil), @"Should require non-nil property name");
}
- (void)testHasAReadableDescription
{
assertDescription(@"an object with name \"Joe\"", hasProperty(@"name", @"Joe"));
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
assertNoMismatchDescription(hasProperty(@"name", @"Joe"), joe);
}
- (void)testMismatchDescriptionShowsActualArgument
{
assertMismatchDescription(@"was \"bad\"", hasProperty(@"foo", @"foo"), @"bad");
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"was \"bad\"", hasProperty(@"foo", @"foo"), @"bad");
}
@end
#pragma mark -
@interface ValueHolder : NSObject
@property (assign, nonatomic) BOOL boolValue;
@property (assign, nonatomic) char charValue;
@property (assign, nonatomic) int intValue;
@property (assign, nonatomic) short shortValue;
@property (assign, nonatomic) long longValue;
@property (assign, nonatomic) long long longLongValue;
@property (assign, nonatomic) unsigned char unsignedCharValue;
@property (assign, nonatomic) unsigned int unsignedIntValue;
@property (assign, nonatomic) unsigned short unsignedShortValue;
@property (assign, nonatomic) unsigned long unsignedLongValue;
@property (assign, nonatomic) unsigned long long unsignedLongLongValue;
@property (assign, nonatomic) float floatValue;
@property (assign, nonatomic) double doubleValue;
@end
@implementation ValueHolder
@end
#pragma mark -
@interface HasPropertyPrimitivesTest : AbstractMatcherTest
@end
@implementation HasPropertyPrimitivesTest
{
ValueHolder *foo;
}
- (void)setUp
{
[super setUp];
foo = [[ValueHolder alloc] init];
}
- (void)tearDown
{
foo = nil;
[super tearDown];
}
- (void)testCanMatchPrimitiveBoolValues
{
foo.boolValue = YES;
assertMatches(@"BOOL should match", hasProperty(@"boolValue", equalToBool(YES)), foo);
assertDoesNotMatch(@"BOOL should not match", hasProperty(@"boolValue", equalToBool(NO)), foo);
}
- (void)testCanMatchPrimitiveCharValues
{
foo.charValue = 'a';
assertMatches(@"char should match", hasProperty(@"charValue", equalToChar('a')), foo);
assertDoesNotMatch(@"char should not match", hasProperty(@"charValue", equalToChar('b')), foo);
}
- (void)testCanMatchPrimitiveIntValues
{
foo.intValue = INT_MIN;
assertMatches(@"int should match", hasProperty(@"intValue", equalToInt(INT_MIN)), foo);
assertDoesNotMatch(@"int should not match", hasProperty(@"intValue", equalToInt(-2)), foo);
}
- (void)testCanMatchPrimitiveShortValues
{
foo.shortValue = -2;
assertMatches(@"short should match", hasProperty(@"shortValue", equalToShort(-2)), foo);
assertDoesNotMatch(@"short should not match", hasProperty(@"shortValue", equalToShort(-1)), foo);
}
- (void)testCanMatchPrimitiveLongValues
{
foo.longValue = LONG_MIN;
assertMatches(@"long should match", hasProperty(@"longValue", equalToLong(LONG_MIN)), foo);
assertDoesNotMatch(@"long should not match",
hasProperty(@"longValue", equalToLong(LONG_MIN + 1)),
foo);
}
- (void)testCanMatchPrimitiveLongLongValues
{
foo.longLongValue = LONG_MIN - 1;
assertMatches(@"long long should match",
hasProperty(@"longLongValue", equalToLongLong(LONG_MIN - 1)),
foo);
assertDoesNotMatch(@"long long should not match",
hasProperty(@"longLongValue", equalToLongLong(LONG_MIN)),
foo);
}
- (void)testCanMatchPrimitiveUnsignedCharValues
{
foo.unsignedCharValue = 'b';
assertMatches(@"unsigned char should match",
hasProperty(@"unsignedCharValue", equalToUnsignedChar('b')),
foo);
assertDoesNotMatch(@"unsigned char should not match",
hasProperty(@"unsignedCharValue", equalToUnsignedChar('c')),
foo);
}
- (void)testCanMatchPrimitiveUnsignedIntValues
{
foo.unsignedIntValue = UINT_MAX;
assertMatches(@"unsigned int should match",
hasProperty(@"unsignedIntValue", equalToUnsignedInt(UINT_MAX)),
foo);
assertDoesNotMatch(@"unsigned int should not match",
hasProperty(@"unsignedIntValue", equalToUnsignedInt(UINT_MAX - 1)),
foo);
}
- (void)testCanMatchPrimitiveUnsignedShortValues
{
foo.unsignedShortValue = 2;
assertMatches(@"unsigned short should match",
hasProperty(@"unsignedShortValue", equalToUnsignedShort(2)),
foo);
assertDoesNotMatch(@"unsigned short should not match",
hasProperty(@"unsignedShortValue", equalToUnsignedShort(3)),
foo);
}
- (void)testCanMatchPrimitiveUnsignedLongValues
{
foo.unsignedLongValue = ULONG_MAX;
assertMatches(@"unsigned long should match",
hasProperty(@"unsignedLongValue", equalToUnsignedLong(ULONG_MAX)),
foo);
assertDoesNotMatch(@"unsigned long should not match",
hasProperty(@"unsignedLongValue", equalToUnsignedLong(ULONG_MAX - 1)),
foo);
}
- (void)testCanMatchPrimitiveUnsignedLongLongValues
{
foo.unsignedLongLongValue = ULLONG_MAX;
assertMatches(@"unsigned long long should match",
hasProperty(@"unsignedLongLongValue", equalToUnsignedLongLong(ULLONG_MAX)),
foo);
assertDoesNotMatch(@"unsigned long long should not match",
hasProperty(@"unsignedLongLongValue", equalToUnsignedLongLong(ULLONG_MAX - 1)),
foo);
}
- (void)testCanMatchPrimitiveFloatValues
{
foo.floatValue = 1.2f;
assertMatches(@"float should match", hasProperty(@"floatValue", equalToFloat(1.2f)), foo);
assertDoesNotMatch(@"float should not match", hasProperty(@"floatValue", equalToFloat(1.3f)), foo);
}
- (void)testCanMatchPrimitiveDoubleValues
{
foo.doubleValue = DBL_MAX;
assertMatches(@"double should match", hasProperty(@"doubleValue", equalToDouble(DBL_MAX)), foo);
assertDoesNotMatch(@"double should not match",
hasProperty(@"doubleValue", equalToDouble(3.14)),
foo);
}
@end
@@ -0,0 +1,111 @@
//
// OCHamcrest - IsEqualTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCIsEqual.h>
// Test support
#import "AbstractMatcherTest.h"
@interface FakeArgument : NSObject
@end
@implementation FakeArgument
- (NSString *)description { return @"ARGUMENT DESCRIPTION"; }
@end
#pragma mark -
@interface AlwaysEqual : NSObject
@end
@implementation AlwaysEqual
- (BOOL)isEqual:(id)anObject { return YES; }
@end
#pragma mark -
@interface NeverEqual : NSObject
@end
@implementation NeverEqual
- (BOOL)isEqual:(id)anObject { return NO; }
@end
#pragma mark -
@interface IsEqualTest : AbstractMatcherTest
@end
@implementation IsEqualTest
- (id<HCMatcher>)createMatcher
{
return equalTo(@"irrelevant");
}
- (void)testComparesObjectsUsingIsEqualMethod
{
assertMatches(@"equal strings", equalTo(@"hi"), @"hi");
assertDoesNotMatch(@"unequal strings", equalTo(@"hi"), @"bye");
}
- (void)testCanCompareNilValues
{
assertMatches(@"nil equals nil", equalTo(nil), nil);
assertDoesNotMatch(@"nil as argument", equalTo(@"hi"), nil);
assertDoesNotMatch(@"nil in equalTo", equalTo(nil), @"hi");
}
- (void)testHonorsIsEqualImplementationEvenWithNilValues
{
assertMatches(@"always equal", equalTo(nil), [[AlwaysEqual alloc] init]);
assertDoesNotMatch(@"never equal", equalTo(nil), [[NeverEqual alloc] init]);
}
- (void)testIncludesTheResultOfCallingDescriptionOnItsArgumentInTheDescription
{
assertDescription(@"<ARGUMENT DESCRIPTION>", equalTo([[FakeArgument alloc] init]));
}
- (void)testReturnsAnObviousDescriptionIfCreatedWithANestedMatcherByMistake
{
id innerMatcher = equalTo(@"NestedMatcher");
assertDescription(([[NSArray arrayWithObjects:@"<", [innerMatcher description], @">", nil]
componentsJoinedByString:@""]),
equalTo(innerMatcher));
}
- (void)testReturnsGoodDescriptionIfCreatedWithNilReference
{
assertDescription(@"nil", equalTo(nil));
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
assertNoMismatchDescription(equalTo(@"hi"), @"hi");
}
- (void)testMismatchDescriptionShowsActualArgument
{
assertMismatchDescription(@"was \"bad\"", equalTo(@"good"), @"bad");
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"was \"bad\"", equalTo(@"good"), @"bad");
}
@end
@@ -0,0 +1,64 @@
//
// OCHamcrest - IsInstanceOfTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCIsInstanceOf.h>
// Test support
#import "AbstractMatcherTest.h"
@interface IsInstanceOfTest : AbstractMatcherTest
@end
@implementation IsInstanceOfTest
- (id<HCMatcher>)createMatcher
{
return instanceOf([NSNumber class]);
}
- (void)testEvaluatesToTrueIfArgumentIsInstanceOfASpecificClass
{
NSNumber *number = @1;
assertMatches(@"same class", instanceOf([NSNumber class]), number);
assertMatches(@"subclass", instanceOf([NSValue class]), number);
assertDoesNotMatch(@"different class", instanceOf([NSNumber class]), @"hi");
assertDoesNotMatch(@"nil", instanceOf([NSNumber class]), nil);
}
- (void)testMatcherCreationRequiresNonNilArgument
{
STAssertThrows(instanceOf(nil), @"Should require non-nil argument");
}
- (void)testHasAReadableDescription
{
assertDescription(@"an instance of NSNumber", instanceOf([NSNumber class]));
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
assertNoMismatchDescription(instanceOf([NSString class]), @"hi");
}
- (void)testMismatchDescriptionShowsActualArgument
{
assertMismatchDescription(@"was \"bad\"", instanceOf([NSNumber class]), @"bad");
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"was \"bad\"", instanceOf([NSNumber class]), @"bad");
}
@end
@@ -0,0 +1,108 @@
//
// OCHamcrest - IsNilTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCIsNil.h>
// Test support
#import "AbstractMatcherTest.h"
@interface IsNilTest : AbstractMatcherTest
@end
@implementation IsNilTest
- (id<HCMatcher>)createMatcher
{
return nilValue();
}
- (void)testEvaluatesToTrueIfArgumentIsNil
{
assertMatches(@"nil", nilValue(), nil);
}
- (void)testEvaluatesToFalseIfArgumentIsNotNil
{
id ANY_NON_NULL_ARGUMENT = [[NSObject alloc] init];
assertDoesNotMatch(@"not nil", nilValue(), ANY_NON_NULL_ARGUMENT);
}
- (void)testHasAReadableDescription
{
assertDescription(@"nil", nilValue());
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
assertNoMismatchDescription(nilValue(), nil);
}
- (void)testMismatchDescriptionShowsActualArgument
{
assertMismatchDescription(@"was \"bad\"", nilValue(), @"bad");
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"was \"bad\"", nilValue(), @"bad");
}
@end
#pragma mark -
@interface NotNilTest : AbstractMatcherTest
@end
@implementation NotNilTest
- (id<HCMatcher>)createMatcher
{
return notNilValue();
}
- (void)testEvaluatesToTrueIfArgumentIsNotNil
{
id ANY_NON_NULL_ARGUMENT = [[NSObject alloc] init];
assertMatches(@"not nil", notNilValue(), ANY_NON_NULL_ARGUMENT);
}
- (void)testEvaluatesToFalseIfArgumentIsNil
{
assertDoesNotMatch(@"nil", notNilValue(), nil);
}
- (void)testHasAReadableDescription
{
assertDescription(@"not nil", notNilValue());
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
assertNoMismatchDescription(notNilValue(), @"hi");
}
- (void)testMismatchDescriptionShowsActualArgument
{
assertMismatchDescription(@"was nil", notNilValue(), nil);
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"was nil", notNilValue(), nil);
}
@end
@@ -0,0 +1,99 @@
//
// OCHamcrest - IsSameTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCIsSame.h>
// Collaborators
#import <OCHamcrest/HCAssertThat.h>
#import <OCHamcrest/HCIsNot.h>
#import <OCHamcrest/HCStringDescription.h>
// Test support
#import "AbstractMatcherTest.h"
@interface IsSameTest : AbstractMatcherTest
@end
@implementation IsSameTest
- (id<HCMatcher>)createMatcher
{
return sameInstance(@"irrelevant");
}
- (void)testEvaluatesToTrueIfArgumentIsReferenceToASpecifiedObject
{
id o1 = [[NSObject alloc] init];
id o2 = [[NSObject alloc] init];
assertThat(o1, sameInstance(o1));
assertThat(o2, isNot(sameInstance(o1)));
}
- (void)testDoesNotMatchEqualObjects
{
NSString *string1 = @"foobar";
NSString *string2 = [@"foo" stringByAppendingString:@"bar"];
assertDoesNotMatch(@"not the same object", sameInstance(string1), string2);
}
- (void)testDescriptionIncludesMemoryAddress
{
HCStringDescription *description = [HCStringDescription stringDescription];
NSPredicate *expected = [NSPredicate predicateWithFormat:
@"SELF MATCHES 'same instance as 0x[0-9a-fA-F]+ \"abc\"'"];
[description appendDescriptionOf:sameInstance(@"abc")];
STAssertTrue([expected evaluateWithObject:[description description]], nil);
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
id o1 = [[NSObject alloc] init];
assertNoMismatchDescription(sameInstance(o1), o1);
}
- (void)testMismatchDescriptionShowsActualArgumentAddress
{
id<HCMatcher> matcher = sameInstance(@"foo");
HCStringDescription *description = [HCStringDescription stringDescription];
NSPredicate *expected = [NSPredicate predicateWithFormat:
@"SELF MATCHES 'was 0x[0-9a-fA-F]+ \"hi\"'"];
BOOL result = [matcher matches:@"hi" describingMismatchTo:description];
STAssertFalse(result, @"Precondition: Matcher should not match item");
STAssertTrue([expected evaluateWithObject:[description description]], nil);
}
- (void)testMismatchDescriptionWithNilShouldNotIncludeAddress
{
assertMismatchDescription(@"was nil", sameInstance(@"foo"), nil);
}
- (void)testDescribeMismatch
{
id<HCMatcher> matcher = sameInstance(@"foo");
HCStringDescription *description = [HCStringDescription stringDescription];
NSPredicate *expected = [NSPredicate predicateWithFormat:
@"SELF MATCHES 'was 0x[0-9a-fA-F]+ \"hi\"'"];
[matcher describeMismatchOf:@"hi" to:description];
STAssertTrue([expected evaluateWithObject:[description description]], nil);
}
- (void)testDescribeMismatchWithNilShouldNotIncludeAddress
{
assertDescribeMismatch(@"was nil", sameInstance(@"foo"), nil);
}
@end
@@ -0,0 +1,88 @@
//
// OCHamcrest - IsEqualIgnoringCaseTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCIsEqualIgnoringCase.h>
// Test support
#import "AbstractMatcherTest.h"
@interface IsEqualIgnoringCaseTest : AbstractMatcherTest
@end
@implementation IsEqualIgnoringCaseTest
{
id <HCMatcher> matcher;
}
- (void)setUp
{
[super setUp];
matcher = equalToIgnoringCase(@"heLLo");
}
- (void)tearDown
{
matcher = nil;
[super tearDown];
}
- (id<HCMatcher>)createMatcher
{
return matcher;
}
- (void)testIgnoresCaseOfCharsInString
{
assertMatches(@"all upper", matcher, @"HELLO");
assertMatches(@"all lower", matcher, @"hello");
assertMatches(@"mixed up", matcher, @"HelLo");
assertDoesNotMatch(@"no match", matcher, @"bye");
}
- (void)testFailsIfAdditionalWhitespaceIsPresent
{
assertDoesNotMatch(@"whitespace suffix", matcher, @"heLLo ");
assertDoesNotMatch(@"whitespace prefix", matcher, @" heLLo");
}
- (void)testMatcherCreationRequiresNonNilArgument
{
STAssertThrows(equalToIgnoringCase(nil), @"Should require non-nil argument");
}
- (void)testFailsIfMatchingAgainstNonString
{
assertDoesNotMatch(@"non-string", matcher, @3);
}
- (void)testHasAReadableDescription
{
assertDescription(@"\"heLLo\" ignoring case", matcher);
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
assertNoMismatchDescription(matcher, @"hello");
}
- (void)testMismatchDescriptionShowsActualArgument
{
assertMismatchDescription(@"was \"bad\"", matcher, @"bad");
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"was \"bad\"", matcher, @"bad");
}
@end
@@ -0,0 +1,93 @@
//
// OCHamcrest - IsEqualIgnoringWhiteSpaceTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCIsEqualIgnoringWhiteSpace.h>
// Test support
#import "AbstractMatcherTest.h"
@interface IsEqualIgnoringWhiteSpaceTest : AbstractMatcherTest
@end
@implementation IsEqualIgnoringWhiteSpaceTest
{
id <HCMatcher> matcher;
}
- (void)setUp
{
[super setUp];
matcher = equalToIgnoringWhiteSpace(@"Hello World how\n are we? ");
}
- (void)tearDown
{
matcher = nil;
[super tearDown];
}
- (id<HCMatcher>)createMatcher
{
return matcher;
}
- (void)testPassesIfWordsAreSameButWhitespaceDiffers
{
assertMatches(@"less whitespace", matcher, @"Hello World how are we?");
assertMatches(@"more whitespace", matcher, @" Hello World how are \n\n\twe?");
}
- (void)testFailsIfTextOtherThanWhitespaceDiffers
{
assertDoesNotMatch(@"wrong word", matcher, @"Hello PLANET how are we?");
assertDoesNotMatch(@"incomplete", matcher, @"Hello World how are we");
}
- (void)testFailsIfWhitespaceIsAddedOrRemovedInMidWord
{
assertDoesNotMatch(@"need whitespace between Hello and World",
matcher, @"HelloWorld how are we?");
assertDoesNotMatch(@"wrong whitespace within World",
matcher, @"Hello Wo rld how are we?");
}
- (void)testMatcherCreationRequiresNonNilArgument
{
STAssertThrows(equalToIgnoringWhiteSpace(nil), @"Should require non-nil argument");
}
- (void)testFailsIfMatchingAgainstNonString
{
assertDoesNotMatch(@"non-string", matcher, @3);
}
- (void)testHasAReadableDescription
{
assertDescription(@"\"Hello World how\\n are we? \" ignoring whitespace", matcher);
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
assertNoMismatchDescription(equalToIgnoringWhiteSpace(@"foo\nbar"), @"foo bar");
}
- (void)testMismatchDescriptionShowsActualArgument
{
assertMismatchDescription(@"was \"bad\"", matcher, @"bad");
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"was \"bad\"", matcher, @"bad");
}
@end
@@ -0,0 +1,96 @@
//
// OCHamcrest - StringContainsInOrderTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCStringContainsInOrder.h>
// Test support
#import "AbstractMatcherTest.h"
@interface StringContainsInOrderTest : AbstractMatcherTest
{
id<HCMatcher> matcher;
}
@end
@implementation StringContainsInOrderTest
- (void)setUp
{
[super setUp];
matcher = stringContainsInOrder(@"string one", @"string two", @"string three", nil);
}
- (void)tearDown
{
matcher = nil;
[super tearDown];
}
- (id<HCMatcher>)createMatcher
{
return matcher;
}
- (void)testMatchesIfOrderIsCorrect
{
assertMatches(@"correct order", matcher, @"string one then string two followed by string three");
}
- (void)testDoesNotMatchIfOrderIsIncorrect
{
assertDoesNotMatch(@"incorrect order", matcher, @"string two then string one followed by string three");
}
- (void)testDoesNotMatchIfExpectedSubstringsAreMissing
{
assertDoesNotMatch(@"missing string one", matcher, @"string two then string three");
assertDoesNotMatch(@"missing string two", matcher, @"string one then string three");
assertDoesNotMatch(@"missing string three", matcher, @"string one then string two");
}
- (void)testMatcherCreationRequiresNonNilArgument
{
STAssertThrows(stringContainsInOrder(nil), @"Should require non-nil argument");
}
- (void)testMatcherCreationRequiresStringArguments
{
STAssertThrows(stringContainsInOrder(@"one", @2, nil), @"Should require strings");
}
- (void)testFailsIfMatchingAgainstNonString
{
assertDoesNotMatch(@"non-string", matcher, @3);
}
- (void)testHasAReadableDescription
{
assertDescription(@"a string containing \"string one\", \"string two\", \"string three\" in order",
matcher);
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
assertNoMismatchDescription(matcher, @"string one then string two followed by string three");
}
- (void)testMismatchDescriptionShowsActualArgument
{
assertMismatchDescription(@"was \"bad\"", matcher, @"bad");
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"was \"bad\"", matcher, @"bad");
}
@end
@@ -0,0 +1,92 @@
//
// OCHamcrest - StringContainsTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCStringContains.h>
// Test support
#import "AbstractMatcherTest.h"
static NSString *EXCERPT = @"EXCERPT";
@interface StringContainsTest : AbstractMatcherTest
@end
@implementation StringContainsTest
{
id <HCMatcher> matcher;
}
- (void)setUp
{
[super setUp];
matcher = containsString(EXCERPT);
}
- (void)tearDown
{
matcher = nil;
[super tearDown];
}
- (id<HCMatcher>)createMatcher
{
return matcher;
}
- (void)testEvaluatesToTrueIfArgumentContainsSpecifiedSubstring
{
assertMatches(@"excerpt at beginning", matcher, [EXCERPT stringByAppendingString:@"END"]);
assertMatches(@"excerpt at end", matcher, [@"START" stringByAppendingString:EXCERPT]);
assertMatches(@"excerpt in middle", matcher,
[[@"START" stringByAppendingString:EXCERPT] stringByAppendingString:@"END"]);
assertMatches(@"excerpt repeated", matcher, [EXCERPT stringByAppendingString:EXCERPT]);
assertDoesNotMatch(@"excerpt not in string", matcher, @"whatever");
assertDoesNotMatch(@"only part of excerpt", matcher, [EXCERPT substringFromIndex:1]);
}
- (void)testEvaluatesToTrueIfArgumentIsEqualToSubstring
{
assertMatches(@"excerpt is entire string", matcher, EXCERPT);
}
- (void)testMatcherCreationRequiresNonNilArgument
{
STAssertThrows(containsString(nil), @"Should require non-nil argument");
}
- (void)testFailsIfMatchingAgainstNonString
{
assertDoesNotMatch(@"non-string", matcher, @3);
}
- (void)testHasAReadableDescription
{
assertDescription(@"a string containing \"EXCERPT\"", matcher);
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
assertNoMismatchDescription(matcher, EXCERPT);
}
- (void)testMismatchDescriptionShowsActualArgument
{
assertMismatchDescription(@"was \"bad\"", matcher, @"bad");
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"was \"bad\"", matcher, @"bad");
}
@end
@@ -0,0 +1,92 @@
//
// OCHamcrest - StringEndsWithTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCStringEndsWith.h>
// Test support
#import "AbstractMatcherTest.h"
static NSString *EXCERPT = @"EXCERPT";
@interface StringEndsWithTest : AbstractMatcherTest
@end
@implementation StringEndsWithTest
{
id <HCMatcher> matcher;
}
- (void)setUp
{
[super setUp];
matcher = endsWith(EXCERPT);
}
- (void)tearDown
{
matcher = nil;
[super tearDown];
}
- (id<HCMatcher>)createMatcher
{
return matcher;
}
- (void)testEvaluatesToTrueIfArgumentContainsSpecifiedSubstring
{
assertDoesNotMatch(@"excerpt at beginning", matcher, [EXCERPT stringByAppendingString:@"END"]);
assertMatches(@"excerpt at end", matcher, [@"START" stringByAppendingString:EXCERPT]);
assertDoesNotMatch(@"excerpt in middle", matcher,
[[@"START" stringByAppendingString:EXCERPT] stringByAppendingString:@"END"]);
assertMatches(@"excerpt repeated", matcher, [EXCERPT stringByAppendingString:EXCERPT]);
assertDoesNotMatch(@"excerpt not in string", matcher, @"whatever");
assertDoesNotMatch(@"only part of excerpt", matcher, [EXCERPT substringFromIndex:1]);
}
- (void)testEvaluatesToTrueIfArgumentIsEqualToSubstring
{
assertMatches(@"excerpt is entire string", matcher, EXCERPT);
}
- (void)testMatcherCreationRequiresNonNilArgument
{
STAssertThrows(endsWith(nil), @"Should require non-nil argument");
}
- (void)testFailsIfMatchingAgainstNonString
{
assertDoesNotMatch(@"non-string", matcher, @3);
}
- (void)testHasAReadableDescription
{
assertDescription(@"a string ending with \"EXCERPT\"", matcher);
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
assertNoMismatchDescription(matcher, EXCERPT);
}
- (void)testMismatchDescriptionShowsActualArgument
{
assertMismatchDescription(@"was \"bad\"", matcher, @"bad");
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"was \"bad\"", matcher, @"bad");
}
@end
@@ -0,0 +1,92 @@
//
// OCHamcrest - StringStartsWithTest.m
// Copyright 2012 hamcrest.org. See LICENSE.txt
//
// Created by: Jon Reid, http://qualitycoding.org/
// Docs: http://hamcrest.github.com/OCHamcrest/
// Source: https://github.com/hamcrest/OCHamcrest
//
// Class under test
#define HC_SHORTHAND
#import <OCHamcrest/HCStringStartsWith.h>
// Test support
#import "AbstractMatcherTest.h"
static NSString *EXCERPT = @"EXCERPT";
@interface StringStartsWithTest : AbstractMatcherTest
@end
@implementation StringStartsWithTest
{
id <HCMatcher> matcher;
}
- (void)setUp
{
[super setUp];
matcher = startsWith(EXCERPT);
}
- (void)tearDown
{
matcher = nil;
[super tearDown];
}
- (id<HCMatcher>)createMatcher
{
return matcher;
}
- (void)testEvaluatesToTrueIfArgumentContainsSpecifiedSubstring
{
assertMatches(@"excerpt at beginning", matcher, [EXCERPT stringByAppendingString:@"END"]);
assertDoesNotMatch(@"excerpt at end", matcher, [@"START" stringByAppendingString:EXCERPT]);
assertDoesNotMatch(@"excerpt in middle", matcher,
[[@"START" stringByAppendingString:EXCERPT] stringByAppendingString:@"END"]);
assertMatches(@"excerpt repeated", matcher, [EXCERPT stringByAppendingString:EXCERPT]);
assertDoesNotMatch(@"excerpt not in string", matcher, @"whatever");
assertDoesNotMatch(@"only part of excerpt", matcher, [EXCERPT substringFromIndex:1]);
}
- (void)testEvaluatesToTrueIfArgumentIsEqualToSubstring
{
assertMatches(@"excerpt is entire string", matcher, EXCERPT);
}
- (void)testMatcherCreationRequiresNonNilArgument
{
STAssertThrows(startsWith(nil), @"Should require non-nil argument");
}
- (void)testFailsIfMatchingAgainstNonString
{
assertDoesNotMatch(@"non-string", matcher, @3);
}
- (void)testHasAReadableDescription
{
assertDescription(@"a string starting with \"EXCERPT\"", matcher);
}
- (void)testSuccessfulMatchDoesNotGenerateMismatchDescription
{
assertNoMismatchDescription(matcher, EXCERPT);
}
- (void)testMismatchDescriptionShowsActualArgument
{
assertMismatchDescription(@"was \"bad\"", matcher, @"bad");
}
- (void)testDescribeMismatch
{
assertDescribeMismatch(@"was \"bad\"", matcher, @"bad");
}
@end