-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from greghaskins/aftereach-always-runs
Bugfix: make sure afterEach always runs
- Loading branch information
Showing
4 changed files
with
176 additions
and
7 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/main/java/com/greghaskins/spectrum/AfterEachBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.greghaskins.spectrum; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.greghaskins.spectrum.Spectrum.Block; | ||
|
||
class AfterEachBlock implements Block { | ||
|
||
private final List<Block> blocks; | ||
|
||
public AfterEachBlock() { | ||
this.blocks = new ArrayList<Block>(); | ||
} | ||
|
||
@Override | ||
public void run() throws Throwable { | ||
runAllBlocksInReverseOrder(this.blocks.size() - 1); | ||
} | ||
|
||
private void runAllBlocksInReverseOrder(final int index) throws Throwable { | ||
if (index < 0) { | ||
return; | ||
} | ||
try { | ||
this.blocks.get(index).run(); | ||
} finally { | ||
runAllBlocksInReverseOrder(index - 1); | ||
} | ||
} | ||
|
||
public void addBlock(final Block block) { | ||
this.blocks.add(block); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/com/greghaskins/spectrum/TryFinallyBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.greghaskins.spectrum; | ||
|
||
import com.greghaskins.spectrum.Spectrum.Block; | ||
|
||
class TryFinallyBlock implements Block { | ||
|
||
private final Block tryBlock; | ||
private final Block finallyBlock; | ||
|
||
public TryFinallyBlock(final Block tryBlock, final Block finallyBlock) { | ||
this.tryBlock = tryBlock; | ||
this.finallyBlock = finallyBlock; | ||
} | ||
|
||
@Override | ||
public void run() throws Throwable { | ||
try { | ||
tryBlock.run(); | ||
} finally { | ||
finallyBlock.run(); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters