Skip to content

Commit

Permalink
Unowned unsafe (#4)
Browse files Browse the repository at this point in the history
creat xcodeproj to pass coverage layout to codecov
  • Loading branch information
ytyubox authored Dec 1, 2019
1 parent d9ea2a6 commit 2c7fe37
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 5 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
38 changes: 38 additions & 0 deletions Sources/Unowned/UnowendUnsafe.swift
Original file line number Diff line number Diff line change
@@ -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<UnowndObject: AnyObject>: 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<Iterator.Element>] {
map(UnownedUnsafe.init)
}

}
2 changes: 1 addition & 1 deletion Sources/Unowned/Unowned.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public protocol UnownedProtocol {
public struct Unowned<UnowndObject: AnyObject>: 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 {
Expand Down
2 changes: 1 addition & 1 deletion Tests/WeakTests/UnownedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

import XCTest
@testable import Weak
import Weak

final class UnownedTests:XCTestCase {
class PrintTarget{ var str:String = ""}
Expand Down
87 changes: 87 additions & 0 deletions Tests/WeakTests/UnownedUnsafeTests.swift
Original file line number Diff line number Diff line change
@@ -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<SomeClass>
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<SomeClass>? = UnownedUnsafe(object)
let Unownedunsafe2:UnownedUnsafe<SomeClass>? = UnownedUnsafe(object)
let weak:Weak<SomeClass>? = 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)
}
}
2 changes: 1 addition & 1 deletion Tests/WeakTests/WeakTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import XCTest
@testable import Weak
import Weak

final class WeakTests: XCTestCase {
class SomeClass:NSObject { }
Expand Down
1 change: 1 addition & 0 deletions Tests/WeakTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import XCTest
public func allTests() -> [XCTestCaseEntry] {
return [
testCase(WeakTests.allTests),
testCase(UnownedTests.allTests)
]
}
#endif

0 comments on commit 2c7fe37

Please sign in to comment.