-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Grand unified theory of hooks #77
Changes from 15 commits
d947193
b8cf10d
105a04b
0bf8a53
6b75425
a08d0ac
55765e2
cd7d350
9077040
44e1da5
728354a
da0d09c
6e2b117
a8c4248
f4e77f7
7e4cafe
a359a60
5ab06c9
9d6df39
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.greghaskins.spectrum; | ||
|
||
import static com.greghaskins.spectrum.Spectrum.assertSpectrumInTestMode; | ||
|
||
/** | ||
* A base class for supplying hooks to use. Override before or after. Return the singleton | ||
* value from the before method. | ||
* You can use this to write any plugin which needs to make a value visible to the specs. | ||
* This is not the only way to achieve that - you can also build from {@link SupplyingHook} | ||
* but this captures the template for a complex hook. | ||
*/ | ||
public abstract class AbstractSupplyingHook<T> extends Variable<T> implements SupplyingHook<T> { | ||
/** | ||
* Override this to supply behaviour for before the block is run. | ||
* @return the value that the singleton will store to supply | ||
*/ | ||
protected abstract T before(); | ||
|
||
/** | ||
* Override this to supply behaviour for after the block is run. | ||
*/ | ||
protected void after() {} | ||
|
||
/** | ||
* Template method for a hook which supplies. | ||
* @param block the inner block that will be run | ||
* @throws Throwable on error | ||
*/ | ||
@Override | ||
public void acceptOrThrow(Block block) throws Throwable { | ||
try { | ||
set(before()); | ||
block.run(); | ||
} finally { | ||
try { | ||
after(); | ||
} finally { | ||
clear(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public T get() { | ||
assertSpectrumInTestMode(); | ||
|
||
return super.get(); | ||
} | ||
|
||
private void clear() { | ||
set(null); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.greghaskins.spectrum; | ||
|
||
import com.greghaskins.spectrum.internal.Child; | ||
import com.greghaskins.spectrum.internal.FailureDetectingRunListener; | ||
import com.greghaskins.spectrum.internal.Parent; | ||
import com.greghaskins.spectrum.model.TaggingFilterCriteria; | ||
|
||
import org.junit.runner.Description; | ||
import org.junit.runner.notification.RunNotifier; | ||
|
||
/** | ||
* Subclass of {@link Suite} that represent the fact that some tests are composed | ||
* of interrelated steps which add up to a single test. | ||
*/ | ||
final class CompositeTest extends Suite { | ||
/** | ||
* Constructs a Composite Test, which is a suite run as an atomic test. | ||
* @param description of the test | ||
* @param parent parent suite | ||
* @param tagging tagging state to inherit from parent | ||
*/ | ||
CompositeTest(final Description description, final Parent parent, | ||
final TaggingFilterCriteria tagging) { | ||
super(description, parent, CompositeTest::abortOnFailureChildRunner, tagging); | ||
} | ||
|
||
@Override | ||
public boolean isAtomic() { | ||
return true; | ||
} | ||
|
||
private static void abortOnFailureChildRunner(final Suite suite, final RunNotifier runNotifier) { | ||
FailureDetectingRunListener listener = new FailureDetectingRunListener(); | ||
runNotifier.addListener(listener); | ||
try { | ||
for (Child child : suite.children) { | ||
if (listener.hasFailedYet()) { | ||
child.ignore(); | ||
} | ||
suite.runChild(child, runNotifier); | ||
} | ||
} finally { | ||
runNotifier.removeListener(listener); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
package com.greghaskins.spectrum; | ||
|
||
/** | ||
* Allows the injection of suite configuration during test definition. A wrapper for the | ||
* suite which exposes configurables. | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checkstyle forced me to write Javadoc as items stopped being internal when they switched packages. |
||
public class Configuration { | ||
|
||
private final Suite suite; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.greghaskins.spectrum; | ||
|
||
/** | ||
* A hook allows you to inject functionality before and/or after a {@link Block}. | ||
* Just implement the {@link ThrowingConsumer#acceptOrThrow(Object)} method and | ||
* call {@link Block#run()} within your implementation. | ||
* If your hook is going to provide an object to the running test, then implement | ||
* {@link SupplyingHook} or subclass {@link AbstractSupplyingHook}. | ||
*/ | ||
public interface Hook extends ThrowingConsumer<Block> { | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really want to modify There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We haven't done a release with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Throwing Consumer can die then. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Borrowed from
let
not a bad thing to have around.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And by borrowed, I mean stolen, given that
let
is now a hook!