From 75b806af3d086a536302c2fe87dea44a09d12c5a Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Mon, 17 Feb 2025 23:01:27 +0100 Subject: [PATCH] Written test cases for the RenderFileTask task in the unit tests target. --- Library/Sources/Public/Models/Project.swift | 2 +- .../Public/Tasks/RenderFilesTaskTests.swift | 41 +++++++++++++++++++ .../Helpers/Spies/TemplateServiceSpy.swift | 2 +- 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 Test/Sources/Cases/Public/Tasks/RenderFilesTaskTests.swift diff --git a/Library/Sources/Public/Models/Project.swift b/Library/Sources/Public/Models/Project.swift index d1505e5..730439c 100644 --- a/Library/Sources/Public/Models/Project.swift +++ b/Library/Sources/Public/Models/Project.swift @@ -1,4 +1,4 @@ -public struct Project: Sendable { +public struct Project: Equatable, Sendable { // MARK: Properties diff --git a/Test/Sources/Cases/Public/Tasks/RenderFilesTaskTests.swift b/Test/Sources/Cases/Public/Tasks/RenderFilesTaskTests.swift new file mode 100644 index 0000000..08cc1ed --- /dev/null +++ b/Test/Sources/Cases/Public/Tasks/RenderFilesTaskTests.swift @@ -0,0 +1,41 @@ +import Foundation +import Testing + +@testable import ColibriLibrary + +struct RenderFilesTaskTests { + + @Test(arguments: [URL.someCurrentFolder], [Project(name: "Some name goes here...")]) + func task(at rootFolder: URL, with project: Project) async throws { + // GIVEN + let fileService = FileServiceSpy() + let templateService = TemplateServiceSpy() + + let renderFiles = RenderFilesTask(fileService: fileService, + templateService: templateService) + + // WHEN + try await renderFiles(at: rootFolder, with: project) + + // THEN + let fileData = Data() + let templates = Template.allCases + + #expect(fileService.actions.count == 3) + #expect(templateService.actions.count == 3) + + fileService.actions.enumerated().forEach { index, action in + #expect(action == .fileCreated(rootFolder.appendingPath(templates[index].filePath), fileData)) + } + + templateService.actions.enumerated().forEach { index, action in + if case let .rendered(object, template) = action { + #expect(object as? Project == project) + #expect(template == templates[index].rawValue) + } else { + Issue.record("Action should have been a case of the `TemplateServiceSpy.Action` enumeration.") + } + } + } + +} diff --git a/Test/Sources/Helpers/Spies/TemplateServiceSpy.swift b/Test/Sources/Helpers/Spies/TemplateServiceSpy.swift index 5922300..cc8a0c5 100644 --- a/Test/Sources/Helpers/Spies/TemplateServiceSpy.swift +++ b/Test/Sources/Helpers/Spies/TemplateServiceSpy.swift @@ -15,7 +15,7 @@ extension TemplateServiceSpy: TemplateServicing { // MARK: Functions @discardableResult - func render(_ object: Any, on template: String) async throws(TemplateServiceError) -> String { + func render(_ object: Any, on template: String) async throws (TemplateServiceError) -> String { actions.append(.rendered(object, template)) return .content