Permalink
Browse files

move to NSObject

  • Loading branch information...
1 parent 7f765a2 commit 5dc2cd8282cb6b406492cdd0c8333f9b6b6c5959 @katsuyoshi committed Feb 23, 2012
@@ -44,11 +44,12 @@ - (void)timeShouldBe1Second
{
__block int second = 0;
- [IUTSurikae surikaeWithClassName:@"NSDate" methodName:@"+date" surikae:^()
+ [NSDate surikaeWithSelector:@selector(date)
+ surikae:^()
{
return [NSDate dateWithYear:2012 month:2 day:17 hour:0 minute:0 second:second];
}
- context:^()
+ context:^()
{
[self.stopWatch start];
second = 1;
@@ -62,11 +63,12 @@ - (void)timeShouldBe1SecondAfterMore1Scond
{
__block int second = 0;
- [IUTSurikae surikaeWithClassName:@"NSDate" methodName:@"+date" surikae:^()
+ [NSDate surikaeWithSelector:@selector(date)
+ surikae:^()
{
return [NSDate dateWithYear:2012 month:2 day:17 hour:0 minute:0 second:second];
}
- context:^()
+ context:^()
{
[self.stopWatch start];
second = 1;
@@ -81,11 +83,12 @@ - (void)timeShouldBe1SecondEvneIfItsNotStopping
{
__block int second = 0;
- [IUTSurikae surikaeWithClassName:@"NSDate" methodName:@"+date" surikae:^()
+ [NSDate surikaeWithSelector:@selector(date)
+ surikae:^()
{
return [NSDate dateWithYear:2012 month:2 day:17 hour:0 minute:0 second:second];
}
- context:^()
+ context:^()
{
[self.stopWatch start];
second = 1;
@@ -98,11 +101,12 @@ - (void)timeShouldBe1SecondEvenIfStartTwice
{
__block int second = 0;
- [IUTSurikae surikaeWithClassName:@"NSDate" methodName:@"+date" surikae:^()
+ [NSDate surikaeWithSelector:@selector(date)
+ surikae:^()
{
return [NSDate dateWithYear:2012 month:2 day:17 hour:0 minute:0 second:second];
}
- context:^()
+ context:^()
{
[self.stopWatch start];
second = 1;
@@ -116,11 +120,12 @@ - (void)timeShouldBeZeroBeforeToStartForTheFirstTime
{
__block int second = 0;
- [IUTSurikae surikaeWithClassName:@"NSDate" methodName:@"+date" surikae:^()
+ [NSDate surikaeWithSelector:@selector(date)
+ surikae:^()
{
return [NSDate dateWithYear:2012 month:2 day:17 hour:0 minute:0 second:second];
}
- context:^()
+ context:^()
{
ASSERT_EQUAL_DOUBLE(0.0, self.stopWatch.time);
}
@@ -131,11 +136,12 @@ - (void)timeShouldBeZeroWhenStopWithoutStarting
{
__block int second = 0;
- [IUTSurikae surikaeWithClassName:@"NSDate" methodName:@"+date" surikae:^()
+ [NSDate surikaeWithSelector:@selector(date)
+ surikae:^()
{
return [NSDate dateWithYear:2012 month:2 day:17 hour:0 minute:0 second:second];
}
- context:^()
+ context:^()
{
[self.stopWatch stop];
ASSERT_EQUAL_DOUBLE(0.0, self.stopWatch.time);
@@ -30,9 +30,12 @@ - (void)setUp
second = 0;
- [IUTSurikae registedSurikaeWithClassName:@"NSDate" methodName:@"+date" surikae:^(){
- return [NSDate dateWithYear:2012 month:2 day:17 hour:0 minute:0 second:second];
- }];
+ [NSDate registedSurikaeWithSelector:@selector(date)
+ surikae:^()
+ {
+ return [NSDate dateWithYear:2012 month:2 day:17 hour:0 minute:0 second:second];
+ }
+ ];
self.stopWatch = [StopWatch new];
@@ -42,7 +45,7 @@ - (void)tearDown
{
self.stopWatch = nil;
- [IUTSurikae clearAll];
+ [NSDate removeAllSurikaes];
[super tearDown];
}
@@ -23,19 +23,22 @@ - (void)setUp
{
[super setUp];
- [IUTSurikae registedSurikaeWithClassName:@"NSURLConnection" methodName:@"+connectionWithRequest:delegate:" surikae:^(id object, NSURLRequest *request, id delegate) {
- self.request = request;
- // return NSURLConnection which isn't starting to connect.
- return [[[NSURLConnection alloc] initWithRequest:request delegate:delegate startImmediately:NO] autorelease];
- }];
+ [NSURLConnection registedSurikaeWithSelector:@selector(connectionWithRequest:delegate:)
+ surikae:^(id object, NSURLRequest *request, id delegate)
+ {
+ self.request = request;
+ // return NSURLConnection which isn't starting to connect.
+ return [[[NSURLConnection alloc] initWithRequest:request delegate:delegate startImmediately:NO] autorelease];
+ }
+ ];
self.weather = [Weather new];
}
- (void)tearDown
{
// Retrive all exchanged methods.
- [IUTSurikae clearAll];
+ [NSURLConnection removeAllSurikaes];
self.weather = nil;
[super tearDown];
View
@@ -31,19 +31,27 @@ OF SUCH DAMAGE.
@interface IUTSurikae : NSObject
++ (void)clearAll;
-#pragma mark - for local scope
+@end
-+ (void)surikaeWithClassName:(NSString *)className methodName:(NSString *)methodName surikae:(void *)surikaeBlock context:(void (^)(void))contextBlock;
+@interface IUTSurikae(deprecated)
-#pragma mark - for wide scope
++ (void)surikaeWithClassName:(NSString *)className methodName:(NSString *)methodName surikae:(void *)surikaeBlock context:(void (^)(void))contextBlock __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_5,__MAC_10_5,__IPHONE_5_0,__IPHONE_5_0);
++ (IUTSurikae *)registedSurikaeWithClassName:(NSString *)className methodName:(NSString *)methodName surikae:(void *)surikaeBlock __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_5,__MAC_10_5,__IPHONE_5_0,__IPHONE_5_0);
-+ (IUTSurikae *)registedSurikaeWithClassName:(NSString *)className methodName:(NSString *)methodName surikae:(void *)surikaeBlock;
-+ (void)clearAll;
+- (id)initWithClassName:(NSString *)className methodName:(NSString *)methodName surikae:(void *)surikaeBlock __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_5,__MAC_10_5,__IPHONE_5_0,__IPHONE_5_0);
+
+@end
+
+@interface NSObject(IUTSurikae)
++ (void)surikaeWithSelector:(SEL)selector surikae:(void *)surikaeBlock context:(void (^)(void))contextBlock;
+- (void)surikaeWithSelector:(SEL)selector surikae:(void *)surikaeBlock context:(void (^)(void))contextBlock;
-#pragma mark - get an instance
++ (void)registedSurikaeWithSelector:(SEL)selector surikae:(void *)surikaeBlock;
+- (void)registedSurikaeWithSelector:(SEL)selector surikae:(void *)surikaeBlock;
-- (id)initWithClassName:(NSString *)className methodName:(NSString *)methodName surikae:(void *)surikaeBlock;
++ (void)removeAllSurikaes;
@end
View
@@ -172,3 +172,52 @@ - (void)dealloc
}
@end
+
+
+@implementation NSObject(IUTSurikae)
+
++ (void)surikaeWithSelector:(SEL)selector surikae:(void *)surikaeBlock context:(void (^)(void))contextBlock
+{
+ IUTSurikae *surikae = [[IUTSurikae alloc] initWithClassMethod:selector class:self block:surikaeBlock];
+ if (surikae) {
+ @try {
+ contextBlock();
+ } @finally {
+ [surikae release];
+ }
+ }
+}
+
+- (void)surikaeWithSelector:(SEL)selector surikae:(void *)surikaeBlock context:(void (^)(void))contextBlock
+{
+ IUTSurikae *surikae = [[IUTSurikae alloc] initWithInstanceMethod:selector class:[self class] block:surikaeBlock];
+ if (surikae) {
+ @try {
+ contextBlock();
+ } @finally {
+ [surikae release];
+ }
+ }
+}
+
++ (void)registedSurikaeWithSelector:(SEL)selector surikae:(void *)surikaeBlock
+{
+ IUTSurikae *surikae = [[IUTSurikae alloc] initWithClassMethod:selector class:self block:surikaeBlock];
+ [IUTSurikae registSurikae:surikae];
+ [surikae release];
+}
+
+- (void)registedSurikaeWithSelector:(SEL)selector surikae:(void *)surikaeBlock
+{
+ IUTSurikae *surikae = [[IUTSurikae alloc] initWithInstanceMethod:selector class:[self class] block:surikaeBlock];
+ [IUTSurikae registSurikae:surikae];
+ [surikae release];
+}
+
++ (void)removeAllSurikaes
+{
+ [IUTSurikae clearAll];
+}
+
+
+@end
@@ -80,9 +80,10 @@ - (void)testRegistedSurikaeWithClassNameMethodNameWithBlock
#pragma mark - convenience methods
-- (void)testSurikaeWithClassNameClassMethodNameWithLocalBlock
+- (void)testSurikaeWithClassNameClassMethodNameWithSurikaeBlock
{
- [IUTSurikae surikaeWithClassName:@"Foo" methodName:@"+foo" surikae:^()
+ [IUTSurikae surikaeWithClassName:@"Foo" methodName:@"+foo"
+ surikae:^()
{
return @"Bar";
}
@@ -93,10 +94,11 @@ - (void)testSurikaeWithClassNameClassMethodNameWithLocalBlock
ASSERT_EQUAL(@"Foo", [Foo foo]);
}
-- (void)testSurikaeWithClassNameInstanceMethodNameWithLocalBlock
+- (void)testSurikaeWithClassNameInstanceMethodNameWithSurikaeBlock
{
__block Foo *foo = [[Foo new] autorelease];
- [IUTSurikae surikaeWithClassName:@"Foo" methodName:@"-foo" surikae:^()
+ [IUTSurikae surikaeWithClassName:@"Foo" methodName:@"-foo"
+ surikae:^()
{
return @"bar";
}
@@ -107,10 +109,11 @@ - (void)testSurikaeWithClassNameInstanceMethodNameWithLocalBlock
ASSERT_EQUAL(@"foo", [foo foo]);
}
-- (void)testSurikaeWithClassNameInstanceMethodNameWithMinusTypeWithLocalBlock
+- (void)testSurikaeWithClassNameInstanceMethodNameWithMinusTypeWithSurikaeBlock
{
__block Foo *foo = [[Foo new] autorelease];
- [IUTSurikae surikaeWithClassName:@"Foo" methodName:@"foo" surikae:^()
+ [IUTSurikae surikaeWithClassName:@"Foo" methodName:@"foo"
+ surikae:^()
{
return @"bar";
}
@@ -122,6 +125,66 @@ - (void)testSurikaeWithClassNameInstanceMethodNameWithMinusTypeWithLocalBlock
}
+#pragma mark - NSObject category extention
+
+- (void)testSurikaeWithClassMethodWithSurikaeBlockAndContextBlock
+{
+ [Foo surikaeWithSelector:@selector(foo)
+ surikae:^()
+ {
+ return @"Bar";
+ }
+ context:^() {
+ ASSERT_EQUAL(@"Bar", [Foo foo]);
+ }
+ ];
+ ASSERT_EQUAL(@"Foo", [Foo foo]);
+}
+
+- (void)testSurikaeWithInstanceMethodWithSurikaeBlockAndContextBlock
+{
+ Foo *foo = [[Foo new] autorelease];
+ [foo surikaeWithSelector:@selector(foo)
+ surikae:^()
+ {
+ return @"bar";
+ }
+ context:^() {
+ ASSERT_EQUAL(@"bar", [foo foo]);
+ }
+ ];
+ ASSERT_EQUAL(@"foo", [foo foo]);
+}
+
+- (void)testRegistedSurikaeWithClassMethodWithSurikaeBlock
+{
+ [Foo registedSurikaeWithSelector:@selector(foo)
+ surikae:^()
+ {
+ return @"Bar";
+ }
+ ];
+ ASSERT_EQUAL(@"Bar", [Foo foo]);
+ [IUTSurikae clearAll];
+ ASSERT_EQUAL(@"Foo", [Foo foo]);
+}
+
+- (void)testRegistedSurikaeWithInstanceMethodWithSurikaeBlock
+{
+ Foo *foo = [[Foo new] autorelease];
+ [foo registedSurikaeWithSelector:@selector(foo)
+ surikae:^()
+ {
+ return @"bar";
+ }
+ ];
+ ASSERT_EQUAL(@"bar", [foo foo]);
+ [IUTSurikae clearAll];
+ ASSERT_EQUAL(@"foo", [foo foo]);
+}
+
+
+
#pragma mark -
#pragma mark Option

0 comments on commit 5dc2cd8

Please sign in to comment.