0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SwiftでTDD写経 第五章 原則をあえて破るとき

Last updated at Posted at 2023-03-12

注意:TDD勉強会会用の資料です。
殴り書きしてあとで清書していくので投稿段階では綺麗ではありません。

原則をあえて破るときとは?
ドルの計算はできた。しかし、フランの計算も行わなくてはならない。
熟練プログラマなら、普通はどちらも計算できる綺麗なコードを書くが、まぁそれは一旦置いといてDollarクラスをそのままコピペしてFracクラスを作ってしまおうぜという話。
依存と重複じゃねぇか!!と怒るのはやめよう。この罪は今後贖うからいいんだぜ!!!って話。
とりまテスト通すためにコピペ許せや!って話ですね。

playground.swift

var money = MoneyTest()

money.TestMultiplication()
money.TestEquality()
class MoneyTest{
    init(){}
    
    func TestMultiplication(){
       var five = Dollar(5)
       assert(Dollar(10) == five.times(2))
       assert(Dollar(15) == five.times(3))
    }

    func TestEquality(){
        assert(Dollar(5) == Dollar(5))
        assert(Dollar(6) == Dollar(6))
    }

  func TestFrancMultiplication(){
       var five = Franc(5)
       assert(Franc(10) == five.times(2))
       assert(Franc(15) == five.times(3))
    }
}

class Dollar :Equatable {
    var amount:Int

    init(_ amount:Int){
        self.amount = amount
    }
    
    func times(_ multiplire:Int)  -> Dollar{
        return Dollar(self.amount * multiplire)
    }

    static func == (lhs: Dollar, rhs: Dollar) -> Bool {
       return lhs.amount == rhs.amount
    }
    
}

class Franc :Equatable {
    var amount:Int

    init(_ amount:Int){
        self.amount = amount
    }
    
    func times(_ multiplire:Int)  -> Franc{
        return Franc(self.amount * multiplire)
    }

    static func == (lhs: Franc, rhs: Franc) -> Bool {
       return lhs.amount == rhs.amount
    }
    
}



0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?