简体   繁体   中英

Custom Cucumber ParamterDefinition to pass a list of strings

I have my custom ParameterType defined but the regex I have defined also matches single strings (not just lists of strings). I need a regex that will match the strings within a list of strings, but won't match the string if it is on its own (I don't have a list of strings if there is only 1 String)

    @ParameterType("(?:[^,]*)(?:,\\s?[^,]*)*")
    public List<String> listOfString(String listString) { 
        List<String> finalList = new ArrayList<String>();
        String[] stringArray = listString.split(", ");
        for (String string : stringArray) {
            finalList.add(string);
        }
        return finalList;
    }

Any help on the regex or an alternative solution would be great

EG If my step read:

  1. Given I have the following "string"
  2. Given I have the following "string1" "string2"

Then my stepDefinitions should be

@Given("I have the following {string}")
public void myFunction(String string) {}
@Given("I have the following {listOfString}")
public void myFunction(List<String> listOfString) {}

And Cucumber should not throw the matches more than one step definition exception. If a step has a single string argument, then it should execute the first stepDefinition (with myFunction(String string) ) and if the step has a list of strings, then it should execute the second stepDefinition (with myFunction(List<String> listOfString) ).

If you don't want to match empty strings, and the separator is a comma which should be there at least once:

[^,]+(?:,[^,]+)+

Regex demo

If you don't want partial matches like a trailing comma, you can append anchors:

^[^,]+(?:,[^,]+)+$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM