Skip to content

Commit

Permalink
test: add logger tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mishamyrt committed Jul 13, 2024
1 parent 3524372 commit 0b8275e
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 6 deletions.
18 changes: 12 additions & 6 deletions Sources/Logger/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import Rainbow
struct Logger {
var level: LogLevel
var out: UnsafeMutablePointer<FILE>
var showTimestamp: Bool

init(level: LogLevel, out: UnsafeMutablePointer<FILE> = stdout) {
init(level: LogLevel = .error, out: UnsafeMutablePointer<FILE> = stdout, showTimestamp: Bool = true) {
self.level = level
self.out = out
self.showTimestamp = showTimestamp
}

mutating func setLevel(_ level: LogLevel) {
Expand All @@ -18,10 +20,14 @@ struct Logger {
/// Print message to stdout.
/// This function is uses low level API to print correct output colors.
func print(_ message: String) {
let timestamp = now().dim
fputs("\(timestamp) \(message)\n", stdout)
fflush(out)
}
var line = ""
if showTimestamp {
line += timestamp() + " "
}
line += message
fputs(line + "\n", out)
fflush(out)
}

/// Print information message to stdout.
func info(_ message: String) {
Expand Down Expand Up @@ -51,7 +57,7 @@ struct Logger {
}
}

private func now() -> String {
private func timestamp() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return dateFormatter.string(from: Date())
Expand Down
66 changes: 66 additions & 0 deletions Tests/Logger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import Testing
import Foundation
@testable import runon

@Suite struct LoggerSuite {
@Test("check basic logger creation")
func testBasicConstructor() {
let logger = Logger(level: .debug)
#expect(logger.level == .debug)
}

@Test("check logger set level")
func testSetLevel() {
var logger = Logger(level: .debug)
#expect(logger.level == .debug)
logger.setLevel(.info)
#expect(logger.level == .info)
}

@Test("check logger print")
func testPrint() {
let out = CStream()
let logger = Logger(
out: out.handle,
showTimestamp: false
)
logger.print("test")
#expect(out.content == "test\n")
}

@Test("check logger print levels")
func testPrintLevels() {
let out = CStream()
var logger = Logger(
level: .error,
out: out.handle,
showTimestamp: false
)
logger.warning("should not be printed")
logger.error("should be printed")
#expect(out.meaningfulLines.count == 1)
logger.setLevel(.info)
logger.debug("should not be printed")
logger.info("should be printed")
logger.warning("should be printed")
#expect(out.meaningfulLines.count == 3)
logger.setLevel(.debug)
logger.debug("should be printed")
logger.info("should be printed")
logger.warning("should be printed")
#expect(out.meaningfulLines.count == 6)
}

@Test("check logger prints timestamp")
func testPrintsTimestamp() {
let out = CStream()
let logger = Logger(
out: out.handle
)
logger.print("test")
let parts = out.lines[0].components(separatedBy: " ")
#expect(parts[0].components(separatedBy: "-").count == 3)
#expect(parts[1].components(separatedBy: ":") .count == 3)
#expect(parts[2] == "test")
}
}
41 changes: 41 additions & 0 deletions Tests/TestUtils.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Testing
import Darwin.C
@testable import runon

func expectEqualActions(_ a: Action?, _ b: Action?) {
Expand All @@ -16,3 +17,43 @@ func expectEqualActions(_ a: Action?, _ b: Action?) {
#expect(a.timeout == b.timeout)
#expect(a.group == b.group)
}

class CStream {
var size: UnsafeMutablePointer<size_t>?
var handle: UnsafeMutablePointer<FILE>!
var buffer: UnsafeMutablePointer<Int8>?

var content: String { read() }
var lines: [String] { content.components(separatedBy: "\n") }
var meaningfulLines: [String] { lines.filter { !$0.isEmpty } }
var isEmpty: Bool { content.isEmpty }

init() {
allocate()
}

deinit {
deallocate()
}

func clear() {
deallocate()
allocate()
}

private func read() -> String {
String(cString: buffer!)
}

private func allocate() {
buffer = UnsafeMutablePointer<Int8>.allocate(capacity: 1)
size = UnsafeMutablePointer<size_t>.allocate(capacity: 1)
handle = open_memstream(&buffer, size)
}

private func deallocate() {
fclose(handle)
buffer?.deallocate()
size?.deallocate()
}
}

0 comments on commit 0b8275e

Please sign in to comment.