130 lines
3.3 KiB
Swift
130 lines
3.3 KiB
Swift
import Foundation
|
|
import Testing
|
|
|
|
@testable import ColibriLibrary
|
|
|
|
struct FileServiceTests {
|
|
|
|
// MARK: Properties
|
|
|
|
private let service: FileServicing
|
|
|
|
// MARK: Initialisers
|
|
|
|
init() {
|
|
self.service = FileService()
|
|
}
|
|
|
|
// MARK: Properties tests
|
|
|
|
@Test func currentFolder() async {
|
|
// GIVEN
|
|
// WHEN
|
|
let url = await service.currentFolder
|
|
|
|
// THEN
|
|
#expect(url == .someExistingFolder)
|
|
#expect(url.isFileURL == true)
|
|
}
|
|
|
|
// MARK: Functions
|
|
|
|
@Test(arguments: [URL.someNewFolder, .someNewFile])
|
|
func createFolder(with url: URL) async throws {
|
|
// GIVEN
|
|
if try await service.exists(at: url) {
|
|
try await service.delete(at: url)
|
|
}
|
|
|
|
// WHEN
|
|
try await service.createFolder(at: url)
|
|
|
|
// THEN
|
|
let result = try await service.exists(at: url)
|
|
|
|
#expect(result == true)
|
|
|
|
try await service.delete(at: url)
|
|
}
|
|
|
|
@Test(arguments: zip([URL.someExistingFolder, .someExistingFile, .someRandomURL],
|
|
[FileServiceError.urlAlreadyExists, .urlAlreadyExists, .urlNotFileURL]))
|
|
func createFolderThrows(
|
|
with url: URL,
|
|
expects error: FileServiceError
|
|
) async throws {
|
|
// GIVEN
|
|
// WHEN
|
|
// THEN
|
|
await #expect(throws: error) {
|
|
try await service.createFolder(at: url)
|
|
}
|
|
}
|
|
|
|
@Test(arguments: [URL.someNewFolder, .someNewFile])
|
|
func delete(with url: URL) async throws {
|
|
// GIVEN
|
|
if try await !service.exists(at: url) {
|
|
try await service.createFolder(at: url)
|
|
}
|
|
|
|
// WHEN
|
|
try await service.delete(at: url)
|
|
|
|
// THEN
|
|
let result = try await service.exists(at: url)
|
|
|
|
#expect(result == false)
|
|
}
|
|
|
|
@Test(arguments: zip([URL.someNewFolder, .someNewFile, .someRandomURL],
|
|
[FileServiceError.urlNotExists, .urlNotExists, .urlNotFileURL]))
|
|
func deleteThrows(
|
|
with url: URL,
|
|
expects error: FileServiceError
|
|
) async throws {
|
|
// GIVEN
|
|
// WHEN
|
|
// THEN
|
|
await #expect(throws: error) {
|
|
try await service.delete(at: url)
|
|
}
|
|
}
|
|
|
|
@Test(arguments: zip([URL.someExistingFolder, .someExistingFile, .someNewFolder, .someNewFile],
|
|
[true, true, false, false]))
|
|
func exists(
|
|
with url: URL,
|
|
expects outcome: Bool
|
|
) async throws {
|
|
// GIVEN
|
|
// WHEN
|
|
let result = try await service.exists(at: url)
|
|
|
|
// THEN
|
|
#expect(result == outcome)
|
|
}
|
|
|
|
@Test func existsThrows() async throws {
|
|
// GIVEN
|
|
let url = URL.someRandomURL
|
|
|
|
// WHEN
|
|
// THEN
|
|
await #expect(throws: FileServiceError.urlNotFileURL) {
|
|
try await service.exists(at: url)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - URL+Constants
|
|
|
|
private extension URL {
|
|
static let someExistingFolder = URL(at: "/private/tmp")
|
|
static let someExistingFile = URL(at: "/etc/ssh/ssh_config")
|
|
static let someNewFolder = URL(at: "/private/tmp/folder")
|
|
static let someNewFile = URL(at: "/private/tmp/file.ext")
|
|
static let someRandomURL = URL(string: "https://some.random.url")!
|
|
}
|