Skip to content

Commit

Permalink
Add logging tests
Browse files Browse the repository at this point in the history
  • Loading branch information
crazytonyli committed Nov 24, 2022
1 parent 8962d23 commit d91d92d
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Sources/WordPressSharedObjC/Logging/WPSharedLogging.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

NS_ASSUME_NONNULL_BEGIN

id<WordPressLoggingDelegate> _Nullable WPSharedGetLoggingDelegate(void);
void WPSharedSetLoggingDelegate(id<WordPressLoggingDelegate> _Nullable logger);
FOUNDATION_EXTERN id<WordPressLoggingDelegate> _Nullable WPSharedGetLoggingDelegate(void);
FOUNDATION_EXTERN void WPSharedSetLoggingDelegate(id<WordPressLoggingDelegate> _Nullable logger);

FOUNDATION_EXTERN void WPSharedLogError(NSString *str, ...) NS_FORMAT_FUNCTION(1, 2);
FOUNDATION_EXTERN void WPSharedLogWarning(NSString *str, ...) NS_FORMAT_FUNCTION(1, 2);
Expand Down
72 changes: 72 additions & 0 deletions Tests/WordPressSharedObjCTests/LoggingTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#import <XCTest/XCTest.h>

@import WordPressSharedObjC;

@interface CaptureLogs : NSObject<WordPressLoggingDelegate>

@property (nonatomic, strong) NSMutableArray *infoLogs;
@property (nonatomic, strong) NSMutableArray *errorLogs;

@end

@implementation CaptureLogs

- (instancetype)init
{
if ((self = [super init])) {
self.infoLogs = [NSMutableArray new];
self.errorLogs = [NSMutableArray new];
}
return self;
}

- (void)logInfo:(NSString *)str
{
[self.infoLogs addObject:str];
}

- (void)logError:(NSString *)str
{
[self.errorLogs addObject:str];
}

@end

@interface LoggingTest : XCTestCase

@property (nonatomic, strong) CaptureLogs *logger;

@end

@implementation LoggingTest

- (void)setUp
{
self.logger = [CaptureLogs new];
WPSharedSetLoggingDelegate(self.logger);
}

- (void)testLogging
{
WPSharedLogInfo(@"This is an info log");
WPSharedLogInfo(@"This is an info log %@", @"with an argument");
XCTAssertEqualObjects(self.logger.infoLogs, (@[@"This is an info log", @"This is an info log with an argument"]));

WPSharedLogError(@"This is an error log");
WPSharedLogError(@"This is an error log %@", @"with an argument");
XCTAssertEqualObjects(self.logger.errorLogs, (@[@"This is an error log", @"This is an error log with an argument"]));
}

- (void)testUnimplementedLoggingMethod
{
XCTAssertNoThrow(WPSharedLogVerbose(@"verbose logging is not implemented"));
}

- (void)testNoLogging
{
WPSharedSetLoggingDelegate(nil);
XCTAssertNoThrow(WPSharedLogInfo(@"this log should not be printed"));
XCTAssertEqual(self.logger.infoLogs.count, 0);
}

@end
70 changes: 70 additions & 0 deletions Tests/WordPressSharedTests/LoggingTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import XCTest

@testable import WordPressShared

private class CaptureLogs: NSObject, WordPressLoggingDelegate {
var verboseLogs = [String]()
var debugLogs = [String]()
var infoLogs = [String]()
var warningLogs = [String]()
var errorLogs = [String]()

func logError(_ str: String) {
errorLogs.append(str)
}

func logWarning(_ str: String) {
warningLogs.append(str)
}

func logInfo(_ str: String) {
infoLogs.append(str)
}

func logDebug(_ str: String) {
debugLogs.append(str)
}

func logVerbose(_ str: String) {
verboseLogs.append(str)
}

}

class LoggingTest: XCTestCase {

private let logger = CaptureLogs()

override func setUp() {
WPSharedSetLoggingDelegate(logger)
}

func testLogging() {
WPSharedLogVerbose("This is a verbose log")
WPSharedLogVerbose("This is a verbose log %@", "with an argument")
XCTAssertEqual(self.logger.verboseLogs, ["This is a verbose log", "This is a verbose log with an argument"])

WPSharedLogDebug("This is a debug log")
WPSharedLogDebug("This is a debug log %@", "with an argument")
XCTAssertEqual(self.logger.debugLogs, ["This is a debug log", "This is a debug log with an argument"])

WPSharedLogInfo("This is an info log")
WPSharedLogInfo("This is an info log %@", "with an argument")
XCTAssertEqual(self.logger.infoLogs, ["This is an info log", "This is an info log with an argument"])

WPSharedLogWarning("This is a warning log")
WPSharedLogWarning("This is a warning log %@", "with an argument")
XCTAssertEqual(self.logger.warningLogs, ["This is a warning log", "This is a warning log with an argument"])

WPSharedLogError("This is an error log")
WPSharedLogError("This is an error log %@", "with an argument")
XCTAssertEqual(self.logger.errorLogs, ["This is an error log", "This is an error log with an argument"])
}

func testNoLogging() {
WPSharedSetLoggingDelegate(nil);
XCTAssertNoThrow(WPSharedLogInfo("this log should not be printed"));
XCTAssertEqual(self.logger.infoLogs.count, 0);
}

}

0 comments on commit d91d92d

Please sign in to comment.