Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tomjadams/3574 to your computer and use it in GitHub Desktop.
Save tomjadams/3574 to your computer and use it in GitHub Desktop.
Finding a specification result using Either
package com.googlecode.instinct.internal.runner;
import static com.googlecode.instinct.expect.Expect.expect;
import com.googlecode.instinct.integrate.junit4.InstinctRunner;
import com.googlecode.instinct.marker.annotate.BeforeSpecification;
import com.googlecode.instinct.marker.annotate.Dummy;
import com.googlecode.instinct.marker.annotate.Specification;
import com.googlecode.instinct.marker.annotate.Stub;
import com.googlecode.instinct.marker.annotate.Subject;
import fj.Unit;
import static fj.Unit.unit;
import fj.data.Either;
import static fj.data.Either.left;
import static fj.data.Either.right;
import fj.data.List;
import org.junit.runner.RunWith;
@RunWith(InstinctRunner.class)
public final class ASpecificationLifecycleResultDeterminatorWithAMockFailure {
@Subject private SpecificationLifecycleResultDeterminator determinator;
@Stub(auto = false) Either<Throwable, Unit> createContextResult;
@Stub(auto = false) Either<Throwable, Unit> restMockeryResult;
@Stub(auto = false) Either<Throwable, Unit> wireActorsResult;
@Stub(auto = false) Either<Throwable, Unit> runBeforeSpecificationMethodsResult;
@Stub(auto = false) Either<Throwable, SpecificationResult> runSpecificationResult;
@Stub(auto = false) Either<Throwable, Unit> runAfterSpecificationMethodsResult;
@Stub(auto = false) Either<Throwable, Unit> verifyMocksResult;
@Dummy SpecificationResult specificationResult;
@Stub private Throwable throwable;
@BeforeSpecification
public void before() {
createContextResult = right(unit());
restMockeryResult = right(unit());
wireActorsResult = right(unit());
runBeforeSpecificationMethodsResult = right(unit());
runSpecificationResult = right(specificationResult);
runAfterSpecificationMethodsResult = right(unit());
verifyMocksResult = left(throwable);
determinator = new SpecificationLifecycleResultDeterminator();
}
@Specification
public void returnsTheMockFailureOnTheLeft() {
final List<Either<Throwable, ?>> results =
List.<Either<Throwable, ?>>nil().cons(createContextResult).cons(restMockeryResult).cons(wireActorsResult)
.cons(runBeforeSpecificationMethodsResult).cons(runSpecificationResult).cons(runAfterSpecificationMethodsResult)
.cons(verifyMocksResult);
final Either<List<Throwable>, SpecificationResult> result = determinator.determineLifecycleResult(runSpecificationResult, results);
expect.that(result).isLeft(List.<Throwable>nil().cons(throwable));
}
}
package com.googlecode.instinct.internal.runner;
import fj.data.Either;
import static fj.data.Either.lefts;
import fj.data.List;
@SuppressWarnings({"ThrowableResultOfMethodCallIgnored"})
public final class SpecificationLifecycleResultDeterminator {
public <T extends Throwable> Either<List<T>, SpecificationResult> determineLifecycleResult(final Either<T, SpecificationResult> runSpecificationResult,
final List<Either<T, ?>> results) {
final List<T> errors = lefts(results);
return errors.isNotEmpty() ? Either.<List<T>, SpecificationResult>left(errors)
: Either.<List<T>, SpecificationResult>right(runSpecificationResult.right().value());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment