Restructured the folder structure in the library and test targets.

This commit is contained in:
2025-01-18 03:50:18 +01:00
parent 1466bff250
commit 1094bbb6c8
15 changed files with 0 additions and 0 deletions
@@ -0,0 +1,197 @@
import ColibriLibrary
import Foundation
import Testing
struct FileServiceTests {
// MARK: Properties
private let spy = FileServiceSpy()
// MARK: Properties tests
@Test func currentFolder() async {
// GIVEN
let url: URL = .someCurrentFolder
let service = FileServiceMock(currentFolder: url)
// WHEN
let folder = await service.currentFolder
// THEN
#expect(folder == url)
#expect(folder.isFileURL == true)
}
// MARK: Functions tests
@Test(arguments: zip([URL.someExistingFile, .someExistingFolder],
[URL.someNewFile, .someNewFolder]))
func copyFile(from source: URL, to destination: URL) async throws {
// GIVEN
let service = FileServiceMock(
currentFolder: .someCurrentFolder,
action: .copyFile(source, destination),
spy: spy
)
// WHEN
try await service.copyFile(from: source, to: destination)
// THENn
#expect(spy.actions.count == 1)
let action = try #require(spy.actions.last)
#expect(action == .fileCopied(source, destination))
}
@Test(arguments: [FileServiceError.itemAlreadyExists, .itemEmptyData, .itemNotCopied])
func copyItem(throws error: FileServiceError) async throws {
// GIVEN
let service = FileServiceMock(
currentFolder: .someCurrentFolder,
action: .error(error),
spy: spy
)
// WHEN
// THEN
await #expect(throws: error) {
try await service.copyFile(from: .someExistingFile, to: .someNewFile)
}
#expect(spy.actions.isEmpty == true)
}
@Test(arguments: [URL.someNewFolder, .someNewFile])
func createFolder(with location: URL) async throws {
// GIVEN
let service = FileServiceMock(
currentFolder: .someCurrentFolder,
action: .createFolder(location),
spy: spy
)
// WHEN
try await service.createFolder(at: location)
// THEN
#expect(spy.actions.count == 1)
let action = try #require(spy.actions.last)
#expect(action == .folderCreated(location))
}
@Test(arguments: zip([URL.someExistingFolder, .someExistingFile, .someRandomURL],
[FileServiceError.itemAlreadyExists, .itemAlreadyExists, .itemNotFileURL]))
func createFolder(
with location: URL,
throws error: FileServiceError
) async throws {
// GIVEN
let service = FileServiceMock(
currentFolder: .someCurrentFolder,
action: .error(error),
spy: spy
)
// WHEN
// THEN
await #expect(throws: error) {
try await service.createFolder(at: location)
}
#expect(spy.actions.isEmpty == true)
}
@Test(arguments: [URL.someNewFolder, .someNewFile])
func deleteItem(with location: URL) async throws {
// GIVEN
let service = FileServiceMock(
currentFolder: .someCurrentFolder,
action: .deleteItem(location),
spy: spy
)
// WHEN
try await service.deleteItem(at: location)
// THEN
#expect(spy.actions.count == 1)
let action = try #require(spy.actions.last)
#expect(action == .itemDeleted(location))
}
@Test(arguments: zip([URL.someNewFolder, .someNewFile, .someRandomURL],
[FileServiceError.itemNotExists, .itemNotExists, .itemNotFileURL]))
func deleteItem(
with location: URL,
throws error: FileServiceError
) async throws {
// GIVEN
let service = FileServiceMock(
currentFolder: .someCurrentFolder,
action: .error(error),
spy: spy
)
// WHEN
// THEN
await #expect(throws: error) {
try await service.deleteItem(at: location)
}
#expect(spy.actions.isEmpty == true)
}
@Test(arguments: zip([URL.someExistingFolder, .someExistingFile, .someNewFolder, .someNewFile],
[true, true, false, false]))
func isItemExists(
with location: URL,
expects outcome: Bool
) async throws {
// GIVEN
let service = FileServiceMock(
currentFolder: .someCurrentFolder,
action: .isItemExists(location, outcome),
spy: spy
)
// WHEN
let result = try await service.isItemExists(at: location)
// THEN
#expect(result == outcome)
let action = try #require(spy.actions.last)
#expect(action == .itemExists(location))
}
@Test(arguments: zip([URL.someRandomURL], [FileServiceError.itemNotFileURL]))
func isItemExists(
with location: URL,
throws error: FileServiceError
) async throws {
// GIVEN
let service = FileServiceMock(
currentFolder: .someCurrentFolder,
action: .error(error),
spy: spy
)
// WHEN
// THEN
await #expect(throws: error) {
try await service.isItemExists(at: location)
}
#expect(spy.actions.isEmpty == true)
}
}
@@ -0,0 +1,80 @@
import Foundation
import Testing
@testable import ColibriLibrary
struct CopyFilesTaskTests {
// MARK: Properties
private let resourceFolder = URL.someExistingFolder
private let rootFolder = URL.someNewFolder
private let spy = FileServiceSpy()
// MARK: Functions tests
@Test func copyFiles() async throws {
// GIVEN
let files = files(of: ResourceFile.allCases)
let actions = files.map { FileServiceMock.Action.copyFile($0.source, $0.destination) }
let copyFiles = CopyFilesTask(fileService: FileServiceMock(
currentFolder: .someCurrentFolder,
actions: actions,
spy: spy
))
// WHEN
try await copyFiles(to: rootFolder)
// THEN
#expect(spy.actions.count == actions.count)
files.enumerated().forEach { index, file in
#expect(spy.actions[index] == .fileCopied(file.source, file.destination))
}
}
@Test(arguments: [FileServiceError.itemAlreadyExists, .itemEmptyData, .itemNotCopied])
func copyFiles(throws error: FileServiceError) async throws {
// GIVEN
let files = files(of: Array(ResourceFile.allCases[0...2]))
let actions = files.map { FileServiceMock.Action.copyFile($0.source, $0.destination) }
let copyFiles = CopyFilesTask(fileService: FileServiceMock(
currentFolder: .someCurrentFolder,
actions: actions + [.error(error)],
spy: spy
))
// WHEN
// THEN
await #expect(throws: error) {
try await copyFiles(to: rootFolder)
}
#expect(spy.actions.count == actions.count)
files.enumerated().forEach { index, file in
#expect(spy.actions[index] == .fileCopied(file.source, file.destination))
}
}
}
// MARK: - Helpers
private extension CopyFilesTaskTests {
// MARK: Type aliases
typealias File = (source: URL, destination: URL)
// MARK: Functions
func files(of resourceFiles: [ResourceFile]) -> [File] {
resourceFiles.map { (resourceFolder.appendingPath($0.rawValue), rootFolder.appendingPath($0.fileName)) }
}
}
@@ -0,0 +1,37 @@
import Foundation
import Testing
@testable import ColibriLibrary
struct CreateFoldersTaskTests {
// MARK: Properties
private let spy = FileServiceSpy()
// MARK: Functions tests
@Test(arguments: [URL.someCurrentFolder, .someDotFolder, .someTildeFolder])
func createFolders(with rootFolder: URL) async throws {
// GIVEN
let folders = CreateFoldersTask.foldersToCreate.map { rootFolder.appendingPath($0) }
let actions: [FileServiceMock.Action] = folders.map { .createFolder($0) }
let service = FileServiceMock(
currentFolder: .someCurrentFolder,
actions: actions,
spy: spy
)
let createFolders = CreateFoldersTask(fileService: service)
// WHEN
try await createFolders(at: rootFolder)
// THEN
for index in actions.indices {
#expect(spy.actions[index] == .folderCreated(folders[index]))
}
}
}
@@ -0,0 +1,92 @@
import Foundation
import Testing
@testable import ColibriLibrary
struct CreateRootFolderTaskTests {
// MARK: Functions tests
@Test(arguments: [String.someProjectName], [URL.someCurrentProjectFolder, .someNewProjectFolder, .someDotProjectFolder, .someTildeProjectFolder])
func task(
name: String,
expects folder: URL
) async throws {
// GIVEN
let location: URL? = switch folder {
case .someNewProjectFolder: .someNewFolder
case .someDotProjectFolder: .someDotFolder
case .someTildeProjectFolder: .someTildeFolder
default: nil
}
let fileService = FileServiceMock(
currentFolder: .someCurrentFolder,
action: .createFolder(folder)
)
let task = CreateRootFolderTask(fileService: fileService)
// WHEN
let result = try await task(name: name,
at: location)
// THEN
#expect(result == folder)
#expect(result.isFileURL == true)
}
@Test(arguments: [String.someProjectName], [FileServiceError.itemAlreadyExists])
func task(
name: String,
throws error: FileServiceError
) async throws {
// GIVEN
let fileService = FileServiceMock(
currentFolder: .someCurrentFolder,
action: .error(error)
)
let task = CreateRootFolderTask(fileService: fileService)
// WHEN
// THEN
await #expect(throws: error) {
try await task(name: name)
}
}
@Test(arguments: [String.someEmptyName], [CreateRootFolderError.nameIsEmpty])
func task(
name: String,
throws error: CreateRootFolderError
) async throws {
// GIVEN
let fileService = FileServiceMock(currentFolder: .someCurrentFolder)
let task = CreateRootFolderTask(fileService: fileService)
// WHEN
// THEN
await #expect(throws: error) {
try await task(name: name)
}
}
}
// MARK: - String+Constants
private extension String {
static let someEmptyName = ""
static let someProjectName = "SomeProjectName"
}
// MARK: - URL+Constants
private extension URL {
static let someCurrentProjectFolder = URL.someCurrentFolder.appendingPath(.someProjectName)
static let someDotProjectFolder = URL.someDotFolder.appendingPath(.someProjectName)
static let someNewProjectFolder = URL.someNewFolder.appendingPath(.someProjectName)
static let someTildeProjectFolder = URL.someTildeFolder.appendingPath(.someProjectName)
}