Skip to content
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

Merged
merged 19 commits into from
Dec 6, 2016
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/main/java/com/greghaskins/spectrum/AbstractSupplyingHook.java
@@ -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();
Copy link
Contributor Author

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.

Copy link
Contributor Author

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!


return super.get();
}

private void clear() {
set(null);
}
}
42 changes: 0 additions & 42 deletions src/main/java/com/greghaskins/spectrum/Child.java

This file was deleted.

46 changes: 46 additions & 0 deletions src/main/java/com/greghaskins/spectrum/CompositeTest.java
@@ -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);
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/greghaskins/spectrum/Configuration.java
@@ -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.
*/
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/greghaskins/spectrum/Hook.java
@@ -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> {
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really want to modify aroundXXX to use this rather than ThrowingConsumer - we probably could, but for now I've left an adapter in place.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We haven't done a release with ThrowingConsumer, so I wouldn't be too worried about protecting that API.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing Consumer can die then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ThrowingSupplier on the other hand, was the item in question and I've removed it.. then restored it.

26 changes: 0 additions & 26 deletions src/main/java/com/greghaskins/spectrum/NotifyingBlock.java

This file was deleted.

18 changes: 0 additions & 18 deletions src/main/java/com/greghaskins/spectrum/Parent.java

This file was deleted.

66 changes: 0 additions & 66 deletions src/main/java/com/greghaskins/spectrum/PreConditionBlock.java

This file was deleted.

25 changes: 0 additions & 25 deletions src/main/java/com/greghaskins/spectrum/SetupBlock.java

This file was deleted.

9 changes: 9 additions & 0 deletions src/main/java/com/greghaskins/spectrum/Spec.java
@@ -1,5 +1,9 @@
package com.greghaskins.spectrum;

import com.greghaskins.spectrum.internal.Child;
import com.greghaskins.spectrum.internal.NotifyingBlock;
import com.greghaskins.spectrum.internal.Parent;

import org.junit.runner.Description;
import org.junit.runner.notification.RunNotifier;

Expand Down Expand Up @@ -52,4 +56,9 @@ public void focus() {
public void ignore() {
this.ignored = true;
}

@Override
public boolean isAtomic() {
return true;
}
}