Created
July 4, 2010 02:01
-
-
Save alexandreaquiles/463019 to your computer and use it in GitHub Desktop.
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
@Test | |
public void shortRegularRental() { | |
Customer customer = createCustomer(); | |
addRental(customer, "Groundhog Day", Movie.REGULAR, 2); | |
String expected = "Rental record for Luke\n"; | |
expected += "\tGroundhog Day\t2.0\n"; | |
expected += "Amount owed is 2.0\n"; | |
expected += "You earned 1 frequent renter points"; | |
Assert.assertEquals(expected, customer.statement()); | |
} | |
@Test | |
public void variousRentals() { | |
Customer customer = createCustomer(); | |
addRental(customer, "Groundhog Day", Movie.REGULAR, 3); | |
addRental(customer, "X-Men X", Movie.NEW_RELEASE, 2); | |
addRental(customer, "Toy Story", Movie.CHILDRENS, 4); | |
String expected = "Rental record for Luke\n"; | |
expected += "\tGroundhog Day\t3.5\n"; | |
expected += "\tX-Men X\t6.0\n"; | |
expected += "\tToy Story\t3.0\n"; | |
expected += "Amount owed is 12.5\n"; | |
expected += "You earned 4 frequent renter points"; | |
Assert.assertEquals(expected, customer.statement()); | |
} |
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
@Test | |
public void shortRegularRental() { | |
Customer customer = createCustomer(); | |
addRental(customer, "Groundhog Day", Movie.REGULAR, 2); | |
String expected = | |
new StatementConfig() | |
.addPartialAmount("Groundhog Day", 2.0) | |
.addTotalAmount(2.0) | |
.addFrequentRenterPoints(1) | |
.expectedStatement(); | |
Assert.assertEquals(expected, customer.statement()); | |
} | |
@Test | |
public void variousRentals() { | |
Customer customer = createCustomer(); | |
addRental(customer, "Groundhog Day", Movie.REGULAR, 3); | |
addRental(customer, "X-Men X", Movie.NEW_RELEASE, 2); | |
addRental(customer, "Toy Story", Movie.CHILDRENS, 4); | |
String expected = | |
new StatementConfig() | |
.addPartialAmount("Groundhog Day", 3.5) | |
.addPartialAmount("X-Men X", 6.0) | |
.addPartialAmount("Toy Story", 3.0) | |
.addTotalAmount(12.5) | |
.addFrequentRenterPoints(4) | |
.expectedStatement(); | |
Assert.assertEquals(expected, customer.statement()); | |
} |
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 StatementConfig { | |
private Map<String, Double> partialAmountsByMovieName; | |
private Double totalAmount; | |
private Integer frequentRenterPoints; | |
public StatementConfig() { | |
this.partialAmountsByMovieName = new LinkedHashMap<String, Double>(); | |
} | |
public StatementConfig addPartialAmount(String movieName, Double partialAmount) { | |
partialAmountsByMovieName.put(movieName, partialAmount); | |
return this; | |
} | |
public StatementConfig addTotalAmount(Double totalAmount) { | |
this.totalAmount = totalAmount; | |
return this; | |
} | |
public StatementConfig addFrequentRenterPoints(Integer frequentRenterPoints) { | |
this.frequentRenterPoints = frequentRenterPoints; | |
return this; | |
} | |
public String expectedStatement() { | |
String format = "Rental record for Luke\n"; | |
for (int i= 0; i < partialAmountsByMovieName.size(); i++) { | |
format += "\t%s\t%.1f\n"; | |
} | |
format += "Amount owed is %.1f\n"; | |
format += "You earned %d frequent renter points"; | |
Formatter formatter = new Formatter(); | |
formatter.format(Locale.US, format, getParameters()); | |
return formatter.toString(); | |
} | |
private Object[] getParameters() { | |
Object[] parameters = new Object[2*partialAmountsByMovieName.size()+2]; | |
int i = -1; | |
for(String movieName : partialAmountsByMovieName.keySet()) { | |
parameters[++i] = movieName; | |
parameters[++i] = partialAmountsByMovieName.get(movieName); | |
} | |
parameters[++i] = totalAmount; | |
parameters[++i] = frequentRenterPoints; | |
return parameters; | |
} | |
} |
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
@Test | |
public void expectedStatementTest() { | |
String result = new StatementConfig() | |
.addPartialAmount("Groundhog Day", 3.0) | |
.addTotalAmount(3.0) | |
.addFrequentRenterPoints(1) | |
.expectedStatement(); | |
String expected = "Rental record for Luke\n"; | |
expected += "\tGroundhog Day\t3.0\n"; | |
expected += "Amount owed is 3.0\n"; | |
expected += "You earned 1 frequent renter points"; | |
Assert.assertEquals(expected, result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To simplify and remove duplication in my test, I had to insert complexity and indirection. Was it worthwhile?