Skip to content

Instantly share code, notes, and snippets.

@elefevre
Last active August 29, 2015 14:24
Show Gist options
  • Save elefevre/2ac1277d5346bb3fd7ed to your computer and use it in GitHub Desktop.
Save elefevre/2ac1277d5346bb3fd7ed to your computer and use it in GitHub Desktop.
Underscores help me force-format my tests
// here is the beginning of a typical test
@Test
public void should_list_the_total_number_of_threads() throws Exception {
User buyer = userRepository.save(user(1L, 0L, BUYER, folder(INBOX), folder(SENT), folder(TRASH), folder(SPAM),
folder(UNWANTED)));
User supplier = userRepository.save(user(2L, 20L, SUPPLIER, folder(INBOX), folder(SENT), folder(TRASH),
folder(SPAM), folder(UNWANTED)));
// ...
}
// I'm bothered by the fact that the lines concerning the 2 users are not perfectly symmetrical,
// as I believe it makes it harder to see the (interesting) differences
// So, here is how I like to rename my variables:
@Test
public void should_list_the_total_number_of_threads() throws Exception {
User buyer___ = userRepository.save(user(1L, 0L, BUYER, folder(INBOX), folder(SENT), folder(TRASH),
folder(SPAM), folder(UNWANTED)));
User supplier = userRepository.save(user(2L, 20L, SUPPLIER, folder(INBOX), folder(SENT), folder(TRASH),
folder(SPAM), folder(UNWANTED)));
// ...
}
// it then becomes obvious that the differences are in the id (1L vs. 2L) and in the type of user (BUYER vs. SUPPLIER)
// This is not perfect. For example, "BUYER" is an enum value which I cannot rename, as it would affect production code.
// This, it seems to me that this is better than the original code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment