diff --git a/UnitTests/Mocks/MockTodoRepository.swift b/UnitTests/Mocks/MockTodoRepository.swift new file mode 100644 index 0000000..c7e1780 --- /dev/null +++ b/UnitTests/Mocks/MockTodoRepository.swift @@ -0,0 +1,49 @@ +@testable import class Todos.Todo +@testable import protocol Todos.TodoRepository + +/// A mock implementation of the TodoRepository protocol for unit testing. +/// +/// This mock repository provides a simple in-memory storage mechanism for testing +/// without requiring a full SwiftData ModelContext setup. It tracks inserted and +/// deleted todos, allowing tests to verify repository operations. +final class MockTodoRepository { + + // MARK: Properties + + /// Array of todo tasks managed by this mock repository. + /// + /// This property tracks all todos in the repository, maintaining state across + /// insert and delete operations. It serves as the in-memory storage for the mock, + /// allowing tests to verify repository behavior without requiring SwiftData. + private(set) var todos: [Todo] + + // MARK: Initializers + + /// Creates a new mock repository with an optional initial set of todo tasks. + /// + /// This initializer allows tests to set up a repository with pre-existing todos + /// or start with an empty repository. The default parameter value provides + /// convenient initialization for most test cases. + /// + /// - Parameter todos: An array of todo tasks to initialize the repository with. Defaults to an empty array. + init(todos: [Todo] = []) { + self.todos = todos + } + +} + +// MARK: - TodoRepository + +extension MockTodoRepository: TodoRepository { + + // MARK: Functions + + func delete(_ todo: Todo) { + todos.removeAll { $0.id == todo.id } + } + + func insert(_ todo: Todo) { + todos.append(todo) + } + +}