From 0125bb99ac4c29902de751070c2cc7627c242272 Mon Sep 17 00:00:00 2001 From: Damien GOUYETTE Date: Fri, 17 Nov 2023 10:04:28 +0100 Subject: [PATCH] chore: add info about transparent referency --- .../scala/ErrorManagementExceptionSpec.scala | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/errormanagment/src/test/scala/ErrorManagementExceptionSpec.scala b/errormanagment/src/test/scala/ErrorManagementExceptionSpec.scala index a9c719f..7741643 100644 --- a/errormanagment/src/test/scala/ErrorManagementExceptionSpec.scala +++ b/errormanagment/src/test/scala/ErrorManagementExceptionSpec.scala @@ -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 @@ -12,7 +19,7 @@ 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) { @@ -20,31 +27,32 @@ class ErrorManagementExceptionSpec extends AnyFlatSpec with Matchers { }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)) } }