-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete prototype with unit testing.
- Loading branch information
Showing
6 changed files
with
148 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters