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

Bugfix/issue 755 #778

Merged
merged 5 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@
import java.util.List;
import java.util.Set;
import java.util.Stack;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ScenarioModelBuilder implements ScenarioListener {
private static final Logger log = LoggerFactory.getLogger(ScenarioModelBuilder.class);

private static final Set<String> STACK_TRACE_FILTER = ImmutableSet
.of("sun.reflect", "com.tngtech.jgiven.impl.intercept", "com.tngtech.jgiven.impl.intercept",
Expand All @@ -61,11 +58,6 @@ public class ScenarioModelBuilder implements ScenarioListener {
private StepModel currentStep;
private final Stack<StepModel> parentSteps = new Stack<>();

/**
* In case the current step is a step with nested steps, this list contains these steps.
*/
private List<StepModel> nestedSteps;

private final SentenceBuilder sentenceBuilder = new SentenceBuilder();

private long scenarioStartedNanos;
Expand All @@ -79,6 +71,8 @@ public void setReportModel(ReportModel reportModel) {
this.reportModel = reportModel;
}

private Stack<Integer> discrepancyOnLayer = new Stack<>();

@Override
public void scenarioStarted(String description) {
scenarioStartedNanos = System.nanoTime();
Expand All @@ -96,10 +90,30 @@ public void scenarioStarted(String description) {
scenarioModel.addCase(scenarioCaseModel);
scenarioModel.setDescription(readableDescription);
this.tagCreator = new TagCreator(configuration);
discrepancyOnLayer.push(0);
}

public void addStepMethod(Method paramMethod, List<NamedArgument> arguments, InvocationMode mode,
boolean hasNestedSteps) {
@Override
public void scenarioStarted(Class<?> testClass, Method method, List<NamedArgument> namedArguments) {
readConfiguration(testClass);
readAnnotations(testClass, method);
scenarioModel.setClassName(testClass.getName());
setParameterNames(getNames(namedArguments));

// must come at last
setMethodName(method.getName());

ParameterFormattingUtil parameterFormattingUtil = new ParameterFormattingUtil(configuration);
List<ObjectFormatter<?>> formatter =
parameterFormattingUtil.getFormatter(method.getParameterTypes(), getNames(namedArguments),
method.getParameterAnnotations());

setArguments(parameterFormattingUtil.toStringList(formatter, getValues(namedArguments)));
setCaseDescription(testClass, method, namedArguments);
}

private void addStepMethod(Method paramMethod, List<NamedArgument> arguments, InvocationMode mode,
boolean hasNestedSteps) {
StepModel stepModel = createStepModel(paramMethod, arguments, mode);

if (parentSteps.empty()) {
Expand All @@ -110,6 +124,7 @@ public void addStepMethod(Method paramMethod, List<NamedArgument> arguments, Inv

if (hasNestedSteps) {
parentSteps.push(stepModel);
discrepancyOnLayer.push(0);
}
currentStep = stepModel;
}
Expand Down Expand Up @@ -197,16 +212,33 @@ private ScenarioCaseModel getCurrentScenarioCase() {
return scenarioCaseModel;
}

private void incrementDiscrepancy() {
int discrepancyOnCurrentLayer = discrepancyOnLayer.pop();
discrepancyOnCurrentLayer++;
discrepancyOnLayer.push(discrepancyOnCurrentLayer);
}

private void decrementDiscrepancy() {
if (discrepancyOnLayer.peek() > 0) {
int discrepancyOnCurrentLayer = discrepancyOnLayer.pop();
discrepancyOnCurrentLayer--;
discrepancyOnLayer.push(discrepancyOnCurrentLayer);
}
}

@Override
public void stepMethodInvoked(Method method, List<NamedArgument> arguments, InvocationMode mode,
boolean hasNestedSteps) {
if (method.isAnnotationPresent(IntroWord.class)) {
introWordAdded(getDescription(method));
incrementDiscrepancy();
} else if (method.isAnnotationPresent(FillerWord.class)) {
FillerWord fillerWord = method.getAnnotation(FillerWord.class);
addToSentence(getDescription(method), fillerWord.joinToPreviousWord(), fillerWord.joinToNextWord());
incrementDiscrepancy();
} else if (method.isAnnotationPresent(StepComment.class)) {
addStepComment(arguments);
incrementDiscrepancy();
} else {
addTags(method.getAnnotations());
addTags(method.getDeclaringClass().getAnnotations());
Expand Down Expand Up @@ -256,7 +288,7 @@ public void setStatus(ExecutionStatus status) {
scenarioCaseModel.setStatus(status);
}

public void setException(Throwable throwable) {
private void setException(Throwable throwable) {
scenarioCaseModel.setErrorMessage(throwable.getClass().getName() + ": " + throwable.getMessage());
scenarioCaseModel.setStackTrace(getStackTrace(throwable, FILTER_STACK_TRACE));
}
Expand Down Expand Up @@ -293,18 +325,24 @@ public void stepMethodFinished(long durationInNanos, boolean hasNestedSteps) {
}

if (currentStep != null) {
currentStep.setDurationInNanos(durationInNanos);
if (discrepancyOnLayer.empty() || discrepancyOnLayer.peek() == 0) {
currentStep.setDurationInNanos(durationInNanos);
}
if (hasNestedSteps) {
if (currentStep.getStatus() != StepStatus.FAILED) {
currentStep.setStatus(getStatusFromNestedSteps(currentStep.getNestedSteps()));
}
parentSteps.pop();
discrepancyOnLayer.pop();
}
}

if (!hasNestedSteps && !parentSteps.isEmpty()) {
currentStep = parentSteps.peek();
}

decrementDiscrepancy();

}

private StepStatus getStatusFromNestedSteps(List<StepModel> nestedSteps) {
Expand All @@ -330,25 +368,6 @@ public void scenarioFailed(Throwable e) {
setException(e);
}

@Override
public void scenarioStarted(Class<?> testClass, Method method, List<NamedArgument> namedArguments) {
readConfiguration(testClass);
readAnnotations(testClass, method);
scenarioModel.setClassName(testClass.getName());
setParameterNames(getNames(namedArguments));

// must come at last
setMethodName(method.getName());

ParameterFormattingUtil parameterFormattingUtil = new ParameterFormattingUtil(configuration);
List<ObjectFormatter<?>> formatter =
parameterFormattingUtil.getFormatter(method.getParameterTypes(), getNames(namedArguments),
method.getParameterAnnotations());

setArguments(parameterFormattingUtil.toStringList(formatter, getValues(namedArguments)));
setCaseDescription(testClass, method, namedArguments);
}

private void setCaseDescription(Class<?> testClass, Method method, List<NamedArgument> namedArguments) {

CaseAs annotation = null;
Expand Down Expand Up @@ -421,8 +440,6 @@ private void readAnnotations(Class<?> testClass, Method method) {
}
}



@Override
public void scenarioFinished() {
AssertionUtil.assertTrue(scenarioStartedNanos > 0, "Scenario has no start time");
Expand Down Expand Up @@ -473,13 +490,13 @@ public void tagAdded(Class<? extends Annotation> annotationClass, String... valu
}


public void addTags(Annotation... annotations) {
private void addTags(Annotation... annotations) {
for (Annotation annotation : annotations) {
addTags(tagCreator.toTags(annotation));
}
}

public void addTags(List<Tag> tags) {
private void addTags(List<Tag> tags) {
if (tags.isEmpty()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,55 @@
public class PlainTextReporter extends PlainTextWriter {
private static final ConfigValue COLOR_CONFIG = Config.config().textColorEnabled();

public static String toString( ScenarioModel scenarioModel ) throws UnsupportedEncodingException {
public static String toString(ScenarioModel scenarioModel) throws UnsupportedEncodingException {
ReportModel model = new ReportModel();
model.addScenarioModel( scenarioModel );
return toString( model );
model.addScenarioModel(scenarioModel);
return toString(model);
}

public static String toString( ReportModel model ) throws UnsupportedEncodingException {
public static String toString(ReportModel model) throws UnsupportedEncodingException {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter( stringWriter );
PlainTextReporter textWriter = new PlainTextReporter( printWriter, ConfigValue.FALSE );
PrintWriter printWriter = new PrintWriter(stringWriter);
PlainTextReporter textWriter = new PlainTextReporter(printWriter, ConfigValue.FALSE);
try {
textWriter.write( model );
textWriter.write(model);
return stringWriter.toString();
} finally {
ResourceUtil.close( printWriter );
ResourceUtil.close(printWriter);
}
}

public PlainTextReporter() {
this( COLOR_CONFIG );
this(COLOR_CONFIG);
}

public PlainTextReporter( ConfigValue colorConfig ) {
this( PrintWriterUtil.getPrintWriter( System.out, colorConfig ), colorConfig );
public PlainTextReporter(ConfigValue colorConfig) {
this(PrintWriterUtil.getPrintWriter(System.out, colorConfig), colorConfig);
}

public PlainTextReporter( PrintWriter printWriter, ConfigValue colorConfig ) {
super( printWriter, colorConfig != ConfigValue.FALSE );
public PlainTextReporter(PrintWriter printWriter, ConfigValue colorConfig) {
super(printWriter, colorConfig != ConfigValue.FALSE);
}

public PlainTextReporter write( ReportModel model ) {
model.accept( this );
public PlainTextReporter write(ReportModel model) {
model.accept(this);
return this;
}

@Override
public void visit( ReportModel multiScenarioModel ) {
public void visit(ReportModel multiScenarioModel) {
writer.println();
String title = bold( "Test Class: " );
String title = bold("Test Class: ");
title += multiScenarioModel.getClassName();
writer.println( title );
writer.println(title);
}

@Override
public void visit( ScenarioModel scenarioModel ) {
if( scenarioModel.isCasesAsTable() ) {
scenarioModel.accept( new DataTablePlainTextScenarioWriter( writer, withColor ) );
public void visit(ScenarioModel scenarioModel) {
if (scenarioModel.isCasesAsTable()) {
scenarioModel.accept(new DataTablePlainTextScenarioWriter(writer, withColor));
} else {
scenarioModel.accept( new PlainTextScenarioWriter( writer, withColor ) );
scenarioModel.accept(new PlainTextScenarioWriter(writer, withColor));
}
}

Expand Down
Loading