Skip to content
This repository has been archived by the owner on Feb 10, 2022. It is now read-only.

Commit

Permalink
Merge pull request #40 from novoda/pop_result
Browse files Browse the repository at this point in the history
Adds `popResult` methods so that we can choose to not cache results
  • Loading branch information
fourlastor committed Dec 23, 2015
2 parents 52c2596 + c2d7bfb commit 31f8737
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 2 deletions.
15 changes: 15 additions & 0 deletions sexp/src/main/java/com/novoda/sexp/finder/BasicElementFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ public T getResultOrThrow() {
return result;
}

@Override
public T popResult() {
try {
return result;
} finally {
result = null;
}
}

@Override
public T popResultOrThrow() {
validateResult();
return popResult();
}

private void validateResult() {
if (result == null) {
throw new ResultNotFoundException();
Expand Down
22 changes: 22 additions & 0 deletions sexp/src/main/java/com/novoda/sexp/finder/ElementFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,29 @@ public interface ElementFinder<R> extends ParseWatcher<R> {
@Override
void onParsed(R item);

/**
* @return the result of the parsing if the parsing was completed and tag was found
* or null if the tag was not found or parsing didn't complete.
*/
R getResult();

/**
* @return the result of the parsing if the parsing was completed and tag was found
* or throws an exception if the tag was not found or parsing didn't complete.
*/
R getResultOrThrow();

/**
* Same as {@link #getResult} but clears the result instead of keeping it.
* @return the result of the parsing if the parsing was completed and tag was found
* or null if the tag was not found or parsing didn't complete.
*/
R popResult();

/**
* Same as {@link #getResultOrThrow} but clears the result instead of keeping it.
* @return the result of the parsing if the parsing was completed and tag was found
* or throws an exception if the tag was not found or parsing didn't complete.
*/
R popResultOrThrow();
}
10 changes: 10 additions & 0 deletions sexp/src/main/java/com/novoda/sexp/finder/ListElementFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,14 @@ public T getResultOrThrow() {
throw new UnsupportedOperationException("Has a listener to pass each item as parsed, so there is no result.");
}

@Override
public T popResult() {
throw new UnsupportedOperationException("Has a listener to pass each item as parsed, so there is no result.");
}

@Override
public T popResultOrThrow() {
throw new UnsupportedOperationException("Has a listener to pass each item as parsed, so there is no result.");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public void create_a_result_when_parsing_finished() throws Exception {

elementCreator.onParsed(result);

assertThat(elementCreator.getResultOrThrow()).isEqualTo(result);
assertThat(elementCreator.getResult()).isEqualTo(result);
}

@Test(expected = BasicElementFinder.ResultNotFoundException.class)
public void throw_exception_when_result_has_not_been_parsed_and_a_required_result_is_asked_for() throws Exception {
public void throw_exception_when_result_has_not_been_parsed_or_found_and_a_required_result_is_asked_for() throws Exception {
elementCreator.getResultOrThrow();
}

Expand All @@ -67,4 +67,36 @@ public void allow_null_results_when_get_result_is_used() throws Exception {
assertThat(result).isNull();
}

@Test
public void pop_a_result_when_parsing_finished() throws Exception {
String result = "result";
elementCreator.onParsed(result);

Object actual = elementCreator.popResult();

assertThat(actual).isEqualTo(result);
}

@Test(expected = BasicElementFinder.ResultNotFoundException.class)
public void throw_exception_when_result_has_not_been_parsed_or_found_and_a_result_is_popped() throws Exception {
elementCreator.popResultOrThrow();
}

@Test
public void allow_null_results_when_get_result_is_popped() throws Exception {
Object result = elementCreator.popResult();

assertThat(result).isNull();
}

@Test
public void not_cache_result_after_a_result_is_popped() throws Exception {
elementCreator.onParsed("ignore");
elementCreator.popResult();

Object actual = elementCreator.popResult();

assertThat(actual).isNull();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,22 @@ public void callWatcher_afterEachItemForTheListIsParsed() throws Exception {

@Test(expected = UnsupportedOperationException.class)
public void notSupportGetResult_asListCreatorWillCallbackAfterEveryListItemIsParsed() throws Exception {
elementCreator.getResult();
}

@Test(expected = UnsupportedOperationException.class)
public void notSupportGetResultOrThrow_asListCreatorWillCallbackAfterEveryListItemIsParsed() throws Exception {
elementCreator.getResultOrThrow();
}

@Test(expected = UnsupportedOperationException.class)
public void notSupportPopResult_asListCreatorWillCallbackAfterEveryListItemIsParsed() throws Exception {
elementCreator.popResult();
}

@Test(expected = UnsupportedOperationException.class)
public void notSupportPopResultOrThrow_asListCreatorWillCallbackAfterEveryListItemIsParsed() throws Exception {
elementCreator.popResultOrThrow();
}

}

0 comments on commit 31f8737

Please sign in to comment.