-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
137 additions
and
12 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
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,14 @@ | ||
@_spi(Advanced) | ||
@propertyWrapper | ||
public final class Weak<T: AnyObject> { | ||
private weak var _wrappedValue: T? | ||
|
||
public var wrappedValue: T? { | ||
get { _wrappedValue } | ||
set { _wrappedValue = newValue } | ||
} | ||
|
||
public init(wrappedValue: T? = nil) { | ||
self._wrappedValue = wrappedValue | ||
} | ||
} |
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,56 @@ | ||
@_spi(Advanced) import SwiftUIIntrospect | ||
import XCTest | ||
|
||
final class WeakTests: XCTestCase { | ||
final class Foo {} | ||
|
||
var strongFoo: Foo? = Foo() | ||
|
||
func testInit_nil() { | ||
@Weak var weakFoo: Foo? | ||
XCTAssertNil(weakFoo) | ||
} | ||
|
||
func testInit_nonNil() { | ||
@Weak var weakFoo: Foo? = strongFoo | ||
XCTAssertIdentical(weakFoo, strongFoo) | ||
} | ||
|
||
func testAssignment_nilToNil() { | ||
@Weak var weakFoo: Foo? | ||
weakFoo = nil | ||
XCTAssertNil(weakFoo) | ||
} | ||
|
||
func testAssignment_nilToNonNil() { | ||
@Weak var weakFoo: Foo? | ||
let otherFoo = Foo() | ||
weakFoo = otherFoo | ||
XCTAssertIdentical(weakFoo, otherFoo) | ||
} | ||
|
||
func testAssignment_nonNilToNil() { | ||
@Weak var weakFoo: Foo? = strongFoo | ||
weakFoo = nil | ||
XCTAssertNil(weakFoo) | ||
} | ||
|
||
func testAssignment_nonNilToNonNil() { | ||
@Weak var weakFoo: Foo? = strongFoo | ||
let otherFoo = Foo() | ||
weakFoo = otherFoo | ||
XCTAssertIdentical(weakFoo, otherFoo) | ||
} | ||
|
||
func testIndirectAssignment_nonNilToNil() { | ||
@Weak var weakFoo: Foo? = strongFoo | ||
strongFoo = nil | ||
XCTAssertNil(weakFoo) | ||
} | ||
|
||
func testIndirectAssignment_nonNilToNonNil() { | ||
@Weak var weakFoo: Foo? = strongFoo | ||
strongFoo = Foo() | ||
XCTAssertNil(weakFoo) | ||
} | ||
} |