Skip to content

Instantly share code, notes, and snippets.

@takasek
Last active November 11, 2017 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takasek/be6be0822c1ba6557ed1884858af304f to your computer and use it in GitHub Desktop.
Save takasek/be6be0822c1ba6557ed1884858af304f to your computer and use it in GitHub Desktop.
privateなAPIをテストする方法を考えてみた ref: http://qiita.com/takasek/items/d1f11394b6df2f3f3cc0
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) }
}
/// 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)
}
}
let tester = FugaTester(args: (x: 1, y: 2))
tester.x // 1
tester.y // 2
tester.doSomething(with: "ふが") // "ふが 1 2"
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)
}
}
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