Slide 10
Slide 10 text
時間の整形処理 (AFTER)
static func remainingTimeFormat(_ seconds: Int) -> String {
let absSecond: Int = abs(seconds)
let hours = String(format: "%02d",(from: absSecond / 3600))
let minutes = String(format: "%02d",(from: (absSecond % 3600) / 60))
let seconds = String(format: "%02d",(from: absSecond % 60))
return "\(negativeSymbol(of: seconds))\(hours):\(minutes):\(seconds)”
}
実装(AFTER)
let testCases: [(actual: Int, expected: String)] = [
( -60, "-00:01:00"), ( -1, "-00:00:01"),
( 0, "00:00:00"), ( 1, "00:00:01"),
( 59, "00:00:59"), ( 60, "00:01:00"),
(3599, "00:59:59"), (3600, "01:00:00"), …
]
testCases.each {
XCTAssertEqual(DateTimeUtil.remainingTimeFormat($0.0), $0.1)
}
テストコード
期待通りに整形されることの確認が容易に✌
⼊出⼒が明確で⼩さな処理にテストを書いていく✅