Skip to content

Commit 89d49ac

Browse files
committed
update cs/ch07 with README's and updated libraries
1 parent 1a1a61c commit 89d49ac

8 files changed

Lines changed: 89 additions & 34 deletions

File tree

cs/ch07/Money.Tests/DollarTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ namespace TheSoftwareGorilla.TDD.Money.Tests;
55

66
public class DollarTests
77
{
8+
89
[SetUp]
910
public void Setup()
1011
{
12+
// No setup required for these tests
1113
}
1214

1315
[Test]
1416
public void TestConstruction()
1517
{
1618
var five = new Dollar(5);
17-
Assert.IsNotNull(five);
18-
Assert.That(five.Amount, Is.EqualTo(5));
19-
var ten = new Dollar(10);
20-
Assert.IsNotNull(ten);
21-
Assert.That(ten.Amount, Is.EqualTo(10));
19+
Assert.That(five, Is.Not.Null);
20+
Assert.That(five, Is.InstanceOf<Dollar>());
21+
Assert.That(five, Is.EqualTo(new Dollar(5)));
2222
}
2323

2424
[Test]

cs/ch07/Money.Tests/FrancTest.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
using NUnit.Framework;
2-
using TheSoftwareGorilla.TDD.Money;
3-
41
namespace TheSoftwareGorilla.TDD.Money.Tests;
52

63
public class FrancTests
74
{
5+
//TODO: 5 CHF * 2 = 10 CHF - DONE
6+
87
[SetUp]
98
public void Setup()
109
{
10+
// No setup required for these tests
1111
}
1212

1313
[Test]
1414
public void TestConstruction()
1515
{
1616
var five = new Franc(5);
17-
Assert.IsNotNull(five);
18-
Assert.That(five.Amount, Is.EqualTo(5));
19-
var ten = new Franc(10);
20-
Assert.IsNotNull(ten);
21-
Assert.That(ten.Amount, Is.EqualTo(10));
17+
Assert.That(five, Is.Not.Null);
18+
Assert.That(five, Is.InstanceOf<Franc>());
19+
Assert.That(five, Is.EqualTo(new Franc(5)));
2220
}
2321

2422
[Test]

cs/ch07/Money.Tests/Money.Tests.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="coverlet.collector" Version="6.0.0" />
13+
<PackageReference Include="coverlet.collector" Version="6.0.4" />
1414
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
15-
<PackageReference Include="NUnit" Version="3.14.0" />
16-
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
17-
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
15+
<PackageReference Include="NUnit" Version="4.4.0" />
16+
<PackageReference Include="NUnit.Analyzers" Version="4.10.0" />
17+
<PackageReference Include="NUnit3TestAdapter" Version="5.1.0" />
1818
</ItemGroup>
1919

2020
<ItemGroup>

cs/ch07/Money.Tests/MoneyTest.cs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,48 @@
1-
using NUnit.Framework;
2-
using TheSoftwareGorilla.TDD.Money;
3-
41
namespace TheSoftwareGorilla.TDD.Money.Tests;
52

63
public class MoneyTests
74
{
5+
//TODO: $5 + 10 CHF = $10 if rate is 2:1
6+
//TODO: $5 * 2 = $10 - DONE
7+
//TODO: Make "amount" private - DONE
8+
//TODO: Dollar side-effects? - DONE
9+
//TODO: Money rounding?
10+
//TODO: equals() -DONE
11+
//TODO: hashCode()
12+
//TODO: Equal null
13+
//TODO: Equal object
14+
//TODO: 5 CHF * 2 = 10 CHF - DONE
15+
//TODO: Dollar/Franc duplication
16+
//TODO: Common equals - DONE
17+
//TODO: Common Times
18+
//TODO: Compare Francs with Dollars - DONE
19+
//TODO: Currency?
20+
821
[SetUp]
922
public void Setup()
1023
{
24+
// No setup required for these tests
1125
}
1226

1327
[Test]
1428
public void TestEquality()
1529
{
16-
Assert.That(new Dollar(5), Is.EqualTo(new Dollar(5)));
17-
Assert.That(new Dollar(6), Is.Not.EqualTo(new Dollar(5)));
18-
Assert.That(new Franc(5), Is.EqualTo(new Franc(5)));
19-
Assert.That(new Franc(6), Is.Not.EqualTo(new Franc(5)));
20-
30+
Dollar fived = new Dollar(5);
31+
Dollar sixd = new Dollar(6);
32+
Franc fivef = new Franc(5);
33+
Franc sixf = new Franc(6);
34+
35+
Assert.That(fived, Is.EqualTo(new Dollar(5)));
36+
Assert.That(sixd, Is.Not.EqualTo(new Dollar(5)));
37+
Assert.That(fivef, Is.EqualTo(new Franc(5)));
38+
Assert.That(sixf, Is.Not.EqualTo(new Franc(5)));
39+
2140
// In NUnit, the following line will not compile because .EqualTo type-checks its argument.
22-
// I'm commenting the line out to avoid compilation errors, and I added Assert.False below to
41+
// I'm commenting the line out to avoid compilation errors, and I added Assert(Is.False) below to
2342
// be more consistent with the book.
2443
// Assert.That(new Franc(5), Is.Not.EqualTo(new Dollar(5)));
25-
Assert.False(new Franc(5).Equals(new Dollar(5)));
44+
Assert.That(new Franc(5).Equals(new Dollar(5)), Is.False);
45+
2646
}
2747

2848
}

cs/ch07/Money/Dollar.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
public class Dollar : Money
44
{
55

6-
public Dollar(int amount)
6+
public Dollar(int amount)
77
{
8-
Amount = amount;
8+
this.amount = amount;
99
}
1010

1111
public Dollar Times(int multiplier)
1212
{
13-
return new Dollar(Amount * multiplier);
13+
return new Dollar(amount * multiplier);
1414
}
1515

1616
}

cs/ch07/Money/Franc.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ public class Franc : Money
55

66
public Franc(int amount)
77
{
8-
Amount = amount;
8+
this.amount = amount;
99
}
1010

1111
public Franc Times(int multiplier)
1212
{
13-
return new Franc(Amount * multiplier);
13+
return new Franc(amount * multiplier);
1414
}
1515

1616
}

cs/ch07/Money/Money.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ namespace TheSoftwareGorilla.TDD.Money;
22

33
public class Money
44
{
5-
public int Amount { get; protected set; }
5+
protected int amount;
66

77
public override bool Equals(object? obj)
88
{
9-
return obj is Money money && GetType().Equals(obj.GetType()) && Amount == money.Amount;
9+
return obj is Money money && GetType().Equals(obj.GetType()) && amount == money.amount;
1010
}
1111

1212

cs/ch07/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Test-Driven Development - C# - Chapter 07
2+
3+
This solution contains a code for Chapter 7 of the book ["Test-Driven Development By Example" by
4+
Kent Beck](https://a.co/d/1sr05eT). The code is written in C# and uses the NUnit testing framework for the tests.
5+
6+
For information on how to set up the repository, please see the [README in the ch00](../ch00/README.md) folder.
7+
8+
## Chapter 7 - Apples and Oranges
9+
This is another really small chapter. For this chapter the focus is making sure that the Equals does a type check for
10+
the object being compared and that it is generalized for all Money objects.
11+
12+
### TODO list at the end of the chapter
13+
By the end of the chapter, the TODO list looks like this:
14+
- [ ] \$5 + 10 CHF = $10 if rate is 2:1
15+
- [x] \$5 * 2 = $10
16+
- [x] Make "amount" private
17+
- [x] Dollar side-effects?
18+
- [ ] Money rounding?
19+
- [x] equals()
20+
- [ ] hashCode()
21+
- [ ] Equal null
22+
- [ ] Equal object
23+
- [x] 5 CHF * 2 = 10 CHF
24+
- [ ] Dollar/Franc duplication
25+
- [x] Common Equals
26+
- [ ] Common Times
27+
- [x] Compare Francs with Dollars
28+
- [ ] Currency?
29+
30+
## Last Update
31+
I try and keep this code up to date with the latest versions. I generally wait until a new version of .NET SDK is
32+
released and I only update it for Long Term Support (LTS) versions. .NET 8 is the latest LTS version as of this writing.
33+
34+
This repository was last updated in September 2025.
35+
- .NET SDK version 8
36+
- NUnit version 4.4.0
37+
- JetBrains Rider version 2025.2.2

0 commit comments

Comments
 (0)