Created
August 5, 2012 02:44
-
-
Save ferclaverino/3261229 to your computer and use it in GitHub Desktop.
Liskov - step 1
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
class ContactInformationAuctionMessageBuilder : IMailMessageBuilder<ContactInformationAuction> | |
{ | |
private MailMessage mailMessage; | |
private StringBuilder body = new StringBuilder(); | |
public ContactInformationAuctionMessageBuilder() | |
{ | |
mailMessage = new MailMessage(); | |
} | |
public IMailMessageBuilder<ContactInformationAuction> WithTo(string to) | |
{ | |
mailMessage.To.Add(to); | |
return this; | |
} | |
public IMailMessageBuilder<ContactInformationAuction> WithSubject(string subject) | |
{ | |
mailMessage.Subject = subject; | |
return this; | |
} | |
public IMailMessageBuilder<ContactInformationAuction> WithFrom(string from) | |
{ | |
mailMessage.From = new MailAddress(from); | |
return this; | |
} | |
public MailMessage BuildMessage() | |
{ | |
mailMessage.Body = body.ToString(); | |
return mailMessage; | |
} | |
public IMailMessageBuilder<ContactInformationAuction> WithEntity(ContactInformationAuction contact) | |
{ | |
AddBodyLine("Nombre: {0}", contact.FirstName); | |
AddBodyLine("Apellido: {0}", contact.LastName); | |
AddBodyLine("Autor: {0}", contact.Author); | |
AddBodyLine("Dimensiones: {0}", contact.Dimensions); | |
return this; | |
} | |
private void AddBodyLine(String line, params object[] args) | |
{ | |
body.AppendLine(String.Format(line, args)); | |
} | |
} |
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
public class ContactInformationMessageBuilder : IMailMessageBuilder<ContactInformation> | |
{ | |
private MailMessage mailMessage; | |
private StringBuilder body = new StringBuilder(); | |
public ContactInformationMessageBuilder() | |
{ | |
mailMessage = new MailMessage(); | |
} | |
public IMailMessageBuilder<ContactInformation> WithTo(string to) | |
{ | |
mailMessage.To.Add(to); | |
return this; | |
} | |
public IMailMessageBuilder<ContactInformation> WithSubject(string subject) | |
{ | |
mailMessage.Subject = subject; | |
return this; | |
} | |
public IMailMessageBuilder<ContactInformation> WithFrom(string from) | |
{ | |
mailMessage.From = new MailAddress(from); | |
return this; | |
} | |
public MailMessage BuildMessage() | |
{ | |
mailMessage.Body = body.ToString(); | |
return mailMessage; | |
} | |
public IMailMessageBuilder<ContactInformation> WithEntity(ContactInformation contact) | |
{ | |
AddBodyLine("Nombre: {0}", contact.FirstName); | |
AddBodyLine("Apellido: {0}", contact.LastName); | |
return this; | |
} | |
private void AddBodyLine(String line, params object[] args) | |
{ | |
body.AppendLine(String.Format(line, args)); | |
} | |
} |
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
class ContactInformationSubsidiaryMessageBuilder : IMailMessageBuilder<ContactInformationSubsidiary> | |
{ | |
private MailMessage mailMessage; | |
private StringBuilder body = new StringBuilder(); | |
public ContactInformationSubsidiaryMessageBuilder() | |
{ | |
mailMessage = new MailMessage(); | |
} | |
public IMailMessageBuilder<ContactInformationSubsidiary> WithTo(string to) | |
{ | |
mailMessage.To.Add(to); | |
return this; | |
} | |
public IMailMessageBuilder<ContactInformationSubsidiary> WithSubject(string subject) | |
{ | |
mailMessage.Subject = subject; | |
return this; | |
} | |
public IMailMessageBuilder<ContactInformationSubsidiary> WithFrom(string from) | |
{ | |
mailMessage.From = new MailAddress(from); | |
return this; | |
} | |
public MailMessage BuildMessage() | |
{ | |
mailMessage.Body = body.ToString(); | |
return mailMessage; | |
} | |
public IMailMessageBuilder<ContactInformationSubsidiary> WithEntity(ContactInformationSubsidiary contact) | |
{ | |
AddBodyLine("Nombre: {0}", contact.FirstName); | |
AddBodyLine("Apellido: {0}", contact.LastName); | |
AddBodyLine("Sucursal: {0}", contact.Subsidiary); | |
return this; | |
} | |
private void AddBodyLine(String line, params object[] args) | |
{ | |
body.AppendLine(String.Format(line, args)); | |
} | |
} |
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
[TestFixture] | |
class MailBuilderTest | |
{ | |
[Test] | |
public void TestContactInformation() | |
{ | |
// arrange | |
ContactInformation contactInformation = new ContactInformation() | |
{ | |
FirstName = "Homero", | |
LastName = "Simpson" | |
}; | |
IMailMessageBuilder<ContactInformation> mailBuilder = new ContactInformationMessageBuilder(); | |
// act | |
MailMessage message = mailBuilder | |
.WithFrom("[email protected]") | |
.WithTo("[email protected]") | |
.WithSubject("Hola") | |
.WithEntity(contactInformation).BuildMessage(); | |
// assert | |
Assert.That(message.From.Address, Is.EqualTo("[email protected]")); | |
Assert.That(message.To, Has.All.Matches<MailAddress>(x => x.Address == "[email protected]")); | |
Assert.That(message.Subject, Is.EqualTo("Hola")); | |
Assert.That(message.Body, Is.StringContaining("Nombre: Homero")); | |
Assert.That(message.Body, Is.StringContaining("Apellido: Simpson")); | |
} | |
[Test] | |
public void TestContactInformationSubsidiary() | |
{ | |
// arrange | |
ContactInformationSubsidiary contactInformation = new ContactInformationSubsidiary() | |
{ | |
FirstName = "Homero", | |
LastName = "Simpson", | |
Subsidiary = "Retiro" | |
}; | |
IMailMessageBuilder<ContactInformationSubsidiary> mailBuilder = new ContactInformationSubsidiaryMessageBuilder(); | |
// act | |
MailMessage message = mailBuilder | |
.WithFrom("[email protected]") | |
.WithTo("[email protected]") | |
.WithSubject("Hola") | |
.WithEntity(contactInformation).BuildMessage(); | |
// assert | |
Assert.That(message.From.Address, Is.EqualTo("[email protected]")); | |
Assert.That(message.To, Has.All.Matches<MailAddress>(x => x.Address == "[email protected]")); | |
Assert.That(message.Subject, Is.EqualTo("Hola")); | |
Assert.That(message.Body, Is.StringContaining("Nombre: Homero")); | |
Assert.That(message.Body, Is.StringContaining("Apellido: Simpson")); | |
Assert.That(message.Body, Is.StringContaining("Sucursal: Retiro")); | |
} | |
[Test] | |
public void TestContactInformationAuction() | |
{ | |
// arrange | |
ContactInformationAuction contactInformation = new ContactInformationAuction() | |
{ | |
FirstName = "Homero", | |
LastName = "Simpson", | |
Author = "Picasso", | |
Dimensions = "3x3" | |
}; | |
IMailMessageBuilder<ContactInformationAuction> mailBuilder = new ContactInformationAuctionMessageBuilder(); | |
// act | |
MailMessage message = mailBuilder | |
.WithFrom("[email protected]") | |
.WithTo("[email protected]") | |
.WithSubject("Hola") | |
.WithEntity(contactInformation).BuildMessage(); | |
// assert | |
Assert.That(message.From.Address, Is.EqualTo("[email protected]")); | |
Assert.That(message.To, Has.All.Matches<MailAddress>(x => x.Address == "[email protected]")); | |
Assert.That(message.Subject, Is.EqualTo("Hola")); | |
Assert.That(message.Body, Is.StringContaining("Nombre: Homero")); | |
Assert.That(message.Body, Is.StringContaining("Apellido: Simpson")); | |
Assert.That(message.Body, Is.StringContaining("Autor: Picasso")); | |
Assert.That(message.Body, Is.StringContaining("Dimensiones: 3x3")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment