forked from rentzsch/JRLogDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JRLogTest.m
210 lines (162 loc) · 9.4 KB
/
JRLogTest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#import "JRLogTest.h"
@implementation JRLogTest
- (void)setUp {
debugLogger = [[MyDebugLogger alloc] init];
JRLogSetLogger(debugLogger);
}
- (JRLogCall*)consumeOnlyMessage {
STAssertEquals([debugLogger->messages count], (unsigned)1, nil);
JRLogCall *result = [[[debugLogger->messages objectAtIndex:0] retain] autorelease];
[debugLogger->messages removeLastObject];
return result;
}
- (void)testLevels {
STAssertEquals((unsigned)0, [debugLogger->messages count], nil);
JRLogDebug(@"testDebug");
JRLogCall *logMessage = [self consumeOnlyMessage];
STAssertEquals(JRLogLevel_Debug, logMessage->callerLevel, nil);
STAssertFalse([logMessage->instance rangeOfString:@"JRLogTest"].location == NSNotFound, nil);
STAssertEqualObjects(@"JRLogTest.m", [[NSString stringWithUTF8String:logMessage->file] lastPathComponent], nil);
STAssertEquals((unsigned)__LINE__-6, logMessage->line, nil);
STAssertEqualObjects(@"-[JRLogTest testLevels]", [NSString stringWithUTF8String:logMessage->function], nil);
STAssertEqualObjects(@"testDebug", logMessage->message, nil);
STAssertTrue([logMessage->formattedMessage length] > [logMessage->message length], nil);
STAssertFalse([logMessage->formattedMessage rangeOfString:@"testDebug"].location == NSNotFound, nil);
STAssertTrue([logMessage->formattedMessage rangeOfString:@"xyzzy"].location == NSNotFound, nil);
JRLogInfo(@"testInfo");
logMessage = [self consumeOnlyMessage];
STAssertEquals(JRLogLevel_Info, logMessage->callerLevel, nil);
STAssertFalse([logMessage->instance rangeOfString:@"JRLogTest"].location == NSNotFound, nil);
STAssertEqualObjects(@"JRLogTest.m", [[NSString stringWithUTF8String:logMessage->file] lastPathComponent], nil);
STAssertEquals((unsigned)__LINE__-6, logMessage->line, nil);
STAssertEqualObjects(@"-[JRLogTest testLevels]", [NSString stringWithUTF8String:logMessage->function], nil);
STAssertEqualObjects(@"testInfo", logMessage->message, nil);
STAssertTrue([logMessage->formattedMessage length] > [logMessage->message length], nil);
STAssertFalse([logMessage->formattedMessage rangeOfString:@"testInfo"].location == NSNotFound, nil);
JRLogWarn(@"testWarn");
logMessage = [self consumeOnlyMessage];
STAssertEquals(JRLogLevel_Warn, logMessage->callerLevel, nil);
STAssertFalse([logMessage->instance rangeOfString:@"JRLogTest"].location == NSNotFound, nil);
STAssertEqualObjects(@"JRLogTest.m", [[NSString stringWithUTF8String:logMessage->file] lastPathComponent], nil);
STAssertEquals((unsigned)__LINE__-6, logMessage->line, nil);
STAssertEqualObjects(@"-[JRLogTest testLevels]", [NSString stringWithUTF8String:logMessage->function], nil);
STAssertEqualObjects(@"testWarn", logMessage->message, nil);
STAssertTrue([logMessage->formattedMessage length] > [logMessage->message length], nil);
STAssertFalse([logMessage->formattedMessage rangeOfString:@"testWarn"].location == NSNotFound, nil);
JRLogError(@"testError");
logMessage = [self consumeOnlyMessage];
STAssertEquals(JRLogLevel_Error, logMessage->callerLevel, nil);
STAssertFalse([logMessage->instance rangeOfString:@"JRLogTest"].location == NSNotFound, nil);
STAssertEqualObjects(@"JRLogTest.m", [[NSString stringWithUTF8String:logMessage->file] lastPathComponent], nil);
STAssertEquals((unsigned)__LINE__-6, logMessage->line, nil);
STAssertEqualObjects(@"-[JRLogTest testLevels]", [NSString stringWithUTF8String:logMessage->function], nil);
STAssertEqualObjects(@"testError", logMessage->message, nil);
STAssertTrue([logMessage->formattedMessage length] > [logMessage->message length], nil);
STAssertFalse([logMessage->formattedMessage rangeOfString:@"testError"].location == NSNotFound, nil);
JRLogAssert(1==2, nil);//JRLogAssert(1==2, @"test%@", @"Assert");
logMessage = [self consumeOnlyMessage];
STAssertEquals(JRLogLevel_Assert, logMessage->callerLevel, nil);
STAssertFalse([logMessage->instance rangeOfString:@"JRLogTest"].location == NSNotFound, nil);
STAssertEqualObjects(@"JRLogTest.m", [[NSString stringWithUTF8String:logMessage->file] lastPathComponent], nil);
STAssertEquals((unsigned)__LINE__-6, logMessage->line, nil);
STAssertEqualObjects(@"-[JRLogTest testLevels]", [NSString stringWithUTF8String:logMessage->function], nil);
STAssertEqualObjects(@"1==2", logMessage->message, nil);
STAssertTrue([logMessage->formattedMessage length] > [logMessage->message length], nil);
STAssertFalse([logMessage->formattedMessage rangeOfString:@"1==2"].location == NSNotFound, nil);
JRLogAssert(1==2, @"test%@", @"Assert");
logMessage = [self consumeOnlyMessage];
STAssertEquals(JRLogLevel_Assert, logMessage->callerLevel, nil);
STAssertFalse([logMessage->instance rangeOfString:@"JRLogTest"].location == NSNotFound, nil);
STAssertEqualObjects(@"JRLogTest.m", [[NSString stringWithUTF8String:logMessage->file] lastPathComponent], nil);
STAssertEquals((unsigned)__LINE__-6, logMessage->line, nil);
STAssertEqualObjects(@"-[JRLogTest testLevels]", [NSString stringWithUTF8String:logMessage->function], nil);
STAssertEqualObjects(@"1==2 (testAssert)", logMessage->message, nil);
STAssertTrue([logMessage->formattedMessage length] > [logMessage->message length], nil);
STAssertFalse([logMessage->formattedMessage rangeOfString:@"1==2"].location == NSNotFound, nil);
STAssertFalse([logMessage->formattedMessage rangeOfString:@"testAssert"].location == NSNotFound, nil);
/** Can't really test fatal since it calls exit(1).
JRLogFatal(@"testFatal");
logMessage = [self consumeOnlyMessage];
STAssertEquals(JRLogLevel_Fatal, logMessage->callerLevel, nil);
STAssertFalse([logMessage->instance rangeOfString:@"JRLogTest"].location == NSNotFound, nil);
STAssertEqualObjects(@"JRLogTest.m", [[NSString stringWithUTF8String:logMessage->file] lastPathComponent], nil);
STAssertEquals((unsigned)__LINE__-6, logMessage->line, nil);
STAssertEqualObjects(@"-[JRLogTest testLevels]", [NSString stringWithUTF8String:logMessage->function], nil);
STAssertEqualObjects(@"testFatal", logMessage->message, nil);
STAssertTrue([logMessage->formattedMessage length] > [logMessage->message length], nil);
STAssertFalse([logMessage->formattedMessage rangeOfString:@"testFatal"].location == NSNotFound, nil);
*/
}
- (void)testCompiletimeLevels {
STAssertEquals((unsigned)0, [debugLogger->messages count], nil);
// TODO
}
- (void)testCustomFormatter {
JRLogDebug(@"testCustomFormatter");
JRLogCall *logMessage = [self consumeOnlyMessage];
STAssertFalse([logMessage->formattedMessage isEqualToString:@"DEBUG"], nil);
JRLogSetFormatter([[[MyFormatter alloc] init] autorelease]);
JRLogDebug(@"testCustomFormatter");
logMessage = [self consumeOnlyMessage];
STAssertEqualObjects(logMessage->formattedMessage, @"DEBUG", nil);
JRLogError(@"testCustomFormatter");
logMessage = [self consumeOnlyMessage];
STAssertEqualObjects(logMessage->formattedMessage, @"ERROR", nil);
JRLogSetFormatter(nil);
}
- (void)testFormatterSubclass {
JRLogDebug(@"testWithoutFormatterSubclass");
JRLogCall *logMessage = [self consumeOnlyMessage];
STAssertFalse([logMessage->formattedMessage hasPrefix:@"SUBCLASS"], nil);
JRLogSetFormatter([[[MyFormatterSubclass alloc] init] autorelease]);
JRLogDebug(@"testWithFormatterSubclass");
logMessage = [self consumeOnlyMessage];
STAssertTrue([logMessage->formattedMessage hasPrefix:@"SUBCLASS"], nil);
JRLogSetFormatter(nil);
}
- (void)testNSLogOverride {
STAssertEquals((unsigned)0, [debugLogger->messages count], nil);
NSLog(@"testNSLogOverride");
JRLogCall *logMessage = [self consumeOnlyMessage];
STAssertEquals(JRLogLevel_Info, logMessage->callerLevel, nil);
STAssertFalse([logMessage->instance rangeOfString:@"JRLogTest"].location == NSNotFound, nil);
STAssertEqualObjects(@"JRLogTest.m", [[NSString stringWithUTF8String:logMessage->file] lastPathComponent], nil);
STAssertEquals((unsigned)__LINE__-6, logMessage->line, nil);
STAssertEqualObjects(@"-[JRLogTest testNSLogOverride]", [NSString stringWithUTF8String:logMessage->function], nil);
STAssertEqualObjects(@"testNSLogOverride", logMessage->message, nil);
STAssertTrue([logMessage->formattedMessage length] > [logMessage->message length], nil);
STAssertFalse([logMessage->formattedMessage rangeOfString:@"testNSLogOverride"].location == NSNotFound, nil);
STAssertTrue([logMessage->formattedMessage rangeOfString:@"xyzzy"].location == NSNotFound, nil);
}
- (void)tearDown {
JRLogSetLogger(nil);
[debugLogger release];
}
@end
@implementation MyDebugLogger
- (id)init {
self = [super init];
if (self) {
messages = [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc {
[messages release];
[super dealloc];
}
- (void)logWithCall:(JRLogCall*)call_ {
[call_ setFormattedMessage:[JRLogGetFormatter() formattedMessageWithCall:call_]];
[messages addObject:call_];
}
@end
@implementation MyFormatter
- (NSString*)formattedMessageWithCall:(JRLogCall*)call_ {
return JRLogLevelNames[call_->callerLevel];
}
@end
@implementation MyFormatterSubclass
- (NSString*)formattedMessageWithCall:(JRLogCall*)call_ {
return [NSString stringWithFormat:@"SUBCLASS %@", [super formattedMessageWithCall:call_]];
}
@end