From 2c7fe37aafe616942001c4d102b37ddf0cbf6a4a Mon Sep 17 00:00:00 2001 From: Tsungyu Yu <42088295+ytyubox@users.noreply.github.com> Date: Sun, 1 Dec 2019 23:53:39 +0800 Subject: [PATCH] Unowned unsafe (#4) creat xcodeproj to pass coverage layout to codecov --- .travis.yml | 6 +- Sources/Unowned/UnowendUnsafe.swift | 38 +++++++++++ Sources/Unowned/Unowned.swift | 2 +- Tests/WeakTests/UnownedTests.swift | 2 +- Tests/WeakTests/UnownedUnsafeTests.swift | 87 ++++++++++++++++++++++++ Tests/WeakTests/WeakTests.swift | 2 +- Tests/WeakTests/XCTestManifests.swift | 1 + 7 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 Sources/Unowned/UnowendUnsafe.swift create mode 100644 Tests/WeakTests/UnownedUnsafeTests.swift diff --git a/.travis.yml b/.travis.yml index 795dabb..1a8c185 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,6 +8,8 @@ install: - (swift package update) script: - (swift build) - - (swift test) + - (swift test --enable-code-coverage) + - swift package generate-xcodeproj --enable-code-coverage + - xcodebuild -scheme Weak-Package test after_success: - - curl -s https://codecov.io/bash | bash + - bash <(curl -s https://codecov.io/bash) diff --git a/Sources/Unowned/UnowendUnsafe.swift b/Sources/Unowned/UnowendUnsafe.swift new file mode 100644 index 0000000..0ccd76d --- /dev/null +++ b/Sources/Unowned/UnowendUnsafe.swift @@ -0,0 +1,38 @@ +// +// UnownedUnsafe.swift +// +// +// Created by 游宗諭 on 2019/12/1. +// + +import Foundation + +/// A wrapper around an `Object` with a `unowned(unsafe)` binding. +public struct UnownedUnsafe: UnownedProtocol, CustomStringConvertible { + + /// The object of `self`. + public unowned(unsafe) var object: UnowndObject! + + /// A textual representation of this instance. + public var description: String { + "UnownedUnsafe(" + String(reflecting: object!) + ")" + } + + /// Creates an instance for an object. + public init(_ object: UnowndObject?) { + self.object = object + } + +} +extension UnownedUnsafe: Hashable, Equatable where UnowndObject: Hashable { + +} + +extension Sequence where Iterator.Element: AnyObject { + + /// return An array of unowned references to the elements in `self`. + public func unownedUnsafe() -> [UnownedUnsafe] { + map(UnownedUnsafe.init) + } + +} diff --git a/Sources/Unowned/Unowned.swift b/Sources/Unowned/Unowned.swift index f14b4b0..f4a3b18 100644 --- a/Sources/Unowned/Unowned.swift +++ b/Sources/Unowned/Unowned.swift @@ -20,7 +20,7 @@ public protocol UnownedProtocol { public struct Unowned: UnownedProtocol, CustomStringConvertible { /// The object of `self`. - public unowned(unsafe) var object: UnowndObject! + public unowned(safe) var object: UnowndObject! /// A textual representation of this instance. public var description: String { diff --git a/Tests/WeakTests/UnownedTests.swift b/Tests/WeakTests/UnownedTests.swift index c3aa2a2..6f9998a 100644 --- a/Tests/WeakTests/UnownedTests.swift +++ b/Tests/WeakTests/UnownedTests.swift @@ -6,7 +6,7 @@ // import XCTest -@testable import Weak +import Weak final class UnownedTests:XCTestCase { class PrintTarget{ var str:String = ""} diff --git a/Tests/WeakTests/UnownedUnsafeTests.swift b/Tests/WeakTests/UnownedUnsafeTests.swift new file mode 100644 index 0000000..d205d49 --- /dev/null +++ b/Tests/WeakTests/UnownedUnsafeTests.swift @@ -0,0 +1,87 @@ +// +// UnownedUnsafeTests.swift +// +// +// Created by 游宗諭 on 2019/12/1. +// + +import XCTest +import Weak + +final class UnownedUnsafeTests:XCTestCase { + class PrintTarget{ var str:String = ""} + class SomeClass:NSObject { + var printTarget:PrintTarget + var text:String + init(printTarget:PrintTarget, text:String) { + self.printTarget = printTarget + self.text = text + } + deinit { + print(text, terminator: "", to: &printTarget.str) + } + } + + func testUnownedunsafe() { + let testString = "deinited" + let UnownedunsafeRef: UnownedUnsafe + let printTarget = PrintTarget() + do { + let instance = SomeClass(printTarget: printTarget, text: testString) + UnownedunsafeRef = UnownedUnsafe(instance) + XCTAssertNotNil(UnownedunsafeRef.object) + XCTAssertEqual("UnownedUnsafe(" + String(reflecting: instance) + ")", UnownedunsafeRef.description) + } + XCTAssertEqual(printTarget.str, testString) + } + func testUnownedunsafeForSequence() { + let expect = (0...9).map{_ in SomeClass(printTarget: UnownedUnsafeTests.PrintTarget(), text: "")} + let UnownedunsafeArray = expect.unownedUnsafe() + XCTAssertEqual(expect, UnownedunsafeArray.map{$0.object}) + XCTAssertEqual(expect, UnownedunsafeArray.objects) + } + + func testUnownEqual() { + let object = SomeClass(printTarget: UnownedUnsafeTests.PrintTarget(), text: "") + let Unownedunsafe1 = UnownedUnsafe(object) + let Unownedunsafe2 = UnownedUnsafe(object) + let weak = Weak(object) + + XCTAssertTrue(Unownedunsafe1 === Unownedunsafe2) + XCTAssertTrue(Unownedunsafe1 === object) + XCTAssertTrue(object === Unownedunsafe1) + XCTAssertTrue(Unownedunsafe1 === weak) + XCTAssertTrue(Unownedunsafe1 == Unownedunsafe2) + XCTAssertTrue(Unownedunsafe1 == object) + XCTAssertTrue(object == Unownedunsafe1) + XCTAssertFalse(Unownedunsafe1 !== Unownedunsafe2) + XCTAssertFalse(Unownedunsafe1 !== object) + XCTAssertFalse(object !== Unownedunsafe1) + XCTAssertFalse(Unownedunsafe1 !== weak) + + + + } + func testOptionalUnownedunsafeEqual() { + let object:SomeClass? = SomeClass(printTarget: UnownedUnsafeTests.PrintTarget(), text: "") + let Unownedunsafe1:UnownedUnsafe? = UnownedUnsafe(object) + let Unownedunsafe2:UnownedUnsafe? = UnownedUnsafe(object) + let weak:Weak? = Weak(object) + XCTAssertTrue(Unownedunsafe1 === Unownedunsafe2) + XCTAssertTrue(Unownedunsafe1 === object) + XCTAssertTrue(object === Unownedunsafe1) + XCTAssertTrue(Unownedunsafe1 === weak) + XCTAssertTrue(Unownedunsafe1 == Unownedunsafe2) + XCTAssertTrue(Unownedunsafe1 == object) + XCTAssertTrue(object == Unownedunsafe1) + XCTAssertTrue(Unownedunsafe1 == weak) + XCTAssertFalse(Unownedunsafe1 !== Unownedunsafe2) + XCTAssertFalse(Unownedunsafe1 !== object) + XCTAssertFalse(object !== Unownedunsafe1) + XCTAssertFalse(Unownedunsafe1 !== weak) + XCTAssertFalse(Unownedunsafe1 != Unownedunsafe2) + XCTAssertFalse(Unownedunsafe1 != object) + XCTAssertFalse(object != Unownedunsafe1) + XCTAssertFalse(Unownedunsafe1 != weak) + } +} diff --git a/Tests/WeakTests/WeakTests.swift b/Tests/WeakTests/WeakTests.swift index bdbb759..7605fa9 100644 --- a/Tests/WeakTests/WeakTests.swift +++ b/Tests/WeakTests/WeakTests.swift @@ -1,5 +1,5 @@ import XCTest -@testable import Weak +import Weak final class WeakTests: XCTestCase { class SomeClass:NSObject { } diff --git a/Tests/WeakTests/XCTestManifests.swift b/Tests/WeakTests/XCTestManifests.swift index 79d20a4..d6ad522 100644 --- a/Tests/WeakTests/XCTestManifests.swift +++ b/Tests/WeakTests/XCTestManifests.swift @@ -4,6 +4,7 @@ import XCTest public func allTests() -> [XCTestCaseEntry] { return [ testCase(WeakTests.allTests), + testCase(UnownedTests.allTests) ] } #endif