Skip to content

Instantly share code, notes, and snippets.

@elefevre
elefevre / TestWithUnderscores.java
Last active August 29, 2015 14:24
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)));
// ...
}
public class ConnectorExecutionCleanerTest {
private static final Connector CONNECTOR = new Connector("type", "name");
private LocalWorkStore mockLocalWorkStore = mock(LocalWorkStore.class);
private LocalDataStore mockLocalDataStore = mock(LocalDataStore.class);
private Statistics mockStatistics = mock(Statistics.class);
private SystemProcessStatus mockSystemProcessStatus = mock(SystemProcessStatus.class);
private ConnectorExecutionCleaner cleaner = new ConnectorExecutionCleaner(mockLocalWorkStore, mockLocalDataStore, mockStatistics, mockSystemProcessStatus);
@Test
@elefevre
elefevre / ContentParserTest.java
Created April 25, 2012 07:27
Multiple asserts in the same test methods are very useful
@Test
public void can_find_simple_hashtags() {
assertThat(parser.hashTags("#hashtag")).containsOnly("#hashtag");
assertThat(parser.hashTags("#HASHTAG")).containsOnly("#HASHTAG");
assertThat(parser.hashTags("#123hashtag")).containsOnly("#123hashtag");
assertThat(parser.hashTags("#hash123tag")).containsOnly("#hash123tag");
assertThat(parser.hashTags("#hashtag123")).containsOnly("#hashtag123");
}
package com.polyspot.connector.filesystem;
import java.util.List;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemManager;
import com.google.common.collect.Lists;
import com.polyspot.connector.framework.Connector;
import com.polyspot.connector.framework.ConnectorConfig;
@elefevre
elefevre / SleepSort.java
Created June 16, 2011 10:26
My attempt at SleepSort in Java. See http://dis.4chan.org/read/prog/1295544154 for original implementation in bash.
public class SleepSort {
private static Thread f(final int value) {
return new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(value);
} catch (InterruptedException e) {
}
System.out.println(value);
@elefevre
elefevre / ExampleWithExpliciteNamingTest
Created May 5, 2011 12:24
Show how variables with names that seem redundant with their values can be very readable
public class ExampleWithExpliciteNamingTest {
private static final DateTime JANUARY_2 = utc(2000, 1, 2);
private static final DateTime JANUARY_3 = utc(2000, 1, 3);
private static final DateTime JANUARY_4 = utc(2000, 1, 4);
@Test
public void canGetRatioComparedToThePastNthDay() {
assertThat(evaluator.evaluateVariationsWithPastNthDay(dailyPerformances(JANUARY_4, 10000.0, 0.0, JANUARY_3, 10000.0, 0.0, JANUARY_2, 1000.0, 0.0), 2, null).getValue()).isEqualTo(9.0);
assertThat(evaluator.evaluateVariationsWithPastNthDay(dailyPerformances(JANUARY_3, 10000.0, 0.0, JANUARY_2, 1000.0, 0.0), 1, null).getValue()).isEqualTo(9.0);
@elefevre
elefevre / gist:667806
Created November 8, 2010 15:24
A real test rewritten with Narrative
/**
* Our original test actually looks a bit better than this (more
* factorization, etc.), but I wanted to make a direct comparison with the
* new test.
*/
@Test
public void canLoadChartsForDefaultSymbolAndCurrencyAndBarType_WITHOUT_NARRATIVE() throws IOException {
putInBacktestStore(getBackTestStore(), "1000", bars(Daily, CAC_BAR_CONTENT), bars(Hourly, FTSE_BAR_CONTENT));
logInThenBeginAt(QUANT, QUANT_PASSWORD, "/users/backtestResults.html?backTestId=1000");
@elefevre
elefevre / gist:665399
Created November 6, 2010 13:00
Sample conversion of a basic JUnit test to one using Narrative
// our existing test
@Test
public void shouldLinkToStrategySourceCodeWithLinkBackToPaperSelectedPage() throws IOException {
getBackTestStore().storeStrategy(QUANT, ENDED_SOLUTION_ID, ZipUtils.zip("import com.algodeal.marketrunner.strategies.*;\n @Instruments(futures=com.algodeal.marketData.instruments.Futures.CAC_40)\npublic class MyStrategy extends AbstractStrategy {}", "MyStrategy.java"));
beginAtAsQuant("/users/paperTradeSolution.do?solutionId=" + ENDED_SOLUTION_ID + "&paperTrading=true"); // clicks on link executing javascript
beginAtAsQuant("/users/backtestHistory.html");
clickLinkWithText("Show only", 1);
assertThat(getSolutionHtml(ENDED_SOLUTION_ID)).contains("<a href=\"/users/editStrategy.html?solutionId=" + ENDED_SOLUTION_ID + "&backUrl=/users/backtestHistory.html%3Fpage=1%26filter=PAPER\">");