-
Notifications
You must be signed in to change notification settings - Fork 233
Initializers for joining a sequence of predicates #1878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -541,4 +541,42 @@ private struct PredicateTests { | |
| #expect(try expression.evaluate(i) == i + 1) | ||
| } | ||
| } | ||
|
|
||
| @Test func all() throws { | ||
| #expect(try Predicate(all: []).evaluate(42)) | ||
| #expect(try !Predicate(all: [Predicate.true, Predicate.false]).evaluate(42)) | ||
| let input = Object(a: 6, b: "xyz", c: 0.27, d: 97, e: "u", f: false, g: [7, 42, 1, 0, 25]) | ||
| var predicates: [Predicate<Object>] = [ | ||
| #Predicate { $0.a == 6 }, | ||
| #Predicate { $0.b == "xyz" }, | ||
| #Predicate { $0.c < 0.3 }, | ||
| #Predicate { $0.d == 97 }, | ||
| #Predicate { $0.d % 2 != 0 }, | ||
| #Predicate { !$0.f }, | ||
| #Predicate { $0.g.contains(42) }, | ||
| #Predicate { $0.g.min() == 0 } | ||
| ] | ||
| #expect(try Predicate(all: predicates).evaluate(input)) | ||
| predicates.append(#Predicate { $0.g.max() == 7 }) | ||
| #expect(try !Predicate(all: predicates).evaluate(input)) | ||
| predicates.append(#Predicate { $0.c < 0.0 }) | ||
| #expect(try !Predicate(all: predicates).evaluate(input)) | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add tests that validate the short circuiting behavior to validate that predicates later in the list aren't evaluated if a prior expression fails? This should be possible with a custom object and a property (referenced in the predicate via a keypath) that records when the property is accessed. |
||
|
|
||
| @Test func any() throws { | ||
| #expect(try !Predicate(any: []).evaluate(42)) | ||
| #expect(try Predicate(any: [Predicate.true, Predicate.false]).evaluate(42)) | ||
| let input = Object(a: 6, b: "xyz", c: 0.27, d: 97, e: "u", f: false, g: [7, 42, 1, 0, 25]) | ||
| let predicates: [Predicate<Object>] = [ | ||
| #Predicate { $0.g.max() == 7 }, | ||
| #Predicate { $0.c < 0.0 }, | ||
| #Predicate { $0.a == 6 }, | ||
| #Predicate { $0.b == "xyz" }, | ||
| #Predicate { $0.c < 0.3 } | ||
| ] | ||
| #expect(try Predicate(any: predicates.dropLast()).evaluate(input)) | ||
| #expect(try Predicate(any: predicates.dropLast(2)).evaluate(input)) | ||
| #expect(try !Predicate(any: predicates.dropLast(3)).evaluate(input)) | ||
| #expect(try !Predicate(any: predicates.dropLast(4)).evaluate(input)) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm actually a bit surprised this compiles without telling it what the
Inputtype of the predicate is here. Does it infer an input of typeIntfrom the call toevaluateusing the type of42?