From a7be1ec0b094ae3073115280f0764f9d857a37f9 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Sat, 11 Jan 2025 11:28:27 +0100 Subject: [PATCH] Implemented the FileServiceMock mock in the Tests target. --- .../Helpers/Mocks/FileServiceMock.swift | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 Tests/Library/Helpers/Mocks/FileServiceMock.swift diff --git a/Tests/Library/Helpers/Mocks/FileServiceMock.swift b/Tests/Library/Helpers/Mocks/FileServiceMock.swift new file mode 100644 index 0000000..fa7b6d7 --- /dev/null +++ b/Tests/Library/Helpers/Mocks/FileServiceMock.swift @@ -0,0 +1,84 @@ +import ColibriLibrary +import Foundation + +struct FileServiceMock { + + // MARK: Properties + + private let action: Action? + private let folder: URL + + private weak var spy: FileServiceSpy? + + // MARK: Initialisers + + init( + currentFolder: URL, + action: Action? = nil, + spy: FileServiceSpy? = nil + ) { + self.action = action + self.folder = currentFolder + self.spy = spy + } + +} + +// MARK: - FileServicing + +extension FileServiceMock: FileServicing { + + // MARK: Computed + + var currentFolder: URL { + get async { folder } + } + + // MARK: Functions + + func createFolder(at url: URL) async throws(FileServiceError) { + switch action { + case .error(let error): + throw error + case let .createFolder(url): + try await spy?.createFolder(at: url) + default: + break + } + } + + func delete(at url: URL) async throws(FileServiceError) { + switch action { + case .error(let error): + throw error + case let .delete(url): + try await spy?.delete(at: url) + default: + break + } + } + + func exists(at url: URL) async throws(FileServiceError) -> Bool { + switch action { + case .error(let error): + throw error + case let .exists(url, exists): + try await spy?.exists(at: url) + return exists + default: + return false + } + } + +} + +// MARK: - Enumerations + +extension FileServiceMock { + enum Action { + case createFolder(URL) + case delete(URL) + case error(FileServiceError) + case exists(URL, Bool) + } +}