Skip to content

Commit

Permalink
chore: add info about transparent referency
Browse files Browse the repository at this point in the history
  • Loading branch information
damiengouyette committed Nov 17, 2023
1 parent c64ad46 commit 0125bb9
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions errormanagment/src/test/scala/ErrorManagementExceptionSpec.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

/**
* Referential transparency is a property of programming language expressions
* which means that an expression can be replaced by
* its value without changing the program's behavior.
*
* A pure function need Referential transparency
*/
class ErrorManagementExceptionSpec extends AnyFlatSpec with Matchers {

trait AccountStatus
Expand All @@ -12,39 +19,40 @@ class ErrorManagementExceptionSpec extends AnyFlatSpec with Matchers {
class AccountClosedException extends RuntimeException

case class BankAccount(availableMoney : Double, status : AccountStatus)
def withDraw(amount: Double, bankAccount: BankAccount): Unit = {
def withDraw(amount: Double, bankAccount: BankAccount): BankAccount = {
if (amount < 0) {
throw new NegativeAmountException
} else if (amount > bankAccount.availableMoney) {
throw new InsufficientFundsException
}else if (bankAccount.status == Closed){
throw new AccountClosedException
}
bankAccount.copy(availableMoney = bankAccount.availableMoney-amount)
}

"Withdrawing more money than the bank account holds" should " return false" in {
"Withdrawing more money than the bank account holds" should "throw a InsufficientFundsException exception" in {
val emptyBankAccount = BankAccount(0, Open)
assertThrows[InsufficientFundsException] {
withDraw(amount = 20_000, bankAccount = emptyBankAccount)
}
}

"Withdrawing negative money" should " return false" in {
"Withdrawing negative money" should "throw a NegativeAmountException exception" in {
val bankAccountWithMoney = BankAccount(10_000, Open)
assertThrows[NegativeAmountException] {
withDraw(-100, bankAccountWithMoney)
}
}
"Withdrawing on closed money" should "return false" in {
"Withdrawing on closed money" should "throw a AccountClosedException exception" in {
val bankAccountWithMoney = BankAccount(200, Closed)
assertThrows[AccountClosedException] {
withDraw(100, bankAccountWithMoney)
}
}

"withdrawing money from an adequately funded account" should "return true" in {
"withdrawing money from an adequately funded account" should "return unit" in {
val bankAccountWithMoney = BankAccount(10_000, Open)
withDraw(100, bankAccountWithMoney) shouldBe ()
withDraw(100, bankAccountWithMoney) shouldBe (BankAccount(9_900, Open))
}

}

0 comments on commit 0125bb9

Please sign in to comment.