Page 1 of 1

Syntactic sugar to make returning values less painful

PostPosted: 18 May 2011, 18:27
by dmaclach
I find returning constant values (especially bools) to be quite unintuitive in OCMock as it's currently implemented.

Before I put work into it, would people be aghast if we added the following to OCMockRecorder:

- (id)andReturnYes;
- (id)andReturnNo;

I'd even prefer if we had:
- (id)andReturnChar:(char)value;
- (id)andReturnUnsignedChar:(unsigned char)value;
- (id)andReturnShort:(short)value;
- (id)andReturnUnsignedShort:(unsigned short)value;
- (id)andReturnInt:(int)value;
- (id)andReturnUnsignedInt:(unsigned int)value;
- (id)andReturnLong:(long)value;
- (id)andReturnUnsignedLong:(unsigned long)value;
- (id)andReturnLongLong:(long long)value;
- (id)andReturnUnsignedLongLong:(unsigned long long)value;
- (id)andReturnFloat:(float)value;
- (id)andReturnDouble:(double)value;
- (id)andReturnBool:(BOOL)value;
#if MAC_OS_X_VERSION_10_5 <= MAC_OS_X_VERSION_MAX_ALLOWED
- (id)andReturnInteger:(NSInteger)value;
- (id)andReturnUnsignedInteger:(NSUInteger)value;
#endif

and possibly
- (id)andReturnNSRect:(NSRect)rect;
- (id)andReturnCGRect:(CGRect)rect;
- (id)andReturnNSPoint:(NSPoint)point;
- (id)andReturnCGPoint:(CGPoint)point;

I find doing things like
BOOL yes = YES;
[[[mock stub] andReturnValue:OCMOCK_VALUE(yes)] didSomething]

to be really odd and
[[[mock stub] andReturnYes] didSomething]

reads significantly better.

Same with

[[[mock stub] andReturnLong:5] getLength];

as opposed to
long five = 5;
[[[mock stub] andReturnValue:OCMOCK_VALUE(five)] didSomething]

I realize that people can argue that the "andReturnBlahs" can grow without bounds, but this would cover 99% of the cases, and is similar to the interfaces already exposed by NSNumber and NSValue.

Re: Syntactic sugar to make returning values less painful

PostPosted: 26 May 2011, 11:38
by erik
I like the idea.

erik

Re: Syntactic sugar to make returning values less painful

PostPosted: 13 Jun 2011, 19:45
by chrispix
Yes, this would be great.