-
Notifications
You must be signed in to change notification settings - Fork 0
/
LensesTest.scala
275 lines (180 loc) · 8.52 KB
/
LensesTest.scala
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package fjab
import fjab.Hotel._
import fjab.Price._
import fjab.Room._
import monocle._
import monocle.function.Field3
import monocle.function.all._
import monocle.law.discipline.PrismTests
import monocle.std.option.some
import monocle.unsafe.UnsafeSelect
import org.scalacheck.{Arbitrary, Gen}
import org.scalatest.FunSuite
import org.typelevel.discipline.scalatest.Discipline
import scalaz.Equal
import scalaz.Scalaz._
import scala.util.Try
class LensesTest extends FunSuite with Discipline {
val rooms = List(
Room("Double", Some("Half Board"), Price(10, "USD"), NonRefundable(1)),
Room("Twin", None, Price(20, "USD"), Flexible(0)) ,
Room("Executive", None, Price(200, "USD"), Flexible(0))
)
val facilities = Map("business" -> List("conference room"))
val hotel = Hotel("Hotel Paradise", "100 High Street", 5, rooms, facilities)
test("double price of even rooms") {
val updatedHotel = (_rooms composeTraversal filterIndex{i: Int => i/2*2 == i} composeLens _price composeLens _amount modify(_ * 2)) (hotel)
assert(updatedHotel.rooms(0).price.amount == hotel.rooms(0).price.amount * 2)
assert(updatedHotel.rooms(1).price.amount == hotel.rooms(1).price.amount)
assert(updatedHotel.rooms(2).price.amount == hotel.rooms(2).price.amount * 2)
}
test("set price of 2nd room") {
val newValue = 12
val roomToUpdate = 1
assert(hotel.rooms(roomToUpdate).price.amount != newValue)
val updatedHotel = (_rooms composeOptional index(roomToUpdate) composeLens _price composeLens _amount set newValue)(hotel)
val updatedRoomList = (index[List[Room], Int, Room](roomToUpdate) composeLens _price composeLens _amount set newValue)(hotel.rooms)
assert(updatedHotel.rooms(roomToUpdate).price.amount == newValue)
assert(updatedRoomList(roomToUpdate).price.amount == newValue)
}
test("no changes are made when attempting to modify a non-existing room") {
val newValue = 12
val roomToUpdate = 3
assert(hotel.rooms.length == 3)
val updatedHotel = (_rooms composeOptional index(roomToUpdate) composeLens _price composeLens _amount set newValue)(hotel)
assert(hotel == updatedHotel)
}
test("hotel 'disappears' when attempting to modify a non-existing room") {
val newValue = 12
val roomToUpdate = 3
assert(hotel.rooms.length == 3)
val updatedHotel = (_rooms composeOptional index(roomToUpdate) composeLens _price composeLens _amount setOption newValue)(hotel)
assert(updatedHotel.isEmpty)
}
test("set a value inside an Option") {
val newValue = "New Board Type"
val roomToUpdate = 0
assert(!hotel.rooms(roomToUpdate).boardType.contains(newValue))
val updatedHotel = (_rooms composeOptional index(roomToUpdate) composeLens _boardType composeOptional some.asOptional set newValue)(hotel)
assert(updatedHotel.rooms(roomToUpdate).boardType.contains(newValue))
}
test("no changes are made when attempting to modify an empty Option") {
val newValue = "New Board Type"
val roomToUpdate = 1
assert(hotel.rooms(roomToUpdate).boardType.isEmpty)
val updatedHotel = (_rooms composeOptional index(roomToUpdate) composeLens _boardType composeOptional some.asOptional set newValue)(hotel)
assert(updatedHotel.rooms(roomToUpdate).boardType.isEmpty)
}
test("hotel 'disappears' when attempting to modify an empty Option") {
val newValue = "New Board Type"
val roomToUpdate = 1
assert(hotel.rooms(roomToUpdate).boardType.isEmpty)
val updatedHotel = (_rooms composeOptional index(roomToUpdate) composeLens _boardType composeOptional some.asOptional setOption newValue)(hotel)
assert(updatedHotel.isEmpty)
}
test("folding over room prices to add them up") {
assert(hotel.rooms(0).price.amount == 10)
assert(hotel.rooms(1).price.amount == 20)
assert(hotel.rooms(2).price.amount == 200)
assert((_rooms composeFold Fold.fromFoldable[List, Room] foldMap(_.price.amount))(hotel) == 230)
}
test("divide prices by 10"){
assert(hotel.rooms(0).price.amount == 10)
assert(hotel.rooms(1).price.amount == 20)
val updatedHotel = (_rooms composeTraversal each composeLens _price composeLens _amount modify(_ / 10))(hotel)
assert(updatedHotel.rooms(0).price.amount == 1)
assert(updatedHotel.rooms(1).price.amount == 2)
}
test("divide prices by 0"){
assert(hotel.rooms(0).price.amount == 10)
assert(hotel.rooms(1).price.amount == 20)
val updatedHotel = (_rooms composeTraversal each composeLens _price composeLens _amount).modifyF[Option](y => Try{y / 0}.toOption)(hotel)
assert(updatedHotel.isEmpty)
}
test("append a room"){
assert(hotel.rooms.length == 3)
val newRoom = Room("Triple", None, Price(1, "USD"), Flexible(0))
val updatedHotel = (_rooms set _snoc(hotel.rooms, newRoom))(hotel)
assert(updatedHotel.rooms.length == 4)
assert(updatedHotel.rooms(3) == newRoom)
}
test("prepend a room"){
assert(hotel.rooms.length == 3)
val newRoom = Room("Triple", None, Price(1, "USD"), Flexible(0))
val updatedHotel = (_rooms set _cons(newRoom, hotel.rooms))(hotel)
assert(updatedHotel.rooms.length == 4)
assert(updatedHotel.rooms(0) == newRoom)
}
test("set prices of Flexible rooms"){
val prism = Prism.partial[RoomTariff, BigDecimal]{case Flexible(x) => x}(Flexible)
val newValue = 100
assert(hotel.rooms(0).roomTariff == NonRefundable(1))
assert(hotel.rooms(1).roomTariff == Flexible(0))
assert(hotel.rooms(2).roomTariff == Flexible(0))
val updatedHotel = (_rooms composeTraversal each composeLens _roomTariff composePrism prism set newValue)(hotel)
assert(hotel.rooms(0).roomTariff == updatedHotel.rooms(0).roomTariff)
assert(updatedHotel.rooms(1).roomTariff == Flexible(newValue))
assert(updatedHotel.rooms(2).roomTariff == Flexible(newValue))
}
test("modifying business facilities") {
val updatedHotel = (_facilities composeLens at("business") set Some(List("")))(hotel)
assert(updatedHotel.facilities("business") == List(""))
}
test("removing business facilities") {
val updatedHotel = (_facilities composeLens at("business") set None)(hotel)
val updatedFacilities = remove("business")(hotel.facilities)
assert(updatedHotel.facilities.get("business").isEmpty)
assert(updatedFacilities.get("business").isEmpty)
}
test("adding entertainment facilities") {
val updatedHotel = (_facilities composeLens at("entertainment") set Some(List("satellite tv", "internet")))(hotel)
assert(updatedHotel.facilities("entertainment") == List("satellite tv", "internet"))
}
test("setting 1st element of tuple") {
val result = (first[(Int, Int), Int] set 1)((3,4))
assert(result == ((1,4)))
}
test("setting 3rd element of List") {
implicit val ev = new Field3[List[Int], Int]{
override def third: Lens[List[Int], Int] = Lens[List[Int], Int] (_(2))((a: Int) => (t: List[Int]) => t.take(2) ::: List(a) ::: t.drop(3))
}
val result = (third[List[Int], Int] set 1)(List(3,4,5))
assert(result == List(3,4,1))
}
val unsafePrism = UnsafeSelect.unsafeSelect[Room](_.name == "Double")
test("double price of Double rooms using unsafe operation") {
val updatedHotel = (_rooms composeTraversal each composePrism unsafePrism composeLens _price composeLens _amount modify (_ * 2)) (hotel)
assert(hotel.rooms.filter(_.name == "Double").map(_.price.amount*2) == updatedHotel.rooms.filter(_.name == "Double").map(_.price.amount))
}
implicit val roomEqual: Equal[Room] = Equal.equalA[Room]
val roomGen: Gen[Room] = for {
name <- Gen.oneOf("Double", "Twin", "Executive")
board <- Gen.option(Gen.alphaStr)
price <- for{
price <- Gen.posNum[Double]
currency <- Gen.oneOf("USD", "GBP", "EUR")
} yield Price(price, currency)
tariff <- Gen.oneOf(Gen.posNum[Double].map(NonRefundable(_)), Gen.posNum[Double].map(Flexible(_)))
} yield Room(name, board, price, tariff)
implicit val roomArb: Arbitrary[Room] = Arbitrary(roomGen)
implicit val arbAA: Arbitrary[Room => Room] = Arbitrary{
for{
room <- roomGen
} yield (_: Room) => room
}
checkAll("unsafe prism", PrismTests(unsafePrism))
val anotherUnlawfulPrism = Prism[Int, Double]
{s => Try(math.sqrt(s)).toOption}
{b => (b * b) toInt}
test("another unlawful prism example") {
2 match {
case anotherUnlawfulPrism(y) => assert(y == 1.4142135623730951)
case _ => fail()
}
-2 match {
case anotherUnlawfulPrism(y) => assert(y.isNaN)
case _ => fail()
}
}
checkAll("prism laws fail for negative numbers", PrismTests(anotherUnlawfulPrism))
}