diff --git a/TDDLesson.BankAccount.Tests/BankAccountTests.cs b/TDDLesson.BankAccount.Tests/BankAccountTests.cs index 99508d3..ef29aa3 100644 --- a/TDDLesson.BankAccount.Tests/BankAccountTests.cs +++ b/TDDLesson.BankAccount.Tests/BankAccountTests.cs @@ -2,7 +2,117 @@ using FluentAssertions; +/* + * - Добавление на счет денеждых средств + * - Снятие средств со счета + */ + + [TestClass] public sealed class BankAccountTests { + [TestMethod] + public void ShouldBe20OnAccount_When20Added() + { + // Arrange + var balance = 20; + var account = new BankAccount(0); + + // Act + var newAccount = account.Add(balance); + + // Assert + newAccount.Balance.Should().Be(balance); + } + + [TestMethod] + public void ShouldBe50OnAccount_When20AddedAnd30Was() + { + // Arrange + var balanceBefore = 30; + var balanceAdded = 20; + var account = new BankAccount(balanceBefore); + + // Act + var newAccount = account.Add(balanceAdded); + + // Assert + newAccount.Balance.Should().Be(50); + } + + [TestMethod] + public void ShouldThrow_WhenNegativeValueAdded() + { + // Arrange + var balanceBefore = 30; + var balanceAdded = -20; + var account = new BankAccount(balanceBefore); + + // Act + var newAccount = () => account.Add(balanceAdded); + + // Assert + newAccount.Should().ThrowExactly(); + } + + [TestMethod] + public void ShouldBe20OnAccount_When10Withdrawn() + { + // Arrange + var balanceBefore = 30; + var balance = 10; + var account = new BankAccount(0, balanceBefore); + + // Act + var newAccount = account.Withdraw(balance); + + // Assert + newAccount.Balance.Should().Be(20); + } + + [TestMethod] + public void ShouldThrow_WhenNegativeValueWithdrawn() + { + // Arrange + var balanceBefore = 30; + var balanceWithdrawn = -20; + var account = new BankAccount(0, balanceBefore); + + // Act + var newAccount = () => account.Withdraw(balanceWithdrawn); + + // Assert + newAccount.Should().ThrowExactly(); + } + + [TestMethod] + public void ShouldThrow_WhenWithdrawnMoreThanOnAccountIncludingOverdraft() + { + // Arrange + var balanceOverdraft = 10; + var balanceBefore = 30; + var balanceWithdrawn = 50; + var account = new BankAccount(0, balanceBefore); + + // Act + var newAccount = () => account.Withdraw(balanceWithdrawn); + + // Assert + newAccount.Should().ThrowExactly(); + } + + [TestMethod] + public void ShouldBeMinus50OnAccount_WhenWithdrawn50AndZeroOnAccount() + { + // Arrange + var balanceBefore = 10; + var balanceWithdrawn = 50; + var account = new BankAccount(0, balanceBefore); + + // Act + var newAccount = account.Withdraw(balanceWithdrawn); + + // Assert + newAccount.Balance.Should().Be(-40); + } } \ No newline at end of file diff --git a/TDDLesson.BankAccount/BankAccount.cs b/TDDLesson.BankAccount/BankAccount.cs index cdc2e4e..a8d565e 100644 --- a/TDDLesson.BankAccount/BankAccount.cs +++ b/TDDLesson.BankAccount/BankAccount.cs @@ -1,13 +1,19 @@ namespace TDDLesson.BankAccount; -public sealed class BankAccount +public record BankAccount(int Overdraft, int Balance = 0) { - public int Balance { get; private set; } + public int Balance { get; private set; } = Balance; - public void Add(int money) + public BankAccount Add(int money) { - if (money < 0) throw new InvalidOperationException(); - - Balance = money; + if (money < 0) throw new ArgumentException(); + return new BankAccount(Balance + money); + } + + public BankAccount Withdraw(int money) + { + if (money < 0) throw new ArgumentException(); + if (Balance + Overdraft - money < 0) throw new InvalidOperationException(); + return new BankAccount(Balance + Overdraft - money, Overdraft); } } \ No newline at end of file