Skip to content

Commit 76d7791

Browse files
Fix fatal bug in is(_:) (#142)
* fix * add test code * wip --------- Co-authored-by: Stephen Celis <[email protected]>
1 parent 6c533b1 commit 76d7791

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

Sources/CasePaths/CasePathable.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,10 +410,15 @@ extension CasePathable {
410410
/// userActions.filter { $0.is(\.home) } // [UserAction.home(.onAppear)]
411411
/// userActions.filter { $0.is(\.settings) } // [UserAction.settings(.subscribeButtonTapped)]
412412
/// ```
413+
@_disfavoredOverload
413414
public func `is`(_ keyPath: PartialCaseKeyPath<Self>) -> Bool {
414415
self[case: keyPath] != nil
415416
}
416417

418+
public func `is`<Wrapped>(_ keyPath: CaseKeyPath<Self, Wrapped?>) -> Bool {
419+
self[case: keyPath] != nil
420+
}
421+
417422
/// Unwraps and yields a mutable associated value to a closure.
418423
///
419424
/// > Warning: If the enum's case does not match the given case key path, the mutation will not be

Tests/CasePathsTests/CasePathsTests.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,14 @@ final class CasePathsTests: XCTestCase {
132132
func testMatch() {
133133
switch Foo.bar(.int(42)) {
134134
case \.bar.int:
135-
return
135+
break
136136
default:
137137
XCTFail()
138138
}
139139

140140
switch Foo.bar(.int(42)) {
141141
case \.bar:
142-
return
142+
break
143143
default:
144144
XCTFail()
145145
}
@@ -150,6 +150,10 @@ final class CasePathsTests: XCTestCase {
150150
XCTAssertFalse(Foo.bar(.int(42)).is(\.baz.string))
151151
XCTAssertFalse(Foo.bar(.int(42)).is(\.blob))
152152
XCTAssertFalse(Foo.bar(.int(42)).is(\.fizzBuzz))
153+
XCTAssertTrue(Foo.foo(nil).is(\.foo))
154+
XCTAssertTrue(Foo.foo(nil).is(\.foo.none))
155+
XCTAssertTrue(Foo.foo("").is(\.foo))
156+
XCTAssertFalse(Foo.foo(nil).is(\.bar))
153157
}
154158

155159
func testPartialCaseKeyPath() {
@@ -169,6 +173,7 @@ final class CasePathsTests: XCTestCase {
169173
case baz(Baz)
170174
case fizzBuzz
171175
case blob(Blob)
176+
case foo(String?)
172177
}
173178
@CasePathable @dynamicMemberLookup enum Bar: Equatable {
174179
case int(Int)

0 commit comments

Comments
 (0)