Syntactic sugar to make returning values less painful

Discussion of the OCMock framework. If you have patches we would prefer you to send them to the mailing list, but attaching them to a topic is possible, too.

Syntactic sugar to make returning values less painful

Postby dmaclach » 18 May 2011, 18:27

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.
dmaclach
 

Re: Syntactic sugar to make returning values less painful

Postby erik » 26 May 2011, 11:38

I like the idea.

erik
erik
 
Posts: 90
Joined: 10 Oct 2009, 15:22
Location: Hamburg, Germany

Re: Syntactic sugar to make returning values less painful

Postby chrispix » 13 Jun 2011, 19:45

Yes, this would be great.
chrispix
 
Posts: 7
Joined: 07 May 2010, 19:32


Return to OCMock



cron