-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent case navigation binding from writing to other cases (#149)
* wip * wip * wip
- Loading branch information
1 parent
2ec6c3a
commit 5f77d0a
Showing
2 changed files
with
67 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#if swift(>=5.9) && canImport(SwiftUI) | ||
import CustomDump | ||
import SwiftUI | ||
import SwiftUINavigation | ||
import XCTest | ||
|
||
final class BindingTests: XCTestCase { | ||
@CasePathable | ||
@dynamicMemberLookup | ||
enum Status: Equatable { | ||
case inStock(quantity: Int) | ||
case outOfStock(isOnBackOrder: Bool) | ||
} | ||
|
||
func testCaseLookup() throws { | ||
@Binding var status: Status | ||
_status = Binding(initialValue: .inStock(quantity: 1)) | ||
|
||
let inStock = try XCTUnwrap($status.inStock) | ||
inStock.wrappedValue += 1 | ||
|
||
XCTAssertEqual(status, .inStock(quantity: 2)) | ||
} | ||
|
||
func testCaseCannotReplaceOtherCase() throws { | ||
@Binding var status: Status | ||
_status = Binding(initialValue: .inStock(quantity: 1)) | ||
|
||
let inStock = try XCTUnwrap($status.inStock) | ||
|
||
status = .outOfStock(isOnBackOrder: true) | ||
|
||
inStock.wrappedValue = 42 | ||
XCTAssertEqual(status, .outOfStock(isOnBackOrder: true)) | ||
} | ||
|
||
func testDestinationCannotReplaceOtherDestination() throws { | ||
@Binding var destination: Status? | ||
_destination = Binding(initialValue: .inStock(quantity: 1)) | ||
|
||
let inStock = try XCTUnwrap($destination.inStock) | ||
|
||
destination = .outOfStock(isOnBackOrder: true) | ||
|
||
inStock.wrappedValue = 42 | ||
XCTAssertEqual(destination, .outOfStock(isOnBackOrder: true)) | ||
} | ||
} | ||
|
||
private extension Binding { | ||
init(initialValue: Value) { | ||
var value = initialValue | ||
self.init( | ||
get: { value }, | ||
set: { value = $0 } | ||
) | ||
} | ||
} | ||
#endif // canImport(SwiftUI) |