From 5e5648a5cb9b508b575be62a52d110d3d64d6cbb Mon Sep 17 00:00:00 2001 From: Tsungyu Yu Date: Tue, 23 Feb 2021 23:32:04 +0800 Subject: [PATCH] post talk update --- .../xcschemes/PropertyWrapper.xcscheme | 3 +- .../LoggingExcludedTests.swift | 9 +++ Tests/PropertyWrapperTests/NestedTests.swift | 9 --- .../PropertyObserverTests.swift | 66 +++++++++++++++++++ .../ThreadSafe/Barrier.swift.deadLock.failure | 3 +- .../ThreadSafe/Lock.swift | 1 + ...WrappedValueGetOnlyOwnerWillSetTests.swift | 37 +++++++++++ 7 files changed, 117 insertions(+), 11 deletions(-) create mode 100644 Tests/PropertyWrapperTests/PropertyObserverTests.swift create mode 100644 Tests/PropertyWrapperTests/WrappedValueGetOnlyOwnerWillSetTests.swift diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/PropertyWrapper.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/PropertyWrapper.xcscheme index d8b826b..f2423da 100644 --- a/.swiftpm/xcode/xcshareddata/xcschemes/PropertyWrapper.xcscheme +++ b/.swiftpm/xcode/xcshareddata/xcschemes/PropertyWrapper.xcscheme @@ -68,7 +68,8 @@ buildConfiguration = "Debug" selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" - shouldUseLaunchSchemeArgsEnv = "YES"> + shouldUseLaunchSchemeArgsEnv = "YES" + codeCoverageEnabled = "YES"> diff --git a/Tests/PropertyWrapperTests/LoggingExcludedTests.swift b/Tests/PropertyWrapperTests/LoggingExcludedTests.swift index 6038b9f..8a1258e 100644 --- a/Tests/PropertyWrapperTests/LoggingExcludedTests.swift +++ b/Tests/PropertyWrapperTests/LoggingExcludedTests.swift @@ -11,6 +11,15 @@ import XCTest class Object { @LoggingExcluded var i = 0 + /** + // uncomment to see compile failure + @LoggingExcluded + weak var i2: NSObject? + @LoggingExcluded + unowned var i3: NSObject? + @LoggingExcluded + lazy var i4: NSObject? + */ } class LogExcludeTests: XCTestCase { diff --git a/Tests/PropertyWrapperTests/NestedTests.swift b/Tests/PropertyWrapperTests/NestedTests.swift index 82f0941..e7a5c6e 100644 --- a/Tests/PropertyWrapperTests/NestedTests.swift +++ b/Tests/PropertyWrapperTests/NestedTests.swift @@ -32,15 +32,6 @@ final class NestedTests: XCTestCase { import SwiftUI -struct Nested { - @State - @LoggingExcluded - var nest1 = 0 - - @LoggingExcluded - @State - var nest2 = 0 -} struct OrderDifferent { @LoggingExcluding diff --git a/Tests/PropertyWrapperTests/PropertyObserverTests.swift b/Tests/PropertyWrapperTests/PropertyObserverTests.swift new file mode 100644 index 0000000..f9bed44 --- /dev/null +++ b/Tests/PropertyWrapperTests/PropertyObserverTests.swift @@ -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 + } + +} diff --git a/Tests/PropertyWrapperTests/ThreadSafe/Barrier.swift.deadLock.failure b/Tests/PropertyWrapperTests/ThreadSafe/Barrier.swift.deadLock.failure index 8d61876..0c6ad45 100644 --- a/Tests/PropertyWrapperTests/ThreadSafe/Barrier.swift.deadLock.failure +++ b/Tests/PropertyWrapperTests/ThreadSafe/Barrier.swift.deadLock.failure @@ -37,7 +37,8 @@ class BarrierTests: XCTestCase { @Atomic var lockSingleValue = 0 func testLockValue() { randomQueueAsync { _ in - self.lockSingleValue += 1 + let value = self.lockSingleValue + self.lockSingleValue = 1 + value } XCTAssertEqual(lockSingleValue, expect) } diff --git a/Tests/PropertyWrapperTests/ThreadSafe/Lock.swift b/Tests/PropertyWrapperTests/ThreadSafe/Lock.swift index f63de7c..6c4fb75 100644 --- a/Tests/PropertyWrapperTests/ThreadSafe/Lock.swift +++ b/Tests/PropertyWrapperTests/ThreadSafe/Lock.swift @@ -86,6 +86,7 @@ class ThreadSaveTests: XCTestCase { } wait(for: [await], timeout: 1) XCTAssertEqual(lockDicWithArray.count, total) + XCTAssertTrue(lockDicWithArray.values.allSatisfy{$0.count == total}) } // MARK: - help diff --git a/Tests/PropertyWrapperTests/WrappedValueGetOnlyOwnerWillSetTests.swift b/Tests/PropertyWrapperTests/WrappedValueGetOnlyOwnerWillSetTests.swift new file mode 100644 index 0000000..10ff0c6 --- /dev/null +++ b/Tests/PropertyWrapperTests/WrappedValueGetOnlyOwnerWillSetTests.swift @@ -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 + } + */ +}