Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.baeldung.scala.implicitclasses

sealed trait Currency
object Currency {
case object EUR extends Currency
case object USD extends Currency
case object GBP extends Currency
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.baeldung.scala.implicitclasses

case class Money(amount: Double, currency: Currency)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.baeldung.scala.implicitclasses

object MoneySyntax {
implicit class RichMoney(val amount: Double) extends AnyVal {
def euros: Money = Money(amount, Currency.EUR)
def dollars: Money = Money(amount, Currency.USD)
def pounds: Money = Money(amount, Currency.GBP)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.baeldung.scala.implicitclasses

import org.scalatest.{Matchers, WordSpec}

class MoneyTest extends WordSpec with Matchers {
val amount: Double = 30.5

"MoneySyntax" should {
import MoneySyntax._
"create dollars" in {
amount.dollars shouldBe Money(amount, Currency.USD)
}
"create euros" in {
amount.euros shouldBe Money(amount, Currency.EUR)
}
"create pounds" in {
amount.pounds shouldBe Money(amount, Currency.GBP)
}
}
}