DDLogTests.m 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. @interface DDTestLogger : NSObject <DDLogger>
  18. @end
  19. @implementation DDTestLogger
  20. @synthesize logFormatter;
  21. - (void)logMessage:(nonnull DDLogMessage *)logMessage {}
  22. @end
  23. @interface DDLogTests : XCTestCase
  24. @end
  25. // The fact thath the DDLog is initialized using +initialize makes it a bit
  26. // dificult to test as the state of the class might be hanging there when
  27. // test examples are run in parallel. Trying to reset the DDLog before & after
  28. // each test.
  29. @implementation DDLogTests
  30. - (void)setUp {
  31. [super setUp];
  32. [DDLog removeAllLoggers];
  33. }
  34. - (void)tearDown {
  35. [DDLog removeAllLoggers];
  36. [super tearDown];
  37. }
  38. #pragma mark - Logger management
  39. - (void)testAddLoggerAddsNewLoggerWithDDLogLevelAll {
  40. __auto_type logger = [DDTestLogger new];
  41. [DDLog addLogger:logger];
  42. XCTAssertEqual([DDLog allLoggers].count, 1);
  43. }
  44. - (void)testAddLoggerWithLevelAddLoggerWithSpecifiedLevelMask {
  45. __auto_type logger = [DDTestLogger new];
  46. [DDLog addLogger:logger withLevel:DDLogLevelDebug | DDLogLevelError];
  47. XCTAssertEqual([DDLog allLoggers].count, 1);
  48. }
  49. - (void)testRemoveLoggerRemovesExistingLogger {
  50. __auto_type logger = [DDTestLogger new];
  51. [DDLog addLogger:logger];
  52. [DDLog addLogger:[DDTestLogger new]];
  53. [DDLog removeLogger:logger];
  54. XCTAssertEqual([DDLog allLoggers].count, 1);
  55. XCTAssertFalse([[DDLog allLoggers] firstObject] == logger);
  56. }
  57. - (void)testRemoveAllLoggersRemovesAllLoggers {
  58. [DDLog addLogger:[DDTestLogger new]];
  59. [DDLog addLogger:[DDTestLogger new]];
  60. [DDLog removeAllLoggers];
  61. XCTAssertEqual([DDLog allLoggers].count, 0);
  62. }
  63. - (void)testAllLoggersReturnsAllLoggers {
  64. [DDLog addLogger:[DDTestLogger new]];
  65. [DDLog addLogger:[DDTestLogger new]];
  66. XCTAssertEqual([DDLog allLoggers].count, 2);
  67. }
  68. - (void)testAllLoggersWithLevelReturnsAllLoggersWithLevel {
  69. [DDLog addLogger:[DDTestLogger new]];
  70. [DDLog addLogger:[DDTestLogger new] withLevel:DDLogLevelDebug];
  71. [DDLog addLogger:[DDTestLogger new] withLevel:DDLogLevelInfo];
  72. XCTAssertEqual([DDLog allLoggersWithLevel].count, 3);
  73. XCTAssertEqual([[[DDLog allLoggersWithLevel] firstObject] level], DDLogLevelAll);
  74. XCTAssertEqual([[DDLog allLoggersWithLevel][1] level], DDLogLevelDebug);
  75. XCTAssertEqual([[DDLog allLoggersWithLevel][2] level], DDLogLevelInfo);
  76. }
  77. @end