Skip to content

Commit 0125bb9

Browse files
chore: add info about transparent referency
1 parent c64ad46 commit 0125bb9

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed
Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import org.scalatest.flatspec.AnyFlatSpec
22
import org.scalatest.matchers.should.Matchers
33

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

613
trait AccountStatus
@@ -12,39 +19,40 @@ class ErrorManagementExceptionSpec extends AnyFlatSpec with Matchers {
1219
class AccountClosedException extends RuntimeException
1320

1421
case class BankAccount(availableMoney : Double, status : AccountStatus)
15-
def withDraw(amount: Double, bankAccount: BankAccount): Unit = {
22+
def withDraw(amount: Double, bankAccount: BankAccount): BankAccount = {
1623
if (amount < 0) {
1724
throw new NegativeAmountException
1825
} else if (amount > bankAccount.availableMoney) {
1926
throw new InsufficientFundsException
2027
}else if (bankAccount.status == Closed){
2128
throw new AccountClosedException
2229
}
30+
bankAccount.copy(availableMoney = bankAccount.availableMoney-amount)
2331
}
2432

25-
"Withdrawing more money than the bank account holds" should " return false" in {
33+
"Withdrawing more money than the bank account holds" should "throw a InsufficientFundsException exception" in {
2634
val emptyBankAccount = BankAccount(0, Open)
2735
assertThrows[InsufficientFundsException] {
2836
withDraw(amount = 20_000, bankAccount = emptyBankAccount)
2937
}
3038
}
3139

32-
"Withdrawing negative money" should " return false" in {
40+
"Withdrawing negative money" should "throw a NegativeAmountException exception" in {
3341
val bankAccountWithMoney = BankAccount(10_000, Open)
3442
assertThrows[NegativeAmountException] {
3543
withDraw(-100, bankAccountWithMoney)
3644
}
3745
}
38-
"Withdrawing on closed money" should "return false" in {
46+
"Withdrawing on closed money" should "throw a AccountClosedException exception" in {
3947
val bankAccountWithMoney = BankAccount(200, Closed)
4048
assertThrows[AccountClosedException] {
4149
withDraw(100, bankAccountWithMoney)
4250
}
4351
}
4452

45-
"withdrawing money from an adequately funded account" should "return true" in {
53+
"withdrawing money from an adequately funded account" should "return unit" in {
4654
val bankAccountWithMoney = BankAccount(10_000, Open)
47-
withDraw(100, bankAccountWithMoney) shouldBe ()
55+
withDraw(100, bankAccountWithMoney) shouldBe (BankAccount(9_900, Open))
4856
}
4957

5058
}

0 commit comments

Comments
 (0)