Skip to content

Commit

Permalink
Complete prototype with unit testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
deanagan committed Jan 27, 2020
1 parent cc1106f commit 4f798a4
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 25 deletions.
15 changes: 15 additions & 0 deletions prototype_pattern/prototype/BasicCustomer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace Billing
{
public abstract class BasicCustomer : ICloneable
{
public string FirstName {get;set;}
public string LastName{get;set;}
public Address HomeAddress{get;set;}
public Address BillingAddress{get;set;}

public abstract object Clone();
public abstract Customer DeepClone();
}
}
37 changes: 30 additions & 7 deletions prototype_pattern/prototype/Customer.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
using System;

namespace Billing
{
public abstract class Customer : ICloneable
public class Customer : BasicCustomer
{
public string FirstName {get;set;}
public string LastName{get;set;}
public Address HomeAddress{get;set;}
public Address BillingAddress{get;set;}
public override object Clone()
{
return this.MemberwiseClone() as BasicCustomer;
}
public override Customer DeepClone()
{
var customer = this.MemberwiseClone() as Customer;

customer.BillingAddress = new Address
{
StreetAddress = this.BillingAddress.StreetAddress,
City = this.BillingAddress.City,
State = this.BillingAddress.State,
Country = this.BillingAddress.Country,
PostCode = this.BillingAddress.PostCode
};

customer.HomeAddress = new Address
{
StreetAddress = this.HomeAddress.StreetAddress,
City = this.HomeAddress.City,
State = this.HomeAddress.State,
Country = this.HomeAddress.Country,
PostCode = this.HomeAddress.PostCode
};

public abstract object Clone();
public abstract Customer DeepClone();
return customer;
}
}
}
30 changes: 30 additions & 0 deletions prototype_pattern/prototype_pattern.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "prototype", "prototype\prototype.csproj", "{9691B873-D070-4816-B6CD-05E6F3A02011}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "prototype_test", "prototype_test\prototype_test.csproj", "{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,4 +19,30 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9691B873-D070-4816-B6CD-05E6F3A02011}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9691B873-D070-4816-B6CD-05E6F3A02011}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9691B873-D070-4816-B6CD-05E6F3A02011}.Debug|x64.ActiveCfg = Debug|Any CPU
{9691B873-D070-4816-B6CD-05E6F3A02011}.Debug|x64.Build.0 = Debug|Any CPU
{9691B873-D070-4816-B6CD-05E6F3A02011}.Debug|x86.ActiveCfg = Debug|Any CPU
{9691B873-D070-4816-B6CD-05E6F3A02011}.Debug|x86.Build.0 = Debug|Any CPU
{9691B873-D070-4816-B6CD-05E6F3A02011}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9691B873-D070-4816-B6CD-05E6F3A02011}.Release|Any CPU.Build.0 = Release|Any CPU
{9691B873-D070-4816-B6CD-05E6F3A02011}.Release|x64.ActiveCfg = Release|Any CPU
{9691B873-D070-4816-B6CD-05E6F3A02011}.Release|x64.Build.0 = Release|Any CPU
{9691B873-D070-4816-B6CD-05E6F3A02011}.Release|x86.ActiveCfg = Release|Any CPU
{9691B873-D070-4816-B6CD-05E6F3A02011}.Release|x86.Build.0 = Release|Any CPU
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Debug|x64.ActiveCfg = Debug|Any CPU
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Debug|x64.Build.0 = Debug|Any CPU
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Debug|x86.ActiveCfg = Debug|Any CPU
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Debug|x86.Build.0 = Debug|Any CPU
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Release|Any CPU.Build.0 = Release|Any CPU
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Release|x64.ActiveCfg = Release|Any CPU
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Release|x64.Build.0 = Release|Any CPU
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Release|x86.ActiveCfg = Release|Any CPU
{5EEB1626-0C27-4FC9-9EFB-6F55E6E08FCE}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
18 changes: 0 additions & 18 deletions prototype_pattern/prototype_test/UnitTest1.cs

This file was deleted.

71 changes: 71 additions & 0 deletions prototype_pattern/prototype_test/prototype_test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using NUnit.Framework;
using FluentAssertions;
using Moq;

namespace Billing.Tests
{
public class Tests
{
private BasicCustomer _basicCustomer;
[SetUp]
public void Setup()
{
_basicCustomer = new Customer();
_basicCustomer.FirstName = "John";
_basicCustomer.LastName = "Smith";
_basicCustomer.HomeAddress = new Address
{
StreetAddress = "1 Kent Street",
City = "Sydney",
State = "New South Wales",
Country = "Australia",
PostCode = "2000"
};
_basicCustomer.BillingAddress = new Address
{
StreetAddress = _basicCustomer.HomeAddress.StreetAddress,
City = _basicCustomer.HomeAddress.City,
State = _basicCustomer.HomeAddress.State,
Country = _basicCustomer.HomeAddress.Country,
PostCode = _basicCustomer.HomeAddress.PostCode
};
}

[Test]
public void NewCustomer_ShallowCloneFromCustomerWithSameAddress_AddressObjectsAreShared()
{
// Arrange
var customer = (Customer)_basicCustomer.Clone();

// Act
var newStreetAddress = "26 York Street";
var relatedCustomer = (Customer)customer.Clone();
relatedCustomer.FirstName = "Jane";
relatedCustomer.HomeAddress.StreetAddress = newStreetAddress;

// Assert
customer.HomeAddress.Should().Be(relatedCustomer.HomeAddress);

}
[Test]
public void NewCustomer_DeepCloneFromCustomerWithDifferentAddressSameFirstName_AddressObjectsAreDifferent()
{
// Arrange
var customer = (Customer)_basicCustomer.Clone();

// Act
var diffCustomer = customer.DeepClone();
diffCustomer.FirstName = "Peter";
diffCustomer.LastName = "Wick";
diffCustomer.HomeAddress.StreetAddress = "57 Hassall Ridge";
diffCustomer.HomeAddress.City = "Lexington";
diffCustomer.HomeAddress.State = "Kentucky";
diffCustomer.HomeAddress.Country = "United States Of America";
diffCustomer.HomeAddress.PostCode = "40511";

// Assert
customer.HomeAddress.Should().NotBeEquivalentTo(diffCustomer.HomeAddress);
customer.FirstName.Should().BeEquivalentTo("John");
}
}
}
2 changes: 2 additions & 0 deletions prototype_pattern/prototype_test/prototype_test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.0" />
<PackageReference Include="Moq" Version="4.13.1" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
Expand Down

0 comments on commit 4f798a4

Please sign in to comment.