Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Team2 #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions TDDLesson.BankAccount.Tests/BankAccountTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<InvalidOperationException>();
}

[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<InvalidOperationException>();
}

[TestMethod]
public void TakeMoneyFromBankAccount_NegativeMoney_ThrowInvalidOperationException()
{
// Arrange
var bankAccount = new BankAccount();
var money = -100;

// Act
var act = () => bankAccount.Take(money);

// Assert
act.Should().Throw<InvalidOperationException>();
}

[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<InvalidOperationException>();
}

[TestMethod]
public void TakeMoneyCreditWithinLimit_MoneySuccessfullyTaken()
{
// Arrange
var bankAccount = new BankAccount(100);
bankAccount.Add(50);

// Act
bankAccount.Take(100);

// Assert
bankAccount.Balance.Should().Be(-50);
}
}
22 changes: 21 additions & 1 deletion TDDLesson.BankAccount/BankAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 2 additions & 2 deletions TDDLesson.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Int64 x:Key="/Default/Environment/Hierarchy/Build/BuildTool/MsbuildVersion/@EntryValue">4294967293</s:Int64>
<s:String x:Key="/Default/Environment/Hierarchy/Build/BuildTool/CustomBuildToolPath/@EntryValue">/usr/local/share/dotnet/sdk/8.0.203/MSBuild.dll</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=46c725ae_002Dcc42_002D4ccd_002Db699_002Df44f04a3f20a/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Solution /&gt;&#xD;
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=46c725ae_002Dcc42_002D4ccd_002Db699_002Df44f04a3f20a/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;Solution /&gt;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>