DDBasicLoggingTests.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2019, Deusty, LLC
  4. // All rights reserved.
  5. //
  6. // Redistribution and use of this software in source and binary forms,
  7. // with or without modification, are permitted provided that the following conditions are met:
  8. //
  9. // * Redistributions of source code must retain the above copyright notice,
  10. // this list of conditions and the following disclaimer.
  11. //
  12. // * Neither the name of Deusty nor the names of its contributors may be used
  13. // to endorse or promote products derived from this software without specific
  14. // prior written permission of Deusty, LLC.
  15. @import XCTest;
  16. #import <CocoaLumberjack/CocoaLumberjack.h>
  17. #import "DDSMocking.h"
  18. static const NSTimeInterval kAsyncExpectationTimeout = 3.0f;
  19. static DDLogLevel ddLogLevel = DDLogLevelVerbose;
  20. static DDBasicMock<DDAbstractLogger *> *createAbstractLogger(void (^didLogBlock)(id)) {
  21. __auto_type logger = [DDBasicMock<DDAbstractLogger *> decoratedInstance:[[DDAbstractLogger alloc] init]];
  22. __auto_type argument = [DDBasicMockArgument alongsideWithBlock:didLogBlock];
  23. [logger addArgument:argument forSelector:@selector(logMessage:) atIndex:2];
  24. return logger;
  25. }
  26. @interface DDSingleLoggerLoggingTests : XCTestCase
  27. @property (nonatomic, strong) NSArray *logs;
  28. @property (nonatomic, strong) XCTestExpectation *expectation;
  29. @property (nonatomic, strong) DDAbstractLogger *logger;
  30. @property (nonatomic, assign) NSUInteger numberMessagesLogged;
  31. @property (nonatomic) dispatch_queue_t serial;
  32. @end
  33. @implementation DDSingleLoggerLoggingTests
  34. - (void)setupLoggers {
  35. __weak __auto_type weakSelf = self;
  36. self.logger = (DDAbstractLogger *)createAbstractLogger(^(DDLogMessage *logMessage) {
  37. dispatch_sync(self->_serial, ^{
  38. __auto_type strongSelf = weakSelf;
  39. XCTAssertTrue([logMessage isKindOfClass:[DDLogMessage class]]);
  40. XCTAssertTrue([strongSelf.logs containsObject:logMessage.message]);
  41. XCTAssertEqualObjects(logMessage.fileName, @"DDBasicLoggingTests");
  42. strongSelf.numberMessagesLogged++;
  43. if (strongSelf.numberMessagesLogged == [strongSelf.logs count]) {
  44. [strongSelf.expectation fulfill];
  45. }
  46. });
  47. });
  48. [DDLog addLogger:self.logger];
  49. }
  50. - (void)setUp {
  51. [super setUp];
  52. self.serial = dispatch_queue_create("serial", NULL);
  53. self.logs = @[];
  54. self.numberMessagesLogged = 0;
  55. ddLogLevel = DDLogLevelVerbose;
  56. [self setupLoggers];
  57. }
  58. - (void)tearDown {
  59. [DDLog removeAllLoggers];
  60. self.logger = nil;
  61. self.expectation = nil;
  62. [super tearDown];
  63. }
  64. - (void)testAll5DefaultLevelsAsync {
  65. self.expectation = [self expectationWithDescription:@"default log levels"];
  66. self.logs = @[ @"Error", @"Warn", @"Info", @"Debug", @"Verbose" ];
  67. DDLogError (@"Error");
  68. DDLogWarn (@"Warn");
  69. DDLogInfo (@"Info");
  70. DDLogDebug (@"Debug");
  71. DDLogVerbose(@"Verbose");
  72. [DDLog flushLog];
  73. [self waitForExpectationsWithTimeout:kAsyncExpectationTimeout handler:^(NSError *timeoutError) {
  74. XCTAssertNil(timeoutError);
  75. }];
  76. }
  77. - (void)testLoggerLogLevelAsync {
  78. self.expectation = [self expectationWithDescription:@"logger level"];
  79. self.logs = @[ @"Error", @"Warn" ];
  80. [DDLog removeLogger:self.logger];
  81. [DDLog addLogger:self.logger withLevel:DDLogLevelWarning];
  82. DDLogError (@"Error");
  83. DDLogWarn (@"Warn");
  84. DDLogInfo (@"Info");
  85. DDLogDebug (@"Debug");
  86. DDLogVerbose(@"Verbose");
  87. [DDLog flushLog];
  88. [self waitForExpectationsWithTimeout:kAsyncExpectationTimeout handler:^(NSError *timeoutError) {
  89. XCTAssertNil(timeoutError);
  90. }];
  91. }
  92. - (void)testGlobalLogLevelAsync {
  93. self.expectation = [self expectationWithDescription:@"ddLogLevel"];
  94. self.logs = @[ @"Error", @"Warn", @"Info" ];
  95. ddLogLevel = DDLogLevelInfo;
  96. DDLogError (@"Error");
  97. DDLogWarn (@"Warn");
  98. DDLogInfo (@"Info");
  99. DDLogDebug (@"Debug");
  100. DDLogVerbose(@"Verbose");
  101. [DDLog flushLog];
  102. [self waitForExpectationsWithTimeout:kAsyncExpectationTimeout handler:^(NSError *timeoutError) {
  103. XCTAssertNil(timeoutError);
  104. }];
  105. ddLogLevel = DDLogLevelVerbose;
  106. }
  107. @end
  108. static int const DDLoggerCount = 3;
  109. @interface DDMultipleLoggerLoggingTests : XCTestCase
  110. @property (nonatomic) NSArray *loggers;
  111. @property (nonatomic) NSArray *logs;
  112. @property (nonatomic) XCTestExpectation *expectation;
  113. @property (nonatomic) NSUInteger numberMessagesLogged;
  114. @property (nonatomic) dispatch_queue_t serial;
  115. @end
  116. @implementation DDMultipleLoggerLoggingTests
  117. - (void)setUp {
  118. [super setUp];
  119. self.serial = dispatch_queue_create("serial", NULL);
  120. self.logs = @[];
  121. self.numberMessagesLogged = 0;
  122. ddLogLevel = DDLogLevelVerbose;
  123. [self setupLoggers];
  124. }
  125. - (void)tearDown {
  126. [DDLog removeAllLoggers];
  127. self.loggers = nil;
  128. self.expectation = nil;
  129. [super tearDown];
  130. }
  131. - (void)setupLoggers {
  132. NSMutableArray *loggers = [NSMutableArray arrayWithCapacity:DDLoggerCount];
  133. for (NSUInteger i = 0; i < DDLoggerCount; i++) {
  134. __weak __auto_type weakSelf = self;
  135. __auto_type logger = (DDAbstractLogger *)createAbstractLogger(^(DDLogMessage *logMessage) {
  136. dispatch_sync(self->_serial, ^{
  137. __auto_type strongSelf = weakSelf;
  138. XCTAssertTrue([logMessage isKindOfClass:[DDLogMessage class]]);
  139. XCTAssertTrue([strongSelf.logs containsObject:logMessage.message]);
  140. XCTAssertEqualObjects(logMessage.fileName, @"DDBasicLoggingTests");
  141. strongSelf.numberMessagesLogged++;
  142. if (strongSelf.numberMessagesLogged == [strongSelf.logs count]) {
  143. [strongSelf.expectation fulfill];
  144. }
  145. });
  146. });
  147. [loggers addObject:logger];
  148. [DDLog addLogger:logger];
  149. }
  150. self.loggers = [loggers copy];
  151. }
  152. - (void)testAll5DefaultLevelsAsync {
  153. self.expectation = [self expectationWithDescription:@"default log levels"];
  154. self.logs = @[ @"Error" ];
  155. DDLogError(@"Error");
  156. [DDLog flushLog];
  157. [self waitForExpectationsWithTimeout:kAsyncExpectationTimeout handler:^(NSError *timeoutError) {
  158. XCTAssertNil(timeoutError);
  159. }];
  160. }
  161. @end