Skip to content

Commit 8d8f0f3

Browse files
committed
#15 Add CoreData fetch test for regex predicates
1 parent aad1c77 commit 8d8f0f3

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

‎Tests/CoreModelTests/CoreDataModelTests.swift‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,27 @@ import Testing
218218
#expect(inMemory.map { $0.attributes["name"] } == [.string("Bob"), .string("Carol")])
219219
}
220220

221+
@available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, *)
222+
@Test func regexPredicateFetch() throws {
223+
224+
let context = try Self.makeContext()
225+
try context.insert([
226+
Person(name: "Alice", age: 30).encode(),
227+
Person(name: "Bob", age: 17).encode()
228+
])
229+
230+
// Foundation.Predicate: the captured pattern becomes a MATCHES comparison
231+
let regex = try Regex("Al[a-z]+")
232+
let converted = try FetchRequest.Predicate(#Predicate<PersonRecord> { $0.name.contains(regex) })
233+
#expect(converted == "name".compare(.matches, .attribute(.string(".*Al[a-z]+.*"))))
234+
let results = try context.fetch(FetchRequest(entity: Person.entityName, predicate: converted))
235+
#expect(results.map { $0.attributes["name"] } == [.string("Alice")])
236+
237+
// the in-memory evaluator agrees with CoreData on the same objects
238+
let objects = try context.fetch(FetchRequest(entity: Person.entityName))
239+
#expect(objects.filtered(by: converted).map { $0.attributes["name"] } == [.string("Alice")])
240+
}
241+
221242
@Test func nullRelationshipInsert() throws {
222243
guard #available(macOS 12, iOS 15, watchOS 8, tvOS 15, *) else {
223244
return

0 commit comments

Comments
 (0)