Upgrade to Pro — share decks privately, control downloads, hide ads and more …

iOSDC RejectCon 20180915: Factoryの自動生成によりテストを書き...

Takeshi Ihara
September 17, 2018

iOSDC RejectCon 20180915: Factoryの自動生成によりテストを書きやすくする

Takeshi Ihara

September 17, 2018
Tweet

More Decks by Takeshi Ihara

Other Decks in Programming

Transcript

  1. Takeshi Ihara • AbemaTV • Twitter: @nonchalant0303 • GitHub: Nonchalant

    • Climbing ! • Game " 2 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  2. Test struct User { let age: Int var isAdult: Bool

    { return age >= 20 } } class UserTests: XCTestCase { func test20ࡀҎ্ͳΒ੒ਓͰ͋Δ() { let user = User(age: 20) XCTAssertTrue(user.isAdult) } func test20ࡀະຬͳΒ੒ਓͰͳ͍() { let user = User(age: 19) XCTAssertFalse(user.isAdult) } } 3 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  3. New Property struct User { let name: String let age:

    Int var isAdult: Bool { return age >= 20 } } class UserTests: XCTestCase { func test20ࡀҎ্ͳΒ੒ਓͰ͋Δ() { let user = User(age: 20) XCTAssertTrue(user.isAdult) } func test20ࡀະຬͳΒ੒ਓͰͳ͍() { let user = User(age: 19) XCTAssertFalse(user.isAdult) } } 4 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  4. Compile Error struct User { let name: String let age:

    Int let birthday: Date var isAdult: Bool { return age >= 20 } } class UserTests: XCTestCase { func test20ࡀҎ্ͳΒ੒ਓͰ͋Δ() { let user = User(age: 20) // Missing argument for parameter 'name' in call XCTAssertTrue(user.isAdult) } func test20ࡀະຬͳΒ੒ਓͰͳ͍() { let user = User(age: 19) // Missing argument for parameter 'name' in call XCTAssertFalse(user.isAdult) } } 5 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  5. Fragile Test struct User { let name: String let age:

    Int var isAdult: Bool { return age >= 20 } } class UserTests: XCTestCase { func test20ࡀҎ্ͳΒ੒ਓͰ͋Δ() { let user = User(name: "Takeshi Ihara", age: 20) XCTAssertTrue(user.isAdult) } func test20ࡀະຬͳΒ੒ਓͰͳ͍() { let user = User(name: "Takeshi Ihara", age: 19) XCTAssertFalse(user.isAdult) } } 6 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  6. Factory Pattern ΦϒδΣΫτͷੜ੒ॲཧΛڞ௨Խ͢Δ struct UserFactory { static func provide(name: String

    = "", age: Int = 0) -> User { return User(name: name, age: age) } } class UserTests: XCTestCase { func test20ࡀҎ্ͳΒ੒ਓͰ͋Δ() { let user = UserFactory.provide(age: 20) XCTAssertTrue(user.isAdult) } func test20ࡀະຬͳΒ੒ਓͰͳ͍() { let user = UserFactory.provide(age: 19) XCTAssertFalse(user.isAdult) } } 7 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  7. FactoryProvider https:/ /github.com/Nonchalant/FactoryProvider • FactoryΛࣗಈੜ੒͢ΔϥΠϒϥϦ • Enum, Struct͕ੜ੒ର৅ • LensΛαϙʔτ

    • ymlͰઃఆ߲໨Λఆٛ • GeneratorΛؚΊΔͨΊʹCocoapodsͰͷΈΠϯετʔϧՄೳ 10 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  8. Factory (Struct) ܕύϥϝʔλʹStructΛࢦఆ͢Δ import FactoryProvider let user = Factory<User>.provide() //

    User(name: "", age: 0) 11 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  9. Factory (Enum) ܕύϥϝʔλʹEnumΛࢦఆ͢Δ import FactoryProvider let season = Factory<Season>.provide() //

    Season.spring enum Season { case spring case summer case automn case winter } 12 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  10. Generated Object ݻఆ஋Ͱੜ੒͞ΕΔ ! import FactoryProvider var user = Factory<User>.provide()

    user.name = "Takeshi Ihara" // Cannot assign to property: 'name' is a 'let' constant struct User { let name: String let age: Int var isAdult: Bool { return age >= 20 } } 13 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  11. Lens import FactoryProvider let user = Factory<User>.provide() // User(name: "",

    age: 0) let newUser = user |> User._age *~ 20 // User._ageΛLensͱݺͿ // User(name: "", age: 20) 15 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  12. Lens (Nested) import FactoryProvider struct User { let id: UserId

    let name: String let age: Int } struct UserId { let value: String } let user = Factory<User>.provide() |> User._id * UserId._value *~ "nonchalant0303" // User(id: UserId(value: "nonchalant0303"), name: "", age: 0) 16 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  13. Config ymlϑΝΠϧͰઃఆ͢Δ includes: # ੜ੒ର৅ͷStruct, EnumΛؚΜͩϑΝΠϧ΁ͷύε (ϑΝΠϧ୯ҐɺσΟϨΫτϦ୯Ґ) - Input/SubInput1 -

    Input/SubInput2/Source.swift excludes: # ੜ੒ର৅ͷStruct, Enumͷྫ֎ΛؚΜͩϑΝΠϧ΁ͷύε - Input/SubInput1/SubSubInput - Input/SubInput2/Source.swift testables: # ςετର৅ͷλʔήοτ - target1 - target2 output: output/Factories.generated.swift # ࣗಈੜ੒͞Εͨίʔυͷύε 17 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  14. How FactoryProvider Works 2ͭͷίʔυϕʔε͔Β੒Γཱͭ • FactoryProviderͷίʔυ (Fixed) • ࣗಈੜ੒͞ΕΔίʔυ (Generated

    in Build Phases) 19 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  15. Providable (FactoryProvider) Providableʹ४ڌͨ͠ܕ͕FactoryͰΦϒδΣΫτΛऔಘͰ͖Δ public protocol Providable { static func provide()

    -> Self } 20 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  16. Primitive Factory (FactoryProvider) PrimitiveͳܕͷFactory͕ఆٛ͞Ε͍ͯΔ (Int, Optional, String, ...) extension Int:

    Providable { public static func provide() -> Int { return 0 } } extension Optional: Providable where Wrapped: Providable { public static func provide() -> Optional { return .some(Wrapped.provide()) } } ... 21 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  17. Factory (FactoryProvider) struct Factory<T: Providable> { static func provide() ->

    T { return T.provide() } } 22 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  18. Specified Factory (Generated Code) import FactoryProvider extension User: Providable {

    static func provide() -> User { return User( id: Factory<UserId>.provide(), name: Factory<String>.provide(), age: Factory<Int>.provide() ) } } extension UserId: Providable { static func provide() -> UserId { return UserId( value: Factory<String>.provide() ) } } 23 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  19. Specified Factory (Generated Code) import FactoryProvider extension User: Providable {

    static func provide() -> User { return User( id: Factory<UserId>.provide(), name: Factory<String>.provide(), age: Factory<Int>.provide() ) } } extension UserId: Providable { static func provide() -> UserId { return UserId( value: Factory<String>.provide() ) } } 24 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  20. Lens (FactoryProvider) public struct Lens<Whole, Part> { private let getter:

    (Whole) -> Part private let setter: (Part, Whole) -> Whole public init(getter: @escaping (Whole) -> Part, setter: @escaping (Part, Whole) -> Whole) { self.getter = getter self.setter = setter } public func get(_ from: Whole) -> Part { return getter(from) } public func set(_ from: Part, _ to: Whole) -> Whole { return setter(from, to) } } 26 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  21. Custom Operator (FactoryProvider) infix operator *~: MultiplicationPrecedence infix operator |>:

    AdditionPrecedence public func * <A, B, C> (lhs: Lens<A, B>, rhs: Lens<B, C>) -> Lens<A, C> { return Lens<A, C>( getter: { a in rhs.get(lhs.get(a)) }, setter: { (c, a) in lhs.set(rhs.set(c, lhs.get(a)), a) } ) } public func *~ <A, B> (lhs: Lens<A, B>, rhs: B) -> (A) -> A { return { a in lhs.set(rhs, a) } } public func |> <A, B> (x: A, f: (A) -> B) -> B { return f(x) } public func |> <A, B, C> (f: @escaping (A) -> B, g: @escaping (B) -> C) -> (A) -> C { return { g(f($0)) } } 27 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  22. Compose (FactoryProvider) public func * <A, B, C> (lhs: Lens<A,

    B>, rhs: Lens<B, C>) -> Lens<A, C> { return Lens<A, C>( getter: { a in rhs.get(lhs.get(a)) }, setter: { (c, a) in lhs.set(rhs.set(c, lhs.get(a)), a) } ) } 28 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  23. Set (FactoryProvider) infix operator *~: MultiplicationPrecedence public func *~ <A,

    B> (lhs: Lens<A, B>, rhs: B) -> (A) -> A { return { a in lhs.set(rhs, a) } } 29 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  24. Modify (FactoryProvider) infix operator |>: AdditionPrecedence public func |> <A,

    B> (x: A, f: (A) -> B) -> B { return f(x) } public func |> <A, B, C> (f: @escaping (A) -> B, g: @escaping (B) -> C) -> (A) -> C { return { g(f($0)) } } 30 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  25. Lens (Generated Code) extension User { static var _name: Lens<User,

    String> { return Lens<User, String>( getter: { $0.name }, setter: { name, base in User(name: name, age: base.age) } ) } static var _age: Lens<User, Int> { return Lens<User, Int>( getter: { $0.age }, setter: { age, base in User(name: base.name, age: age) } ) } } 31 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  26. Decompose let user = Factory<User>.provide() |> User._name *~ "Takeshi Ihara"

    = (User._name *~ "Takeshi Ihara")(Factory<User>.provide()) = { user in Lens<User, String>( getter: { $0.name }, setter: { name, base in User(name: name, age: base.age) } ).set("Takeshi Ihara", user) }(Factory<User>.provide()) = { name, base in User(name: name, age: base.age) }("Takeshi Ihara", Factory<User>.provide()) = User(name: "Takeshi Ihara", age: 0) 32 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  27. Decompose let user = Factory<User>.provide() |> User._name *~ "Takeshi Ihara"

    = (User._name *~ "Takeshi Ihara")(Factory<User>.provide()) = { user in Lens<User, String>( getter: { $0.name }, setter: { name, base in User(name: name, age: base.age) } ).set("Takeshi Ihara", user) }(Factory<User>.provide()) = { name, base in User(name: name, age: base.age) }("Takeshi Ihara", Factory<User>.provide()) = User(name: "Takeshi Ihara", age: 0) 33 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  28. Decompose (|>) let user = Factory<User>.provide() |> User._name *~ "Takeshi

    Ihara" = (User._name *~ "Takeshi Ihara")(Factory<User>.provide()) = { user in Lens<User, String>( getter: { $0.name }, setter: { name, base in User(name: name, age: base.age) } ).set("Takeshi Ihara", user) }(Factory<User>.provide()) = { name, base in User(name: name, age: base.age) }("Takeshi Ihara", Factory<User>.provide()) = User(name: "Takeshi Ihara", age: 0) 34 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  29. Decompose (Lens, *~) let user = Factory<User>.provide() |> User._name *~

    "Takeshi Ihara" = (User._name *~ "Takeshi Ihara")(Factory<User>.provide()) = { user in Lens<User, String>( getter: { $0.name }, setter: { name, base in User(name: name, age: base.age) } ).set("Takeshi Ihara", user) }(Factory<User>.provide()) = { name, base in User(name: name, age: base.age) }("Takeshi Ihara", Factory<User>.provide()) = User(name: "Takeshi Ihara", age: 0) 35 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  30. Decompose (Closure) let user = Factory<User>.provide() |> User._name *~ "Takeshi

    Ihara" = (User._name *~ "Takeshi Ihara")(Factory<User>.provide()) = { user in Lens<User, String>( getter: { $0.name }, setter: { name, base in User(name: name, age: base.age) } ).set("Takeshi Ihara", user) }(Factory<User>.provide()) = { name, base in User(name: name, age: base.age) }("Takeshi Ihara", Factory<User>.provide()) = User(name: "Takeshi Ihara", age: 0) 36 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  31. Decompose let user = Factory<User>.provide() |> User._name *~ "Takeshi Ihara"

    = (User._name *~ "Takeshi Ihara")(Factory<User>.provide()) = { user in Lens<User, String>( getter: { $0.name }, setter: { name, base in User(name: name, age: base.age) } ).set("Takeshi Ihara", user) }(Factory<User>.provide()) = { name, base in User(name: name, age: base.age) }("Takeshi Ihara", Factory<User>.provide()) = User(name: "Takeshi Ihara", age: 0) 37 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  32. Result struct User { let name: String let age: Int

    var isAdult: Bool { return age >= 20 } } import FactoryProvider class UserTests: XCTestCase { func test20ࡀҎ্ͳΒ੒ਓͰ͋Δ() { let user = Factory<User>.provide() |> User._age *~ 20 XCTAssertTrue(user.isAdult) } func test20ࡀະຬͳΒ੒ਓͰͳ͍() { let user = Factory<User>.provide() |> User._age *~ 19 XCTAssertFalse(user.isAdult) } } 38 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  33. Future Work (Protocol) Not support protocol ! protocol A {}

    // Type 'A' does not conform to protocol 'Providable' let a = Factory<A>.provide() 40 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  34. Future Work (Protocol) Extension of protocol cannot have an inheritance

    clause // Extension of protocol 'A' cannot have an inheritance clause extension A: Providable { public static func provide() -> Self { fatalError() } } let a = Factory<A>.provide() 41 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  35. Future Work (Protocol) Need concrete type ! protocol A: Providable

    {} extension A { static func provide() -> Self { fatalError() } } // Using 'A' as a concrete type conforming to protocol 'Providable' is not supported let a = Factory<A>.provide() 42 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  36. Future Work (Protocol) No problem using concrete type ! protocol

    A: Providable {} extension A { static func provide() -> Self { fatalError() } } struct B: A {} let a = Factory<B>.provide() 43 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  37. Future Work (interface) User.provide()ͷΑ͏ʹ௚઀ੜ੒ϝιου͕ݺ΂ͯ͠·͏ extension User: Providable { static func

    provide() -> Self { return User( ... ) } } let user = User.provide() or Factory<User>.provide() 45 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  38. Future Work (interface) fileprivateͳGenericsΛ࣋ͭͳΒͦͷStructࣗ਎΋fileprivateʹ ͠ͳͯ͘͸ͳΒͳ͍ ! fileprivate Protocol { static

    func provide() -> Self } fileprivate extension User: Providable { ... } // Generic struct must be declared private or fileprivate because its generic parameter uses a fileprivate type struct Factory<T: Providable> { static func provide() -> T { return T.provide() } } 46 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  39. Future Work (interface) Factoryʹ͢΂ͯهड़͢Δ ! struct Factory<T> { static func

    provide() -> T { switch T.self { case is User.Type: return User( name: Factory<String>.provide(), age: Factory<Int>.provide() ) case is String.Type: return "" ... default: fatalError() } } } 47 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  40. Future Work (interface) Inner TypeΛαϙʔτ͕ࠔ೉ ! (ύʔε͕େม) struct User {

    let id: Id struct Id {} } struct Factory<T> { static func provide() -> T { switch T.self { case is User.Type: return User( id: Factory<Id>.provide() // Use of undeclared type 'Id' ) ... } } } 48 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1
  41. Conclusion • ʮߏ଄มԽʹڧ͍ςετʯ͕࣮ݱͰ͖ͨ • FactoryΛ༻ҙ͢Δख͕ؒͳ͘ͳͬͨͷͰςετʹूதͰ͖Δ • ςετର৅ͷϓϩύςΟ͕෼͔Γ΍͘͢ͳͬͨ • Factory<User>.provide() |>

    User._age *~ 19 • SourceKitten + StencilΛ࢖ͬͯύʔεͯࣗ͠ಈੜ੒͢Δͷ ָ͍͠ $ 50 Factoryͷࣗಈੜ੒ʹΑΓςετΛॻ͖΍͘͢͢Δ, iOSDC 2018 Reject Conference days1