DiagnosticLogTests.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // DiagnosticLogTests.swift
  3. // LoopKitTests
  4. //
  5. // Created by Darin Krauss on 8/23/19.
  6. // Copyright © 2019 LoopKit Authors. All rights reserved.
  7. //
  8. import XCTest
  9. import os.log
  10. @testable import LoopKit
  11. class DiagnosticLogTests: XCTestCase {
  12. fileprivate var testLogging: TestLogging!
  13. override func setUp() {
  14. testLogging = TestLogging()
  15. SharedLogging.instance = testLogging
  16. }
  17. override func tearDown() {
  18. SharedLogging.instance = nil
  19. testLogging = nil
  20. }
  21. func testInitializer() {
  22. XCTAssertNotNil(DiagnosticLog(subsystem: "subsystem", category: "category"))
  23. }
  24. func testDebugWithoutArguments() {
  25. let diagnosticLog = DiagnosticLog(subsystem: "debug subsystem", category: "debug category")
  26. diagnosticLog.debug("debug message without arguments")
  27. XCTAssertEqual(testLogging.message.description, "debug message without arguments")
  28. XCTAssertEqual(testLogging.subsystem, "debug subsystem")
  29. XCTAssertEqual(testLogging.category, "debug category")
  30. XCTAssertEqual(testLogging.type, .debug)
  31. XCTAssertEqual(testLogging.args.count, 0)
  32. }
  33. func testDebugWithArguments() {
  34. let diagnosticLog = DiagnosticLog(subsystem: "debug subsystem", category: "debug category")
  35. diagnosticLog.debug("debug message with arguments", "a")
  36. XCTAssertEqual(testLogging.message.description, "debug message with arguments")
  37. XCTAssertEqual(testLogging.subsystem, "debug subsystem")
  38. XCTAssertEqual(testLogging.category, "debug category")
  39. XCTAssertEqual(testLogging.type, .debug)
  40. XCTAssertEqual(testLogging.args.count, 1)
  41. }
  42. func testInfoWithoutArguments() {
  43. let diagnosticLog = DiagnosticLog(subsystem: "info subsystem", category: "info category")
  44. diagnosticLog.info("info message without arguments")
  45. XCTAssertEqual(testLogging.message.description, "info message without arguments")
  46. XCTAssertEqual(testLogging.subsystem, "info subsystem")
  47. XCTAssertEqual(testLogging.category, "info category")
  48. XCTAssertEqual(testLogging.type, .info)
  49. XCTAssertEqual(testLogging.args.count, 0)
  50. }
  51. func testInfoWithArguments() {
  52. let diagnosticLog = DiagnosticLog(subsystem: "info subsystem", category: "info category")
  53. diagnosticLog.info("info message with arguments", "a", "b")
  54. XCTAssertEqual(testLogging.message.description, "info message with arguments")
  55. XCTAssertEqual(testLogging.subsystem, "info subsystem")
  56. XCTAssertEqual(testLogging.category, "info category")
  57. XCTAssertEqual(testLogging.type, .info)
  58. XCTAssertEqual(testLogging.args.count, 2)
  59. }
  60. func testDefaultWithoutArguments() {
  61. let diagnosticLog = DiagnosticLog(subsystem: "default subsystem", category: "default category")
  62. diagnosticLog.default("default message without arguments")
  63. XCTAssertEqual(testLogging.message.description, "default message without arguments")
  64. XCTAssertEqual(testLogging.subsystem, "default subsystem")
  65. XCTAssertEqual(testLogging.category, "default category")
  66. XCTAssertEqual(testLogging.type, .default)
  67. XCTAssertEqual(testLogging.args.count, 0)
  68. }
  69. func testDefaultWithArguments() {
  70. let diagnosticLog = DiagnosticLog(subsystem: "default subsystem", category: "default category")
  71. diagnosticLog.default("default message with arguments", "a", "b", "c")
  72. XCTAssertEqual(testLogging.message.description, "default message with arguments")
  73. XCTAssertEqual(testLogging.subsystem, "default subsystem")
  74. XCTAssertEqual(testLogging.category, "default category")
  75. XCTAssertEqual(testLogging.type, .default)
  76. XCTAssertEqual(testLogging.args.count, 3)
  77. }
  78. func testErrorWithoutArguments() {
  79. let diagnosticLog = DiagnosticLog(subsystem: "error subsystem", category: "error category")
  80. diagnosticLog.error("error message without arguments")
  81. XCTAssertEqual(testLogging.message.description, "error message without arguments")
  82. XCTAssertEqual(testLogging.subsystem, "error subsystem")
  83. XCTAssertEqual(testLogging.category, "error category")
  84. XCTAssertEqual(testLogging.type, .error)
  85. XCTAssertEqual(testLogging.args.count, 0)
  86. }
  87. func testErrorWithArguments() {
  88. let diagnosticLog = DiagnosticLog(subsystem: "error subsystem", category: "error category")
  89. diagnosticLog.error("error message with arguments", "a", "b", "c", "d")
  90. XCTAssertEqual(testLogging.message.description, "error message with arguments")
  91. XCTAssertEqual(testLogging.subsystem, "error subsystem")
  92. XCTAssertEqual(testLogging.category, "error category")
  93. XCTAssertEqual(testLogging.type, .error)
  94. XCTAssertEqual(testLogging.args.count, 4)
  95. }
  96. }
  97. fileprivate class TestLogging: Logging {
  98. var message: StaticString!
  99. var subsystem: String!
  100. var category: String!
  101. var type: OSLogType!
  102. var args: [CVarArg]!
  103. init() {}
  104. func log (_ message: StaticString, subsystem: String, category: String, type: OSLogType, _ args: [CVarArg]) {
  105. self.message = message
  106. self.subsystem = subsystem
  107. self.category = category
  108. self.type = type
  109. self.args = args
  110. }
  111. }