DDContextFilterLogFormatterTests.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2025, 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/XCTest.h>
  16. #import <CocoaLumberjack/DDContextFilterLogFormatter.h>
  17. static DDLogMessage *testLogMessage(void) {
  18. return [[DDLogMessage alloc] initWithFormat:@"test log message"
  19. formatted:@"test log message"
  20. level:DDLogLevelDebug
  21. flag:DDLogFlagError
  22. context:1
  23. file:@(__FILE__)
  24. function:@(__func__)
  25. line:__LINE__
  26. tag:NULL
  27. options:(DDLogMessageOptions)0
  28. timestamp:nil];
  29. }
  30. @interface DDContextAllowlistFilterLogFormatterTests : XCTestCase
  31. @property (nonatomic, strong, readwrite) DDContextAllowlistFilterLogFormatter *filterLogFormatter;
  32. @end
  33. @implementation DDContextAllowlistFilterLogFormatterTests
  34. - (void)setUp {
  35. [super setUp];
  36. self.filterLogFormatter = [[DDContextAllowlistFilterLogFormatter alloc] init];
  37. }
  38. - (void)tearDown {
  39. self.filterLogFormatter = nil;
  40. [super tearDown];
  41. }
  42. - (void)testAllowlistFilterLogFormatter {
  43. XCTAssertEqualObjects([self.filterLogFormatter allowlist], @[]);
  44. XCTAssertFalse([self.filterLogFormatter isOnAllowlist:1]);
  45. XCTAssertNil([self.filterLogFormatter formatLogMessage:testLogMessage()]);
  46. [self.filterLogFormatter addToAllowlist:1];
  47. XCTAssertEqualObjects([self.filterLogFormatter allowlist], @[@1]);
  48. XCTAssertTrue([self.filterLogFormatter isOnAllowlist:1]);
  49. XCTAssertEqualObjects([self.filterLogFormatter formatLogMessage:testLogMessage()], @"test log message");
  50. [self.filterLogFormatter removeFromAllowlist:1];
  51. XCTAssertEqualObjects([self.filterLogFormatter allowlist], @[]);
  52. XCTAssertFalse([self.filterLogFormatter isOnAllowlist:1]);
  53. XCTAssertNil([self.filterLogFormatter formatLogMessage:testLogMessage()]);
  54. }
  55. @end
  56. @interface DDContextDenylistFilterLogFormatterTests : XCTestCase
  57. @property (nonatomic, strong, readwrite) DDContextDenylistFilterLogFormatter *filterLogFormatter;
  58. @end
  59. @implementation DDContextDenylistFilterLogFormatterTests
  60. - (void)setUp {
  61. [super setUp];
  62. self.filterLogFormatter = [[DDContextDenylistFilterLogFormatter alloc] init];
  63. }
  64. - (void)tearDown {
  65. self.filterLogFormatter = nil;
  66. [super tearDown];
  67. }
  68. - (void)testDDContextDenylistFilterLogFormatterTests {
  69. XCTAssertEqualObjects([self.filterLogFormatter denylist], @[]);
  70. XCTAssertFalse([self.filterLogFormatter isOnDenylist:1]);
  71. XCTAssertEqualObjects([self.filterLogFormatter formatLogMessage:testLogMessage()], @"test log message");
  72. [self.filterLogFormatter addToDenylist:1];
  73. XCTAssertEqualObjects([self.filterLogFormatter denylist], @[@1]);
  74. XCTAssertTrue([self.filterLogFormatter isOnDenylist:1]);
  75. XCTAssertNil([self.filterLogFormatter formatLogMessage:testLogMessage()]);
  76. [self.filterLogFormatter removeFromDenylist:1];
  77. XCTAssertEqualObjects([self.filterLogFormatter denylist], @[]);
  78. XCTAssertFalse([self.filterLogFormatter isOnDenylist:1]);
  79. XCTAssertEqualObjects([self.filterLogFormatter formatLogMessage:testLogMessage()], @"test log message");
  80. }
  81. @end