- Scala Programming Projects
- Mikael Valot Nicolas Jorand
- 201字
- 2021-07-23 16:25:22
Writing the unit test using the column selection mode
First, copy sp500_2017.tsv from https://github.com/PacktPublishing/Scala-Programming-Projects/blob/master/Chapter02/retirement-calculator/src/main/resources/sp500.tsv to src/test/resources. Then, create a new unit test called EquityDataSpec in the retcalc package. If you retype this example, try the column selection mode (Alt + Shift + Insert). Copy the content of the .tsv file in the test, then select the first column with Shift + Down 13 times, and then type EquityData(". Finally, edit the rest of the lines using the arrow keys, delete, comma, and so on:
package retcalc
import org.scalatest.{Matchers, WordSpec}
class EquityDataSpec extends WordSpec with Matchers {
"EquityData.fromResource" should {
"load market data from a tsv file" in {
val data = EquityData.fromResource("sp500_2017.tsv")
data should ===(Vector(
EquityData("2016.09", 2157.69, 45.03),
EquityData("2016.10", 2143.02, 45.25),
EquityData("2016.11", 2164.99, 45.48),
EquityData("2016.12", 2246.63, 45.7),
EquityData("2017.01", 2275.12, 45.93),
EquityData("2017.02", 2329.91, 46.15),
EquityData("2017.03", 2366.82, 46.38),
EquityData("2017.04", 2359.31, 46.66),
EquityData("2017.05", 2395.35, 46.94),
EquityData("2017.06", 2433.99, 47.22),
EquityData("2017.07", 2454.10, 47.54),
EquityData("2017.08", 2456.22, 47.85),
EquityData("2017.09", 2492.84, 48.17)
))
}
}
"EquityData.monthlyDividend" should {
"return a monthly dividend" in {
EquityData("2016.09", 2157.69, 45.03).monthlyDividend should ===
(45.03 / 12)
}
}
}
The first lines of sp500_2017.tsv look like this:
month SP500 dividend
2016.09 2157.69 45.03
2016.10 2143.02 45.25
2016.11 2164.99 45.48
推薦閱讀
- 智慧城市:大數(shù)據(jù)、互聯(lián)網(wǎng)時(shí)代的城市治理(第4版)
- 物聯(lián)網(wǎng)與北斗應(yīng)用
- Web安全防護(hù)指南:基礎(chǔ)篇
- SOA用戶指南
- 物聯(lián)網(wǎng)安全:理論、實(shí)踐與創(chuàng)新
- 物聯(lián)網(wǎng)之魂:物聯(lián)網(wǎng)協(xié)議與物聯(lián)網(wǎng)操作系統(tǒng)
- TCP/IP入門經(jīng)典(第5版)
- 面向云平臺(tái)的物聯(lián)網(wǎng)多源異構(gòu)信息融合方法
- 物聯(lián)網(wǎng)時(shí)代
- 城域網(wǎng)與廣域網(wǎng)(第2版)
- CCNP TSHOOT(642-832)認(rèn)證考試指南
- 端到端QoS網(wǎng)絡(luò)設(shè)計(jì)
- Getting Started with tmux
- 華為HCIA-Datacom認(rèn)證指南
- 智慧的物聯(lián)網(wǎng):感知中國和世界的技術(shù)