]> git.wincent.com - WOTest.git/blob - NSException+WOTest.m
Fix object-to-pointer comparisons on Leopard
[WOTest.git] / NSException+WOTest.m
1 //
2 //  NSException+WOTest.m
3 //  WOTest
4 //
5 //  Created by Wincent Colaiuta on 19 October 2004.
6 //
7 //  Copyright 2004-2007 Wincent Colaiuta.
8 //  This program is free software: you can redistribute it and/or modify
9 //  it under the terms of the GNU General Public License as published by
10 //  the Free Software Foundation, either version 3 of the License, or
11 //  (at your option) any later version.
12 //
13 //  This program is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 //  GNU General Public License for more details.
17 //
18 //  You should have received a copy of the GNU General Public License
19 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 //
21
22 #import "NSException+WOTest.h"
23 #import "NSObject+WOTest.h"
24
25 @implementation NSException (WOTest)
26
27 + (NSString *)WOTest_descriptionForException:(id)exception
28 {
29     @try
30     {
31         if ([NSObject WOTest_object:exception isKindOfClass:[NSException class]])
32             return [NSString stringWithFormat:@"%@: %@", [exception name], [exception reason]];
33         else if ([NSObject WOTest_object:exception respondsToSelector:@selector(name)])
34         {
35             NSString *name = nil; // attempt to get name
36             
37             if ([NSObject WOTest_objectReturnsId:exception forSelector:@selector(name)])
38             {
39                 name = [exception name];
40                 if (![NSObject WOTest_object:name isKindOfClass:[NSString class]])
41                     @throw self; // the returned id is not an NSString; bail
42             }
43             else if ([NSObject WOTest_objectReturnsCharacterString:exception forSelector:@selector(name)] ||
44                      [NSObject WOTest_objectReturnsConstantCharacterString:exception forSelector:@selector(name)])
45             {
46                 const char *charString = (const char *)[exception name];
47                 name = [NSString stringWithUTF8String:charString];
48             }
49             
50             NSString *reason = nil; // attempt to get reason
51             if ([NSObject WOTest_object:exception respondsToSelector:@selector(reason)])
52             {
53                 if ([NSObject WOTest_objectReturnsId:exception forSelector:@selector(reason)])
54                 {
55                     reason = [exception reason];
56                     if (![NSObject WOTest_object:reason isKindOfClass:[NSString class]])
57                         @throw self; // the returned id is not an NSString; bail
58                 }
59                 else if ([NSObject WOTest_objectReturnsCharacterString:exception forSelector:@selector(reason)] ||
60                          [NSObject WOTest_objectReturnsConstantCharacterString:exception forSelector:@selector(reason)])
61                 {
62                     const char *charString = (const char *)[exception reason];
63                     reason = [NSString stringWithUTF8String:charString];
64                 }
65             }
66             
67             if (name && reason)
68                 return [NSString stringWithFormat:@"%@: %@", name, reason];
69             else if (name)
70                 return [NSString stringWithFormat:@"%@ (%x)", name, exception];
71             else if (reason)
72                 return [NSString stringWithFormat:@"%@ (%x)", reason, exception];
73         }
74         else
75         {
76             return [NSObject WOTest_descriptionForObject:exception];
77         }            
78     }
79     @catch (id e)
80     {
81         // fall through
82     }
83     
84     // last resort
85     return [NSString stringWithFormat:@"unknown exception (%x)", exception];
86 }
87
88 + (NSString *)WOTest_nameForException:(id)exception
89 {
90     NSString *returnString = nil;
91     @try
92     {
93         if (!exception)
94             returnString = @"no exception";
95         else if ([NSObject WOTest_object:exception isKindOfClass:[NSException class]])
96             returnString = [exception name];
97         else if ([NSObject WOTest_object:exception respondsToSelector:@selector(name)])
98         {
99             if ([NSObject WOTest_objectReturnsId:exception forSelector:@selector(name)])
100             {
101                 returnString = [exception name];
102                 if (![NSObject WOTest_object:returnString isKindOfClass:[NSString class]])
103                     @throw self; // the returned id is not an NSString; bail
104             }
105             else if ([NSObject WOTest_objectReturnsCharacterString:exception forSelector:@selector(name)] ||
106                      [NSObject WOTest_objectReturnsConstantCharacterString:exception forSelector:@selector(name)])
107             {
108                 const char *charString = (const char *)[exception name];
109                 returnString = [NSString stringWithUTF8String:charString];
110             }
111         }
112         else 
113             returnString = [NSObject WOTest_descriptionForObject:exception];
114     }
115     @catch (id e)
116     {
117         returnString = @"(exception caught trying to determine exception name)";
118     }    
119     return returnString;
120 }
121
122 // TODO: replace with var_args versions
123 + (NSException *)WOTest_exceptionWithName:(NSString *)aName reason:(NSString *)aReason inFile:(char *)path atLine:(int)line
124 {
125     NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
126         [NSString stringWithUTF8String:path],   WO_TEST_USERINFO_PATH,
127         [NSNumber numberWithInt:line],          WO_TEST_USERINFO_LINE, nil];
128     return [self exceptionWithName:aName reason:aReason userInfo:theUserInfo];
129 }
130
131 + (void)WOTest_raise:(NSString *)aName reason:(NSString *)aReason inFile:(char *)path atLine:(int)line
132 {
133     [[self WOTest_exceptionWithName:aName reason:aReason inFile:path atLine:line] raise];
134 }
135
136 @end