-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
260 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import XCTest | ||
@testable import Orchard | ||
|
||
class ConsoleLoggerTests: XCTestCase { | ||
|
||
var consoleLogger: ConsoleLogger! | ||
var outputCapture: OutputCapture! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
consoleLogger = ConsoleLogger() | ||
outputCapture = OutputCapture() | ||
} | ||
|
||
override func tearDown() { | ||
consoleLogger = nil | ||
outputCapture = nil | ||
super.tearDown() | ||
} | ||
|
||
func testVerboseLogging() { | ||
outputCapture.startCapture() | ||
consoleLogger.verbose("Verbose test", nil, nil, #file, #fileID, #function, #line) | ||
let output = outputCapture.stopCapture() | ||
XCTAssertTrue(output.contains("🔬")) | ||
XCTAssertTrue(output.contains("Verbose test")) | ||
} | ||
|
||
func testDebugLogging() { | ||
outputCapture.startCapture() | ||
consoleLogger.debug("Debug test", nil, nil, #file, #fileID, #function, #line) | ||
let output = outputCapture.stopCapture() | ||
XCTAssertTrue(output.contains("🔍")) | ||
XCTAssertTrue(output.contains("Debug test")) | ||
} | ||
|
||
func testInfoLogging() { | ||
outputCapture.startCapture() | ||
consoleLogger.info("Info test", nil, nil, #file, #fileID, #function, #line) | ||
let output = outputCapture.stopCapture() | ||
XCTAssertTrue(output.contains("ℹ️")) | ||
XCTAssertTrue(output.contains("Info test")) | ||
} | ||
|
||
func testWarningLogging() { | ||
outputCapture.startCapture() | ||
consoleLogger.warning("Warning test", nil, nil, #file, #fileID, #function, #line) | ||
let output = outputCapture.stopCapture() | ||
XCTAssertTrue(output.contains("⚠️")) | ||
XCTAssertTrue(output.contains("Warning test")) | ||
} | ||
|
||
func testErrorLogging() { | ||
outputCapture.startCapture() | ||
consoleLogger.error("Error test", nil, nil, #file, #fileID, #function, #line) | ||
let output = outputCapture.stopCapture() | ||
XCTAssertTrue(output.contains("❌")) | ||
XCTAssertTrue(output.contains("Error test")) | ||
} | ||
|
||
func testFatalLogging() { | ||
outputCapture.startCapture() | ||
consoleLogger.fatal("Fatal test", nil, nil, #file, #fileID, #function, #line) | ||
let output = outputCapture.stopCapture() | ||
XCTAssertTrue(output.contains("⚡️")) | ||
XCTAssertTrue(output.contains("Fatal test")) | ||
} | ||
|
||
func testLoggingWithTimestamp() { | ||
consoleLogger.showTimesStamp = true | ||
outputCapture.startCapture() | ||
consoleLogger.info("Timestamp test", nil, nil, #file, #fileID, #function, #line) | ||
let output = outputCapture.stopCapture() | ||
XCTAssertTrue(output.matches("\\d{2}:\\d{2}:\\d{2}\\.\\d{3}:")) | ||
} | ||
|
||
func testLoggingWithoutTimestamp() { | ||
consoleLogger.showTimesStamp = false | ||
outputCapture.startCapture() | ||
consoleLogger.info("No timestamp test", nil, nil, #file, #fileID, #function, #line) | ||
let output = outputCapture.stopCapture() | ||
XCTAssertFalse(output.matches("\\d{2}:\\d{2}:\\d{2}\\.\\d{3}:")) | ||
} | ||
|
||
func testLoggingWithInvocation() { | ||
consoleLogger.showInvocation = true | ||
outputCapture.startCapture() | ||
consoleLogger.info("Invocation test", nil, nil, #file, #fileID, #function, #line) | ||
let output = outputCapture.stopCapture() | ||
XCTAssertTrue(output.contains("/ConsoleLoggerTests.testLoggingWithInvocation")) | ||
} | ||
|
||
func testLoggingWithoutInvocation() { | ||
consoleLogger.showInvocation = false | ||
outputCapture.startCapture() | ||
consoleLogger.info("No invocation test", nil, nil, #file, #fileID, #function, #line) | ||
let output = outputCapture.stopCapture() | ||
XCTAssertFalse(output.contains("/ConsoleLoggerTests.testLoggingWithoutInvocation")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import XCTest | ||
@testable import Orchard | ||
|
||
class MockLogger: Orchard.Logger { | ||
var level: Orchard.Level = .verbose | ||
var tag: String? | ||
var icon: Character? | ||
|
||
var lastLogLevel: Orchard.Level? | ||
var lastMessage: String? | ||
var lastError: Error? | ||
var lastArgs: [String: CustomStringConvertible]? | ||
var lastTag: String? | ||
var lastIcon: Character? | ||
|
||
func log(_ level: Orchard.Level, _ message: String?, _ error: Error?, _ args: [String: CustomStringConvertible]?, _ file: String, _ fileId: String, _ function: String, _ line: Int) { | ||
lastLogLevel = level | ||
lastMessage = message | ||
lastError = error | ||
lastArgs = args | ||
lastTag = tag | ||
lastIcon = icon | ||
} | ||
|
||
func verbose(_ message: String?, _ error: Error?, _ args: [String: CustomStringConvertible]?, _ file: String, _ fileId: String, _ function: String, _ line: Int) { | ||
log(.verbose, message, error, args, file, fileId, function, line) | ||
} | ||
|
||
func debug(_ message: String?, _ error: Error?, _ args: [String: CustomStringConvertible]?, _ file: String, _ fileId: String, _ function: String, _ line: Int) { | ||
log(.debug, message, error, args, file, fileId, function, line) | ||
} | ||
|
||
func info(_ message: String?, _ error: Error?, _ args: [String: CustomStringConvertible]?, _ file: String, _ fileId: String, _ function: String, _ line: Int) { | ||
log(.info, message, error, args, file, fileId, function, line) | ||
} | ||
|
||
func warning(_ message: String?, _ error: Error?, _ args: [String: CustomStringConvertible]?, _ file: String, _ fileId: String, _ function: String, _ line: Int) { | ||
log(.warning, message, error, args, file, fileId, function, line) | ||
} | ||
|
||
func error(_ message: String?, _ error: Error?, _ args: [String: CustomStringConvertible]?, _ file: String, _ fileId: String, _ function: String, _ line: Int) { | ||
log(.error, message, error, args, file, fileId, function, line) | ||
} | ||
|
||
func fatal(_ message: String?, _ error: Error?, _ args: [String: CustomStringConvertible]?, _ file: String, _ fileId: String, _ function: String, _ line: Int) { | ||
log(.fatal, message, error, args, file, fileId, function, line) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import XCTest | ||
@testable import Orchard | ||
|
||
class OutputCapture { | ||
private var originalStdout: Int32? | ||
private var pipe: Pipe? | ||
|
||
func startCapture() { | ||
originalStdout = dup(FileHandle.standardOutput.fileDescriptor) | ||
pipe = Pipe() | ||
dup2(pipe!.fileHandleForWriting.fileDescriptor, FileHandle.standardOutput.fileDescriptor) | ||
} | ||
|
||
func stopCapture() -> String { | ||
pipe?.fileHandleForWriting.closeFile() | ||
let data = pipe?.fileHandleForReading.readDataToEndOfFile() | ||
dup2(originalStdout!, FileHandle.standardOutput.fileDescriptor) | ||
return String(data: data ?? Data(), encoding: .utf8) ?? "" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
extension String { | ||
func matches(_ regex: String) -> Bool { | ||
self.range(of: regex, options: .regularExpression) != nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,92 @@ | ||
import XCTest | ||
@testable import Orchard | ||
|
||
final class OrchardTests: XCTestCase { | ||
func testExample() throws { | ||
// XCTest Documentation | ||
// https://developer.apple.com/documentation/xctest | ||
class OrchardTests: XCTestCase { | ||
|
||
// Defining Test Cases and Test Methods | ||
// https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods | ||
var mockLogger: MockLogger! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
mockLogger = MockLogger() | ||
Orchard.loggers = [mockLogger] | ||
} | ||
|
||
override func tearDown() { | ||
Orchard.loggers = [] | ||
mockLogger = nil | ||
super.tearDown() | ||
} | ||
|
||
func testVerboseLogging() { | ||
Orchard.v("Verbose message") | ||
XCTAssertEqual(mockLogger.lastLogLevel, .verbose) | ||
XCTAssertEqual(mockLogger.lastMessage, "Verbose message") | ||
} | ||
|
||
func testDebugLogging() { | ||
Orchard.d("Debug message") | ||
XCTAssertEqual(mockLogger.lastLogLevel, .debug) | ||
XCTAssertEqual(mockLogger.lastMessage, "Debug message") | ||
} | ||
|
||
func testInfoLogging() { | ||
Orchard.i("Info message") | ||
XCTAssertEqual(mockLogger.lastLogLevel, .info) | ||
XCTAssertEqual(mockLogger.lastMessage, "Info message") | ||
} | ||
|
||
func testWarningLogging() { | ||
Orchard.w("Warning message") | ||
XCTAssertEqual(mockLogger.lastLogLevel, .warning) | ||
XCTAssertEqual(mockLogger.lastMessage, "Warning message") | ||
} | ||
|
||
func testErrorLogging() { | ||
Orchard.e("Error message") | ||
XCTAssertEqual(mockLogger.lastLogLevel, .error) | ||
XCTAssertEqual(mockLogger.lastMessage, "Error message") | ||
} | ||
|
||
func testFatalLogging() { | ||
Orchard.f("Fatal message") | ||
XCTAssertEqual(mockLogger.lastLogLevel, .fatal) | ||
XCTAssertEqual(mockLogger.lastMessage, "Fatal message") | ||
} | ||
|
||
func testLoggingWithError() { | ||
let error = NSError(domain: "TestDomain", code: 100, userInfo: nil) | ||
Orchard.e("Error occurred", error) | ||
XCTAssertEqual(mockLogger.lastLogLevel, .error) | ||
XCTAssertEqual(mockLogger.lastMessage, "Error occurred") | ||
XCTAssertEqual(mockLogger.lastError as NSError?, error) | ||
} | ||
|
||
func testLoggingWithArgs() { | ||
Orchard.i("User action", ["userId": "123", "action": "login"]) | ||
XCTAssertEqual(mockLogger.lastLogLevel, .info) | ||
XCTAssertEqual(mockLogger.lastMessage, "User action") | ||
XCTAssertEqual(mockLogger.lastArgs?["userId"] as? String, "123") | ||
XCTAssertEqual(mockLogger.lastArgs?["action"] as? String, "login") | ||
} | ||
|
||
func testTagging() { | ||
Orchard.tag("NetworkManager").i("Network request") | ||
XCTAssertEqual(mockLogger.lastTag, "NetworkManager") | ||
XCTAssertEqual(mockLogger.lastMessage, "Network request") | ||
} | ||
|
||
func testIconSetting() { | ||
Orchard.icon("🌐").i("Custom icon log") | ||
XCTAssertEqual(mockLogger.lastIcon, "🌐") | ||
XCTAssertEqual(mockLogger.lastMessage, "Custom icon log") | ||
} | ||
|
||
func testChainedTagAndIcon() { | ||
Orchard.tag("UI").icon("👁️").d("UI update") | ||
XCTAssertEqual(mockLogger.lastTag, "UI") | ||
XCTAssertEqual(mockLogger.lastIcon, "👁️") | ||
XCTAssertEqual(mockLogger.lastMessage, "UI update") | ||
} | ||
} | ||
|
||
|