|
| 1 | +// |
| 2 | +// BranchLoggerTests.m |
| 3 | +// Branch-SDK-Tests |
| 4 | +// |
| 5 | +// Created by Nipun Singh on 2/5/24. |
| 6 | +// Copyright © 2024 Branch, Inc. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import <XCTest/XCTest.h> |
| 10 | +#import "BranchLogger.h" |
| 11 | +#import "Branch.h" |
| 12 | + |
| 13 | +@interface BranchLoggerTests : XCTestCase |
| 14 | +@end |
| 15 | + |
| 16 | +@implementation BranchLoggerTests |
| 17 | + |
| 18 | +- (void)testEnableLoggingSetsCorrectDefaultLevel { |
| 19 | + [[Branch getInstance] enableLogging]; |
| 20 | + XCTAssertEqual([BranchLogger shared].logLevelThreshold, BranchLogLevelDebug, "Default log level should be Debug."); |
| 21 | +} |
| 22 | + |
| 23 | +- (void)testLogLevelThresholdBlocksLowerLevels { |
| 24 | + BranchLogger *logger = [BranchLogger new]; |
| 25 | + logger.loggingEnabled = true; |
| 26 | + logger.logLevelThreshold = BranchLogLevelDebug; |
| 27 | + |
| 28 | + XCTestExpectation *expectation = [self expectationWithDescription:@"Log callback expectation for message that should pass the threshold"]; |
| 29 | + |
| 30 | + logger.logCallback = ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error) { |
| 31 | + if ([message isEqualToString:@"[BranchSDK][Debug][BranchLoggerTests testLogLevelThresholdBlocksLowerLevels] This message should trigger the log callback."] && logLevel >= BranchLogLevelDebug) { |
| 32 | + [expectation fulfill]; |
| 33 | + } else if (logLevel == BranchLogLevelVerbose) { |
| 34 | + XCTFail(); |
| 35 | + } |
| 36 | + }; |
| 37 | + |
| 38 | + [logger logVerbose:@"This verbose message should not trigger the log callback."]; |
| 39 | + [logger logDebug:@"This message should trigger the log callback."]; |
| 40 | + |
| 41 | + [self waitForExpectationsWithTimeout:1 handler:nil]; |
| 42 | +} |
| 43 | + |
| 44 | +- (void)testLogCallbackExecutesWithCorrectParameters { |
| 45 | + XCTestExpectation *expectation = [self expectationWithDescription:@"Log callback expectation"]; |
| 46 | + NSString *expectedMessage = @"[BranchSDK][Info][BranchLoggerTests testLogCallbackExecutesWithCorrectParameters] Test message"; |
| 47 | + BranchLogLevel expectedLevel = BranchLogLevelInfo; |
| 48 | + |
| 49 | + BranchLogger *logger = [BranchLogger new]; |
| 50 | + |
| 51 | + logger.logCallback = ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error) { |
| 52 | + XCTAssertEqualObjects(message, expectedMessage, "Logged message does not match expected message."); |
| 53 | + XCTAssertEqual(logLevel, expectedLevel, "Logged level does not match expected level."); |
| 54 | + XCTAssertNil(error, "Error should be nil."); |
| 55 | + [expectation fulfill]; |
| 56 | + }; |
| 57 | + |
| 58 | + logger.loggingEnabled = YES; |
| 59 | + logger.logLevelThreshold = BranchLogLevelInfo; |
| 60 | + [logger logInfo:@"Test message"]; |
| 61 | + |
| 62 | + [self waitForExpectationsWithTimeout:1 handler:nil]; |
| 63 | +} |
| 64 | + |
| 65 | +- (void)testLogLevelSpecificityFiltersLowerLevels { |
| 66 | + BranchLogger *logger = [BranchLogger new]; |
| 67 | + logger.loggingEnabled = YES; |
| 68 | + logger.logLevelThreshold = BranchLogLevelWarning; |
| 69 | + |
| 70 | + XCTestExpectation *verboseExpectation = [self expectationWithDescription:@"Verbose log callback"]; |
| 71 | + verboseExpectation.inverted = YES; |
| 72 | + XCTestExpectation *errorExpectation = [self expectationWithDescription:@"Error log callback"]; |
| 73 | + |
| 74 | + __block NSUInteger callbackCount = 0; |
| 75 | + logger.logCallback = ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error) { |
| 76 | + if (logLevel == BranchLogLevelVerbose) { |
| 77 | + [verboseExpectation fulfill]; |
| 78 | + } else if (logLevel == BranchLogLevelError) { |
| 79 | + [errorExpectation fulfill]; |
| 80 | + } |
| 81 | + callbackCount++; |
| 82 | + }; |
| 83 | + |
| 84 | + [logger logVerbose:@"This should not be logged due to log level threshold."]; |
| 85 | + [logger logError:@"This should be logged" error:nil]; |
| 86 | + |
| 87 | + [self waitForExpectations:@[verboseExpectation, errorExpectation] timeout:2]; |
| 88 | + XCTAssertEqual(callbackCount, 1, "Only one log callback should have been invoked."); |
| 89 | +} |
| 90 | + |
| 91 | +- (void)testErrorLoggingIncludesErrorDetails { |
| 92 | + BranchLogger *logger = [BranchLogger new]; |
| 93 | + logger.loggingEnabled = YES; |
| 94 | + |
| 95 | + XCTestExpectation *expectation = [self expectationWithDescription:@"Error log includes error details"]; |
| 96 | + |
| 97 | + NSError *testError = [NSError errorWithDomain:@"TestDomain" code:42 userInfo:@{NSLocalizedDescriptionKey: @"Test error description"}]; |
| 98 | + logger.logCallback = ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error) { |
| 99 | + if ([message containsString:@"Test error description"] && error == testError) { |
| 100 | + [expectation fulfill]; |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + [logger logError:@"Testing error logging" error:testError]; |
| 105 | + |
| 106 | + [self waitForExpectationsWithTimeout:1 handler:nil]; |
| 107 | +} |
| 108 | + |
| 109 | +@end |
0 commit comments