Implemented the "createFile(at: with: )" function for the FileService service in the library target.

This commit is contained in:
2025-02-07 21:50:50 +01:00
parent 3dcb110de1
commit 33ae67fc58
5 changed files with 78 additions and 9 deletions
@@ -35,7 +35,7 @@ struct FileServiceTests {
// WHEN
try await service.copyFile(from: source, to: destination)
// THENn
// THEN
#expect(spy.actions.count == 1)
let action = try #require(spy.actions.last)
@@ -57,6 +57,37 @@ struct FileServiceTests {
#expect(spy.actions.isEmpty == true)
}
@Test(arguments: zip([URL.someNewFile],
[Data("some data goes here...".utf8)]))
func createFile(with location: URL, and data: Data) async throws {
// GIVEN
let service = service(action: .createFile(location, data))
// WHEN
try await service.createFile(at: location, with: data)
// THEN
#expect(spy.actions.count == 1)
let action = try #require(spy.actions.last)
#expect(action == .fileCreated(location, data))
}
@Test(arguments: [FileServiceError.itemAlreadyExists, .fileDataIsEmpty, .fileNotCreated])
func createFile(throws error: FileServiceError) async throws {
// GIVEN
let service = service(action: .error(error))
// WHEN
// THEN
await #expect(throws: error) {
try await service.createFile(at: .someNewFile, with: .init())
}
#expect(spy.actions.isEmpty == true)
}
@Test(arguments: [URL.someNewFolder, .someNewFile])
func createFolder(with location: URL) async throws {
// GIVEN
@@ -64,6 +64,19 @@ extension FileServiceMock: FileServicing {
}
}
func createFile(at location: URL, with data: Data) async throws (FileServiceError) {
guard let nextAction else { return }
switch nextAction {
case .error(let error):
throw error
case let .createFile(location, data):
try await spy?.createFile(at: location, with: data)
default:
break
}
}
func createFolder(at location: URL) async throws (FileServiceError) {
guard let nextAction else { return }
@@ -127,6 +140,7 @@ private extension FileServiceMock {
extension FileServiceMock {
enum Action {
case copyFile(URL, URL)
case createFile(URL, Data)
case createFolder(URL)
case deleteItem(URL)
case error(FileServiceError)
@@ -21,6 +21,10 @@ extension FileServiceSpy: FileServicing {
actions.append(.fileCopied(source, destination))
}
func createFile(at location: URL, with data: Data) async throws (FileServiceError) {
actions.append(.fileCreated(location, data))
}
func createFolder(at location: URL) async throws (FileServiceError) {
actions.append(.folderCreated(location))
}
@@ -42,6 +46,7 @@ extension FileServiceSpy: FileServicing {
extension FileServiceSpy {
enum Action: Equatable {
case fileCreated(_ location: URL, _ data: Data)
case fileCopied(_ source: URL, _ destination: URL)
case folderCreated(_ location: URL)
case itemDeleted(_ location: URL)