1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022-2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#if canImport(TestSupport)
import TestSupport
#endif
#if canImport(RegexBuilder)
import RegexBuilder
#endif
#if !FOUNDATION_FRAMEWORK
// Resolve ambiguity between Foundation.#Predicate and FoundationEssentials.#Predicate
@freestanding(expression)
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
macro Predicate<each Input>(_ body: (repeat each Input) -> Bool) -> Predicate<repeat each Input> = #externalMacro(module: "FoundationMacros", type: "PredicateMacro")
#endif
// Work around an issue issue on older Swift compilers
#if compiler(>=6.0)
final class PredicateTests: XCTestCase {
override func setUp() async throws {
guard #available(macOS 14, iOS 17, tvOS 17, watchOS 10, *) else {
throw XCTSkip("This test is not available on this OS version")
}
}
struct Object {
var a: Int
var b: String
var c: Double
var d: Int
var e: Character
var f: Bool
var g: [Int]
var h: Date = .now
var i: Any = 3
}
struct Object2 {
var a: Bool
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testBasic() throws {
let compareTo = 2
let predicate = #Predicate<Object> {
$0.a == compareTo
}
try XCTAssertFalse(predicate.evaluate(Object(a: 1, b: "", c: 0, d: 0, e: "c", f: true, g: [])))
try XCTAssertTrue(predicate.evaluate(Object(a: 2, b: "", c: 0, d: 0, e: "c", f: true, g: [])))
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testVariadic() throws {
let predicate = #Predicate<Object, Int> {
$0.a == $1 + 1
}
XCTAssert(try predicate.evaluate(Object(a: 3, b: "", c: 0, d: 0, e: "c", f: true, g: []), 2))
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testArithmetic() throws {
let predicate = #Predicate<Object> {
$0.a + 2 == 4
}
XCTAssert(try predicate.evaluate(Object(a: 2, b: "", c: 0, d: 0, e: "c", f: true, g: [])))
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testDivision() throws {
let predicate = #Predicate<Object> {
$0.a / 2 == 3
}
let predicate2 = #Predicate<Object> {
$0.c / 2.1 <= 3.0
}
XCTAssert(try predicate.evaluate(Object(a: 6, b: "", c: 0, d: 0, e: "c", f: true, g: [])))
XCTAssert(try predicate2.evaluate(Object(a: 2, b: "", c: 6.0, d: 0, e: "c", f: true, g: [])))
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testBuildDivision() throws {
let predicate = #Predicate<Object> {
$0.a / 2 == 3
}
XCTAssert(try predicate.evaluate(Object(a: 6, b: "", c: 0, d: 0, e: "c", f: true, g: [])))
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testUnaryMinus() throws {
let predicate = #Predicate<Object> {
-$0.a == 17
}
XCTAssert(try predicate.evaluate(Object(a: -17, b: "", c: 0, d: 0, e: "c", f: true, g: [])))
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testCount() throws {
let predicate = #Predicate<Object> {
$0.g.count == 5
}
XCTAssert(try predicate.evaluate(Object(a: 0, b: "", c: 0, d: 0, e: "c", f: true, g: [2, 3, 5, 7, 11])))
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testFilter() throws {
let predicate = #Predicate<Object> { object in
!object.g.filter {
$0 == object.d
}.isEmpty
}
XCTAssert(try predicate.evaluate(Object(a: 0, b: "", c: 0.0, d: 17, e: "c", f: true, g: [3, 5, 7, 11, 13, 17, 19])))
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testContains() throws {
let predicate = #Predicate<Object> {
$0.g.contains($0.a)
}
XCTAssert(try predicate.evaluate(Object(a: 13, b: "", c: 0.0, d: 0, e: "c", f: true, g: [2, 3, 5, 11, 13, 17])))
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testContainsWhere() throws {
let predicate = #Predicate<Object> { object in
object.g.contains {
$0 % object.a == 0
}
}
XCTAssert(try predicate.evaluate(Object(a: 2, b: "", c: 0.0, d: 0, e: "c", f: true, g: [3, 5, 7, 2, 11, 13])))
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testAllSatisfy() throws {
let predicate = #Predicate<Object> { object in
object.g.allSatisfy {
$0 % object.d != 0
}
}
XCTAssert(try predicate.evaluate(Object(a: 0, b: "", c: 0.0, d: 2, e: "c", f: true, g: [3, 5, 7, 11, 13, 17, 19])))
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testOptional() throws {
struct Wrapper<T> {
let wrapped: T?
}
let predicate = #Predicate<Wrapper<Int>> {
($0.wrapped.flatMap { $0 + 1 } ?? 7) % 2 == 1
}
let predicate2 = #Predicate<Wrapper<Int>> {
$0.wrapped! == 19
}
XCTAssert(try predicate.evaluate(Wrapper<Int>(wrapped: 4)))
XCTAssert(try predicate.evaluate(Wrapper<Int>(wrapped: nil)))
XCTAssert(try predicate2.evaluate(Wrapper<Int>(wrapped: 19)))
XCTAssertThrowsError(try predicate2.evaluate(Wrapper<Int>(wrapped: nil)))
struct _NonCodableType : Equatable {}
let predicate3 = #Predicate<Wrapper<_NonCodableType>> {
$0.wrapped == nil
}
XCTAssertFalse(try predicate3.evaluate(Wrapper(wrapped: _NonCodableType())))
XCTAssertTrue(try predicate3.evaluate(Wrapper(wrapped: nil)))
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testConditional() throws {
let predicate = #Predicate<Bool, String, String> {
($0 ? $1 : $2) == "if branch"
}
XCTAssert(try predicate.evaluate(true, "if branch", "else branch"))
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testClosedRange() throws {
let predicate = #Predicate<Object> {
(3...5).contains($0.a)
}
let predicate2 = #Predicate<Object> {
($0.a ... $0.d).contains(4)
}
XCTAssert(try predicate.evaluate(Object(a: 4, b: "", c: 0.0, d: 0, e: "c", f: true, g: [])))
XCTAssert(try predicate2.evaluate(Object(a: 3, b: "", c: 0.0, d: 5, e: "c", f: true, g: [])))
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testRange() throws {
let predicate = #Predicate<Object> {
(3 ..< 5).contains($0.a)
}
let toMatch = 4
let predicate2 = #Predicate<Object> {
($0.a ..< $0.d).contains(toMatch)
}
XCTAssert(try predicate.evaluate(Object(a: 4, b: "", c: 0.0, d: 0, e: "c", f: true, g: [])))
XCTAssert(try predicate2.evaluate(Object(a: 3, b: "", c: 0.0, d: 5, e: "c", f: true, g: [])))
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testRangeContains() throws {
let date = Date.distantPast
let predicate = #Predicate<Object> {
(date ..< date).contains($0.h)
}
XCTAssertFalse(try predicate.evaluate(Object(a: 3, b: "", c: 0.0, d: 5, e: "c", f: true, g: [])))
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testTypes() throws {
let predicate = #Predicate<Object> {
($0.i as? Int).flatMap { $0 == 3 } ?? false
}
let predicate2 = #Predicate<Object> {
$0.i is Int
}
XCTAssert(try predicate.evaluate(Object(a: 3, b: "", c: 0.0, d: 0, e: "c", f: true, g: [])))
XCTAssert(try predicate2.evaluate(Object(a: 3, b: "", c: 0.0, d: 5, e: "c", f: true, g: [])))
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testSubscripts() throws {
var predicate = #Predicate<Object> {
$0.g[0] == 0
}
XCTAssertTrue(try predicate.evaluate(Object(a: 3, b: "", c: 0.0, d: 0, e: "c", f: true, g: [0])))
XCTAssertFalse(try predicate.evaluate(Object(a: 3, b: "", c: 0.0, d: 0, e: "c", f: true, g: [1])))
XCTAssertThrowsError(try predicate.evaluate(Object(a: 3, b: "", c: 0.0, d: 0, e: "c", f: true, g: [])))
predicate = #Predicate<Object> {
$0.g[0 ..< 2].isEmpty
}
XCTAssertFalse(try predicate.evaluate(Object(a: 3, b: "", c: 0.0, d: 0, e: "c", f: true, g: [0, 1, 2])))
XCTAssertFalse(try predicate.evaluate(Object(a: 3, b: "", c: 0.0, d: 0, e: "c", f: true, g: [0, 1])))
XCTAssertThrowsError(try predicate.evaluate(Object(a: 3, b: "", c: 0.0, d: 0, e: "c", f: true, g: [0])))
XCTAssertThrowsError(try predicate.evaluate(Object(a: 3, b: "", c: 0.0, d: 0, e: "c", f: true, g: [])))
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testLazyDefaultValueSubscript() throws {
struct Foo : Codable, Sendable {
var property: Int {
fatalError("This property should not have been accessed")
}
}
let foo = Foo()
let predicate = #Predicate<[String : Int]> {
$0["key", default: foo.property] == 1
}
XCTAssertFalse(try predicate.evaluate(["key" : 2]))
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testStaticValues() throws {
func assertPredicate<T>(_ pred: Predicate<T>, value: T, expected: Bool) throws {
XCTAssertEqual(try pred.evaluate(value), expected)
}
try assertPredicate(.true, value: "Hello", expected: true)
try assertPredicate(.false, value: "Hello", expected: false)
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testMaxMin() throws {
var predicate = #Predicate<Object> {
$0.g.max() == 2
}
XCTAssertFalse(try predicate.evaluate(Object(a: 3, b: "", c: 0.0, d: 0, e: "c", f: true, g: [1, 3])))
XCTAssertTrue(try predicate.evaluate(Object(a: 3, b: "", c: 0.0, d: 0, e: "c", f: true, g: [1, 2])))
predicate = #Predicate<Object> {
$0.g.min() == 2
}
XCTAssertFalse(try predicate.evaluate(Object(a: 3, b: "", c: 0.0, d: 0, e: "c", f: true, g: [1, 3])))
XCTAssertTrue(try predicate.evaluate(Object(a: 3, b: "", c: 0.0, d: 0, e: "c", f: true, g: [2, 3])))
}
#if FOUNDATION_FRAMEWORK
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testCaseInsensitiveCompare() throws {
let equal = ComparisonResult.orderedSame
let predicate = #Predicate<Object> {
$0.b.caseInsensitiveCompare("ABC") == equal
}
XCTAssertTrue(try predicate.evaluate(Object(a: 3, b: "abc", c: 0.0, d: 0, e: "c", f: true, g: [1, 3])))
XCTAssertFalse(try predicate.evaluate(Object(a: 3, b: "def", c: 0.0, d: 0, e: "c", f: true, g: [1, 3])))
}
#endif
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testBuildDynamically() throws {
func _build(_ equal: Bool) -> Predicate<Int> {
Predicate<Int> {
if equal {
PredicateExpressions.Equal(
lhs: $0,
rhs: PredicateExpressions.Value(1)
)
} else {
PredicateExpressions.NotEqual(
lhs: $0,
rhs: PredicateExpressions.Value(1)
)
}
}
}
XCTAssertTrue(try _build(true).evaluate(1))
XCTAssertFalse(try _build(false).evaluate(1))
}
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
func testResilientKeyPaths() {
// Local, non-resilient type
struct Foo {
let a: String // Non-resilient
let b: Date // Resilient (in Foundation)
let c: String // Non-resilient
}
let now = Date.now
let _ = #Predicate<Foo> {
$0.a == $0.c && $0.b == now
}
}
#if compiler(>=5.11)
func testRegex() throws {
guard #available(FoundationPredicateRegex 0.4, *) else {
throw XCTSkip("This test is not available on this OS version")
}
let literalRegex = #/[AB0-9]\/?[^\n]+/#
var predicate = #Predicate<Object> {
$0.b.contains(literalRegex)
}
XCTAssertTrue(try predicate.evaluate(Object(a: 0, b: "_0/bc", c: 0, d: 0, e: " ", f: true, g: [])))
XCTAssertFalse(try predicate.evaluate(Object(a: 0, b: "_C/bc", c: 0, d: 0, e: " ", f: true, g: [])))
predicate = #Predicate<Object> {
$0.b.contains(#/[AB0-9]\/?[^\n]+/#)
}
XCTAssertTrue(try predicate.evaluate(Object(a: 0, b: "_0/bc", c: 0, d: 0, e: " ", f: true, g: [])))
XCTAssertFalse(try predicate.evaluate(Object(a: 0, b: "_C/bc", c: 0, d: 0, e: " ", f: true, g: [])))
}
func testRegex_RegexBuilder() throws {
#if !canImport(RegexBuilder)
throw XCTSkip("RegexBuilder is unavavailable on this platform")
#elseif !os(Linux) && !os(Android) && !FOUNDATION_FRAMEWORK
// Disable this test in swift-foundation macOS CI because of incorrect availability annotations in the StringProcessing module
throw XCTSkip("This test is currently disabled on this platform")
#else
guard #available(FoundationPredicateRegex 0.4, *) else {
throw XCTSkip("This test is not available on this OS version")
}
let builtRegex = Regex {
ChoiceOf {
"A"
"B"
CharacterClass.digit
}
Optionally("/")
OneOrMore(.anyNonNewline)
}
let predicate = #Predicate<Object> {
$0.b.contains(builtRegex)
}
XCTAssertTrue(try predicate.evaluate(Object(a: 0, b: "_0/bc", c: 0, d: 0, e: " ", f: true, g: [])))
XCTAssertFalse(try predicate.evaluate(Object(a: 0, b: "_C/bc", c: 0, d: 0, e: " ", f: true, g: [])))
#endif
}
#endif
func testDebugDescription() throws {
guard #available(FoundationPredicate 0.3, *) else {
throw XCTSkip("This test is not available on this OS version")
}
let date = Date.now
let predicate = #Predicate<Object> {
if let num = $0.i as? Int {
num == 3
} else {
$0.h == date
}
}
#if FOUNDATION_FRAMEWORK
let moduleName = "Foundation"
let testModuleName = "Unit"
#else
let moduleName = "FoundationEssentials"
let testModuleName = "FoundationEssentialsTests"
#endif
XCTAssertEqual(
predicate.description,
"""
capture1 (Swift.Int): 3
capture2 (\(moduleName).Date): <Date \(date.timeIntervalSince1970)>
Predicate<\(testModuleName).PredicateTests.Object> { input1 in
(input1.i as? Swift.Int).flatMap({ variable1 in
variable1 == capture1
}) ?? (input1.h == capture2)
}
"""
)
let debugDescription = predicate.debugDescription.replacing(#/Variable\([0-9]+\)/#, with: "Variable(#)")
XCTAssertEqual(
debugDescription,
"\(moduleName).Predicate<Pack{\(testModuleName).PredicateTests.Object}>(variable: (Variable(#)), expression: NilCoalesce(lhs: OptionalFlatMap(wrapped: ConditionalCast(input: KeyPath(root: Variable(#), keyPath: \\Object.i), desiredType: Swift.Int), variable: Variable(#), transform: Equal(lhs: Variable(#), rhs: Value<Swift.Int>(3))), rhs: Equal(lhs: KeyPath(root: Variable(#), keyPath: \\Object.h), rhs: Value<\(moduleName).Date>(\(date.debugDescription)))))"
)
}
#if FOUNDATION_FRAMEWORK
func testNested() throws {
guard #available(FoundationPredicate 0.3, *) else {
throw XCTSkip("This test is not available on this OS version")
}
let predicateA = #Predicate<Object> {
$0.a == 3
}
let predicateB = #Predicate<Object> {
predicateA.evaluate($0) && $0.a > 2
}
XCTAssertTrue(try predicateA.evaluate(Object(a: 3, b: "abc", c: 0.0, d: 0, e: "c", f: true, g: [1, 3])))
XCTAssertFalse(try predicateA.evaluate(Object(a: 2, b: "abc", c: 0.0, d: 0, e: "c", f: true, g: [1, 3])))
XCTAssertTrue(try predicateB.evaluate(Object(a: 3, b: "abc", c: 0.0, d: 0, e: "c", f: true, g: [1, 3])))
XCTAssertFalse(try predicateB.evaluate(Object(a: 2, b: "abc", c: 0.0, d: 0, e: "c", f: true, g: [1, 3])))
XCTAssertFalse(try predicateB.evaluate(Object(a: 4, b: "abc", c: 0.0, d: 0, e: "c", f: true, g: [1, 3])))
}
#endif
func testExpression() throws {
guard #available(FoundationPredicate 0.4, *) else {
throw XCTSkip("This test is not available on this OS version")
}
let expression = #Expression<Int, Int> {
$0 + 1
}
for i in 0 ..< 10 {
XCTAssertEqual(try expression.evaluate(i), i + 1)
}
}
}
#endif // compiler(>=6.0)
|