2017-06-17 19 views
0

我想爲以下以24小時格式(00:00至24:00)轉換時間的Scala對象編寫我的第一組ScalaTest測試)分爲時間(中午八點前,中午八點前八點,中午四點前九點)。我正在尋找如何編寫使用模式匹配和Map集合的hourFmt,minutesFmt和Fmt的測試的起點,以便爲小時和分鐘返回整數值的適當單詞。如何爲使用Map集合的模式匹配方法編寫ScalaTest

我已閱讀了ScalaTest用戶指南文檔的大部分內容,但未能找到有關模式匹配方法或地圖集合的任何文檔。我只是尋找一個起點或社區知道的例子,因爲以下將是我第一次編寫單元測試。

我附上了我的代碼,以闡明我已經提到的三種方法。

object TimeAsWords extends App { 
    def parseTime(hhmm: String): (Int, Int) = { 
    """^([01]\d|2[0-4]):([0-5]\d)$""".r.findAllIn(hhmm).matchData foreach { 
     md => return (md.group(1).toInt, md.group(2).toInt) 
    } 
    throw new IllegalArgumentException("Input string doesn't match required format: 00:00 - 24:00") 
    } 

    def formatTime(hhmm: (Int, Int)): String = { 
    val englishNum = Map(
     1 -> "one", 2 -> "two", 3 -> "three", 4 -> "four", 5 -> "five", 
     6 -> "six", 7 -> "seven", 8 -> "eight", 9 -> "nine", 10 -> "ten", 
     11 -> "eleven", 12 -> "twelve", 13 -> "thirteen", 14 -> "fourteen", 
     16 -> "sixteen", 17 -> "seventeen", 18 -> "eighteen", 19 -> "nineteen", 
     20 -> "twenty", 21 -> "twenty-one", 22 -> "twenty-two", 
     23 -> "twenty-three", 24 -> "twenty-four", 25 -> "twenty-five", 
     26 -> "twenty-six", 27 -> "twenty-seven", 28 -> "twenty-eight", 
     29 -> "twenty-nine" 
    ) 

    def hourFmt(h: Int): String = h match { 
     case 0 | 24  => "midnight" 
     case 12   => "noon" 
     case _ if h < 12 => englishNum(h) + " o'clock before midday" 
     case _   => englishNum(h - 12) + " o'clock after midday" 
    } 

    def minuteFmt(m: Int): String = "%s %s".format(
     englishNum(m), 
     if (m == 1) "minute" else "minutes" 
    ) 

    def fmt(m: Int): (Int => String) = m match { 
     case 0   => hourFmt 
     case 15   => "quarter past " + hourFmt(_) 
     case 30   => "half past " + hourFmt(_) 
     case 45   => h => "quarter to " + hourFmt(h + 1) 
     case _ if m < 30 => h => "%s past %s".format(minuteFmt(m), hourFmt(h)) 
     case _   => h => "%s to %s".format(minuteFmt(60 - m), hourFmt(h + 1)) 
    } 

    val (hh, mm) = hhmm 
    fmt(mm)(hh).capitalize 
    } 

    try { 
    println(formatTime(parseTime(args(0)))) 
    } catch { 
    case e: IllegalArgumentException => System.err.println(e.getMessage) 
     System.exit(1) 
    } 
} 

回答

0

我張貼的答案我自己的問題,但想找到編寫單元測試對於使用模式匹配這對其他兩個功能hourFmt和minutesFmt依賴的函數FMT一種更好的方式。

當前ScalaTest,我寫道:

package time 

import TimeAsWords._ 
import org.scalatest.FunSuite 

/** 
    * Created by PeterW on 6/18/2017. 
    */ 
class TimeAsWordsTest extends FunSuite { 
    test("testParseTime - 00:00") { 
    assert(parseTime("00:00") == (0,0)) 
    } 
    test("testParseTime - IllegalArgumentException") { 
    val thrown = intercept[Exception] { 
     parseTime("25:00") 
    } 
    assert(thrown.getMessage == "Input string doesn't match required format: 00:00 - 24:00") 
    } 
    test("testFormatTime - Midnight") { 
    assert(formatTime((0,0)) == "Midnight") 
    } 
    test("testFormatTime - Noon") { 
    assert(formatTime((12,0)) == "Noon") 
    } 
    test("testFormatTime - o'clock before midday") { 
    assert(formatTime((8,0)) == "Eight o'clock before midday") 
    } 
    test("testFormatTime - o'clock after midday") { 
    assert(formatTime((13,0)) == "One o'clock after midday") 
    } 
    test("testFormatTime - Quater past") { 
    assert(formatTime((13,15)) == "Quarter past one after midday") 
    } 
    test("testFormatTime - Half past") { 
    assert(formatTime((13,30)) == "Half past one after midday") 
    } 
    test("testFormatTime - Quarter to") { 
    assert(formatTime((13,45)) == "Quarter to two after midday") 
    } 
    test("testFormatTime - Minutes past hour") { 
    assert(formatTime((13,25)) == "Twenty-five minutes past one after midday") 
    } 
    test("testFormatTime - Minutes to hour") { 
    assert(formatTime((13,35)) == "Twenty-five minutes to two after midday") 
    } 
    test("testFormatTime - Minute singular") { 
    assert(formatTime((13,1)) == "One minute past one after midday") 
    } 
    test("testFormatTime - Minutes plural") { 
    assert(formatTime((13,2)) == "Two minutes past one after midday") 
    } 
}