io.github.testra.java.reporters.cucumber3.Mappers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of testra-java-cucumber3-reporter Show documentation
Show all versions of testra-java-cucumber3-reporter Show documentation
testra-java-cucumber3-reporter for Testra
The newest version!
package io.github.testra.java.reporters.cucumber3;
import cucumber.api.PickleStepTestStep;
import gherkin.ast.DataTable;
import gherkin.ast.Step;
import gherkin.ast.TableCell;
import gherkin.ast.TableRow;
import gherkin.pickles.PickleCell;
import gherkin.pickles.PickleRow;
import gherkin.pickles.PickleTable;
import io.github.testra.java.client.model.DataTableCell;
import io.github.testra.java.client.model.DataTableRow;
import io.github.testra.java.client.model.TestStep;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public final class Mappers {
private Mappers() {
}
public static TestStep mapToBackgroundTestStep(Step step, int index) {
TestStep testStep = new TestStep().index(index)
.text(step.getKeyword() + step.getText());
if (step.getArgument() != null && step.getArgument() instanceof DataTable) {
List tableRows = ((DataTable) step.getArgument()).getRows();
IntStream.range(0, tableRows.size())
.mapToObj(i -> mapToDataTableRow(tableRows.get(i), i))
.collect(Collectors.toList())
.forEach(testStep::addDataTableRowsItem);
}
return testStep;
}
public static DataTableRow mapToDataTableRow(TableRow tableRow, int index) {
List tableCells = tableRow.getCells();
List dataTableCells =
IntStream.range(0, tableCells.size())
.mapToObj(i -> mapToDataTableCell(tableCells.get(i), i))
.collect(Collectors.toList());
return new DataTableRow().index(index)
.cells(dataTableCells);
}
public static DataTableCell mapToDataTableCell(TableCell tableCell, int i) {
return new DataTableCell().index(i)
.value(tableCell.getValue());
}
public static TestStep mapToTestStep(PickleStepTestStep PickleTestStep, String keyword,
int index) {
TestStep testStep = new TestStep().index(index)
.text(keyword + PickleTestStep.getStepText());
if (PickleTestStep.getStepArgument()
.size() > 0) {
List pickleRows = ((PickleTable) PickleTestStep.getStepArgument()
.get(0)).getRows();
IntStream.range(0, pickleRows.size())
.mapToObj(i -> mapToDataTableRow(pickleRows.get(i), i))
.collect(Collectors.toList())
.forEach(testStep::addDataTableRowsItem);
}
return testStep;
}
public static DataTableRow mapToDataTableRow(PickleRow pickleRow, int index) {
List pickleCells = pickleRow.getCells();
List dataTableCells =
IntStream.range(0, pickleCells.size())
.mapToObj(i -> mapToDataTableCell(pickleCells.get(i), i))
.collect(Collectors.toList());
return new DataTableRow().index(index)
.cells(dataTableCells);
}
public static DataTableCell mapToDataTableCell(PickleCell pickleCell, int i) {
return new DataTableCell().index(i)
.value(pickleCell.getValue());
}
public static List mapToObjectList(List list, Function fn) {
return list.stream()
.map(fn::apply)
.collect(Collectors.toList());
}
public static List mapToObjectList(List list, Predicate predicate,
Function fn) {
return list.stream()
.filter(predicate)
.map(fn::apply)
.collect(Collectors.toList());
}
}