All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.hsac.fitnesse.slim.AllArgScenarioTable Maven / Gradle / Ivy

There is a newer version: 1.32.11
Show newest version
package nl.hsac.fitnesse.slim;

import fitnesse.testsystems.TestExecutionException;
import fitnesse.testsystems.slim.HtmlTable;
import fitnesse.testsystems.slim.SlimTestContext;
import fitnesse.testsystems.slim.Table;
import fitnesse.testsystems.slim.tables.ScenarioTable;
import fitnesse.testsystems.slim.tables.SlimAssertion;
import fitnesse.testsystems.slim.tables.SlimTable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Scenario table that allows usage @{*} symbol to expand all input variables as a constructor parameter for
 * fixtures. See ArgTableTemplateTest1.wiki for example usage.
 */
public class AllArgScenarioTable extends AutoArgScenarioTable {
    private static final String ALL_ARG_PATTERN = "@{*}";
    private Map inputArgs;

    public AllArgScenarioTable(Table table, String tableId, SlimTestContext testContext) {
        super(table, tableId, testContext);
    }

    @Override
    public List call(Map scenarioArguments,
                                    SlimTable parentTable, int row) throws TestExecutionException {
        if(scenarioArguments.size() == 0){
            scenarioArguments.putAll(getInputArgs(parentTable,this.getTestContext()));
        }
        /* pass all input arguments to super inputs to bypass "Variables not used in scenario" error */
        for(Map.Entry entry : scenarioArguments.entrySet()){
            if(!getInputs().contains(entry.getKey())) {
                addInput(entry.getKey());
            }
        }
        /* map scenario arguments to string args */
        String arrayArgs = arrayArgs(scenarioArguments);

        /* replace occurence of symbol @{*} with array of arguments */
        if(getTable() instanceof HtmlTable){
            HtmlTable nTable = (HtmlTable) getTable();
            for(int i = 0; i scenarioArguments) throws TestExecutionException {
        if(scenarioArguments.isEmpty()) return "[]";
        String out = scenarioArguments.entrySet().stream()
                        .map(e -> e.getKey() + ","+"@{" + e.getKey() + "}")
                        .reduce((e, v) -> e = e.concat("," + v))
                        .get();
        return "["+out+"]";
    }

    private Map getInputArgs(SlimTable parentTable, SlimTestContext context){
        HtmlTable parentHtml = (HtmlTable)parentTable.getTable();
        try{
            String parentid = parentHtml.getCellContents(1,0);
            ScenarioTable scenarioTable = context.getScenario(parentid);
            if(scenarioTable instanceof AllArgScenarioTable) {
                return ((AllArgScenarioTable) scenarioTable).inputArgs;
            }
            else return null;
        } catch(IndexOutOfBoundsException e){
            return null;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy