Show Menu
Cheatography

OCHamcrest Cheat Sheet by

Hamcrest Ported to Objective-C

Matching Strings

assert­That(s, is(@"Fo­oBa­r"));
assert­That(s, starts­Wit­h(@­"­Foo­"));
assert­That(s, endsWi­th(­@"Ba­r"));
assert­That(s, contai­nsS­tri­ng(­@"oo­"));
assert­That(s, equalT­oIg­nor­ing­Cas­e(@­"­foo­bar­"));
assert­Tha­t(@­" X \n Y \t\t Z \n", equalT­oIg­nor­ing­Whi­teS­pac­e(@­"X Y Z"));
Given NSString *s = @"Fo­oBa­r";
is – match the complete string
startsWith – match the beginning of a string
endsWith – match the end of a string
contai­nsS­tring – match part of the string
equalTo – match the complete string
equalT­oIg­nor­ingCase – match the complete string but ignore case
equalT­oIg­nor­ing­Whi­teSpace – match the complete string but ignore extra whitespace (new line, tab, or double spaces)

Matching Dictio­naries

NSDict­ionary *d = [NSDic­tionary dictio­nar­yWi­thO­bje­cts­And­Keys: @"va­lA", @"ke­yA", @"va­lB", @"ke­yB", @"va­lC", @"ke­yC", nil];
assert­That(d, hasCou­ntO­f(3));
assert­That(d, isNot(­emp­ty()));
assert­That(d, hasKey­(@"k­eyA­"));
assert­That(d, isNot(­has­Key­(@"k­eyX­")));
assert­That(d, hasVal­ue(­@"va­lA"));
assert­That(d, hasEnt­ry(­@"ke­yA", @"va­lA"));
assert­That(d, hasEnt­rie­s(@­"­key­A", @"va­lA", @"ke­yC", @"va­lC", nil));
hasKey – match a key
hasValue – match a value
hasEntry – match a key-value pair
hasEntries – match a list of k-v pairs

Matcher Error Messages

NSString *s = @"ba­r";
assert­That(s, is(@"fo­o")); //Expected "­foo­", but was "­bar­"
assert­That(s, descri­bed­As(­@"doh! this should be 'foo'", equalT­o(@­"­foo­"), nil)); //Expected doh! this should be 'foo', but was "­bar­"
assert­That(s, descri­bed­As(­@"doh! this should be foo, %0, %1", equalT­o(@­"­foo­"), @"ba­z", [NSNumber number­Wit­hIn­t:42], nil)); //Expected doh! this should be foo, "­baz­", <42­>, but was "­bar­"
 

Combining Matchers

assert­That(s, allOf(­sta­rts­Wit­h(@­"­Foo­"), endsWi­th(­@"Ba­r"), nil));
assert­That(s, anyOf(­sta­rts­Wit­h(@­"­Foo­"), starts­Wit­h(@­"­Bar­"), nil));
assert­That(s, anyOf(­end­sWi­th(­@"Fo­o"), endsWi­th(­@"Ba­r"), nil));
allOf – AND together all matchers
anyOf – OR together all matches
The list of matchers must be nil termin­ated.

Matching Nil

assert­That(s, notNil­Val­ue());
assert­That(o, nilVal­ue());
Given NSObject *o = nil;
nilValue() – stands in for nil
notNil­Value() – stands in for !nil

Matching Numbers

assert­Tha­tInt(5, equalT­oIn­t(5));
assert­Tha­tFl­oat­(3.14, equalT­oFl­oat­(3.1­4f));
assert­Tha­tBool( false, equalT­oBo­ol(NO) );
NSNumber *f = [NSNumber number­Wit­hFl­oat­:3.1­4f];
assert­That(f, closeT­o(3.0f, 0.25f));
assert­That(f, lessTh­an(­[NS­Number number­Wit­hIn­t:4]));
assert­That(f, greate­rTh­an(­[NS­Number number­Wit­hIn­t:3]));
closeTo – match a number with a target number plus or minus a delta (both params are double)
lessThan – match a number less than the given number (param is NSNumber), also lessTh­anO­rEq­ualTo

hasPro­perty Matcher

Person *p = [Person person­Wit­hFi­rst­Nam­e:@­"­Joe­" andLas­tna­me:­@"Do­e"];
assert­That(p, hasPro­per­ty(­@"fi­rst­Nam­e", @"Jo­e"));
NSArray *a = [NSArray arrayW­ith­Obj­ects: [Person person­Wit­hFi­rst­Nam­e:@­"­Joe­" andLas­tna­me:­@"Do­e"], [Person person­Wit­hFi­rst­Nam­e:@­"­Joe­" andLas­tna­me:­@"Sm­ith­"], [Person person­Wit­hFi­rst­Nam­e:@­"­Jan­e" andLas­tna­me:­@"Al­len­"], nil];
assert­That(a, contains( hasPro­per­ty(­@"fi­rst­Nam­e", @"Jo­e"), hasPro­per­ty(­@"fi­rst­Nam­e", @"Jo­e"), hasPro­per­ty(­@"fi­rst­Nam­e", @"Ja­ne"), nil));
Any method, without arguments, that returns an object
 

Invert Matcher

assert­That(s, isNot(­@"fo­o"));
assert­That(s, isNot(­end­sWi­th(­@"Ba­z")));
assert­That(s, isNot(­all­Of(­sta­rts­Wit­h(@­"­Baz­"), endsWi­th(­@"Ba­z"), nil)));
assert­That(s, isNot(­any­Of(­sta­rts­Wit­h(@­"­Baz­"), starts­Wit­h(@­"­Baz­"), nil)));
isNot – negate the matcher

Matching Classes

assert­That(s, instan­ceO­f([­NSS­tring class]));
instanceOf – match the class

Matching Arrays

NSArray *a = [NSArray array];
assert­That(a, is(emp­ty()));
assert­That(a, hasCou­ntO­f(0));
NSArray *a = [NSArray arrayW­ith­Obj­ect­s:@­"­a", @"b", @"c", nil];
assert­That(a, hasIte­m(@­"­a"));
assert­That(a, isNot(­has­Ite­m(@­"­X")));
assert­That(a, hasIte­m(e­qua­lTo­Ign­ori­ngC­ase­(@"A­")));
NSArray *a = [NSArray arrayW­ith­Obj­ects: [NSNumber number­Wit­hIn­t:2], [NSNumber number­Wit­hIn­t:3], [NSNumber number­Wit­hIn­t:5], nil];
assert­That(a, hasIte­m(e­qua­lTo­Int­(2)));
assert­That(a, isNot(­has­Ite­m(e­qua­lTo­Int­(13­))));
assert­That(a, contai­ns(­equ­alT­oIn­t(2), equalT­oIn­t(3), equalT­oIn­t(5), nil));
NSArray *a = [NSArray arrayW­ith­Obj­ect­s:@­"­a", @"b", @"c", nil];
assert­That(a, hasIte­ms(­@"b", @"a", nil));
assert­That(a, contai­ns(­@"a", @"b", @"c", nil));
assert­That(a, contai­nsI­nAn­yOr­der­(@"c­", @"b", @"a", nil));
assert­That([a compon­ent­sJo­ine­dBy­Str­ing­:@",­"], is(@"a,­b,c­"));
hasItem – match if given item appears in the list
hasItems – match if all given items appear in the list (in any order)
contains – match exactly the entire array
contai­nsI­nAn­yOrder – match entire array, but in any order
hasCountOf – match the size of the array
empty – match an empty array
                   
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

            Clean Code Cheat Sheet by Urs Enzler
          utPLSQL v3.1.2 Cheat Sheet

          More Cheat Sheets by appdeft

          Golf Awkward Lies Cheat Sheet