From 1fc45e81276f83876c44ead10ce40b6a42f22cb7 Mon Sep 17 00:00:00 2001 From: Pedro Date: Sun, 9 Feb 2025 17:18:33 +0100 Subject: [PATCH] Fix tests --- tests/VM/VMDetailsPrinterTests.swift | 98 ++++++++++++++++------------ 1 file changed, 56 insertions(+), 42 deletions(-) diff --git a/tests/VM/VMDetailsPrinterTests.swift b/tests/VM/VMDetailsPrinterTests.swift index fad026c2..95a31ff1 100644 --- a/tests/VM/VMDetailsPrinterTests.swift +++ b/tests/VM/VMDetailsPrinterTests.swift @@ -5,50 +5,64 @@ import Foundation struct VMDetailsPrinterTests { @Test func printStatus_whenJSON() throws { - // Given - let vms: [VMDetails] = [VMDetails(name: "name", - os: "os", - cpuCount: 2, - memorySize: 1024, - diskSize: .init(allocated: 24, total: 30), - status: "status", - vncUrl: "vncUrl", - ipAddress: "0.0.0.0")] - let jsonEncoder = JSONEncoder() - jsonEncoder.outputFormatting = .prettyPrinted - let expectedOutput = try String(data: jsonEncoder.encode(vms), encoding: .utf8)! - - // When - var printedStatus: String? - try VMDetailsPrinter.printStatus(vms, format: .json, print: { printedStatus = $0 }) + // Given + let vms: [VMDetails] = [VMDetails(name: "name", + os: "os", + cpuCount: 2, + memorySize: 1024, + diskSize: .init(allocated: 24, total: 30), + status: "status", + vncUrl: "vncUrl", + ipAddress: "0.0.0.0")] + let jsonEncoder = JSONEncoder() + jsonEncoder.outputFormatting = .prettyPrinted + let expectedOutput = try String(data: jsonEncoder.encode(vms), encoding: .utf8)! + + // When + var printedStatus: String? + try VMDetailsPrinter.printStatus(vms, format: .json, print: { printedStatus = $0 }) - // Then - #expect(printedStatus == expectedOutput) - } - - @Test func printStatus_whenNotJSON() throws { - // Given - let vms: [VMDetails] = [VMDetails(name: "name", - os: "os", - cpuCount: 2, - memorySize: 1024, - diskSize: .init(allocated: 24, total: 30), - status: "status", - vncUrl: "vncUrl", - ipAddress: "0.0.0.0")] + // Then + // Decode both JSONs and compare the actual data structures + let jsonDecoder = JSONDecoder() + let printedVMs = try jsonDecoder.decode([VMDetails].self, from: printedStatus!.data(using: .utf8)!) + let expectedVMs = try jsonDecoder.decode([VMDetails].self, from: expectedOutput.data(using: .utf8)!) + + #expect(printedVMs.count == expectedVMs.count) + for (printed, expected) in zip(printedVMs, expectedVMs) { + #expect(printed.name == expected.name) + #expect(printed.os == expected.os) + #expect(printed.cpuCount == expected.cpuCount) + #expect(printed.memorySize == expected.memorySize) + #expect(printed.diskSize.allocated == expected.diskSize.allocated) + #expect(printed.diskSize.total == expected.diskSize.total) + #expect(printed.status == expected.status) + #expect(printed.vncUrl == expected.vncUrl) + #expect(printed.ipAddress == expected.ipAddress) + } + } - // When - var printedLines: [String] = [] - try VMDetailsPrinter.printStatus(vms, format: .text, print: { printedLines.append($0) }) + @Test func printStatus_whenNotJSON() throws { + // Given + let vms: [VMDetails] = [VMDetails(name: "name", + os: "os", + cpuCount: 2, + memorySize: 1024, + diskSize: .init(allocated: 24, total: 30), + status: "status", + vncUrl: "vncUrl", + ipAddress: "0.0.0.0")] + + // When + var printedLines: [String] = [] + try VMDetailsPrinter.printStatus(vms, format: .text, print: { printedLines.append($0) }) - // Then - #expect(printedLines.count == 2) - - - let headerParts = printedLines[0].split(whereSeparator: \.isWhitespace) - #expect(headerParts == ["name", "os", "cpu", "memory", "disk", "status", "ip", "vnc"]) + // Then + #expect(printedLines.count == 2) + + let headerParts = printedLines[0].split(whereSeparator: \.isWhitespace) + #expect(headerParts == ["name", "os", "cpu", "memory", "disk", "status", "ip", "vnc"]) - let vmParts = printedLines[1].split(whereSeparator: \.isWhitespace) - #expect(vmParts == ["name", "os", "2", "0.00G", "24.0B/30.0B", "status", "0.0.0.0", "vncUrl"]) - } + #expect(printedLines[1].split(whereSeparator: \.isWhitespace).map(String.init) == ["name", "os", "2", "0.00G", "24.0B/30.0B", "status", "0.0.0.0", "vncUrl"]) + } }