diff --git a/TDDLesson.BankAccount.Tests/BankAccountTests.cs b/TDDLesson.BankAccount.Tests/BankAccountTests.cs index 99508d3..4c4739a 100644 --- a/TDDLesson.BankAccount.Tests/BankAccountTests.cs +++ b/TDDLesson.BankAccount.Tests/BankAccountTests.cs @@ -5,4 +5,153 @@ [TestClass] public sealed class BankAccountTests { + [TestMethod] + public void AddMoneyToBankAccount_MoneySuccessfullyAdded() + { + // Arrange + var bankAccount = new BankAccount(); + var money = 100; + + // Act + bankAccount.Add(money); + + // Assert + bankAccount.Balance.Should().Be(100); + } + + [TestMethod] + public void AddMoneyToNonEmptyBankAccount_MoneySuccessfullyAddedToExistingBalance() + { + // Arrange + var bankAccount = new BankAccount(); + var money = 100; + + // Act + bankAccount.Add(money); + bankAccount.Add(money); + + // Assert + bankAccount.Balance.Should().Be(200); + } + + [TestMethod] + public void AddMoneyToNonEmptyBankAccount_NegativeMoney_ThrowInvalidOperationException() + { + // Arrange + var bankAccount = new BankAccount(); + var money = -100; + + // Act + var act = () => bankAccount.Add(money); + + // Assert + act.Should().Throw(); + } + + [TestMethod] + public void TakeAllMoneyFromBankAccount_MoneySuccessfullyTaken() + { + // Arrange + var bankAccount = new BankAccount(); + var money = 100; + bankAccount.Add(money); + + // Act + bankAccount.Take(money); + + // Assert + bankAccount.Balance.Should().Be(0); + } + + [TestMethod] + public void TakeSomeMoneyFromBankAccount_MoneySuccessfullyTaken() + { + // Arrange + var bankAccount = new BankAccount(); + bankAccount.Add(100); + + // Act + bankAccount.Take(50); + + // Assert + bankAccount.Balance.Should().Be(50); + } + + [TestMethod] + public void TakeMoreMoneyThenHaveFromBankAccount_Error_MoneyNotEnough() + { + // Arrange + var bankAccount = new BankAccount(); + bankAccount.Add(50); + + // Act + var act = () => bankAccount.Take(100); + + // Assert + act.Should().Throw(); + } + + [TestMethod] + public void TakeMoneyFromBankAccount_NegativeMoney_ThrowInvalidOperationException() + { + // Arrange + var bankAccount = new BankAccount(); + var money = -100; + + // Act + var act = () => bankAccount.Take(money); + + // Assert + act.Should().Throw(); + } + + [TestMethod] + public void CreateBankAccount_WithoutLimit_LimitIsZero() + { + // Act + var bankAccount = new BankAccount(); + + // Assert + bankAccount.Limit.Should().Be(0); + } + + [TestMethod] + public void CreateBankAccount_WithLimit_LimitSetSuccessfully() + { + // Arrange + var limit = 1000; + + // Act + var bankAccount = new BankAccount(limit); + + // Assert + bankAccount.Limit.Should().Be(limit); + } + + [TestMethod] + public void CreateBankAccount_WithNegativeLimit_ThrowInvalidOperationException() + { + // Arrange + var limit = -1000; + + // Act + var act = () => new BankAccount(limit); + + // Assert + act.Should().Throw(); + } + + [TestMethod] + public void TakeMoneyCreditWithinLimit_MoneySuccessfullyTaken() + { + // Arrange + var bankAccount = new BankAccount(100); + bankAccount.Add(50); + + // Act + bankAccount.Take(100); + + // Assert + bankAccount.Balance.Should().Be(-50); + } } \ No newline at end of file diff --git a/TDDLesson.BankAccount/BankAccount.cs b/TDDLesson.BankAccount/BankAccount.cs index cdc2e4e..acef1af 100644 --- a/TDDLesson.BankAccount/BankAccount.cs +++ b/TDDLesson.BankAccount/BankAccount.cs @@ -2,12 +2,32 @@ public sealed class BankAccount { + public BankAccount(int limit = 0) + { + if (limit < 0) + throw new InvalidOperationException(); + + Limit = limit; + } public int Balance { get; private set; } + public int Limit { get; } + public void Add(int money) { if (money < 0) throw new InvalidOperationException(); - Balance = money; + Balance += money; + } + + public void Take(int money) + { + if (money < 0) throw new InvalidOperationException(); + + var allMoney = Balance + Limit; + if (allMoney < money) + throw new InvalidOperationException("Money not enough"); + + Balance -= money; } } \ No newline at end of file diff --git a/TDDLesson.sln.DotSettings.user b/TDDLesson.sln.DotSettings.user index 12ec2da..ebcb961 100644 --- a/TDDLesson.sln.DotSettings.user +++ b/TDDLesson.sln.DotSettings.user @@ -1,6 +1,6 @@  4294967293 /usr/local/share/dotnet/sdk/8.0.203/MSBuild.dll - <SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> - <Solution /> + <SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <Solution /> </SessionState> \ No newline at end of file