Skip to content

Commit

Permalink
add predicate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fatbobman committed Feb 26, 2024
1 parent d98b643 commit 8b93d52
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 0 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file modified NewFeaturesOfScrollView/.DS_Store
Binary file not shown.
8 changes: 8 additions & 0 deletions PredicateStudy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
27 changes: 27 additions & 0 deletions PredicateStudy/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "PredicateStudy",
platforms: [
.iOS(.v17),
.macOS(.v14)
],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "PredicateStudy",
targets: ["PredicateStudy"]),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(
name: "PredicateStudy"),
.testTarget(
name: "PredicateStudyTests",
dependencies: ["PredicateStudy"]),
]
)
59 changes: 59 additions & 0 deletions PredicateStudy/Sources/PredicateStudy/PredicateStudy.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// The Swift Programming Language
// https://docs.swift.org/swift-book
import Foundation
import SwiftData

public struct People {
public var name: String
public var age: Int

public init(name: String, age: Int) {
self.name = name
self.age = age
}

public static let fat = People(name: "Fat", age: 19)
}

public struct Dog {
public var name: String
public var age: Int
public init(name: String, age: Int) {
self.name = name
self.age = age
}

public static let patton = Dog(name: "Patton", age: 1)
}

public class NSDog: NSObject {
public var name: String
public var age: Int
public init(name: String, age: Int) {
self.name = name
self.age = age
}
}

@Model
public final class Note {
public var name: String?
@Relationship(deleteRule: .cascade)
public var item: [Item]?
public init(name: String? = nil) {
self.name = name
item = item
}
}

@Model
public final class Item {
public var name: String?
public var content: String?
@Relationship(deleteRule: .nullify)
public var note: Note?
public init(name: String? = nil, content: String? = nil) {
self.name = name
self.content = content
}
}
114 changes: 114 additions & 0 deletions PredicateStudy/Tests/PredicateStudyTests/PredicateStudyTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
@testable import PredicateStudy
import XCTest

final class PredicateStudyTests: XCTestCase {
func testContains() throws {
let names = ["fat", "bob", "man"]
let fat = People.fat
let predicate = #Predicate<People> { people in
names.contains(where: { $0.localizedLowercase == people.name.localizedLowercase })
}
XCTAssertTrue(try predicate.evaluate(fat))
}

func testSubscript() throws {
let names = ["fat", "bob", "man"]
let predicate = #Predicate<[String]> { $0.first == "fat" && $0[2] != "n" && $0.count == 3 }
try XCTAssertTrue(predicate.evaluate(names))
}

func testMultiInput() throws {
let dog = Dog.patton
let fat = People.fat
let predicate = #Predicate<Dog, People> { $0.age < 10 && $1.age < 10 }
try XCTAssertFalse(predicate.evaluate(dog, fat))
}

func testNSObject() throws {
class MyObject: NSObject {
@objc var name: String
init(name: String) {
self.name = name
}
}
let object = MyObject(name: "fat")
let predicate = NSPredicate(format: "name = %@", "fat")
XCTAssertTrue(predicate.evaluate(with: object)) // true

let objs = [object]
// filter object by predicate
let filteredObjs = (objs as NSArray).filtered(using: predicate) as! [MyObject]
XCTAssertEqual(filteredObjs.count, 1)
}

func testSwiftPredicate() throws {
class MyObject {
var name:String
init(name: String) {
self.name = name
}
}

let object = MyObject(name: "fat")
let predicate = #Predicate<MyObject>{ object in
object.name == "fat" && object.name.count >= 3
}
try XCTAssertTrue(predicate.evaluate(object)) // true

let objs = [object]
let filteredObjs = try objs.filter(predicate)
XCTAssertEqual(filteredObjs.count, 1)
}

func testHexCompare() throws {
let fat = People.fat
let predicate = #Predicate<People> { $0.age == 0x13 }
try XCTAssertTrue(predicate.evaluate(fat))
}

func testExpression() throws {
let express = { (value1: PredicateExpressions.Variable<Int>, value2: PredicateExpressions.Variable<Int>) in
PredicateExpressions.build_Comparison(
lhs: value1,
rhs: value2,
op: .lessThan
)
}

let predicate = Predicate {
express($0, $1)
}

let n = 3
let m = 4

try XCTAssertTrue(predicate.evaluate(n, m)) // true
}

func testExpression1() throws {
let predicate = #Predicate<Int, Int> { $0 < $1 }

let n = 3
let m = 4

try XCTAssertTrue(predicate.evaluate(n, m)) // true
}

func testFunction() throws {
struct Address {
var city: String
}
struct People {
var address: [Address]
}

let predicate = #Predicate<People> { people in
people.address.contains { address in
address.city == "Dalian"
}
}

let p = People(address: [.init(city: "Dalian")])
XCTAssertTrue(try predicate.evaluate(p))
}
}
Binary file added iCloudDocumentsDemo/.DS_Store
Binary file not shown.

0 comments on commit 8b93d52

Please sign in to comment.