Skip to content

Instantly share code, notes, and snippets.

@elefevre
Created November 8, 2010 15:24
Show Gist options
  • Save elefevre/667806 to your computer and use it in GitHub Desktop.
Save elefevre/667806 to your computer and use it in GitHub Desktop.
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");
assertThat(getImageCount("barChart.png.do", //
"backTestId", "1000", //
"currentBarType", "Daily", //
"currentCurrency", "EUR", //
"currentSymbol", "CAC 40", //
"endDate", "2010/03/28 00:00:00.000", //
"paperStartDate", "", //
"paperView", "false", //
"startDate", "2010/01/05 00:00:00.000", //
"username", "quant")) //
.isEqualTo(1);
assertThat(getImageCount("equityChart.png.do", //
"backTestId", "1000", //
"currentBarType", "Daily", //
"currentCurrency", "EUR", //
"currentSymbol", "CAC 40", //
"endDate", "2010/03/28 00:00:00.000", //
"paperStartDate", "", //
"paperView", "false", //
"startDate", "2010/01/05 00:00:00.000", //
"username", "quant")) //
.isEqualTo(1);
}
/**
* The same test using Narrative. The biggest benefit is that it forces me
* to abstract away things in methods. The flip side is that there are lots
* of helper methods & classes. However, I expect to write fewer of them as
* future tests will reuse many of them.
*/
@Test
public void canLoadChartsForDefaultSymbolAndCurrencyAndBarType_WITH_NARRATIVE() {
Given.the(quant).was_able_to(run_a_strategy_that_received(bars(Daily, CAC_BAR_CONTENT), bars(Hourly, FTSE_BAR_CONTENT)));
When.the(quant).attempts_to(open_backtest_results_page());
Then.the(quant).expects_that(the_number_of_barcharts(CAC_40, EUR, Daily)).should_be(equalTo(1));
Then.the(quant).expects_that(the_number_of_equitycharts(CAC_40, EUR, Daily)).should_be(equalTo(1));
}
// below are all the supporting methods & classes required by Narrative
private final Quant quant = new Quant();
private Action<Object, Quant> run_a_strategy_that_received(final String... csvBars) {
return new Action<Object, Quant>() {
@Override
public void performFor(Quant actor) {
try {
putInBacktestStore(getBackTestStore(), actor.getBackTestId(), csvBars);
} catch (Throwable t) {
t.printStackTrace();
}
}
};
}
private Action<Object, Quant> open_backtest_results_page() {
return new Action<Object, Quant>() {
@Override
public void performFor(Quant actor) {
actor.tool().logInThenBeginAt(actor.getUserName(), actor.getPassword(), "/users/backtestResults.html?backTestId=" + actor.getBackTestId());
}
};
}
private Extractor<Integer, Quant> the_number_of_barcharts(final Instrument instrument, final Currencies currency, final BarType barType) {
return the_number_of_images("barChart.png.do", instrument, currency, barType);
}
private Extractor<Integer, Quant> the_number_of_equitycharts(final Instrument instrument, final Currencies currency, final BarType barType) {
return the_number_of_images("equityChart.png.do", instrument, currency, barType);
}
private Extractor<Integer, Quant> the_number_of_images(final String baseImageUrl, final Instrument instrument, final Currencies currency, final BarType barType) {
return new Extractor<Integer, Quant>() {
@Override
public Integer grabFor(Quant actor) {
return getImageCount(baseImageUrl, //
"backTestId", actor.getBackTestId(), //
"currentBarType", barType.name(), //
"currentCurrency", currency.name(), //
"currentSymbol", instrument.getSymbol(), //
"endDate", "2010/03/28 00:00:00.000", //
"paperStartDate", "", //
"paperView", "false", //
"startDate", "2010/01/05 00:00:00.000", //
"username", actor.getUserName());
}
};
}
private class Quant implements Actor<Object, Quant> {
public Quant() {
// public constructor to avoid warnings under Eclipse
}
public String getBackTestId() {
return BACKTEST_ID;
}
@Override
public AbstractBackTestTestFunctional tool() {
// this is there because I shoe-horned Narrative into our existing test support classes
return BackTestResultControllerTestFunctional.this;
}
@Override
public void perform(Action<Object, Quant> action) {
action.performFor(this);
}
@Override
public <DATA> DATA grabUsing(Extractor<DATA, Quant> extractor) {
return extractor.grabFor(this);
}
public String getUserName() {
return QUANT;
}
public String getPassword() {
return QUANT_PASSWORD;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment