testasyouthink.GivenWhenSteps Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of test-as-you-think-core Show documentation
Show all versions of test-as-you-think-core Show documentation
The TestAsYouThink Core is a DSL style fluent API in Java to promote good coding practices in tests.
/*-
* #%L
* Test As You Think
* %%
* Copyright (C) 2017 Xavier Pigeon and TestAsYouThink contributors
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
package testasyouthink;
import testasyouthink.GivenWhenThenDsl.PreparationStage.AndGiven;
import testasyouthink.GivenWhenThenDsl.PreparationStage.AndGivenArgument;
import testasyouthink.GivenWhenThenDsl.PreparationStage.Given;
import testasyouthink.GivenWhenThenDsl.VerificationStage.Then;
import testasyouthink.GivenWhenThenDsl.VerificationStage.ThenFailure;
import testasyouthink.GivenWhenThenDsl.VerificationStage.ThenWithoutResult;
import testasyouthink.function.CheckedConsumer;
import testasyouthink.function.CheckedFunction;
import testasyouthink.function.Functions;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class GivenWhenSteps<$SystemUnderTest> implements Given<$SystemUnderTest>, AndGiven<$SystemUnderTest> {
private final Functions functions = Functions.INSTANCE;
private final Preparation<$SystemUnderTest> preparation;
private final ThenStepFactory thenStepFactory = ThenStepFactory.INSTANCE;
GivenWhenSteps($SystemUnderTest systemUnderTest) {
preparation = new Preparation<>(systemUnderTest);
}
@Override
public AndGiven<$SystemUnderTest> given(Runnable givenStep) {
preparation.recordGivenStep(givenStep);
return this;
}
@Override
public AndGiven<$SystemUnderTest> given(Consumer<$SystemUnderTest> givenStep) {
preparation.recordGivenStep(givenStep);
return this;
}
@Override
public AndGiven<$SystemUnderTest> given(String fixtureSpecification, Runnable givenStep) {
preparation.recordGivenStep(givenStep);
return this;
}
@Override
public AndGiven<$SystemUnderTest> given(String fixtureSpecification, Consumer<$SystemUnderTest> givenStep) {
preparation.recordGivenStep(givenStep);
return this;
}
@Override
public AndGiven<$SystemUnderTest> and(String fixtureSpecification, Runnable givenStep) {
return given(fixtureSpecification, givenStep);
}
@Override
public AndGiven<$SystemUnderTest> and(String fixtureSpecification, Consumer<$SystemUnderTest> givenStep) {
return given(fixtureSpecification, givenStep);
}
@Override
public <$Argument> AndGivenArgument<$SystemUnderTest, $Argument> givenArgument(Supplier<$Argument> givenStep) {
preparation.recordGivenStep(givenStep);
return new GivenArgumentWhenSteps<>(preparation);
}
@Override
public <$Argument> AndGivenArgument<$SystemUnderTest, $Argument> givenArgument(String description,
Supplier<$Argument> givenStep) {
return givenArgument(givenStep);
}
@Override
public <$Argument> AndGivenArgument<$SystemUnderTest, $Argument> givenArgument(String description,
$Argument argument) {
preparation.recordGivenStep(functions.toSupplier(argument));
return new GivenArgumentWhenSteps<>(preparation);
}
@Override
public <$Result> Then<$SystemUnderTest, $Result> when(CheckedFunction<$SystemUnderTest, $Result> whenStep) {
return thenStepFactory.createThenStep(preparation, whenStep);
}
@Override
public ThenWithoutResult<$SystemUnderTest> when(CheckedConsumer<$SystemUnderTest> whenStep) {
return thenStepFactory.createThenStep(preparation, whenStep);
}
@Override
public ThenFailure whenSutRunsOutsideOperatingConditions(CheckedConsumer<$SystemUnderTest> whenStep) {
return thenStepFactory.createThenStep(preparation, functions.toFunctionWithThrowableAsResult(whenStep));
}
}