Skip to content

Commit

Permalink
Merge pull request #245 from pivotal-brian-croom/and_return_nil
Browse files Browse the repository at this point in the history
Allow returning `nil` from stubs on appropriate methods
  • Loading branch information
akitchen committed May 29, 2014
2 parents 564c5dd + 33d32b8 commit 271f0d0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Source/Doubles/StubbedMethod.mm
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
void StubbedMethod::verify_return_value_type(id instance) const {
if (this->has_return_value()) {
const char * const methodReturnType = [[instance methodSignatureForSelector:this->selector()] methodReturnType];
if (!this->return_value().matches_encoding(methodReturnType)) {
if (!this->return_value().compatible_with_encoding(methodReturnType)) {
[[NSException exceptionWithName:NSInternalInconsistencyException
reason:[NSString stringWithFormat:@"Invalid return value type '%@' instead of '%@' for <%@>", [CDRTypeUtilities typeNameForEncoding:this->return_value().value_encoding()], [CDRTypeUtilities typeNameForEncoding:methodReturnType], NSStringFromSelector(this->selector())]
userInfo:nil] raise];
Expand Down
20 changes: 16 additions & 4 deletions Source/Headers/Doubles/Arguments/ReturnValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Cedar { namespace Doubles {

virtual const char * const value_encoding() const = 0;
virtual void * value_bytes() const = 0;
virtual bool matches_encoding(const char *) const = 0;
virtual bool compatible_with_encoding(const char *) const = 0;

typedef std::shared_ptr<ReturnValue> shared_ptr_t;
};
Expand All @@ -26,7 +26,10 @@ namespace Cedar { namespace Doubles {

virtual const char * const value_encoding() const;
virtual void * value_bytes() const;
virtual bool matches_encoding(const char *) const;
virtual bool compatible_with_encoding(const char *) const;

private:
bool matches_encoding(const char *)const;

private:
const T value_;
Expand All @@ -49,8 +52,17 @@ namespace Cedar { namespace Doubles {
}

template<typename T>
/* virtual */ bool TypedReturnValue<T>::matches_encoding(const char * actual_argument_encoding) const {
return 0 == strcmp(@encode(T), actual_argument_encoding);
/* virtual */ bool TypedReturnValue<T>::compatible_with_encoding(const char * actual_argument_encoding) const {
return matches_encoding(actual_argument_encoding);
}

template<>
/* virtual */ inline bool TypedReturnValue<NSInteger>::compatible_with_encoding(const char * actual_argument_encoding) const {
return (value_ == (NSInteger)nil && 0 == strcmp(@encode(id), actual_argument_encoding)) || this->matches_encoding(actual_argument_encoding);
}

template<typename T>
bool TypedReturnValue<T>::matches_encoding(const char * actual_argument_encoding) const {
return 0 == strcmp(@encode(T), actual_argument_encoding);
}
}}
10 changes: 10 additions & 0 deletions Spec/Doubles/CedarDoubleSharedExamples.mm
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,16 @@ myDouble stub_method("incrementByNumber:").with((id)nil).and_do(^(NSInvocation *
memcmp(&returnValue, &returnedValue, sizeof(LargeIncrementerStruct)) should equal(0);
});
});

context(@"with `nil`", ^{
beforeEach(^{
myDouble stub_method("valueAsNumber").and_return(nil);
});

it(@"should return the expected value", ^{
[myDouble valueAsNumber] should be_nil;
});
});
});
});
});
Expand Down
1 change: 1 addition & 0 deletions Spec/Support/SimpleIncrementer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ typedef LargeIncrementerStruct (^ComplexIncrementerBlock)(NSNumber *, LargeIncre
@required
- (size_t)value;
- (size_t)aVeryLargeNumber;
- (NSNumber *)valueAsNumber;
- (void)increment;
- (void)incrementBy:(size_t)amount;
- (void)incrementByNumber:(NSNumber *)number;
Expand Down
4 changes: 4 additions & 0 deletions Spec/Support/SimpleIncrementer.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ - (size_t)aVeryLargeNumber {
return 0x7fffffff;
}

- (NSNumber *)valueAsNumber {
return @(self.value);
}

- (void)incrementBy:(size_t)amount {
self.value += amount;
}
Expand Down

0 comments on commit 271f0d0

Please sign in to comment.