How to skip forwards and backwards command line Mac OSX

Leave a comment

To move faster on the command line edit your terminal profile preferences and check the ‘Use Option as Meta key’ checkbox.

This will let you then move forwards and backwards on the command line with Option + B/F.

Screen Shot 2015-10-02 at 7.08.47 AM

OCMockito Examples

Leave a comment

Here are some examples I copied from the README files of the webs site for learning. Go here to see a latest documentation

#import <XCTest/XCTest.h>
#import <OCHamcrestIOS/OCHamcrestIOS.h>

#import <OCMockitoIOS/OCMockitoIOS.h>

#import <XCTest/XCTest.h>

#import "WeatherService.h"

@interface Examples : XCTestCase


@implementation Examples

- (void)testMocking {
    // mock creation
    NSMutableArray *mockArray = mock([NSMutableArray class]);
    // using mock object
    [mockArray addObject:@"one"];
    [mockArray removeAllObjects];
    // verification
    [verify(mockArray) addObject:@"one"]; // verify -> MKTVerify if 'Bad Receiver type 'int'
    [verify(mockArray) removeAllObjects];
    // Once created, the mock will remember all interactions.
    // Then you can selectively verify whatever interactions you are interested in later.

- (void)testStubbing {
    // mock creation
    NSArray *mockArray = mock([NSArray class]);
    // stubbing
    [given([mockArray objectAtIndex:0]) willReturn:@"first"];
    [given([mockArray objectAtIndex:1]) willThrow:[NSException exceptionWithName:@"name"
    // following prints "first"
    NSLog(@"%@", [mockArray objectAtIndex:0]);
    // follows throws exception
//    NSLog(@"%@", [mockArray objectAtIndex:1]);
    // following prints "(null)" because objectAtIndex:999 was not stubbed
    NSLog(@"%@", [mockArray objectAtIndex:999]);

- (void)testProtocol {
    id <WeatherServiceDelegate> delegate = mockProtocol(@protocol(WeatherServiceDelegate));

- (void)testStubMethodsThatReturnPrimitives {
    NSMutableArray *mockArray = mock([NSMutableArray class]);
    [given([mockArray count]) willReturn:@3];

- (void)testArgumentMatchers {
    // OCMockito normally verifies based on exact equality. But you can also do this.
    // mock creation
    NSMutableArray *mockArray = mock([NSMutableArray class]);

    // using mock object
    [mockArray removeObject:@"This is a test"];

    // verification
    [verify(mockArray) removeObject:startsWith(@"This is")];


- (void)testSpecifyMatchersForNonObjectArguments {
    // to stub a methd that takes a non-object arg, invoke method with dummy then can withMatcher:forArgument
    NSMutableArray *mockArray = mock([NSMutableArray class]);

    [[given([mockArray objectAtIndex:0]) withMatcher:anything() forArgument:0]
    [[given([mockArray objectAtIndex:0]) withMatcher:anything()]

- (void)testExactNumberOfInnovations {
    NSMutableArray *mockArray = mock([NSMutableArray class]);

    // using mock
    [mockArray addObject:@"once"];

    [mockArray addObject:@"twice"];
    [mockArray addObject:@"twice"];

    // the following two verifications work exactly the same
    [verify(mockArray) addObject:@"once"];
    [verifyCount(mockArray, times(1)) addObject:@"once"];

    // verify exact number of invocations
    [verifyCount(mockArray, times(2)) addObject:@"twice"];
    [verifyCount(mockArray, times(0)) addObject:@"three times"];

    // verify using never(), which is an alias for times(0)
    [verifyCount(mockArray, never()) addObject:@"never happened"];

    // verify using atLeast()/atMost()
    [verifyCount(mockArray, atLeastOnce()) addObject:@"once"];
    [verifyCount(mockArray, atLeast(2)) addObject:@"twice"];
//    [verifyCount(mockArray, atMost(5)) addObject:@"at most five times"];

- (void)testCanStubConsecutiveCalls {

    // is you ever need to stub consequtive calls together you can do it like this
//    [[given([mockObject someMethod:@"some arg"])
//      willThrow:[NSException exceptionWithName:@"name" reason:@"reason" userInfo:nil]]
//     willReturn:@"foo"];

    // First call: throws exception
//    [mockObject someMethod:@"some arg"];

    // Second call: prints "foo"
//    NSLog(@"%@", [mockObject someMethod:@"some arg"]);

    // Any consecutive call: prints "foo" as well. (Last stubbing wins.)
//    NSLog(@"%@", [mockObject someMethod:@"some arg"]);



// Links
// Bad Receiver Type

Coding guidelines for Cocoa

Leave a comment

Here are some recommended coding guidelines from Apple for Cocoa.

Here are some I have written down to help me remember.

Good Coding conventions


== Naming methods

For methods representing actions objects take start with a verb
– (void)invokeWithTarget:(id)target;
– (void)selectTabViewItem:(NSTabViewItem *)tabViewItem

If a method returns an attribute name the method after the attribute. No gets.
– (NSSize)cellSize;

Use keywords before all arguments
– (void)sendAction:(SEL)aSelector toObject:(id)anObject forAllCells:(BOOL)flag; — good
– (void)sendAction:(SEL)aSelector :(id)anObject :(BOOL)flag; — bad

Make the word before the argument describe the argument
– (id)viewWithTag:(NSInteger)aTag; – good
– (id)taggedView:(int)aTag; — bad

Add new keywords to the end of existing methods when you create a method more specific
– (id)initWithFrame:(CGRect)frameRect;
– (id)initWithFrame:(NSRect)frameRect mode:(int)aMode cellClass:(Class)factoryId numberOfRows:(int)rowsHigh numberOfColumns:(int)colsWide;

And don’t use and to link keywords
– (int)runModalForDirectory:(NSString *)path file:(NSString *) name types:(NSArray *)fileTypes; — right
– (int)runModalForDirectory:(NSString *)path andFile:(NSString *)name andTypes:(NSArray *)fileTypes; — wrong

If the method describe two separate actions, can use and to link them
– (BOOL)openFile:(NSString *)fullPath withApplication:(NSString *)appName andDeactivate:(BOOL)flag;

== Accessor Methods

Access methods are those that set and return the value of a property of an object.

– (type)noun;
– (void)setNoun:(type)aNoun;

– (NSString *)title;
– (void)setTitle:(NSString *)aTitle;

– (BOOL)isAdjective;
– (void)setAdjective:(BOOL)flag;

– (BOOL)isEditable;
– (void)setEditable:(BOOL)flag;

– (BOOL)verbObject;
– (void)setVerbObject:(BOOL)flag;

– (BOOL)showsAlpha;
– (void)setShowsAlpha:(BOOL)flag;

Verb should be in simple present tense.

Don’t twist a verb into an adjective by using a participle:
– (void)setAcceptsGlyphInfo:(BOOL)flag; — Right.
– (BOOL)acceptsGlyphInfo; — Right.
– (void)setGlyphInfoAccepted:(BOOL)flag; — Wrong.
– (BOOL)glyphInfoAccepted; — Wrong

You may use modal verbs (verbs preceded by “can”, “should”, “will”, and so on) to clarify meaning, but don’t use “do” or “does”.

– (void)setCanHide:(BOOL)flag; — Right.
– (BOOL)canHide; — Right.
– (void)setShouldCloseDocument:(BOOL)flag; — Right.
– (BOOL)shouldCloseDocument; — Right.
– (void)setDoesAcceptGlyphInfo:(BOOL)flag; — Wrong.
– (BOOL)doesAcceptGlyphInfo; — Wrong.

== Delegate Methods

Start the name by identifying the class of the object that’s sending the message
– (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(int)row;
– (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename;

Use ‘did’ or ‘will’ for methods that are invoked to notify the delegate that something has happened or is about to happen.
– (void)browserDidScroll:(NSBrowser *)sender;
– (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window;

How setup OCMockito XCode 7

Leave a comment

This was actually pretty easy. Go to this web site

Download the zip releases from each of these frameworks

Expand them and drag in the iOS framework into your project.

Screen Shot 2015-10-01 at 8.28.56 AM

And then set the ‘Other linker flags’ by double clicking on your blue project icon.

Screen Shot 2015-10-01 at 8.29.55 AM

Then goto ‘Build Settings’ and type ‘Other linker flags’ in the search box and add ‘-ObjC’ to both the debug and release.

Screen Shot 2015-10-01 at 8.31.03 AM

That’s it for setup. Next post I will see if I can get an example going.

Simple OCMock stub example

Leave a comment

Here’s a simple example of stubbing with OCMock.
Go here to see how to setup OCMock in XCode.


#import <Foundation/Foundation.h>
#import "SubscriptionManager.h"

@interface TaxCalculator : NSObject
-(id)initWith:(SubscriptionManager *)subscriptionManager;
- (double)calculateTax;


#import "TaxCalculator.h"

@interface TaxCalculator()
@property (nonatomic, strong) SubscriptionManager *subscriptionManager;

@implementation TaxCalculator

-(id)initWith:(SubscriptionManager *)subscriptionManager {
    if (self = [super init]) {
        self.subscriptionManager = subscriptionManager;
        return self;
    return nil;

-(double)calculateTax {
    if ([self.subscriptionManager isPremium]) {
        return 0.0;
    return 1.0;



#import <Foundation/Foundation.h>

@interface SubscriptionManager : NSObject


#import "SubscriptionManager.h"

@implementation SubscriptionManager
-(BOOL)isPremium {
    return true;


#import <XCTest/XCTest.h>
#import <OCMock/OCMock.h>
#import "TaxCalculator.h"
#import "SubscriptionManager.h"

@interface TaxCalculatorTest : XCTestCase
@property (nonatomic, strong) TaxCalculator *calculator;
@property (nonatomic, strong) SubscriptionManager *mock;

@implementation TaxCalculatorTest

- (void)setUp {
    [super setUp];
    self.mock = OCMClassMock([SubscriptionManager class]);
    self.calculator = [[TaxCalculator alloc] initWith:self.mock];

- (void)testTaxesAreZeroForPremiumUsers {
    OCMStub([self.mock isPremium]).andReturn(true);
    XCTAssertEqual(0.0, [self.calculator calculateTax]);

- (void)testTaxesAreNonZeroForNonPremiumUsers {
    OCMStub([self.mock isPremium]).andReturn(false);
    XCTAssertEqual(1.0, [self.calculator calculateTax]);


How to add OCMock to XCode 7


Based on instructions from here

Down here and extract to somewhere into your project like this.

Screen Shot 2015-09-25 at 8.47.50 AM

Then follow instructions here

Add the library
Screen Shot 2015-09-25 at 8.49.19 AM

Set the linker flags
Screen Shot 2015-09-25 at 8.51.08 AM

Make headers available
Screen Shot 2015-09-25 at 8.50.22 AM

Add code

#import <XCTest/XCTest.h>
#import <OCMock/OCMock.h>
#import "TaxCalculator.h"
#import "SubscriptionManager.h"

@interface TaxCalculatorTest : XCTestCase
@property (nonatomic, strong) TaxCalculator *calculator;

@implementation TaxCalculatorTest

- (void)setUp {
    [super setUp];
    self.calculator = [TaxCalculator new];

- (void)testExample {
    XCTAssertEqual(0, [self.calculator calculateTax]);
    id mockSubscriptionManager = OCMClassMock([SubscriptionManager class]);


How to add IntelliJ Google Style Guide to IntelliJ

Leave a comment

For some reason intelliJ doesn’t let you import code style guides.
Like the Java one I wanted to use from Google.

To import the style guide (intellij-java-google-style.xml) after cloning the repository, to this.

> cd ~/Library/Preferences/IntelliJIdeaXX
> mkdir codestyles
> cp /intellij-java-google-style.xml .

Fire up intelliJ, goto preferences, search for Coding Style, and you should see Google Coding style there.

Screen Shot 2015-08-07 at 2.19.55 PM

Links that help

Older Entries


Get every new post delivered to your Inbox.

Join 334 other followers

%d bloggers like this: