Skip to content

Commit

Permalink
. td First step of Savers docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottBob committed Sep 12, 2024
1 parent d32887c commit e30d7c1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,15 @@ private DataBase initializeDatabase()
return null;
}
}
class Step4
{

class Step4 {
// begin-snippet: step4
public void sendOutSeniorDiscounts(DataBase database, MailServer mailServer)
{
Loader<List<Customer>> seniorCustomerLoader = database::getSeniorCustomers;
sendOutSeniorDiscounts(mailServer, seniorCustomerLoader);
}

public void sendOutSeniorDiscounts(MailServer mailServer, Loader<List<Customer>> seniorCustomerLoader)
{
List<Customer> seniorCustomers = seniorCustomerLoader.load();
Expand All @@ -165,24 +166,44 @@ public void sendOutSeniorDiscounts(MailServer mailServer, Loader<List<Customer>>
}
// end-snippet
}
class Step4_b
{

class Step4_a {
// begin-snippet: step4
public void sendOutSeniorDiscounts(DataBase database, MailServer mailServer)
{
Loader<List<Customer>> seniorCustomerLoader = database::getSeniorCustomers;
sendOutSeniorDiscounts(mailServer, seniorCustomerLoader); // *
}

public void sendOutSeniorDiscounts(MailServer mailServer, Loader<List<Customer>> seniorCustomerLoader) // *
{
List<Customer> seniorCustomers = seniorCustomerLoader.load();
for (Customer customer : seniorCustomers)
{
Discount seniorDiscount = getSeniorDiscount();
String message = generateDiscountMessage(customer, seniorDiscount);
mailServer.sendMessage(customer, message); // -
}
}
// end-snippet
}
class Step4_b {
// begin-snippet: step4_b
public void sendOutSeniorDiscounts(DataBase database, MailServer mailServer)
{
Loader<List<Customer>> seniorCustomerLoader = database::getSeniorCustomers;
Saver<Tuple<Customer, String>> mailSaver = Saver2.create(mailServer::sendMessage);
sendOutSeniorDiscounts(mailSaver, seniorCustomerLoader);
Saver<Tuple<Customer, String>> mailSaver = Saver2.create(mailServer::sendMessage); // +
sendOutSeniorDiscounts(mailSaver, seniorCustomerLoader); // *
}
public void sendOutSeniorDiscounts(Saver<Tuple<Customer, String>> mailSaver,
Loader<List<Customer>> seniorCustomerLoader)

public void sendOutSeniorDiscounts(Saver<Tuple<Customer, String>> mailSaver, Loader<List<Customer>> seniorCustomerLoader) // *
{
List<Customer> seniorCustomers = seniorCustomerLoader.load();
for (Customer customer : seniorCustomers)
{
Discount seniorDiscount = getSeniorDiscount();
String message = generateDiscountMessage(customer, seniorDiscount);
mailSaver.save(new Tuple(customer, message));
mailSaver.save(new Tuple(customer, message)); // +
}
}
// end-snippet
Expand Down Expand Up @@ -228,4 +249,5 @@ public String toString()
private class Discount
{
}

}
59 changes: 41 additions & 18 deletions approvaltests-util/docs/how_to/LoadersAndSavers.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,29 +233,52 @@ public void sendOutSeniorDiscounts(MailServer mailServer, Loader<List<Customer>>
}
}
```
Here we're sending out an email message. But we don't really care if it gets sent, we just want to make sure it contains the information we expect. Replacing the MailServer object with a Saver is very similar to the process of introducing a Loader.
Here we're sending out an email message.
But we don't really care if it gets sent, we just want to make sure it contains the information we expect.
Replacing the MailServer object with a Saver is very similar to the process of introducing a Loader.

Step 1: Determine the function that we call to save (or in this case, send) the data.
<!-- Snippet Compare: step4_a, step4_b -->
<pre style="color: gray">
public void sendOutSeniorDiscounts(DataBase database, MailServer mailServer)
{
Loader&lt;List&lt;Customer>> seniorCustomerLoader = database::getSeniorCustomers;
sendOutSeniorDiscounts(<b style="color: red">mailServer,</b> seniorCustomerLoader);
}

public void sendOutSeniorDiscounts(MailServer mailServer, Loader&lt;List<Customer&gt;> seniorCustomerLoader) {

List seniorCustomers = seniorCustomerLoader.load();

for (Customer customer : seniorCustomers) {

Discount seniorDiscount = getSeniorDiscount();

String message = generateDiscountMessage(customer, seniorDiscount);

**mailServer.sendMessage(new Email(customer, message));**

**mailSaver.save(new Email(customer, message));**

}
public void sendOutSeniorDiscounts(<b style="color: red">MailServer</b> <b style="color: red">mailServer,</b> Loader&lt;List&lt;Customer>> seniorCustomerLoader)
{
List&lt;Customer> seniorCustomers = seniorCustomerLoader.load();
for (Customer customer : seniorCustomers)
{
Discount seniorDiscount = getSeniorDiscount();
String message = generateDiscountMessage(customer, seniorDiscount);
<b style="color: red"> mailServer.sendMessage(customer, message); </b>
}
}
</pre>
#
<pre style="color: gray">
public void sendOutSeniorDiscounts(DataBase database, MailServer mailServer)
{
Loader&lt;List&lt;Customer>> seniorCustomerLoader = database::getSeniorCustomers;
<b style="color: green"> Saver&lt;Tuple&lt;Customer, String>> mailSaver = Saver2.create(mailServer::sendMessage); </b>
sendOutSeniorDiscounts(<s style="color: red">mailServer,</s> <b style="color: green">mailSaver,</b> seniorCustomerLoader);
}

Record Email(Customer customer, String message) {}
public void sendOutSeniorDiscounts(<s style="color: red">MailServer</s> <s style="color: red">mailServer,</s> <b style="color: green">Saver&lt;Tuple&lt;Customer, </b><b style="color: green">String>></b> <b style="color: green">mailSaver,</b> Loader&lt;List&lt;Customer>> seniorCustomerLoader)
{
List&lt;Customer> seniorCustomers = seniorCustomerLoader.load();
for (Customer customer : seniorCustomers)
{
Discount seniorDiscount = getSeniorDiscount();
String message = generateDiscountMessage(customer, seniorDiscount);
<s style="color: red"> mailServer.sendMessage(customer, message); </s>
<b style="color: green"> mailSaver.save(new Tuple(customer, message)); </b>
}
}
</pre>

}

Step 2: Determine the data we want to test for its saved state. We start with a test. Unfortunately, since the MailServer is a “write-only” object, it’s hard to determine what’s being sent to it. The Saver gives us the ability to easily test for this state now.:

Expand Down

0 comments on commit e30d7c1

Please sign in to comment.