PageRenderTime 127ms CodeModel.GetById 14ms app.highlight 107ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/externals/google-toolbox-for-mac/Foundation/GTMLoggerTest.m

http://macfuse.googlecode.com/
Objective C | 601 lines | 436 code | 97 blank | 68 comment | 13 complexity | 09c1f099ec95cd0770eb0ea8d1966fec MD5 | raw file
  1//
  2//  GTMLoggerTest.m
  3//
  4//  Copyright 2007-2008 Google Inc.
  5//
  6//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7//  use this file except in compliance with the License.  You may obtain a copy
  8//  of the License at
  9//
 10//  http://www.apache.org/licenses/LICENSE-2.0
 11//
 12//  Unless required by applicable law or agreed to in writing, software
 13//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 14//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 15//  License for the specific language governing permissions and limitations under
 16//  the License.
 17//
 18
 19#import "GTMLogger.h"
 20#import "GTMRegex.h"
 21#import "GTMSenTestCase.h"
 22
 23
 24// A test writer that stores log messages in an array for easy retrieval.
 25@interface ArrayWriter : NSObject <GTMLogWriter> {
 26 @private
 27  NSMutableArray *messages_;
 28}
 29- (NSArray *)messages;
 30- (void)clear;
 31@end
 32@implementation ArrayWriter
 33- (id)init {
 34  if ((self = [super init])) {
 35    messages_ = [[NSMutableArray alloc] init];
 36  }
 37  return self;
 38}
 39- (void)dealloc {
 40  [messages_ release];
 41  [super dealloc];
 42}
 43- (NSArray *)messages {
 44  return messages_;
 45}
 46- (void)logMessage:(NSString *)msg level:(GTMLoggerLevel)level {
 47  [messages_ addObject:msg];
 48}
 49- (void)clear {
 50  [messages_ removeAllObjects];
 51}
 52@end  // ArrayWriter
 53
 54
 55// A formatter for testing that prepends the word DUMB to log messages, along
 56// with the log level number.
 57@interface DumbFormatter : GTMLogBasicFormatter
 58@end
 59@implementation DumbFormatter
 60
 61#if !defined(__clang__) && (__GNUC__*10+__GNUC_MINOR__ >= 42)
 62// Some versions of GCC (4.2 and below AFAIK) aren't great about supporting
 63// -Wmissing-format-attribute
 64// when the function is anything more complex than foo(NSString *fmt, ...).
 65// You see the error inside the function when you turn ... into va_args and
 66// attempt to call another function (like vsprintf for example).
 67// So we just shut off the warning for this function.
 68#pragma GCC diagnostic ignored "-Wmissing-format-attribute"
 69#endif  // !__clang__
 70
 71- (NSString *)stringForFunc:(NSString *)func
 72                 withFormat:(NSString *)fmt
 73                     valist:(va_list)args
 74                      level:(GTMLoggerLevel)level {
 75  return [NSString stringWithFormat:@"DUMB [%d] %@", level,
 76          [super stringForFunc:nil withFormat:fmt valist:args level:level]];
 77}
 78
 79#if !defined(__clang__) && (__GNUC__*10+__GNUC_MINOR__ >= 42)
 80#pragma GCC diagnostic error "-Wmissing-format-attribute"
 81#endif  // !__clang__
 82
 83@end  // DumbFormatter
 84
 85
 86// A test filter that ignores messages with the string "ignore".
 87@interface IgnoreFilter : NSObject <GTMLogFilter>
 88@end
 89@implementation IgnoreFilter
 90- (BOOL)filterAllowsMessage:(NSString *)msg level:(GTMLoggerLevel)level {
 91  NSRange range = [msg rangeOfString:@"ignore"];
 92  return (range.location == NSNotFound);
 93}
 94@end  // IgnoreFilter
 95
 96//
 97// Begin test harness
 98//
 99
100@interface GTMLoggerTest : GTMTestCase {
101 @private
102  NSString *path_;
103}
104
105- (NSString *)stringFromFormatter:(id<GTMLogFormatter>)formatter
106                            level:(GTMLoggerLevel)level
107                           format:(NSString *)fmt, ... NS_FORMAT_FUNCTION(3,4);
108@end
109
110@implementation GTMLoggerTest
111
112- (void)setUp {
113  path_ = [[NSTemporaryDirectory() stringByAppendingPathComponent:
114            @"GTMLoggerUnitTest.log"] retain];
115  STAssertNotNil(path_, nil);
116  // Make sure we're cleaned up from the last run
117#if GTM_MACOS_SDK && (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
118  [[NSFileManager defaultManager] removeFileAtPath:path_ handler:nil];
119#else
120  [[NSFileManager defaultManager] removeItemAtPath:path_ error:NULL];
121#endif
122}
123
124- (void)tearDown {
125  STAssertNotNil(path_, nil);
126#if GTM_MACOS_SDK && (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
127  [[NSFileManager defaultManager] removeFileAtPath:path_ handler:nil];
128#else
129  [[NSFileManager defaultManager] removeItemAtPath:path_ error:NULL];
130#endif
131  [path_ release];
132  path_ = nil;
133}
134
135- (void)testCreation {
136  GTMLogger *logger1 = nil, *logger2 = nil;
137
138  logger1 = [GTMLogger sharedLogger];
139  logger2 = [GTMLogger sharedLogger];
140
141  STAssertTrue(logger1 == logger2, nil);
142
143  STAssertNotNil([logger1 writer], nil);
144  STAssertNotNil([logger1 formatter], nil);
145  STAssertNotNil([logger1 filter], nil);
146
147  // Get a new instance; not the shared instance
148  logger2 = [GTMLogger standardLogger];
149
150  STAssertTrue(logger1 != logger2, nil);
151  STAssertNotNil([logger2 writer], nil);
152  STAssertNotNil([logger2 formatter], nil);
153  STAssertNotNil([logger2 filter], nil);
154
155  // Set the new instance to be the shared logger.
156  [GTMLogger setSharedLogger:logger2];
157  STAssertTrue(logger2 == [GTMLogger sharedLogger], nil);
158  STAssertTrue(logger1 != [GTMLogger sharedLogger], nil);
159
160  // Set the shared logger to nil, which should reset it to a new "standard"
161  // logger.
162  [GTMLogger setSharedLogger:nil];
163  STAssertNotNil([GTMLogger sharedLogger], nil);
164  STAssertTrue(logger2 != [GTMLogger sharedLogger], nil);
165  STAssertTrue(logger1 != [GTMLogger sharedLogger], nil);
166
167  GTMLogger *logger = [GTMLogger logger];
168  STAssertNotNil(logger, nil);
169
170  logger = [GTMLogger standardLoggerWithStderr];
171  STAssertNotNil(logger, nil);
172
173  logger = [GTMLogger standardLoggerWithPath:path_];
174  STAssertNotNil(logger, nil);
175}
176
177- (void)testAccessors {
178  GTMLogger *logger = [GTMLogger standardLogger];
179  STAssertNotNil(logger, nil);
180
181  STAssertNotNil([logger writer], nil);
182  STAssertNotNil([logger formatter], nil);
183  STAssertNotNil([logger filter], nil);
184
185  [logger setWriter:nil];
186  [logger setFormatter:nil];
187  [logger setFilter:nil];
188
189  // These attributes should NOT be nil. They should be set to their defaults.
190  STAssertNotNil([logger writer], nil);
191  STAssertNotNil([logger formatter], nil);
192  STAssertNotNil([logger filter], nil);
193}
194
195- (void)testLogger {
196  ArrayWriter *writer = [[[ArrayWriter alloc] init] autorelease];
197  IgnoreFilter *filter = [[[IgnoreFilter alloc] init] autorelease];
198
199  // We actually only need the array writer instance for this unit test to pass,
200  // but we combine that writer with a stdout writer for two reasons:
201  //
202  //   1. To test the NSArray composite writer object
203  //   2. To make debugging easier by sending output to stdout
204  //
205  // We also include in the array an object that is not a GTMLogWriter to make
206  // sure that we don't crash when presented with an array of non-GTMLogWriters.
207  NSArray *writers = [NSArray arrayWithObjects:writer,
208                      [NSFileHandle fileHandleWithStandardOutput],
209                      @"blah", nil];
210
211  GTMLogger *logger = [GTMLogger loggerWithWriter:writers
212                                        formatter:nil  // basic formatter
213                                           filter:filter];
214
215  STAssertNotNil(logger, nil);
216
217  // Log a few messages to test with
218  [logger logInfo:@"hi"];
219  [logger logDebug:@"foo"];
220  [logger logError:@"blah"];
221  [logger logAssert:@"baz"];
222
223  // Makes sure the messages got logged
224  NSArray *messages = [writer messages];
225  STAssertNotNil(messages, nil);
226  STAssertEquals([messages count], (NSUInteger)4, nil);
227  STAssertEqualObjects([messages objectAtIndex:0], @"hi", nil);
228  STAssertEqualObjects([messages objectAtIndex:1], @"foo", nil);
229  STAssertEqualObjects([messages objectAtIndex:2], @"blah", nil);
230  STAssertEqualObjects([messages objectAtIndex:3], @"baz", nil);
231
232  // Log a message that should be ignored, and make sure it did NOT get logged
233  [logger logInfo:@"please ignore this"];
234  messages = [writer messages];
235  STAssertNotNil(messages, nil);
236  STAssertEquals([messages count], (NSUInteger)4, nil);
237  STAssertEqualObjects([messages objectAtIndex:0], @"hi", nil);
238  STAssertEqualObjects([messages objectAtIndex:1], @"foo", nil);
239  STAssertEqualObjects([messages objectAtIndex:2], @"blah", nil);
240  STAssertEqualObjects([messages objectAtIndex:3], @"baz", nil);
241
242  // Change the formatter to our "dumb formatter"
243  id<GTMLogFormatter> formatter = [[[DumbFormatter alloc] init] autorelease];
244  [logger setFormatter:formatter];
245
246  [logger logInfo:@"bleh"];
247  messages = [writer messages];
248  STAssertNotNil(messages, nil);
249  STAssertEquals([messages count], (NSUInteger)5, nil);  // Message count should increase
250  // The previously logged messages should not change
251  STAssertEqualObjects([messages objectAtIndex:0], @"hi", nil);
252  STAssertEqualObjects([messages objectAtIndex:1], @"foo", nil);
253  STAssertEqualObjects([messages objectAtIndex:2], @"blah", nil);
254  STAssertEqualObjects([messages objectAtIndex:3], @"baz", nil);
255  STAssertEqualObjects([messages objectAtIndex:4], @"DUMB [2] bleh", nil);
256}
257
258- (void)testConvenienceMacros {
259  ArrayWriter *writer = [[[ArrayWriter alloc] init] autorelease];
260  NSArray *writers = [NSArray arrayWithObjects:writer,
261                      [NSFileHandle fileHandleWithStandardOutput], nil];
262
263  [[GTMLogger sharedLogger] setWriter:writers];
264
265  // Here we log a message using a convenience macro, which should log the
266  // message along with the name of the function it was called from. Here we
267  // test to make sure the logged message does indeed contain the name of the
268  // current function "testConvenienceMacros".
269  GTMLoggerError(@"test ========================");
270  STAssertEquals([[writer messages] count], (NSUInteger)1, nil);
271  NSRange rangeOfFuncName =
272    [[[writer messages] objectAtIndex:0] rangeOfString:@"testConvenienceMacros"];
273  STAssertTrue(rangeOfFuncName.location != NSNotFound, nil);
274  [writer clear];
275
276  [[GTMLogger sharedLogger] setFormatter:nil];
277
278  GTMLoggerInfo(@"test %d", 1);
279  GTMLoggerDebug(@"test %d", 2);
280  GTMLoggerError(@"test %d", 3);
281  GTMLoggerAssert(@"test %d", 4);
282
283  NSArray *messages = [writer messages];
284  STAssertNotNil(messages, nil);
285
286#ifdef DEBUG
287  STAssertEquals([messages count], (NSUInteger)4, nil);
288  STAssertEqualObjects([messages objectAtIndex:0], @"test 1", nil);
289  STAssertEqualObjects([messages objectAtIndex:1], @"test 2", nil);
290  STAssertEqualObjects([messages objectAtIndex:2], @"test 3", nil);
291  STAssertEqualObjects([messages objectAtIndex:3], @"test 4", nil);
292#else
293  // In Release builds, only the Error and Assert messages will be logged
294  STAssertEquals([messages count], (NSUInteger)2, nil);
295  STAssertEqualObjects([messages objectAtIndex:0], @"test 3", nil);
296  STAssertEqualObjects([messages objectAtIndex:1], @"test 4", nil);
297#endif
298
299}
300
301- (void)testFileHandleWriter {
302  NSFileHandle *fh = nil;
303
304  fh = [NSFileHandle fileHandleForWritingAtPath:path_];
305  STAssertNil(fh, nil);
306
307  fh = [NSFileHandle fileHandleForLoggingAtPath:path_ mode:0644];
308  STAssertNotNil(fh, nil);
309
310  [fh logMessage:@"test 0" level:kGTMLoggerLevelUnknown];
311  [fh logMessage:@"test 1" level:kGTMLoggerLevelDebug];
312  [fh logMessage:@"test 2" level:kGTMLoggerLevelInfo];
313  [fh logMessage:@"test 3" level:kGTMLoggerLevelError];
314  [fh logMessage:@"test 4" level:kGTMLoggerLevelAssert];
315  [fh closeFile];
316
317  NSError *err = nil;
318  NSString *contents = [NSString stringWithContentsOfFile:path_
319                                                 encoding:NSUTF8StringEncoding
320                                                    error:&err];
321  STAssertNotNil(contents, @"Error loading log file: %@", err);
322  STAssertEqualObjects(@"test 0\ntest 1\ntest 2\ntest 3\ntest 4\n", contents, nil);
323}
324
325- (void)testLoggerAdapterWriter {
326  ArrayWriter *writer = [[[ArrayWriter alloc] init] autorelease];
327  STAssertNotNil(writer, nil);
328
329  GTMLogger *sublogger = [GTMLogger loggerWithWriter:writer
330                                         formatter:nil
331                                            filter:nil];
332  STAssertNotNil(sublogger, nil);
333
334  GTMLogger *logger = [GTMLogger loggerWithWriter:sublogger
335                                      formatter:nil
336                                         filter:nil];
337
338  STAssertNotNil(logger, nil);
339
340  // Log a few messages to test with
341  [logger logInfo:@"hi"];
342  [logger logDebug:@"foo"];
343  [logger logError:@"blah"];
344  [logger logAssert:@"assert"];
345
346  // Makes sure the messages got logged
347  NSArray *messages = [writer messages];
348  STAssertNotNil(messages, nil);
349  STAssertEquals([messages count], (NSUInteger)4, nil);
350  STAssertEqualObjects([messages objectAtIndex:0], @"hi", nil);
351  STAssertEqualObjects([messages objectAtIndex:1], @"foo", nil);
352  STAssertEqualObjects([messages objectAtIndex:2], @"blah", nil);
353  STAssertEqualObjects([messages objectAtIndex:3], @"assert", nil);
354}
355
356// Helper method to help testing GTMLogFormatters
357- (NSString *)stringFromFormatter:(id<GTMLogFormatter>)formatter
358                            level:(GTMLoggerLevel)level
359                           format:(NSString *)fmt, ... {
360  va_list args;
361  va_start(args, fmt);
362  NSString *msg = [formatter stringForFunc:nil
363                                withFormat:fmt
364                                    valist:args
365                                     level:level];
366  va_end(args);
367  return msg;
368}
369
370- (void)testFunctionPrettifier {
371  GTMLogBasicFormatter *fmtr = [[[GTMLogBasicFormatter alloc] init]
372                                 autorelease];
373  STAssertNotNil(fmtr, nil);
374
375  // Nil, empty and whitespace
376  STAssertEqualObjects([fmtr prettyNameForFunc:nil], @"(unknown)", nil);
377  STAssertEqualObjects([fmtr prettyNameForFunc:@""], @"(unknown)", nil);
378  STAssertEqualObjects([fmtr prettyNameForFunc:@"   \n\t"], @"(unknown)", nil);
379
380  // C99 __func__
381  STAssertEqualObjects([fmtr prettyNameForFunc:@"main"], @"main()", nil);
382  STAssertEqualObjects([fmtr prettyNameForFunc:@"main"], @"main()", nil);
383  STAssertEqualObjects([fmtr prettyNameForFunc:@" main "], @"main()", nil);
384
385  // GCC Obj-C __func__ and __PRETTY_FUNCTION__
386  STAssertEqualObjects([fmtr prettyNameForFunc:@"+[Foo bar]"], @"+[Foo bar]",
387                        nil);
388  STAssertEqualObjects([fmtr prettyNameForFunc:@" +[Foo bar] "], @"+[Foo bar]",
389                        nil);
390  STAssertEqualObjects([fmtr prettyNameForFunc:@"-[Foo baz]"], @"-[Foo baz]",
391                        nil);
392  STAssertEqualObjects([fmtr prettyNameForFunc:@" -[Foo baz] "], @"-[Foo baz]",
393                        nil);
394
395  // GCC C++ __PRETTY_FUNCTION__
396  STAssertEqualObjects([fmtr prettyNameForFunc:@"void a::sub(int)"],
397                        @"void a::sub(int)", nil);
398  STAssertEqualObjects([fmtr prettyNameForFunc:@" void a::sub(int) "],
399                        @"void a::sub(int)", nil);
400}
401
402- (void)testBasicFormatter {
403  id<GTMLogFormatter> fmtr = [[[GTMLogBasicFormatter alloc] init] autorelease];
404  STAssertNotNil(fmtr, nil);
405  NSString *msg = nil;
406
407  msg = [self stringFromFormatter:fmtr
408                            level:kGTMLoggerLevelDebug
409                           format:@"test"];
410  STAssertEqualObjects(msg, @"test", nil);
411
412  msg = [self stringFromFormatter:fmtr
413                            level:kGTMLoggerLevelDebug
414                           format:@"test %d", 1];
415  STAssertEqualObjects(msg, @"test 1", nil);
416
417  msg = [self stringFromFormatter:fmtr
418                            level:kGTMLoggerLevelDebug
419                           format:@"test %@", @"foo"];
420  STAssertEqualObjects(msg, @"test foo", nil);
421
422  msg = [self stringFromFormatter:fmtr
423                            level:kGTMLoggerLevelDebug
424                           format:@""];
425  STAssertEqualObjects(msg, @"", nil);
426
427  msg = [self stringFromFormatter:fmtr
428                            level:kGTMLoggerLevelDebug
429                           format:@"     "];
430  STAssertEqualObjects(msg, @"     ", nil);
431}
432
433- (void)testStandardFormatter {
434  id<GTMLogFormatter> fmtr = [[[GTMLogStandardFormatter alloc] init] autorelease];
435  STAssertNotNil(fmtr, nil);
436
437  NSString * kFormatBasePattern;
438#if GTM_MACOS_SDK
439  // E.g. 2008-01-04 09:16:26.906 otest[5567/0xa07d0f60] [lvl=1] (no func) test
440  // E.g. 2009-10-26 22:26:25.086 otest-i386[53200/0xa0438500] [lvl=1] (no func) test
441  kFormatBasePattern =
442  @"[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3} ((otest)|(otest-i386)|(otest-x86_64)|(otest-ppc))\\[[0-9]+/0x[0-9a-f]+\\] \\[lvl=[0-3]\\] \\(unknown\\) ";
443#else  // GTM_MACOS_SDK
444  // E.g. 2008-01-04 09:16:26.906 otest[5567/0xa07d0f60] [lvl=1] (no func) test
445  //      2011-11-29 12:32:24.129 GTMiPhoneUnitTestingOCUnitTestRig[31540/0xa04dd540] [lvl=1] (unknown) test
446  kFormatBasePattern =
447  @"[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3} (GTMiPhoneTest|GTMiPhoneUnitTestingOCUnitTestRig)\\[[0-9]+/0x[0-9a-f]+\\] \\[lvl=[0-3]\\] \\(unknown\\) ";
448#endif   // GTM_MACOS_SDK
449
450  NSString *msg = nil;
451
452  msg = [self stringFromFormatter:fmtr
453                            level:kGTMLoggerLevelDebug
454                           format:@"test"];
455  STAssertTrue([msg gtm_matchesPattern:[kFormatBasePattern stringByAppendingString:@"test"]],
456               @"msg: %@", msg);
457
458  msg = [self stringFromFormatter:fmtr
459                            level:kGTMLoggerLevelError
460                           format:@"test %d", 1];
461  STAssertTrue([msg gtm_matchesPattern:[kFormatBasePattern stringByAppendingString:@"test 1"]],
462               @"msg: %@", msg);
463
464
465  msg = [self stringFromFormatter:fmtr
466                            level:kGTMLoggerLevelInfo
467                           format:@"test %@", @"hi"];
468  STAssertTrue([msg gtm_matchesPattern:[kFormatBasePattern stringByAppendingString:@"test hi"]],
469               @"msg: %@", msg);
470
471
472  msg = [self stringFromFormatter:fmtr
473                            level:kGTMLoggerLevelUnknown
474                           format:@"test"];
475  STAssertTrue([msg gtm_matchesPattern:[kFormatBasePattern stringByAppendingString:@"test"]],
476               @"msg: %@", msg);
477}
478
479- (void)testNoFilter {
480  id<GTMLogFilter> filter = [[[GTMLogNoFilter alloc] init] autorelease];
481  STAssertNotNil(filter, nil);
482
483  STAssertTrue([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelUnknown], nil);
484  STAssertTrue([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelDebug], nil);
485  STAssertTrue([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelInfo], nil);
486  STAssertTrue([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelError], nil);
487  STAssertTrue([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelAssert], nil);
488  STAssertTrue([filter filterAllowsMessage:@"" level:kGTMLoggerLevelDebug], nil);
489  STAssertTrue([filter filterAllowsMessage:nil level:kGTMLoggerLevelDebug], nil);
490}
491
492- (void)testMinimumFilter {
493  id<GTMLogFilter> filter = [[[GTMLogMininumLevelFilter alloc]
494                                initWithMinimumLevel:kGTMLoggerLevelInfo]
495                                    autorelease];
496  STAssertNotNil(filter, nil);
497  STAssertFalse([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelUnknown],
498                nil);
499  STAssertFalse([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelDebug],
500                nil);
501  STAssertTrue([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelInfo],
502               nil);
503  STAssertTrue([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelError],
504               nil);
505  STAssertTrue([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelAssert],
506               nil);
507
508  filter = [[[GTMLogMininumLevelFilter alloc]
509               initWithMinimumLevel:kGTMLoggerLevelDebug] autorelease];
510  STAssertNotNil(filter, nil);
511  STAssertFalse([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelUnknown],
512                nil);
513  STAssertTrue([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelDebug],
514               nil);
515  STAssertTrue([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelInfo],
516               nil);
517  STAssertTrue([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelError],
518               nil);
519  STAssertTrue([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelAssert],
520               nil);
521
522  // Cannot exceed min/max levels filter
523  filter = [[[GTMLogMininumLevelFilter alloc]
524               initWithMinimumLevel:kGTMLoggerLevelAssert + 1] autorelease];
525  STAssertNil(filter, nil);
526  filter = [[[GTMLogMininumLevelFilter alloc]
527               initWithMinimumLevel:kGTMLoggerLevelUnknown - 1] autorelease];
528  STAssertNil(filter, nil);
529}
530
531- (void)testMaximumFilter {
532  id<GTMLogFilter> filter = [[[GTMLogMaximumLevelFilter alloc]
533                                initWithMaximumLevel:kGTMLoggerLevelInfo]
534                                    autorelease];
535  STAssertNotNil(filter, nil);
536  STAssertTrue([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelUnknown],
537                nil);
538  STAssertTrue([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelDebug],
539                nil);
540  STAssertTrue([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelInfo],
541               nil);
542  STAssertFalse([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelError],
543               nil);
544  STAssertFalse([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelAssert],
545               nil);
546
547  filter = [[[GTMLogMaximumLevelFilter alloc]
548               initWithMaximumLevel:kGTMLoggerLevelDebug] autorelease];
549  STAssertNotNil(filter, nil);
550  STAssertTrue([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelUnknown],
551                nil);
552  STAssertTrue([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelDebug],
553               nil);
554  STAssertFalse([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelInfo],
555               nil);
556  STAssertFalse([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelError],
557               nil);
558  STAssertFalse([filter filterAllowsMessage:@"hi" level:kGTMLoggerLevelAssert],
559               nil);
560
561  // Cannot exceed min/max levels filter
562  filter = [[[GTMLogMaximumLevelFilter alloc]
563               initWithMaximumLevel:kGTMLoggerLevelAssert + 1] autorelease];
564  STAssertNil(filter, nil);
565  filter = [[[GTMLogMaximumLevelFilter alloc]
566               initWithMaximumLevel:kGTMLoggerLevelUnknown - 1] autorelease];
567  STAssertNil(filter, nil);
568}
569
570- (void)testFileHandleCreation {
571  NSFileHandle *fh = nil;
572
573  fh = [NSFileHandle fileHandleForLoggingAtPath:nil mode:0644];
574  STAssertNil(fh, nil);
575
576  fh = [NSFileHandle fileHandleForLoggingAtPath:path_ mode:0644];
577  STAssertNotNil(fh, nil);
578
579  [fh logMessage:@"test 1" level:kGTMLoggerLevelInfo];
580  [fh logMessage:@"test 2" level:kGTMLoggerLevelInfo];
581  [fh logMessage:@"test 3" level:kGTMLoggerLevelInfo];
582  [fh closeFile];
583
584  // Re-open file and make sure our log messages get appended
585  fh = [NSFileHandle fileHandleForLoggingAtPath:path_ mode:0644];
586  STAssertNotNil(fh, nil);
587
588  [fh logMessage:@"test 4" level:kGTMLoggerLevelInfo];
589  [fh logMessage:@"test 5" level:kGTMLoggerLevelInfo];
590  [fh logMessage:@"test 6" level:kGTMLoggerLevelInfo];
591  [fh closeFile];
592
593  NSError *err = nil;
594  NSString *contents = [NSString stringWithContentsOfFile:path_
595                                                 encoding:NSUTF8StringEncoding
596                                                    error:&err];
597  STAssertNotNil(contents, @"Error loading log file: %@", err);
598  STAssertEqualObjects(@"test 1\ntest 2\ntest 3\ntest 4\ntest 5\ntest 6\n", contents, nil);
599}
600
601@end