DDLogCombineTests.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2024, 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. #if arch(arm64) || arch(x86_64)
  16. #if canImport(Combine)
  17. import XCTest
  18. import Combine
  19. import CocoaLumberjack
  20. @testable import CocoaLumberjackSwift
  21. @available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
  22. final class DDLogCombineTests: XCTestCase {
  23. private var subscriptions = Set<AnyCancellable>()
  24. private var logFormatter: DDLogFileFormatterDefault {
  25. // let's return a formatter that doesn't change based where the test is being run.
  26. let formatter = DateFormatter()
  27. formatter.dateFormat = "yyyy/MM/dd HH:mm:ss:SSS"
  28. formatter.calendar = Calendar(identifier: .gregorian)
  29. formatter.locale = Locale(identifier: "en_US_POSIX")
  30. formatter.timeZone = TimeZone(secondsFromGMT: 0)
  31. return DDLogFileFormatterDefault(dateFormatter: formatter)
  32. }
  33. override func setUp() {
  34. super.setUp()
  35. DDLog.removeAllLoggers()
  36. }
  37. override func tearDown() {
  38. DDLog.removeAllLoggers()
  39. self.subscriptions.removeAll()
  40. super.tearDown()
  41. }
  42. func testMessagePublisherWithDDLogLevelAll() {
  43. DDLog.sharedInstance.messagePublisher()
  44. .sink(receiveValue: { _ in })
  45. .store(in: &self.subscriptions)
  46. XCTAssertEqual(DDLog.allLoggers.count, 1)
  47. XCTAssertEqual(DDLog.allLoggersWithLevel.count, 1)
  48. XCTAssertEqual(DDLog.allLoggersWithLevel.last?.level, .all)
  49. }
  50. func testMessagePublisherWithSpecifiedLevelMask() {
  51. DDLog.sharedInstance.messagePublisher(with: .error)
  52. .sink(receiveValue: { _ in })
  53. .store(in: &self.subscriptions)
  54. XCTAssertEqual(DDLog.allLoggers.count, 1)
  55. XCTAssertEqual(DDLog.allLoggersWithLevel.count, 1)
  56. XCTAssertEqual(DDLog.allLoggersWithLevel.last?.level, .error)
  57. }
  58. func testMessagePublisherRemovedWhenSubscriptionIsCanceled() {
  59. let sub = DDLog.sharedInstance.messagePublisher()
  60. .sink(receiveValue: { _ in })
  61. XCTAssertEqual(DDLog.allLoggers.count, 1)
  62. XCTAssertEqual(DDLog.allLoggersWithLevel.count, 1)
  63. XCTAssertEqual(DDLog.allLoggersWithLevel.last?.level, .all)
  64. sub.cancel()
  65. XCTAssertTrue(DDLog.allLoggers.isEmpty)
  66. XCTAssertTrue(DDLog.allLoggersWithLevel.isEmpty)
  67. }
  68. func testReceivedValuesWithDDLogLevelAll() {
  69. var receivedValue = [DDLogMessage]()
  70. DDLog.sharedInstance.messagePublisher()
  71. .sink(receiveValue: { receivedValue.append($0) })
  72. .store(in: &self.subscriptions)
  73. DDLogError("Error")
  74. DDLogWarn("Warn")
  75. DDLogInfo("Info")
  76. DDLogDebug("Debug")
  77. DDLogVerbose("Verbose")
  78. DDLog.flushLog()
  79. let messages = receivedValue.map { $0.message }
  80. XCTAssertEqual(messages, ["Error",
  81. "Warn",
  82. "Info",
  83. "Debug",
  84. "Verbose"])
  85. let levels = receivedValue.map { $0.flag }
  86. XCTAssertEqual(levels, [.error,
  87. .warning,
  88. .info,
  89. .debug,
  90. .verbose])
  91. }
  92. func testReceivedValuesWithDDLogLevelWarning() {
  93. var receivedValue = [DDLogMessage]()
  94. DDLog.sharedInstance.messagePublisher(with: .warning)
  95. .sink(receiveValue: { receivedValue.append($0) })
  96. .store(in: &self.subscriptions)
  97. DDLogError("Error")
  98. DDLogWarn("Warn")
  99. DDLogInfo("Info")
  100. DDLogDebug("Debug")
  101. DDLogVerbose("Verbose")
  102. DDLog.flushLog()
  103. let messages = receivedValue.map { $0.message }
  104. XCTAssertEqual(messages, ["Error", "Warn"])
  105. let levels = receivedValue.map { $0.flag }
  106. XCTAssertEqual(levels, [.error, .warning])
  107. }
  108. func testFormatted() {
  109. let subject = PassthroughSubject<DDLogMessage, Never>()
  110. var receivedValue = [String]()
  111. subject
  112. .formatted(with: logFormatter)
  113. .sink(receiveValue: { receivedValue.append($0) })
  114. .store(in: &subscriptions)
  115. subject.send(DDLogMessage("An error occurred",
  116. level: .all,
  117. flag: .error,
  118. context: 42,
  119. file: "Combine.swift",
  120. function: "PerformFailure",
  121. line: 67,
  122. timestamp: Date(timeIntervalSinceReferenceDate: 100)))
  123. subject.send(DDLogMessage("WARNING: this is incorrect",
  124. level: .all,
  125. flag: .warning,
  126. context: 23,
  127. file: "Combine.swift",
  128. function: "PerformWarning",
  129. line: 90,
  130. timestamp: Date(timeIntervalSinceReferenceDate: 200)))
  131. XCTAssertEqual(receivedValue, ["2001/01/01 00:01:40:000 An error occurred",
  132. "2001/01/01 00:03:20:000 WARNING: this is incorrect"])
  133. }
  134. func testQOSNameInstantiation() {
  135. let name = "UI"
  136. let qos: qos_class_t = {
  137. switch DDQualityOfServiceName(rawValue: name) {
  138. case .userInteractive:
  139. return QOS_CLASS_USER_INTERACTIVE
  140. default:
  141. return QOS_CLASS_UNSPECIFIED
  142. }
  143. }()
  144. XCTAssertEqual(qos, QOS_CLASS_USER_INTERACTIVE)
  145. }
  146. }
  147. #endif
  148. #endif