-
Notifications
You must be signed in to change notification settings - Fork 1
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
7 changed files
with
117 additions
and
11 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
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,66 @@ | ||
// | ||
/* | ||
* Created by 游宗諭 in 2021/2/23 | ||
* | ||
* Using Swift 5.0 | ||
* | ||
* Running on macOS 11.2 | ||
*/ | ||
|
||
|
||
import XCTest | ||
@propertyWrapper struct Wrapper { | ||
var wrappedValue: Int | ||
var anotherValue = 0 | ||
} | ||
class ProperyObserverTests: XCTestCase { | ||
@Wrapper var i = 0 { | ||
willSet { | ||
captured.append(.willSet(newValue)) | ||
} | ||
didSet { | ||
captured.append(.didSet(i)) | ||
} | ||
} | ||
var captured = [Setting]() | ||
func test() { | ||
XCTAssertTrue(captured.isEmpty) | ||
_i.anotherValue = 1 | ||
XCTAssertEqual(captured, []) | ||
_i.wrappedValue = 1 | ||
XCTAssertEqual(captured, []) | ||
i = 1 | ||
XCTAssertEqual(captured, [.willSet(1), .didSet(1)]) | ||
} | ||
func testInStruct() { | ||
var owner = Owner() | ||
owner._addAnother() | ||
XCTAssertEqual(owner.captured, []) | ||
owner._addWrapped() | ||
XCTAssertEqual(owner.captured, []) | ||
owner.i = 1 | ||
XCTAssertEqual(owner.captured, [.willSet(1), .didSet(1)]) | ||
} | ||
} | ||
|
||
enum Setting:Equatable { | ||
case didSet(Int), willSet(Int) | ||
} | ||
struct Owner { | ||
@Wrapper var i = 0 { | ||
willSet { | ||
captured.append(.willSet(newValue)) | ||
} | ||
didSet { | ||
captured.append(.didSet(i)) | ||
} | ||
} | ||
var captured = [Setting]() | ||
mutating func _addAnother() { | ||
_i.anotherValue += 1 | ||
} | ||
mutating func _addWrapped() { | ||
_i.wrappedValue += 1 | ||
} | ||
|
||
} |
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
37 changes: 37 additions & 0 deletions
37
Tests/PropertyWrapperTests/WrappedValueGetOnlyOwnerWillSetTests.swift
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,37 @@ | ||
// | ||
/* | ||
* Created by 游宗諭 in 2021/2/23 | ||
* | ||
* Using Swift 5.0 | ||
* | ||
* Running on macOS 11.2 | ||
*/ | ||
|
||
|
||
import XCTest | ||
@propertyWrapper struct GetOnly { | ||
var wrappedValue: Int {0} | ||
} | ||
class WrappedValueGetOnlyOwnerWillSetTests: XCTestCase { | ||
@GetOnly var i | ||
@GetOnly var i2 { | ||
willSet {} | ||
} | ||
@GetOnly var i3 { | ||
didSet {} | ||
} | ||
@GetOnly var i4 { | ||
willSet {} | ||
didSet {} | ||
} | ||
|
||
/** | ||
//uncomment to see | ||
func test() { | ||
i = 1 | ||
i2 = 2 | ||
i3 = 3 | ||
i4 = 4 | ||
} | ||
*/ | ||
} |