TestHelpers.swift (1254B)
1 import Foundation 2 import Testing 3 4 #if os(macOS) 5 @testable import Listless 6 #else 7 @testable import Listless_iOS 8 #endif 9 10 /// Creates a fresh ItemStore with in-memory persistence for isolated testing. 11 @MainActor 12 func makeTestStore() -> ItemStore { 13 let controller = PersistenceController(inMemory: true) 14 return ItemStore(persistenceController: controller) 15 } 16 17 /// Creates a ItemStore pre-populated with test items. 18 /// - Parameters: 19 /// - count: Number of items to create (default: 3) 20 /// - titles: Optional array of titles; if nil, generates "Item 1", "Item 2", etc. 21 /// - Returns: Tuple of (store, array of created item IDs) 22 @MainActor 23 func makeTestStoreWithItems(count: Int = 3, titles: [String]? = nil) throws -> (ItemStore, [UUID]) { 24 let store = makeTestStore() 25 var itemIDs: [UUID] = [] 26 27 for i in 0..<count { 28 let title = titles?[safe: i] ?? "Task \(i + 1)" 29 let item = try store.createItem(title: title) 30 try store.save() 31 itemIDs.append(item.id) 32 } 33 34 return (store, itemIDs) 35 } 36 37 /// Safe array subscript that returns nil instead of crashing on out-of-bounds access. 38 extension Array { 39 subscript(safe index: Int) -> Element? { 40 return indices.contains(index) ? self[index] : nil 41 } 42 }