-
Notifications
You must be signed in to change notification settings - Fork 0
/
SilverBarsTests.kt
106 lines (89 loc) · 3.4 KB
/
SilverBarsTests.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package org.kware
import org.junit.jupiter.api.Test
import org.kware.silverbars.*
import org.tenkiv.physikal.core.gram
import org.tenkiv.physikal.core.kilo
import strikt.api.expectThat
import strikt.assertions.containsExactly
import java.math.BigDecimal
fun Order.toSummary() = SummaryItem(this.quantity, this.price)
class SilverBarsTests {
private val userId = UserId("user1")
private val buyOrder1 = Order(userId, 9.2.kilo.gram, OrderType.BUY, 303.toBigDecimal())
private val buyOrder2 = Order(userId, 9.2.kilo.gram, OrderType.BUY, 304.toBigDecimal())
private val sellOrder1 = Order(userId, 9.2.kilo.gram, OrderType.SELL, 303.toBigDecimal())
private val sellOrder2 = Order(userId, 9.2.kilo.gram, OrderType.SELL, 304.toBigDecimal())
private val sellOrder3 = Order(userId, 2.kilo.gram, OrderType.SELL, 305.toBigDecimal())
@Test
fun `user can register an order and see summary`() {
val market = Market()
market.register(buyOrder1)
expectThat(market.summary()[OrderType.BUY].orEmpty())
.containsExactly(SummaryItem(9.2.kilo.gram, 303.toBigDecimal()))
}
@Test
fun `user can register 2 buy orders of different prices and see them in the summary items in the correct order`() {
val market = with(Market()) {
register(buyOrder2)
register(buyOrder1)
this
}
expectThat(market.summary()[OrderType.BUY].orEmpty())
.containsExactly(
buyOrder1.toSummary(),
buyOrder2.toSummary()
)
}
@Test
fun `user can register 2 sell orders of different prices and see them in the summary in the correct order`() {
val market = with(Market()) {
register(sellOrder1)
register(sellOrder2)
this
}
expectThat(market.summary()[OrderType.SELL].orEmpty())
.containsExactly(
sellOrder2.toSummary(),
sellOrder1.toSummary()
)
}
@Test
fun `user can register 3 sell orders and 2 orders of same price and see them merged in the summary`() {
val market = Market()
market.register(sellOrder1)
market.register(sellOrder2)
market.register(sellOrder2)
expectThat(market.summary()[OrderType.SELL].orEmpty())
.containsExactly(
SummaryItem(18.4.kilo.gram, 304.toBigDecimal()),
sellOrder1.toSummary()
)
}
@Test
fun `user can specify the same price with different precisions and it will merge`() {
val market = with(Market()) {
register(Order(userId, 2.kilo.gram, OrderType.BUY, BigDecimal(0.00500)))
register(Order(userId, 2.kilo.gram, OrderType.BUY, BigDecimal(0.005)))
this
}
expectThat(market.summary()[OrderType.BUY].orEmpty())
.containsExactly(
SummaryItem(4.kilo.gram, BigDecimal(0.005))
)
}
@Test
fun `the user can cancel an order`() {
val market = with(Market()) {
register(sellOrder3)
register(sellOrder1)
this
}
val orderToCancel = market.register(sellOrder2)
orderToCancel.cancel()
expectThat(market.summary()[OrderType.SELL].orEmpty())
.containsExactly(
sellOrder3.toSummary(),
sellOrder1.toSummary()
)
}
}