Skip to content

Commit

Permalink
Fix fatal bug in is(_:) (#142)
Browse files Browse the repository at this point in the history
* fix

* add test code

* wip

---------

Co-authored-by: Stephen Celis <stephen@stephencelis.com>
  • Loading branch information
Ryu0118 and stephencelis authored Jan 10, 2024
1 parent 6c533b1 commit 76d7791
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Sources/CasePaths/CasePathable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,15 @@ extension CasePathable {
/// userActions.filter { $0.is(\.home) } // [UserAction.home(.onAppear)]
/// userActions.filter { $0.is(\.settings) } // [UserAction.settings(.subscribeButtonTapped)]
/// ```
@_disfavoredOverload
public func `is`(_ keyPath: PartialCaseKeyPath<Self>) -> Bool {
self[case: keyPath] != nil
}

public func `is`<Wrapped>(_ keyPath: CaseKeyPath<Self, Wrapped?>) -> Bool {
self[case: keyPath] != nil
}

/// Unwraps and yields a mutable associated value to a closure.
///
/// > Warning: If the enum's case does not match the given case key path, the mutation will not be
Expand Down
9 changes: 7 additions & 2 deletions Tests/CasePathsTests/CasePathsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ final class CasePathsTests: XCTestCase {
func testMatch() {
switch Foo.bar(.int(42)) {
case \.bar.int:
return
break
default:
XCTFail()
}

switch Foo.bar(.int(42)) {
case \.bar:
return
break
default:
XCTFail()
}
Expand All @@ -150,6 +150,10 @@ final class CasePathsTests: XCTestCase {
XCTAssertFalse(Foo.bar(.int(42)).is(\.baz.string))
XCTAssertFalse(Foo.bar(.int(42)).is(\.blob))
XCTAssertFalse(Foo.bar(.int(42)).is(\.fizzBuzz))
XCTAssertTrue(Foo.foo(nil).is(\.foo))
XCTAssertTrue(Foo.foo(nil).is(\.foo.none))
XCTAssertTrue(Foo.foo("").is(\.foo))
XCTAssertFalse(Foo.foo(nil).is(\.bar))
}

func testPartialCaseKeyPath() {
Expand All @@ -169,6 +173,7 @@ final class CasePathsTests: XCTestCase {
case baz(Baz)
case fizzBuzz
case blob(Blob)
case foo(String?)
}
@CasePathable @dynamicMemberLookup enum Bar: Equatable {
case int(Int)
Expand Down

0 comments on commit 76d7791

Please sign in to comment.