Skip to content
This repository
Browse code

move to NSObject

  • Loading branch information...
commit 5dc2cd8282cb6b406492cdd0c8333f9b6b6c5959 1 parent 7f765a2
Katsuyoshi Ito authored February 23, 2012
30  sample/iUnitTest/Tests/StopWatchTest.m
@@ -44,11 +44,12 @@ - (void)timeShouldBe1Second
44 44
 {
45 45
     __block int second = 0;
46 46
 
47  
-    [IUTSurikae surikaeWithClassName:@"NSDate" methodName:@"+date" surikae:^()
  47
+    [NSDate surikaeWithSelector:@selector(date)
  48
+        surikae:^()
48 49
         {
49 50
             return [NSDate dateWithYear:2012 month:2 day:17 hour:0 minute:0 second:second];
50 51
         }
51  
-    context:^()
  52
+        context:^()
52 53
         {
53 54
             [self.stopWatch start];
54 55
             second = 1;
@@ -62,11 +63,12 @@ - (void)timeShouldBe1SecondAfterMore1Scond
62 63
 {
63 64
     __block int second = 0;
64 65
 
65  
-    [IUTSurikae surikaeWithClassName:@"NSDate" methodName:@"+date" surikae:^()
  66
+    [NSDate surikaeWithSelector:@selector(date)
  67
+        surikae:^()
66 68
         {
67 69
             return [NSDate dateWithYear:2012 month:2 day:17 hour:0 minute:0 second:second];
68 70
         }
69  
-    context:^()
  71
+        context:^()
70 72
         {
71 73
             [self.stopWatch start];
72 74
             second = 1;
@@ -81,11 +83,12 @@ - (void)timeShouldBe1SecondEvneIfItsNotStopping
81 83
 {
82 84
     __block int second = 0;
83 85
 
84  
-    [IUTSurikae surikaeWithClassName:@"NSDate" methodName:@"+date" surikae:^()
  86
+    [NSDate surikaeWithSelector:@selector(date)
  87
+        surikae:^()
85 88
         {
86 89
             return [NSDate dateWithYear:2012 month:2 day:17 hour:0 minute:0 second:second];
87 90
         }
88  
-    context:^()
  91
+        context:^()
89 92
         {
90 93
             [self.stopWatch start];
91 94
             second = 1;
@@ -98,11 +101,12 @@ - (void)timeShouldBe1SecondEvenIfStartTwice
98 101
 {
99 102
     __block int second = 0;
100 103
 
101  
-    [IUTSurikae surikaeWithClassName:@"NSDate" methodName:@"+date" surikae:^()
  104
+    [NSDate surikaeWithSelector:@selector(date)
  105
+        surikae:^()
102 106
         {
103 107
             return [NSDate dateWithYear:2012 month:2 day:17 hour:0 minute:0 second:second];
104 108
         }
105  
-    context:^()
  109
+        context:^()
106 110
         {
107 111
             [self.stopWatch start];
108 112
             second = 1;
@@ -116,11 +120,12 @@ - (void)timeShouldBeZeroBeforeToStartForTheFirstTime
116 120
 {
117 121
     __block int second = 0;
118 122
 
119  
-    [IUTSurikae surikaeWithClassName:@"NSDate" methodName:@"+date" surikae:^()
  123
+    [NSDate surikaeWithSelector:@selector(date)
  124
+        surikae:^()
120 125
         {
121 126
             return [NSDate dateWithYear:2012 month:2 day:17 hour:0 minute:0 second:second];
122 127
         }
123  
-    context:^()
  128
+        context:^()
124 129
         {
125 130
             ASSERT_EQUAL_DOUBLE(0.0, self.stopWatch.time);
126 131
         }
@@ -131,11 +136,12 @@ - (void)timeShouldBeZeroWhenStopWithoutStarting
131 136
 {
132 137
     __block int second = 0;
133 138
 
134  
-    [IUTSurikae surikaeWithClassName:@"NSDate" methodName:@"+date" surikae:^()
  139
+    [NSDate surikaeWithSelector:@selector(date)
  140
+        surikae:^()
135 141
         {
136 142
             return [NSDate dateWithYear:2012 month:2 day:17 hour:0 minute:0 second:second];
137 143
         }
138  
-    context:^()
  144
+        context:^()
139 145
         {
140 146
             [self.stopWatch stop];
141 147
             ASSERT_EQUAL_DOUBLE(0.0, self.stopWatch.time);
11  sample/iUnitTest/Tests/StopWatchTest2.m
@@ -30,9 +30,12 @@ - (void)setUp
30 30
     
31 31
     second = 0;
32 32
 
33  
-    [IUTSurikae registedSurikaeWithClassName:@"NSDate" methodName:@"+date" surikae:^(){
34  
-        return [NSDate dateWithYear:2012 month:2 day:17 hour:0 minute:0 second:second];
35  
-    }];
  33
+    [NSDate registedSurikaeWithSelector:@selector(date)
  34
+        surikae:^()
  35
+        {
  36
+            return [NSDate dateWithYear:2012 month:2 day:17 hour:0 minute:0 second:second];
  37
+        }
  38
+    ];
36 39
     
37 40
     
38 41
     self.stopWatch = [StopWatch new];
@@ -42,7 +45,7 @@ - (void)tearDown
42 45
 {
43 46
     self.stopWatch = nil;
44 47
     
45  
-    [IUTSurikae clearAll];
  48
+    [NSDate removeAllSurikaes];
46 49
     
47 50
     [super tearDown];
48 51
 }
15  sample/iUnitTest/Tests/WeatherTest.m
@@ -23,11 +23,14 @@ - (void)setUp
23 23
 {
24 24
     [super setUp];
25 25
         
26  
-    [IUTSurikae registedSurikaeWithClassName:@"NSURLConnection" methodName:@"+connectionWithRequest:delegate:" surikae:^(id object, NSURLRequest *request, id delegate) {
27  
-        self.request = request;
28  
-        // return NSURLConnection which isn't starting to connect.
29  
-        return [[[NSURLConnection alloc] initWithRequest:request delegate:delegate startImmediately:NO] autorelease];
30  
-    }];
  26
+    [NSURLConnection registedSurikaeWithSelector:@selector(connectionWithRequest:delegate:)
  27
+        surikae:^(id object, NSURLRequest *request, id delegate)
  28
+        {
  29
+            self.request = request;
  30
+            // return NSURLConnection which isn't starting to connect.
  31
+            return [[[NSURLConnection alloc] initWithRequest:request delegate:delegate startImmediately:NO] autorelease];
  32
+        }
  33
+    ];
31 34
     
32 35
     self.weather = [Weather new];
33 36
 }
@@ -35,7 +38,7 @@ - (void)setUp
35 38
 - (void)tearDown
36 39
 {
37 40
     // Retrive all exchanged methods.
38  
-    [IUTSurikae clearAll];
  41
+    [NSURLConnection removeAllSurikaes];
39 42
     
40 43
     self.weather = nil;
41 44
     [super tearDown];
22  surikae/IUTSurikae.h
@@ -31,19 +31,27 @@ OF SUCH DAMAGE.
31 31
 
32 32
 @interface IUTSurikae : NSObject
33 33
 
  34
++ (void)clearAll;
34 35
 
35  
-#pragma mark - for local scope
  36
+@end
36 37
 
37  
-+ (void)surikaeWithClassName:(NSString *)className methodName:(NSString *)methodName surikae:(void *)surikaeBlock context:(void (^)(void))contextBlock;
  38
+@interface IUTSurikae(deprecated)
38 39
 
39  
-#pragma mark - for wide scope
  40
++ (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);
  41
++ (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);
40 42
 
41  
-+ (IUTSurikae *)registedSurikaeWithClassName:(NSString *)className methodName:(NSString *)methodName surikae:(void *)surikaeBlock;
42  
-+ (void)clearAll;
  43
+- (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);
  44
+
  45
+@end
  46
+
  47
+@interface NSObject(IUTSurikae)
43 48
 
  49
++ (void)surikaeWithSelector:(SEL)selector surikae:(void *)surikaeBlock context:(void (^)(void))contextBlock;
  50
+- (void)surikaeWithSelector:(SEL)selector surikae:(void *)surikaeBlock context:(void (^)(void))contextBlock;
44 51
 
45  
-#pragma mark - get an instance
  52
++ (void)registedSurikaeWithSelector:(SEL)selector surikae:(void *)surikaeBlock;
  53
+- (void)registedSurikaeWithSelector:(SEL)selector surikae:(void *)surikaeBlock;
46 54
 
47  
-- (id)initWithClassName:(NSString *)className methodName:(NSString *)methodName surikae:(void *)surikaeBlock;
  55
++ (void)removeAllSurikaes;
48 56
 
49 57
 @end
49  surikae/IUTSurikae.m
@@ -172,3 +172,52 @@ - (void)dealloc
172 172
 }
173 173
 
174 174
 @end
  175
+
  176
+
  177
+@implementation NSObject(IUTSurikae)
  178
+
  179
++ (void)surikaeWithSelector:(SEL)selector surikae:(void *)surikaeBlock context:(void (^)(void))contextBlock
  180
+{
  181
+    IUTSurikae *surikae = [[IUTSurikae alloc] initWithClassMethod:selector class:self block:surikaeBlock];
  182
+    if (surikae) {
  183
+        @try {
  184
+            contextBlock();
  185
+        } @finally {
  186
+            [surikae release];
  187
+        }
  188
+    }
  189
+}
  190
+
  191
+- (void)surikaeWithSelector:(SEL)selector surikae:(void *)surikaeBlock context:(void (^)(void))contextBlock
  192
+{
  193
+    IUTSurikae *surikae = [[IUTSurikae alloc] initWithInstanceMethod:selector class:[self class] block:surikaeBlock];
  194
+    if (surikae) {
  195
+        @try {
  196
+            contextBlock();
  197
+        } @finally {
  198
+            [surikae release];
  199
+        }
  200
+    }
  201
+}
  202
+
  203
++ (void)registedSurikaeWithSelector:(SEL)selector surikae:(void *)surikaeBlock
  204
+{
  205
+    IUTSurikae *surikae = [[IUTSurikae alloc] initWithClassMethod:selector class:self block:surikaeBlock];
  206
+    [IUTSurikae registSurikae:surikae];
  207
+    [surikae release];
  208
+}
  209
+
  210
+- (void)registedSurikaeWithSelector:(SEL)selector surikae:(void *)surikaeBlock
  211
+{
  212
+    IUTSurikae *surikae = [[IUTSurikae alloc] initWithInstanceMethod:selector class:[self class] block:surikaeBlock];
  213
+    [IUTSurikae registSurikae:surikae];
  214
+    [surikae release];
  215
+}
  216
+
  217
++ (void)removeAllSurikaes
  218
+{
  219
+    [IUTSurikae clearAll];
  220
+}
  221
+
  222
+
  223
+@end
75  test/SurikaeTest/iUnitTest/Tests/IUTSurikaeTest.m
@@ -80,9 +80,10 @@ - (void)testRegistedSurikaeWithClassNameMethodNameWithBlock
80 80
 
81 81
 #pragma mark - convenience methods
82 82
 
83  
-- (void)testSurikaeWithClassNameClassMethodNameWithLocalBlock
  83
+- (void)testSurikaeWithClassNameClassMethodNameWithSurikaeBlock
84 84
 {
85  
-    [IUTSurikae surikaeWithClassName:@"Foo" methodName:@"+foo" surikae:^()
  85
+    [IUTSurikae surikaeWithClassName:@"Foo" methodName:@"+foo"
  86
+        surikae:^()
86 87
         {
87 88
             return @"Bar";
88 89
         }
@@ -93,10 +94,11 @@ - (void)testSurikaeWithClassNameClassMethodNameWithLocalBlock
93 94
     ASSERT_EQUAL(@"Foo", [Foo foo]);
94 95
 }
95 96
 
96  
-- (void)testSurikaeWithClassNameInstanceMethodNameWithLocalBlock
  97
+- (void)testSurikaeWithClassNameInstanceMethodNameWithSurikaeBlock
97 98
 {
98 99
     __block Foo *foo = [[Foo new] autorelease];
99  
-    [IUTSurikae surikaeWithClassName:@"Foo" methodName:@"-foo" surikae:^()
  100
+    [IUTSurikae surikaeWithClassName:@"Foo" methodName:@"-foo"
  101
+        surikae:^()
100 102
         {
101 103
             return @"bar";
102 104
         }
@@ -107,10 +109,11 @@ - (void)testSurikaeWithClassNameInstanceMethodNameWithLocalBlock
107 109
     ASSERT_EQUAL(@"foo", [foo foo]);
108 110
 }
109 111
 
110  
-- (void)testSurikaeWithClassNameInstanceMethodNameWithMinusTypeWithLocalBlock
  112
+- (void)testSurikaeWithClassNameInstanceMethodNameWithMinusTypeWithSurikaeBlock
111 113
 {
112 114
     __block Foo *foo = [[Foo new] autorelease];
113  
-    [IUTSurikae surikaeWithClassName:@"Foo" methodName:@"foo" surikae:^()
  115
+    [IUTSurikae surikaeWithClassName:@"Foo" methodName:@"foo"
  116
+        surikae:^()
114 117
         {
115 118
             return @"bar";
116 119
         }
@@ -122,6 +125,66 @@ - (void)testSurikaeWithClassNameInstanceMethodNameWithMinusTypeWithLocalBlock
122 125
 }
123 126
 
124 127
 
  128
+#pragma mark - NSObject category extention
  129
+
  130
+- (void)testSurikaeWithClassMethodWithSurikaeBlockAndContextBlock
  131
+{
  132
+    [Foo surikaeWithSelector:@selector(foo)
  133
+        surikae:^()
  134
+        {
  135
+            return @"Bar";
  136
+        }
  137
+        context:^() {
  138
+            ASSERT_EQUAL(@"Bar", [Foo foo]);
  139
+        }
  140
+    ];
  141
+    ASSERT_EQUAL(@"Foo", [Foo foo]);
  142
+}
  143
+
  144
+- (void)testSurikaeWithInstanceMethodWithSurikaeBlockAndContextBlock
  145
+{
  146
+    Foo *foo = [[Foo new] autorelease];
  147
+    [foo surikaeWithSelector:@selector(foo)
  148
+        surikae:^()
  149
+        {
  150
+            return @"bar";
  151
+        }
  152
+        context:^() {
  153
+            ASSERT_EQUAL(@"bar", [foo foo]);
  154
+        }
  155
+    ];
  156
+    ASSERT_EQUAL(@"foo", [foo foo]);
  157
+}
  158
+
  159
+- (void)testRegistedSurikaeWithClassMethodWithSurikaeBlock
  160
+{
  161
+    [Foo registedSurikaeWithSelector:@selector(foo)
  162
+        surikae:^()
  163
+        {
  164
+            return @"Bar";
  165
+        }
  166
+    ];
  167
+    ASSERT_EQUAL(@"Bar", [Foo foo]);
  168
+    [IUTSurikae clearAll];
  169
+    ASSERT_EQUAL(@"Foo", [Foo foo]);
  170
+}
  171
+
  172
+- (void)testRegistedSurikaeWithInstanceMethodWithSurikaeBlock
  173
+{
  174
+    Foo *foo = [[Foo new] autorelease];
  175
+    [foo registedSurikaeWithSelector:@selector(foo)
  176
+        surikae:^()
  177
+        {
  178
+            return @"bar";
  179
+        }
  180
+    ];
  181
+    ASSERT_EQUAL(@"bar", [foo foo]);
  182
+    [IUTSurikae clearAll];
  183
+    ASSERT_EQUAL(@"foo", [foo foo]);
  184
+}
  185
+
  186
+
  187
+
125 188
 #pragma mark -
126 189
 #pragma mark Option
127 190
 

0 notes on commit 5dc2cd8

Please sign in to comment.
Something went wrong with that request. Please try again.