Improved the CreatedRootFolderTask task in the library target to throw error in case the function receives an empty name.
This commit is contained in:
parent
db1df0ec62
commit
6bf9c30ad1
@ -18,6 +18,10 @@ public struct CreateRootFolderTask {
|
||||
name: String,
|
||||
at location: URL? = nil
|
||||
) async throws -> URL {
|
||||
guard !name.isEmpty else {
|
||||
throw CreateRootFolderError.nameIsEmpty
|
||||
}
|
||||
|
||||
let rootFolder = if let location {
|
||||
location
|
||||
} else {
|
||||
@ -36,3 +40,9 @@ public struct CreateRootFolderTask {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Errors
|
||||
|
||||
public enum CreateRootFolderError: Error {
|
||||
case nameIsEmpty
|
||||
}
|
||||
|
@ -6,12 +6,19 @@ struct CreateRootFolderTaskTests {
|
||||
|
||||
// MARK: Functions tests
|
||||
|
||||
@Test(arguments: [String.someProjectName], [URL.someCurrentProjectFolder, .someNewProjectFolder])
|
||||
@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)
|
||||
@ -21,7 +28,7 @@ struct CreateRootFolderTaskTests {
|
||||
|
||||
// WHEN
|
||||
let result = try await task(name: name,
|
||||
at: folder == .someNewProjectFolder ? .someNewFolder : nil)
|
||||
at: location)
|
||||
|
||||
// THEN
|
||||
#expect(result == folder)
|
||||
@ -47,12 +54,30 @@ struct CreateRootFolderTaskTests {
|
||||
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"
|
||||
}
|
||||
|
||||
@ -60,5 +85,7 @@ private extension String {
|
||||
|
||||
private extension URL {
|
||||
static let someCurrentProjectFolder = URL.someCurrentFolder.appending(component: String.someProjectName)
|
||||
static let someDotProjectFolder = URL.someDotFolder.appending(component: String.someProjectName)
|
||||
static let someNewProjectFolder = URL.someNewFolder.appending(component: String.someProjectName)
|
||||
static let someTildeProjectFolder = URL.someTildeFolder.appending(component: String.someProjectName)
|
||||
}
|
||||
|
@ -7,10 +7,12 @@ extension URL {
|
||||
// MARK: Constants
|
||||
|
||||
static let someCurrentFolder = URL(at: "/some/current/folder")
|
||||
static let someDotFolder = URL(at: ".")
|
||||
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: "http://some.random.url")!
|
||||
static let someTildeFolder = URL(at: "~")
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user