Written test cases for the FileServicing conformance using relevant mock and spy in the Tests target.
This commit is contained in:
parent
a7be1ec0b0
commit
3f98d08a00
166
Tests/Library/Cases/Services/FileServiceTests.swift
Normal file
166
Tests/Library/Cases/Services/FileServiceTests.swift
Normal file
@ -0,0 +1,166 @@
|
|||||||
|
import Foundation
|
||||||
|
import Testing
|
||||||
|
|
||||||
|
@testable import ColibriLibrary
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
@Test(arguments: [URL.someNewFolder, .someNewFile])
|
||||||
|
func createFolder(with url: URL) async throws {
|
||||||
|
// GIVEN
|
||||||
|
let service = FileServiceMock(
|
||||||
|
currentFolder: .someCurrentFolder,
|
||||||
|
action: .createFolder(url),
|
||||||
|
spy: spy
|
||||||
|
)
|
||||||
|
|
||||||
|
// WHEN
|
||||||
|
try await service.createFolder(at: url)
|
||||||
|
|
||||||
|
// THEN
|
||||||
|
#expect(spy.isCreateFolderCalled == true)
|
||||||
|
#expect(spy.urlCalled == url)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(arguments: zip([URL.someExistingFolder, .someExistingFile, .someRandomURL],
|
||||||
|
[FileServiceError.urlAlreadyExists, .urlAlreadyExists, .urlNotFileURL]))
|
||||||
|
func createFolder(
|
||||||
|
with url: 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: url)
|
||||||
|
}
|
||||||
|
|
||||||
|
#expect(spy.isCreateFolderCalled == false)
|
||||||
|
#expect(spy.urlCalled == nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(arguments: [URL.someNewFolder, .someNewFile])
|
||||||
|
func delete(with url: URL) async throws {
|
||||||
|
// GIVEN
|
||||||
|
let service = FileServiceMock(
|
||||||
|
currentFolder: .someCurrentFolder,
|
||||||
|
action: .delete(url),
|
||||||
|
spy: spy
|
||||||
|
)
|
||||||
|
|
||||||
|
// WHEN
|
||||||
|
try await service.delete(at: url)
|
||||||
|
|
||||||
|
// THEN
|
||||||
|
#expect(spy.isDeleteCalled == true)
|
||||||
|
#expect(spy.urlCalled == url)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(arguments: zip([URL.someNewFolder, .someNewFile, .someRandomURL],
|
||||||
|
[FileServiceError.urlNotExists, .urlNotExists, .urlNotFileURL]))
|
||||||
|
func delete(
|
||||||
|
with url: 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.delete(at: url)
|
||||||
|
}
|
||||||
|
|
||||||
|
#expect(spy.isDeleteCalled == false)
|
||||||
|
#expect(spy.urlCalled == nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(arguments: zip([URL.someExistingFolder, .someExistingFile, .someNewFolder, .someNewFile],
|
||||||
|
[true, true, false, false]))
|
||||||
|
func exists(
|
||||||
|
with url: URL,
|
||||||
|
expects outcome: Bool
|
||||||
|
) async throws {
|
||||||
|
// GIVEN
|
||||||
|
let service = FileServiceMock(
|
||||||
|
currentFolder: .someCurrentFolder,
|
||||||
|
action: .exists(url, outcome),
|
||||||
|
spy: spy
|
||||||
|
)
|
||||||
|
|
||||||
|
// WHEN
|
||||||
|
let result = try await service.exists(at: url)
|
||||||
|
|
||||||
|
// THEN
|
||||||
|
#expect(result == outcome)
|
||||||
|
|
||||||
|
#expect(spy.isExistsAtCalled == true)
|
||||||
|
#expect(spy.urlCalled == url)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(arguments: zip([URL.someRandomURL], [FileServiceError.urlNotFileURL]))
|
||||||
|
func exists(
|
||||||
|
with url: 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.exists(at: url)
|
||||||
|
}
|
||||||
|
|
||||||
|
#expect(spy.isExistsAtCalled == false)
|
||||||
|
#expect(spy.urlCalled == nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - URL+Constants
|
||||||
|
|
||||||
|
private extension URL {
|
||||||
|
static let someCurrentFolder = URL(at: "/some/current/folder")
|
||||||
|
static let someExistingFolder = URL(at: "/some/existing/folder")
|
||||||
|
static let someExistingFile = URL(at: "/some/existing/file")
|
||||||
|
static let someNewFolder = URL(at: "/some/new/folder")
|
||||||
|
static let someNewFile = URL(at: "/some/new/file")
|
||||||
|
static let someRandomURL = URL(string: "some.random.url")!
|
||||||
|
}
|
@ -1,129 +0,0 @@
|
|||||||
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")!
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user