From 3122e0b9df187052d2867d9ba8a13d5a358f928a Mon Sep 17 00:00:00 2001 From: Marat Elagin Date: Thu, 23 May 2024 11:25:45 +0300 Subject: [PATCH 1/5] addMoney tests --- .../BankAccountTests.cs | 28 +++++++++++++++++++ TDDLesson.BankAccount/BankAccount.cs | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/TDDLesson.BankAccount.Tests/BankAccountTests.cs b/TDDLesson.BankAccount.Tests/BankAccountTests.cs index 99508d3..cbaba96 100644 --- a/TDDLesson.BankAccount.Tests/BankAccountTests.cs +++ b/TDDLesson.BankAccount.Tests/BankAccountTests.cs @@ -5,4 +5,32 @@ [TestClass] public sealed class BankAccountTests { + [TestMethod] + public void AddMoneyToBankAccount_MoneySuccessfullyAdded() + { + // Arrange + var bankAccount = new BankAccount(); + var money = 100; + + // Act + bankAccount.Add(money); + + // Assert + Assert.AreEqual(100, bankAccount.Balance); + } + + [TestMethod] + public void AddMoneyToNonEmptyBankAccount_MoneySuccessfullyAddedToExistingBalance() + { + // Arrange + var bankAccount = new BankAccount(); + var money = 100; + + // Act + bankAccount.Add(money); + bankAccount.Add(money); + + // Assert + Assert.AreEqual(200, bankAccount.Balance); + } } \ No newline at end of file diff --git a/TDDLesson.BankAccount/BankAccount.cs b/TDDLesson.BankAccount/BankAccount.cs index cdc2e4e..5cfb92d 100644 --- a/TDDLesson.BankAccount/BankAccount.cs +++ b/TDDLesson.BankAccount/BankAccount.cs @@ -8,6 +8,6 @@ public void Add(int money) { if (money < 0) throw new InvalidOperationException(); - Balance = money; + Balance += money; } } \ No newline at end of file From d1b03c5e5ebc6c971dcb40e3b35ba985603863bb Mon Sep 17 00:00:00 2001 From: Marat Elagin Date: Thu, 23 May 2024 11:33:37 +0300 Subject: [PATCH 2/5] improvements, add new test --- .../BankAccountTests.cs | 18 ++++++++++++++++-- TDDLesson.BankAccount/BankAccount.cs | 7 +++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/TDDLesson.BankAccount.Tests/BankAccountTests.cs b/TDDLesson.BankAccount.Tests/BankAccountTests.cs index cbaba96..640f926 100644 --- a/TDDLesson.BankAccount.Tests/BankAccountTests.cs +++ b/TDDLesson.BankAccount.Tests/BankAccountTests.cs @@ -16,7 +16,7 @@ public void AddMoneyToBankAccount_MoneySuccessfullyAdded() bankAccount.Add(money); // Assert - Assert.AreEqual(100, bankAccount.Balance); + bankAccount.Should().Be(100); } [TestMethod] @@ -31,6 +31,20 @@ public void AddMoneyToNonEmptyBankAccount_MoneySuccessfullyAddedToExistingBalanc bankAccount.Add(money); // Assert - Assert.AreEqual(200, bankAccount.Balance); + bankAccount.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(); } } \ No newline at end of file diff --git a/TDDLesson.BankAccount/BankAccount.cs b/TDDLesson.BankAccount/BankAccount.cs index 5cfb92d..ff25ba9 100644 --- a/TDDLesson.BankAccount/BankAccount.cs +++ b/TDDLesson.BankAccount/BankAccount.cs @@ -10,4 +10,11 @@ public void Add(int money) Balance += money; } + + public void Take(int money) + { + //if (money < 0) throw new InvalidOperationException(); + + //Balance = money; + } } \ No newline at end of file From 15f24a723473b144c84fdd26cdde8f2041ad4252 Mon Sep 17 00:00:00 2001 From: Nataly Turbina Date: Thu, 23 May 2024 12:41:59 +0400 Subject: [PATCH 3/5] - --- .../BankAccountTests.cs | 61 ++++++++++++++++++- TDDLesson.BankAccount/BankAccount.cs | 5 +- TDDLesson.sln.DotSettings.user | 4 +- 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/TDDLesson.BankAccount.Tests/BankAccountTests.cs b/TDDLesson.BankAccount.Tests/BankAccountTests.cs index 640f926..fb2bcf6 100644 --- a/TDDLesson.BankAccount.Tests/BankAccountTests.cs +++ b/TDDLesson.BankAccount.Tests/BankAccountTests.cs @@ -16,7 +16,7 @@ public void AddMoneyToBankAccount_MoneySuccessfullyAdded() bankAccount.Add(money); // Assert - bankAccount.Should().Be(100); + bankAccount.Balance.Should().Be(100); } [TestMethod] @@ -31,7 +31,7 @@ public void AddMoneyToNonEmptyBankAccount_MoneySuccessfullyAddedToExistingBalanc bankAccount.Add(money); // Assert - bankAccount.Should().Be(200); + bankAccount.Balance.Should().Be(200); } [TestMethod] @@ -47,4 +47,61 @@ public void AddMoneyToNonEmptyBankAccount_NegativeMoney_ThrowInvalidOperationExc // 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.Add(money); + + // Assert + act.Should().Throw(); + } } \ No newline at end of file diff --git a/TDDLesson.BankAccount/BankAccount.cs b/TDDLesson.BankAccount/BankAccount.cs index ff25ba9..4d373cf 100644 --- a/TDDLesson.BankAccount/BankAccount.cs +++ b/TDDLesson.BankAccount/BankAccount.cs @@ -14,7 +14,10 @@ public void Add(int money) public void Take(int money) { //if (money < 0) throw new InvalidOperationException(); + + if (money > Balance) + throw new InvalidOperationException("Money not enough"); - //Balance = money; + 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 From 33a295bb8a37ba250bd70f3cf56b8a5739511184 Mon Sep 17 00:00:00 2001 From: Marat Elagin Date: Thu, 23 May 2024 11:58:30 +0300 Subject: [PATCH 4/5] add limit property --- .../BankAccountTests.cs | 38 ++++++++++++++++++- TDDLesson.BankAccount/BankAccount.cs | 11 +++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/TDDLesson.BankAccount.Tests/BankAccountTests.cs b/TDDLesson.BankAccount.Tests/BankAccountTests.cs index fb2bcf6..f704bbf 100644 --- a/TDDLesson.BankAccount.Tests/BankAccountTests.cs +++ b/TDDLesson.BankAccount.Tests/BankAccountTests.cs @@ -99,8 +99,44 @@ public void TakeMoneyFromBankAccount_NegativeMoney_ThrowInvalidOperationExceptio var money = -100; // Act - var act = () => bankAccount.Add(money); + 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(); } diff --git a/TDDLesson.BankAccount/BankAccount.cs b/TDDLesson.BankAccount/BankAccount.cs index 4d373cf..2719545 100644 --- a/TDDLesson.BankAccount/BankAccount.cs +++ b/TDDLesson.BankAccount/BankAccount.cs @@ -2,8 +2,17 @@ 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(); @@ -13,7 +22,7 @@ public void Add(int money) public void Take(int money) { - //if (money < 0) throw new InvalidOperationException(); + if (money < 0) throw new InvalidOperationException(); if (money > Balance) throw new InvalidOperationException("Money not enough"); From 891b3f9781cf6ed4dfb5b43b0ded53d23efc1d8c Mon Sep 17 00:00:00 2001 From: Nataly Turbina Date: Thu, 23 May 2024 13:08:14 +0400 Subject: [PATCH 5/5] up --- TDDLesson.BankAccount.Tests/BankAccountTests.cs | 14 ++++++++++++++ TDDLesson.BankAccount/BankAccount.cs | 3 ++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/TDDLesson.BankAccount.Tests/BankAccountTests.cs b/TDDLesson.BankAccount.Tests/BankAccountTests.cs index f704bbf..4c4739a 100644 --- a/TDDLesson.BankAccount.Tests/BankAccountTests.cs +++ b/TDDLesson.BankAccount.Tests/BankAccountTests.cs @@ -140,4 +140,18 @@ public void CreateBankAccount_WithNegativeLimit_ThrowInvalidOperationException() // 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 2719545..acef1af 100644 --- a/TDDLesson.BankAccount/BankAccount.cs +++ b/TDDLesson.BankAccount/BankAccount.cs @@ -24,7 +24,8 @@ public void Take(int money) { if (money < 0) throw new InvalidOperationException(); - if (money > Balance) + var allMoney = Balance + Limit; + if (allMoney < money) throw new InvalidOperationException("Money not enough"); Balance -= money;