Permalink
Browse files

Add `raiseWithReason` matcher for checking that an exception is raise…

…d and its reason matches a given value
  • Loading branch information...
1 parent 21406df commit a6cf8b4f4b246bb81f268f4248037ea412238942 @blakewatters blakewatters committed with petejkim Nov 25, 2012
@@ -7,6 +7,12 @@
objects = {
/* Begin PBXBuildFile section */
+ 2546A95C16629D500078E044 /* EXPMatchers+raiseWithReason.h in Headers */ = {isa = PBXBuildFile; fileRef = 2546A95A16629D4F0078E044 /* EXPMatchers+raiseWithReason.h */; };
+ 2546A95D16629D500078E044 /* EXPMatchers+raiseWithReason.h in Headers */ = {isa = PBXBuildFile; fileRef = 2546A95A16629D4F0078E044 /* EXPMatchers+raiseWithReason.h */; };
+ 2546A95E16629D500078E044 /* EXPMatchers+raiseWithReason.m in Sources */ = {isa = PBXBuildFile; fileRef = 2546A95B16629D4F0078E044 /* EXPMatchers+raiseWithReason.m */; };
+ 2546A95F16629D500078E044 /* EXPMatchers+raiseWithReason.m in Sources */ = {isa = PBXBuildFile; fileRef = 2546A95B16629D4F0078E044 /* EXPMatchers+raiseWithReason.m */; };
+ 2546A96216629DF70078E044 /* EXPMatchers+raiseWithReasonTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 2546A96116629DF70078E044 /* EXPMatchers+raiseWithReasonTest.m */; };
+ 2546A96316629DF70078E044 /* EXPMatchers+raiseWithReasonTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 2546A96116629DF70078E044 /* EXPMatchers+raiseWithReasonTest.m */; };
4913B4C71411E01A00040ECB /* EXPMatchers+beGreaterThanTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 4913B4C61411E01A00040ECB /* EXPMatchers+beGreaterThanTest.m */; };
4913B4C81411E01A00040ECB /* EXPMatchers+beGreaterThanTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 4913B4C61411E01A00040ECB /* EXPMatchers+beGreaterThanTest.m */; };
4913B4CC1411E18A00040ECB /* EXPMatchers+beGreaterThan.h in Headers */ = {isa = PBXBuildFile; fileRef = 4913B4CA1411E18A00040ECB /* EXPMatchers+beGreaterThan.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -205,6 +211,9 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
+ 2546A95A16629D4F0078E044 /* EXPMatchers+raiseWithReason.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "EXPMatchers+raiseWithReason.h"; sourceTree = "<group>"; };
+ 2546A95B16629D4F0078E044 /* EXPMatchers+raiseWithReason.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "EXPMatchers+raiseWithReason.m"; sourceTree = "<group>"; };
+ 2546A96116629DF70078E044 /* EXPMatchers+raiseWithReasonTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "EXPMatchers+raiseWithReasonTest.m"; sourceTree = "<group>"; };
4913B4C61411E01A00040ECB /* EXPMatchers+beGreaterThanTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "EXPMatchers+beGreaterThanTest.m"; sourceTree = "<group>"; };
4913B4CA1411E18A00040ECB /* EXPMatchers+beGreaterThan.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "EXPMatchers+beGreaterThan.h"; sourceTree = "<group>"; };
4913B4CB1411E18A00040ECB /* EXPMatchers+beGreaterThan.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "EXPMatchers+beGreaterThan.m"; sourceTree = "<group>"; };
@@ -454,6 +463,8 @@
63F6F354150A542D009E1BC3 /* EXPMatchers+beCloseTo.m */,
E9D3DF14157A7EB40054978E /* EXPMatchers+raise.h */,
E9D3DF11157A7EA30054978E /* EXPMatchers+raise.m */,
+ 2546A95A16629D4F0078E044 /* EXPMatchers+raiseWithReason.h */,
+ 2546A95B16629D4F0078E044 /* EXPMatchers+raiseWithReason.m */,
);
path = matchers;
sourceTree = "<group>";
@@ -488,6 +499,7 @@
E9ACDF7513B2DEB70010F4D7 /* EXPMatchers+beFalsyTest.m */,
E9ACDF7B13B2DEB70010F4D7 /* EXPMatchers+equalTest.m */,
E9D3DF0D157A7B7E0054978E /* EXPMatchers+raiseTest.m */,
+ 2546A96116629DF70078E044 /* EXPMatchers+raiseWithReasonTest.m */,
E94296F313B42E160038708B /* EXPMatchers+containTest.m */,
63B349E215135EB100C955DC /* EXPMatchers+haveCountOfTest.m */,
4913B4C61411E01A00040ECB /* EXPMatchers+beGreaterThanTest.m */,
@@ -575,6 +587,7 @@
A30575641520BDCE00DA19BD /* EXPBlockDefinedMatcher.h in Headers */,
E95368521521BEE900AA3B81 /* EXPBackwardCompatibility.h in Headers */,
E95368911521C58D00AA3B81 /* EXPDefines.h in Headers */,
+ 2546A95D16629D500078E044 /* EXPMatchers+raiseWithReason.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -613,6 +626,7 @@
A30575631520BDCE00DA19BD /* EXPBlockDefinedMatcher.h in Headers */,
E95368511521BEE900AA3B81 /* EXPBackwardCompatibility.h in Headers */,
E95368921521C58D00AA3B81 /* EXPDefines.h in Headers */,
+ 2546A95C16629D500078E044 /* EXPMatchers+raiseWithReason.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -798,6 +812,7 @@
A30575661520BDCE00DA19BD /* EXPBlockDefinedMatcher.m in Sources */,
E95368561521BFDB00AA3B81 /* EXPBackwardCompatibility.m in Sources */,
E9D3DF13157A7EA30054978E /* EXPMatchers+raise.m in Sources */,
+ 2546A95F16629D500078E044 /* EXPMatchers+raiseWithReason.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -832,6 +847,7 @@
A30575AB1520F61500DA19BD /* DynamicPredicateMatcherTest.m in Sources */,
E9D3DF0F157A7B7E0054978E /* EXPMatchers+raiseTest.m in Sources */,
E9D3DF1C157A8AC30054978E /* EXPMatchers+beCloseToTest.m in Sources */,
+ 2546A96316629DF70078E044 /* EXPMatchers+raiseWithReasonTest.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -866,6 +882,7 @@
A30575651520BDCE00DA19BD /* EXPBlockDefinedMatcher.m in Sources */,
E95368551521BFDB00AA3B81 /* EXPBackwardCompatibility.m in Sources */,
E9D3DF12157A7EA30054978E /* EXPMatchers+raise.m in Sources */,
+ 2546A95E16629D500078E044 /* EXPMatchers+raiseWithReason.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -900,6 +917,7 @@
A30575A91520F5FE00DA19BD /* DynamicPredicateMatcherTest.m in Sources */,
E9D3DF0E157A7B7E0054978E /* EXPMatchers+raiseTest.m in Sources */,
E9D3DF1B157A8AC30054978E /* EXPMatchers+beCloseToTest.m in Sources */,
+ 2546A96216629DF70078E044 /* EXPMatchers+raiseWithReasonTest.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -0,0 +1,3 @@
+#import "Expecta.h"
+
+EXPMatcherInterface(raiseWithReason, (NSString *expectedExceptionName, NSString *expectedReason));
@@ -0,0 +1,35 @@
+#import "EXPMatchers+raiseWithReason.h"
+#import "EXPDefines.h"
+
+EXPMatcherImplementationBegin(raiseWithReason, (NSString *expectedExceptionName, NSString *expectedReason)) {
+ __block NSException *exceptionCaught = nil;
+
+ match(^BOOL{
+ BOOL expectedExceptionCaught = NO;
+ @try {
+ ((EXPBasicBlock)actual)();
+ } @catch(NSException *e) {
+ exceptionCaught = e;
+ expectedExceptionCaught = (((expectedExceptionName == nil) || [[exceptionCaught name] isEqualToString:expectedExceptionName]) &&
+ ((expectedReason == nil) || ([[exceptionCaught reason] isEqualToString:expectedReason])));
+ }
+ return expectedExceptionCaught;
+ });
+
+ failureMessageForTo(^NSString *{
+ return [NSString stringWithFormat:@"expected: %@ (%@), got: %@ (%@)",
+ expectedExceptionName ?: @"any exception",
+ expectedReason ?: @"any reason",
+ exceptionCaught ? [exceptionCaught name] : @"no exception",
+ exceptionCaught ? [exceptionCaught reason] : @""];
+ });
+
+ failureMessageForNotTo(^NSString *{
+ return [NSString stringWithFormat:@"expected: %@ (%@), got: %@ (%@)",
+ expectedExceptionName ? [NSString stringWithFormat:@"not %@", expectedExceptionName] : @"no exception",
+ expectedReason ? [NSString stringWithFormat:@"not '%@'", expectedReason] : @"no reason",
+ exceptionCaught ? [exceptionCaught name] : @"no exception",
+ exceptionCaught ? [exceptionCaught reason] : @"no reason"];
+ });
+}
+EXPMatcherImplementationEnd
@@ -15,3 +15,4 @@
#import "EXPMatchers+beInTheRangeOf.h"
#import "EXPMatchers+beCloseTo.h"
#import "EXPMatchers+raise.h"
+#import "EXPMatchers+raiseWithReason.h"
@@ -0,0 +1,44 @@
+#import "TestHelper.h"
+
+@interface EXPMatchers_raiseWithReasonTest : SenTestCase
+@end
+
+@implementation EXPMatchers_raiseWithReasonTest
+
+- (void)test_raiseWithReason {
+ assertPass(test_expect(^{
+ [NSException raise:@"TestException" format:@"This is the reason"];
+ }).to.raiseWithReason(@"TestException", @"This is the reason"));
+
+ assertFail(test_expect(^{
+ // not raising...
+ }).to.raiseWithReason(@"TestException", @"This is the reason"), @"expected: TestException (This is the reason), got: no exception ()");
+
+ assertFail(test_expect(^{
+ NSException *exception = [NSException exceptionWithName:@"AnotherException" reason:@"This is the reason" userInfo:nil];
+ [exception raise];
+ }).to.raiseWithReason(@"TestException", @"This is the reason"), @"expected: TestException (This is the reason), got: AnotherException (This is the reason)");
+}
+
+- (void)test_toNot_raiseWithReason {
+ assertFail(test_expect(^{
+ [NSException raise:@"TestException" format:@"This is the reason"];
+ }).notTo.raiseWithReason(@"TestException", @"This is the reason"), @"expected: not TestException (not 'This is the reason'), got: TestException (This is the reason)");
+
+ assertPass(test_expect(^{
+ // Different reason text than expected
+ [NSException raise:@"TestException" format:@"A different reason"];
+ }).notTo.raiseWithReason(@"TestException", @"This is the reason"));
+
+ assertPass(test_expect(^{
+ // not raising...
+ }).notTo.raiseWithReason(@"TestException", @"This is the reason"));
+
+ assertPass(test_expect(^{
+ // Different exception class
+ NSException *exception = [NSException exceptionWithName:@"AnotherException" reason:nil userInfo:nil];
+ [exception raise];
+ }).notTo.raiseWithReason(@"TestException", @"This is the reason"));
+}
+
+@end

0 comments on commit a6cf8b4

Please sign in to comment.