Last active
November 11, 2017 11:34
-
-
Save takasek/be6be0822c1ba6557ed1884858af304f to your computer and use it in GitHub Desktop.
privateなAPIをテストする方法を考えてみた ref: http://qiita.com/takasek/items/d1f11394b6df2f3f3cc0
This file contains hidden or 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
public protocol PrivatePublicatable { | |
associatedtype Publicater | |
var `private`: Publicater { get } | |
} | |
public final class Private<Base> { | |
public let base: Base | |
public init(_ base: Base) { self.base = base } | |
} | |
public extension PrivatePublicatable { | |
var `private`: Private<Self> { return Private(self) } | |
} |
This file contains hidden or 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
/// Fugaをprivateに保つために作るTest用ラッパー。Fugaと同じファイル内に書く | |
final class FugaTester { | |
private let base: Fuga | |
init(args: (x: Int, y: Int)) { | |
base = Fuga(x: args.x, y: args.y) | |
} | |
var x: Int { return base.x } | |
var y: Int { return base.y } | |
func doSomething(with string: String) -> String { | |
return base.doSomething(with: string) | |
} | |
} |
This file contains hidden or 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
let tester = FugaTester(args: (x: 1, y: 2)) | |
tester.x // 1 | |
tester.y // 2 | |
tester.doSomething(with: "ふが") // "ふが 1 2" |
This file contains hidden or 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
extension Hoge: PrivatePublicatable {} | |
public extension Private where Base == Hoge { | |
var x: Int { return base.x } | |
func doSomething(with string: String) -> String { | |
return base.doSomething(with: string) | |
} | |
} |
This file contains hidden or 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
let hoge = Hoge(x: 1) | |
hoge.private.x // 1 | |
hoge.private.doSomething(with: "ほげ") // ほげ 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment