123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- // Software License Agreement (BSD License)
- //
- // Copyright (c) 2010-2019, Deusty, LLC
- // All rights reserved.
- //
- // Redistribution and use of this software in source and binary forms,
- // with or without modification, are permitted provided that the following conditions are met:
- //
- // * Redistributions of source code must retain the above copyright notice,
- // this list of conditions and the following disclaimer.
- //
- // * Neither the name of Deusty nor the names of its contributors may be used
- // to endorse or promote products derived from this software without specific
- // prior written permission of Deusty, LLC.
- @import XCTest;
- #import <CocoaLumberjack/CocoaLumberjack.h>
- #import "DDSMocking.h"
- static const NSTimeInterval kAsyncExpectationTimeout = 3.0f;
- static DDLogLevel ddLogLevel = DDLogLevelVerbose;
- static DDBasicMock<DDAbstractLogger *> *createAbstractLogger(void (^didLogBlock)(id)) {
- __auto_type logger = [DDBasicMock<DDAbstractLogger *> decoratedInstance:[[DDAbstractLogger alloc] init]];
- __auto_type argument = [DDBasicMockArgument alongsideWithBlock:didLogBlock];
- [logger addArgument:argument forSelector:@selector(logMessage:) atIndex:2];
- return logger;
- }
- @interface DDSingleLoggerLoggingTests : XCTestCase
- @property (nonatomic, strong) NSArray *logs;
- @property (nonatomic, strong) XCTestExpectation *expectation;
- @property (nonatomic, strong) DDAbstractLogger *logger;
- @property (nonatomic, assign) NSUInteger numberMessagesLogged;
- @property (nonatomic) dispatch_queue_t serial;
- @end
- @implementation DDSingleLoggerLoggingTests
- - (void)setupLoggers {
- __weak __auto_type weakSelf = self;
- self.logger = (DDAbstractLogger *)createAbstractLogger(^(DDLogMessage *logMessage) {
- dispatch_sync(self->_serial, ^{
- __auto_type strongSelf = weakSelf;
- XCTAssertTrue([logMessage isKindOfClass:[DDLogMessage class]]);
- XCTAssertTrue([strongSelf.logs containsObject:logMessage.message]);
- XCTAssertEqualObjects(logMessage.fileName, @"DDBasicLoggingTests");
- strongSelf.numberMessagesLogged++;
- if (strongSelf.numberMessagesLogged == [strongSelf.logs count]) {
- [strongSelf.expectation fulfill];
- }
- });
- });
- [DDLog addLogger:self.logger];
- }
- - (void)setUp {
- [super setUp];
- self.serial = dispatch_queue_create("serial", NULL);
- self.logs = @[];
- self.numberMessagesLogged = 0;
- ddLogLevel = DDLogLevelVerbose;
- [self setupLoggers];
- }
- - (void)tearDown {
- [DDLog removeAllLoggers];
- self.logger = nil;
- self.expectation = nil;
- [super tearDown];
- }
- - (void)testAll5DefaultLevelsAsync {
- self.expectation = [self expectationWithDescription:@"default log levels"];
- self.logs = @[ @"Error", @"Warn", @"Info", @"Debug", @"Verbose" ];
-
- DDLogError (@"Error");
- DDLogWarn (@"Warn");
- DDLogInfo (@"Info");
- DDLogDebug (@"Debug");
- DDLogVerbose(@"Verbose");
- [DDLog flushLog];
- [self waitForExpectationsWithTimeout:kAsyncExpectationTimeout handler:^(NSError *timeoutError) {
- XCTAssertNil(timeoutError);
- }];
- }
- - (void)testLoggerLogLevelAsync {
- self.expectation = [self expectationWithDescription:@"logger level"];
- self.logs = @[ @"Error", @"Warn" ];
-
- [DDLog removeLogger:self.logger];
- [DDLog addLogger:self.logger withLevel:DDLogLevelWarning];
-
- DDLogError (@"Error");
- DDLogWarn (@"Warn");
- DDLogInfo (@"Info");
- DDLogDebug (@"Debug");
- DDLogVerbose(@"Verbose");
- [DDLog flushLog];
- [self waitForExpectationsWithTimeout:kAsyncExpectationTimeout handler:^(NSError *timeoutError) {
- XCTAssertNil(timeoutError);
- }];
- }
- - (void)testGlobalLogLevelAsync {
- self.expectation = [self expectationWithDescription:@"ddLogLevel"];
- self.logs = @[ @"Error", @"Warn", @"Info" ];
-
- ddLogLevel = DDLogLevelInfo;
-
- DDLogError (@"Error");
- DDLogWarn (@"Warn");
- DDLogInfo (@"Info");
- DDLogDebug (@"Debug");
- DDLogVerbose(@"Verbose");
- [DDLog flushLog];
- [self waitForExpectationsWithTimeout:kAsyncExpectationTimeout handler:^(NSError *timeoutError) {
- XCTAssertNil(timeoutError);
- }];
-
- ddLogLevel = DDLogLevelVerbose;
- }
- @end
- static int const DDLoggerCount = 3;
- @interface DDMultipleLoggerLoggingTests : XCTestCase
- @property (nonatomic) NSArray *loggers;
- @property (nonatomic) NSArray *logs;
- @property (nonatomic) XCTestExpectation *expectation;
- @property (nonatomic) NSUInteger numberMessagesLogged;
- @property (nonatomic) dispatch_queue_t serial;
- @end
- @implementation DDMultipleLoggerLoggingTests
- - (void)setUp {
- [super setUp];
- self.serial = dispatch_queue_create("serial", NULL);
- self.logs = @[];
- self.numberMessagesLogged = 0;
- ddLogLevel = DDLogLevelVerbose;
- [self setupLoggers];
- }
- - (void)tearDown {
- [DDLog removeAllLoggers];
- self.loggers = nil;
- self.expectation = nil;
- [super tearDown];
- }
- - (void)setupLoggers {
- NSMutableArray *loggers = [NSMutableArray arrayWithCapacity:DDLoggerCount];
- for (NSUInteger i = 0; i < DDLoggerCount; i++) {
- __weak __auto_type weakSelf = self;
- __auto_type logger = (DDAbstractLogger *)createAbstractLogger(^(DDLogMessage *logMessage) {
- dispatch_sync(self->_serial, ^{
- __auto_type strongSelf = weakSelf;
- XCTAssertTrue([logMessage isKindOfClass:[DDLogMessage class]]);
- XCTAssertTrue([strongSelf.logs containsObject:logMessage.message]);
- XCTAssertEqualObjects(logMessage.fileName, @"DDBasicLoggingTests");
- strongSelf.numberMessagesLogged++;
- if (strongSelf.numberMessagesLogged == [strongSelf.logs count]) {
- [strongSelf.expectation fulfill];
- }
- });
- });
- [loggers addObject:logger];
- [DDLog addLogger:logger];
- }
- self.loggers = [loggers copy];
- }
- - (void)testAll5DefaultLevelsAsync {
- self.expectation = [self expectationWithDescription:@"default log levels"];
- self.logs = @[ @"Error" ];
-
- DDLogError(@"Error");
- [DDLog flushLog];
- [self waitForExpectationsWithTimeout:kAsyncExpectationTimeout handler:^(NSError *timeoutError) {
- XCTAssertNil(timeoutError);
- }];
- }
- @end
|