Skip to content

Commit 4f798a4

Browse files
committed
Complete prototype with unit testing.
1 parent cc1106f commit 4f798a4

File tree

6 files changed

+148
-25
lines changed

6 files changed

+148
-25
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace Billing
4+
{
5+
public abstract class BasicCustomer : ICloneable
6+
{
7+
public string FirstName {get;set;}
8+
public string LastName{get;set;}
9+
public Address HomeAddress{get;set;}
10+
public Address BillingAddress{get;set;}
11+
12+
public abstract object Clone();
13+
public abstract Customer DeepClone();
14+
}
15+
}
Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,36 @@
1+
using System;
2+
13
namespace Billing
24
{
3-
public abstract class Customer : ICloneable
5+
public class Customer : BasicCustomer
46
{
5-
public string FirstName {get;set;}
6-
public string LastName{get;set;}
7-
public Address HomeAddress{get;set;}
8-
public Address BillingAddress{get;set;}
7+
public override object Clone()
8+
{
9+
return this.MemberwiseClone() as BasicCustomer;
10+
}
11+
public override Customer DeepClone()
12+
{
13+
var customer = this.MemberwiseClone() as Customer;
14+
15+
customer.BillingAddress = new Address
16+
{
17+
StreetAddress = this.BillingAddress.StreetAddress,
18+
City = this.BillingAddress.City,
19+
State = this.BillingAddress.State,
20+
Country = this.BillingAddress.Country,
21+
PostCode = this.BillingAddress.PostCode
22+
};
23+
24+
customer.HomeAddress = new Address
25+
{
26+
StreetAddress = this.HomeAddress.StreetAddress,
27+
City = this.HomeAddress.City,
28+
State = this.HomeAddress.State,
29+
Country = this.HomeAddress.Country,
30+
PostCode = this.HomeAddress.PostCode
31+
};
932

10-
public abstract object Clone();
11-
public abstract Customer DeepClone();
33+
return customer;
34+
}
1235
}
1336
}

prototype_pattern/prototype_pattern.sln

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
44
VisualStudioVersion = 15.0.26124.0
55
MinimumVisualStudioVersion = 15.0.26124.0
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "prototype", "prototype\prototype.csproj", "{9691B873-D070-4816-B6CD-05E6F3A02011}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "prototype_test", "prototype_test\prototype_test.csproj", "{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}"
9+
EndProject
610
Global
711
GlobalSection(SolutionConfigurationPlatforms) = preSolution
812
Debug|Any CPU = Debug|Any CPU
@@ -15,4 +19,30 @@ Global
1519
GlobalSection(SolutionProperties) = preSolution
1620
HideSolutionNode = FALSE
1721
EndGlobalSection
22+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
23+
{9691B873-D070-4816-B6CD-05E6F3A02011}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{9691B873-D070-4816-B6CD-05E6F3A02011}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{9691B873-D070-4816-B6CD-05E6F3A02011}.Debug|x64.ActiveCfg = Debug|Any CPU
26+
{9691B873-D070-4816-B6CD-05E6F3A02011}.Debug|x64.Build.0 = Debug|Any CPU
27+
{9691B873-D070-4816-B6CD-05E6F3A02011}.Debug|x86.ActiveCfg = Debug|Any CPU
28+
{9691B873-D070-4816-B6CD-05E6F3A02011}.Debug|x86.Build.0 = Debug|Any CPU
29+
{9691B873-D070-4816-B6CD-05E6F3A02011}.Release|Any CPU.ActiveCfg = Release|Any CPU
30+
{9691B873-D070-4816-B6CD-05E6F3A02011}.Release|Any CPU.Build.0 = Release|Any CPU
31+
{9691B873-D070-4816-B6CD-05E6F3A02011}.Release|x64.ActiveCfg = Release|Any CPU
32+
{9691B873-D070-4816-B6CD-05E6F3A02011}.Release|x64.Build.0 = Release|Any CPU
33+
{9691B873-D070-4816-B6CD-05E6F3A02011}.Release|x86.ActiveCfg = Release|Any CPU
34+
{9691B873-D070-4816-B6CD-05E6F3A02011}.Release|x86.Build.0 = Release|Any CPU
35+
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
36+
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Debug|Any CPU.Build.0 = Debug|Any CPU
37+
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Debug|x64.ActiveCfg = Debug|Any CPU
38+
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Debug|x64.Build.0 = Debug|Any CPU
39+
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Debug|x86.ActiveCfg = Debug|Any CPU
40+
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Debug|x86.Build.0 = Debug|Any CPU
41+
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Release|Any CPU.ActiveCfg = Release|Any CPU
42+
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Release|Any CPU.Build.0 = Release|Any CPU
43+
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Release|x64.ActiveCfg = Release|Any CPU
44+
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Release|x64.Build.0 = Release|Any CPU
45+
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Release|x86.ActiveCfg = Release|Any CPU
46+
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Release|x86.Build.0 = Release|Any CPU
47+
EndGlobalSection
1848
EndGlobal

prototype_pattern/prototype_test/UnitTest1.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using NUnit.Framework;
2+
using FluentAssertions;
3+
using Moq;
4+
5+
namespace Billing.Tests
6+
{
7+
public class Tests
8+
{
9+
private BasicCustomer _basicCustomer;
10+
[SetUp]
11+
public void Setup()
12+
{
13+
_basicCustomer = new Customer();
14+
_basicCustomer.FirstName = "John";
15+
_basicCustomer.LastName = "Smith";
16+
_basicCustomer.HomeAddress = new Address
17+
{
18+
StreetAddress = "1 Kent Street",
19+
City = "Sydney",
20+
State = "New South Wales",
21+
Country = "Australia",
22+
PostCode = "2000"
23+
};
24+
_basicCustomer.BillingAddress = new Address
25+
{
26+
StreetAddress = _basicCustomer.HomeAddress.StreetAddress,
27+
City = _basicCustomer.HomeAddress.City,
28+
State = _basicCustomer.HomeAddress.State,
29+
Country = _basicCustomer.HomeAddress.Country,
30+
PostCode = _basicCustomer.HomeAddress.PostCode
31+
};
32+
}
33+
34+
[Test]
35+
public void NewCustomer_ShallowCloneFromCustomerWithSameAddress_AddressObjectsAreShared()
36+
{
37+
// Arrange
38+
var customer = (Customer)_basicCustomer.Clone();
39+
40+
// Act
41+
var newStreetAddress = "26 York Street";
42+
var relatedCustomer = (Customer)customer.Clone();
43+
relatedCustomer.FirstName = "Jane";
44+
relatedCustomer.HomeAddress.StreetAddress = newStreetAddress;
45+
46+
// Assert
47+
customer.HomeAddress.Should().Be(relatedCustomer.HomeAddress);
48+
49+
}
50+
[Test]
51+
public void NewCustomer_DeepCloneFromCustomerWithDifferentAddressSameFirstName_AddressObjectsAreDifferent()
52+
{
53+
// Arrange
54+
var customer = (Customer)_basicCustomer.Clone();
55+
56+
// Act
57+
var diffCustomer = customer.DeepClone();
58+
diffCustomer.FirstName = "Peter";
59+
diffCustomer.LastName = "Wick";
60+
diffCustomer.HomeAddress.StreetAddress = "57 Hassall Ridge";
61+
diffCustomer.HomeAddress.City = "Lexington";
62+
diffCustomer.HomeAddress.State = "Kentucky";
63+
diffCustomer.HomeAddress.Country = "United States Of America";
64+
diffCustomer.HomeAddress.PostCode = "40511";
65+
66+
// Assert
67+
customer.HomeAddress.Should().NotBeEquivalentTo(diffCustomer.HomeAddress);
68+
customer.FirstName.Should().BeEquivalentTo("John");
69+
}
70+
}
71+
}

prototype_pattern/prototype_test/prototype_test.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="FluentAssertions" Version="5.10.0" />
11+
<PackageReference Include="Moq" Version="4.13.1" />
1012
<PackageReference Include="nunit" Version="3.12.0" />
1113
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
1214
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />

0 commit comments

Comments
 (0)