Skip to content

Commit f739cbf

Browse files
committed
[Samples][L7] Add UnitTests for the Account inheritance demo
1 parent 6b89ff2 commit f739cbf

6 files changed

Lines changed: 111 additions & 8 deletions

File tree

LessonsSamples/LessonsSamples/Lesson7/InheritanceComposition/Account.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
namespace LessonsSamples.Lesson7.InheritanceComposition
44
{
5-
class Account
5+
public class Account
66
{
77
private decimal amount;
8-
private decimal interestRate;
8+
private readonly decimal interestRate;
9+
10+
public Account(decimal interestRate)
11+
{
12+
this.interestRate = interestRate;
13+
}
914

1015
protected decimal CalculateTaxesForMoth(Month month)
1116
{
@@ -16,7 +21,7 @@ protected decimal CalculateTaxesForMoth(Month month)
1621
public decimal Amount
1722
{
1823
get { return amount; }
19-
protected set { amount = value; }
24+
set { amount = value; }
2025
}
2126

2227
public decimal MonthlyInterest()
@@ -37,6 +42,10 @@ public virtual void MonthlyRenewal()
3742

3843
class SavingsAccount : Account
3944
{
45+
public SavingsAccount(decimal interestRate) : base(interestRate)
46+
{
47+
}
48+
4049
public decimal YearlyProfit()
4150
{
4251
// ...
@@ -60,25 +69,33 @@ private decimal CalculateYearlyDeposits()
6069

6170
class CheckingAccount : Account
6271
{
72+
public CheckingAccount(decimal interestRate) : base(interestRate)
73+
{
74+
}
75+
6376
public decimal TransactionsCosts()
6477
{
6578
// ...
6679
Month lastMonth = new Month();
6780
var lastMonthTaxes = CalculateTaxesForMoth(lastMonth);
68-
decimal lastMonthCommissions = CalculateLastMonthCommision();
81+
decimal lastMonthBankCharges = CalculateLastMonthBankCharges();
6982
// ...
7083

71-
return lastMonthCommissions + lastMonthTaxes;
84+
return lastMonthBankCharges + lastMonthTaxes;
7285
}
7386

74-
private decimal CalculateLastMonthCommision()
87+
private decimal CalculateLastMonthBankCharges()
7588
{
7689
throw new NotImplementedException();
7790
}
7891
}
7992

8093
class AutoLoanAccount : Account
8194
{
95+
public AutoLoanAccount(decimal interestRate) : base(interestRate)
96+
{
97+
}
98+
8299
public override void MonthlyRenewal()
83100
{
84101
decimal interest = MonthlyInterest();

LessonsSamples/LessonsSamples/Lesson7/InheritanceComposition/AccountClientCode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
static class AccountClientCode
44
{
5-
public static decimal CalculateTotalMonthlyInterest(Account[] accounts)
5+
public static decimal CalculateTotalInterestValue(Account[] accounts)
66
{
77
decimal amount = 0;
88
for (int i = 0; i < accounts.Length; i++)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
3+
namespace LessonsSamples.Lesson7.InheritanceComposition
4+
{
5+
[TestClass]
6+
public class AccountClientCodeTests
7+
{
8+
[TestMethod]
9+
public void CalculateTotalInterestValue_MoreAccounts_InterestSummedFromAll()
10+
{
11+
Account[] accounts =
12+
{
13+
new Account(50) {Amount = 1}, //+0.5
14+
new Account(50) {Amount = 1}, //+0.5
15+
new Account(50) {Amount = 1}, //+0.5
16+
};
17+
decimal actualInterest = AccountClientCode.CalculateTotalInterestValue(accounts);
18+
19+
Assert.AreEqual(1.5m, actualInterest);
20+
}
21+
22+
[TestMethod]
23+
public void CalculateTotalInterestValue_AllAccountTypes_InterestSummedFromAll()
24+
{
25+
Account[] accounts =
26+
{
27+
new SavingsAccount(50) {Amount = 1}, //+0.5
28+
new CheckingAccount(50) {Amount = 1}, //+0.5
29+
new AutoLoanAccount(50) {Amount = 1}, //-0.5
30+
};
31+
decimal actualInterest = AccountClientCode.CalculateTotalInterestValue(accounts);
32+
33+
Assert.AreEqual(0.5m, actualInterest);
34+
}
35+
}
36+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
3+
namespace LessonsSamples.Lesson7.InheritanceComposition
4+
{
5+
[TestClass]
6+
public abstract class AccountContractTests
7+
{
8+
[TestMethod]
9+
public void MonthlyInterest_AmountIsPositive_InterestAppliedAsPercentage()
10+
{
11+
Account account = GetTarget(50);
12+
account.Amount = 100;
13+
14+
decimal interest = account.MonthlyInterest();
15+
16+
Assert.AreEqual(50m, interest);
17+
}
18+
19+
protected abstract Account GetTarget(decimal interestRate);
20+
}
21+
22+
[TestClass]
23+
public class SavingsAccountTests : AccountContractTests
24+
{
25+
protected override Account GetTarget(decimal interestRate)
26+
{
27+
return new SavingsAccount(interestRate);
28+
}
29+
}
30+
31+
[TestClass]
32+
public class CheckingAccountTests : AccountContractTests
33+
{
34+
protected override Account GetTarget(decimal interestRate)
35+
{
36+
return new CheckingAccount(interestRate);
37+
}
38+
}
39+
40+
[TestClass]
41+
public class AutoLoanAccountTests : AccountContractTests
42+
{
43+
protected override Account GetTarget(decimal interestRate)
44+
{
45+
return new AutoLoanAccount(interestRate);
46+
}
47+
}
48+
}

LessonsSamples/LessonsSamples/Lesson7/InheritanceComposition/Month.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace LessonsSamples.Lesson7.InheritanceComposition
22
{
3-
internal struct Month
3+
public struct Month
44
{
55
public int Year;
66
public int MonthOfYear;

LessonsSamples/LessonsSamples/LessonsSamples.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@
206206
<Compile Include="Lesson7\ErrorHandling\CodeSnippet.cs" />
207207
<Compile Include="Lesson7\InheritanceComposition\Account.cs" />
208208
<Compile Include="Lesson7\InheritanceComposition\AccountClientCode.cs" />
209+
<Compile Include="Lesson7\InheritanceComposition\AccountClientCodeTests.cs" />
210+
<Compile Include="Lesson7\InheritanceComposition\AccountTests.cs" />
209211
<Compile Include="Lesson7\InheritanceComposition\ILogMessageParser.cs" />
210212
<Compile Include="Lesson7\InheritanceComposition\LogEntry.cs" />
211213
<Compile Include="Lesson7\InheritanceComposition\LogSource.cs" />

0 commit comments

Comments
 (0)