@Test public void generates_all_combinations_of_expressions_when_several_parameter_types_match() { parameterTypeRegistry.defineParameterType(new ParameterType<>( "currency", "x", Currency.class, (Transformer<Currency>) Currency::getInstance, true, true )); parameterTypeRegistry.defineParameterType(new ParameterType<>( "date", "x", Date.class, new Transformer<Date>() { @Override public Date transform(String arg) { try { return df.parse(arg); } catch (ParseException e) { throw new RuntimeException(e); } } }, true, false )); List<GeneratedExpression> generatedExpressions = generator.generateExpressions("I have x and x and another x"); List<String> expressions = new ArrayList<>(); for (GeneratedExpression generatedExpression : generatedExpressions) { String source = generatedExpression.getSource(); expressions.add(source); } assertEquals(asList( "I have {currency} and {currency} and another {currency}", "I have {currency} and {currency} and another {date}", "I have {currency} and {date} and another {currency}", "I have {currency} and {date} and another {date}", "I have {date} and {currency} and another {currency}", "I have {date} and {currency} and another {date}", "I have {date} and {date} and another {currency}", "I have {date} and {date} and another {date}" ), expressions); }
public List<GeneratedExpression> generateExpressions(String text) { List<List<ParameterType<?>>> parameterTypeCombinations = new ArrayList<>(); List<ParameterTypeMatcher> parameterTypeMatchers = createParameterTypeMatchers(text); StringBuilder expressionTemplate = new StringBuilder(); int pos = 0; while (true) { List<ParameterTypeMatcher> matchingParameterTypeMatchers = new ArrayList<>(); for (ParameterTypeMatcher parameterTypeMatcher : parameterTypeMatchers) { if (parameterTypeMatcher.advanceToAndFind(pos)) { matchingParameterTypeMatchers.add(parameterTypeMatcher); } } if (!matchingParameterTypeMatchers.isEmpty()) { Collections.sort(matchingParameterTypeMatchers); ParameterTypeMatcher bestParameterTypeMatcher = matchingParameterTypeMatchers.get(0); List<ParameterTypeMatcher> bestParameterTypeMatchers = new ArrayList<>(); for (ParameterTypeMatcher m : matchingParameterTypeMatchers) { if (m.compareTo(bestParameterTypeMatcher) == 0) { bestParameterTypeMatchers.add(m); } } Set<ParameterType<?>> set = new HashSet<>(); for (ParameterTypeMatcher parameterTypeMatcher : bestParameterTypeMatchers) { ParameterType<?> parameterType = parameterTypeMatcher.getParameterType(); set.add(parameterType); } SortedSet<ParameterType<?>> parameterTypes = new TreeSet<>(set); parameterTypeCombinations.add(new ArrayList<>(parameterTypes)); expressionTemplate .append(escape(text.substring(pos, bestParameterTypeMatcher.start()))) .append("{%s}"); pos = bestParameterTypeMatcher.start() + bestParameterTypeMatcher.group().length(); } else { break; } if (pos >= text.length()) { break; } } expressionTemplate.append(escape(text.substring(pos))); return new CombinatorialGeneratedExpressionFactory(expressionTemplate.toString(), parameterTypeCombinations).generateExpressions(); }
CucumberExpressionGenerator { public List<GeneratedExpression> generateExpressions(String text) { List<List<ParameterType<?>>> parameterTypeCombinations = new ArrayList<>(); List<ParameterTypeMatcher> parameterTypeMatchers = createParameterTypeMatchers(text); StringBuilder expressionTemplate = new StringBuilder(); int pos = 0; while (true) { List<ParameterTypeMatcher> matchingParameterTypeMatchers = new ArrayList<>(); for (ParameterTypeMatcher parameterTypeMatcher : parameterTypeMatchers) { if (parameterTypeMatcher.advanceToAndFind(pos)) { matchingParameterTypeMatchers.add(parameterTypeMatcher); } } if (!matchingParameterTypeMatchers.isEmpty()) { Collections.sort(matchingParameterTypeMatchers); ParameterTypeMatcher bestParameterTypeMatcher = matchingParameterTypeMatchers.get(0); List<ParameterTypeMatcher> bestParameterTypeMatchers = new ArrayList<>(); for (ParameterTypeMatcher m : matchingParameterTypeMatchers) { if (m.compareTo(bestParameterTypeMatcher) == 0) { bestParameterTypeMatchers.add(m); } } Set<ParameterType<?>> set = new HashSet<>(); for (ParameterTypeMatcher parameterTypeMatcher : bestParameterTypeMatchers) { ParameterType<?> parameterType = parameterTypeMatcher.getParameterType(); set.add(parameterType); } SortedSet<ParameterType<?>> parameterTypes = new TreeSet<>(set); parameterTypeCombinations.add(new ArrayList<>(parameterTypes)); expressionTemplate .append(escape(text.substring(pos, bestParameterTypeMatcher.start()))) .append("{%s}"); pos = bestParameterTypeMatcher.start() + bestParameterTypeMatcher.group().length(); } else { break; } if (pos >= text.length()) { break; } } expressionTemplate.append(escape(text.substring(pos))); return new CombinatorialGeneratedExpressionFactory(expressionTemplate.toString(), parameterTypeCombinations).generateExpressions(); } }
CucumberExpressionGenerator { public List<GeneratedExpression> generateExpressions(String text) { List<List<ParameterType<?>>> parameterTypeCombinations = new ArrayList<>(); List<ParameterTypeMatcher> parameterTypeMatchers = createParameterTypeMatchers(text); StringBuilder expressionTemplate = new StringBuilder(); int pos = 0; while (true) { List<ParameterTypeMatcher> matchingParameterTypeMatchers = new ArrayList<>(); for (ParameterTypeMatcher parameterTypeMatcher : parameterTypeMatchers) { if (parameterTypeMatcher.advanceToAndFind(pos)) { matchingParameterTypeMatchers.add(parameterTypeMatcher); } } if (!matchingParameterTypeMatchers.isEmpty()) { Collections.sort(matchingParameterTypeMatchers); ParameterTypeMatcher bestParameterTypeMatcher = matchingParameterTypeMatchers.get(0); List<ParameterTypeMatcher> bestParameterTypeMatchers = new ArrayList<>(); for (ParameterTypeMatcher m : matchingParameterTypeMatchers) { if (m.compareTo(bestParameterTypeMatcher) == 0) { bestParameterTypeMatchers.add(m); } } Set<ParameterType<?>> set = new HashSet<>(); for (ParameterTypeMatcher parameterTypeMatcher : bestParameterTypeMatchers) { ParameterType<?> parameterType = parameterTypeMatcher.getParameterType(); set.add(parameterType); } SortedSet<ParameterType<?>> parameterTypes = new TreeSet<>(set); parameterTypeCombinations.add(new ArrayList<>(parameterTypes)); expressionTemplate .append(escape(text.substring(pos, bestParameterTypeMatcher.start()))) .append("{%s}"); pos = bestParameterTypeMatcher.start() + bestParameterTypeMatcher.group().length(); } else { break; } if (pos >= text.length()) { break; } } expressionTemplate.append(escape(text.substring(pos))); return new CombinatorialGeneratedExpressionFactory(expressionTemplate.toString(), parameterTypeCombinations).generateExpressions(); } CucumberExpressionGenerator(ParameterTypeRegistry parameterTypeRegistry); }
CucumberExpressionGenerator { public List<GeneratedExpression> generateExpressions(String text) { List<List<ParameterType<?>>> parameterTypeCombinations = new ArrayList<>(); List<ParameterTypeMatcher> parameterTypeMatchers = createParameterTypeMatchers(text); StringBuilder expressionTemplate = new StringBuilder(); int pos = 0; while (true) { List<ParameterTypeMatcher> matchingParameterTypeMatchers = new ArrayList<>(); for (ParameterTypeMatcher parameterTypeMatcher : parameterTypeMatchers) { if (parameterTypeMatcher.advanceToAndFind(pos)) { matchingParameterTypeMatchers.add(parameterTypeMatcher); } } if (!matchingParameterTypeMatchers.isEmpty()) { Collections.sort(matchingParameterTypeMatchers); ParameterTypeMatcher bestParameterTypeMatcher = matchingParameterTypeMatchers.get(0); List<ParameterTypeMatcher> bestParameterTypeMatchers = new ArrayList<>(); for (ParameterTypeMatcher m : matchingParameterTypeMatchers) { if (m.compareTo(bestParameterTypeMatcher) == 0) { bestParameterTypeMatchers.add(m); } } Set<ParameterType<?>> set = new HashSet<>(); for (ParameterTypeMatcher parameterTypeMatcher : bestParameterTypeMatchers) { ParameterType<?> parameterType = parameterTypeMatcher.getParameterType(); set.add(parameterType); } SortedSet<ParameterType<?>> parameterTypes = new TreeSet<>(set); parameterTypeCombinations.add(new ArrayList<>(parameterTypes)); expressionTemplate .append(escape(text.substring(pos, bestParameterTypeMatcher.start()))) .append("{%s}"); pos = bestParameterTypeMatcher.start() + bestParameterTypeMatcher.group().length(); } else { break; } if (pos >= text.length()) { break; } } expressionTemplate.append(escape(text.substring(pos))); return new CombinatorialGeneratedExpressionFactory(expressionTemplate.toString(), parameterTypeCombinations).generateExpressions(); } CucumberExpressionGenerator(ParameterTypeRegistry parameterTypeRegistry); List<GeneratedExpression> generateExpressions(String text); @Deprecated GeneratedExpression generateExpression(String text); }
CucumberExpressionGenerator { public List<GeneratedExpression> generateExpressions(String text) { List<List<ParameterType<?>>> parameterTypeCombinations = new ArrayList<>(); List<ParameterTypeMatcher> parameterTypeMatchers = createParameterTypeMatchers(text); StringBuilder expressionTemplate = new StringBuilder(); int pos = 0; while (true) { List<ParameterTypeMatcher> matchingParameterTypeMatchers = new ArrayList<>(); for (ParameterTypeMatcher parameterTypeMatcher : parameterTypeMatchers) { if (parameterTypeMatcher.advanceToAndFind(pos)) { matchingParameterTypeMatchers.add(parameterTypeMatcher); } } if (!matchingParameterTypeMatchers.isEmpty()) { Collections.sort(matchingParameterTypeMatchers); ParameterTypeMatcher bestParameterTypeMatcher = matchingParameterTypeMatchers.get(0); List<ParameterTypeMatcher> bestParameterTypeMatchers = new ArrayList<>(); for (ParameterTypeMatcher m : matchingParameterTypeMatchers) { if (m.compareTo(bestParameterTypeMatcher) == 0) { bestParameterTypeMatchers.add(m); } } Set<ParameterType<?>> set = new HashSet<>(); for (ParameterTypeMatcher parameterTypeMatcher : bestParameterTypeMatchers) { ParameterType<?> parameterType = parameterTypeMatcher.getParameterType(); set.add(parameterType); } SortedSet<ParameterType<?>> parameterTypes = new TreeSet<>(set); parameterTypeCombinations.add(new ArrayList<>(parameterTypes)); expressionTemplate .append(escape(text.substring(pos, bestParameterTypeMatcher.start()))) .append("{%s}"); pos = bestParameterTypeMatcher.start() + bestParameterTypeMatcher.group().length(); } else { break; } if (pos >= text.length()) { break; } } expressionTemplate.append(escape(text.substring(pos))); return new CombinatorialGeneratedExpressionFactory(expressionTemplate.toString(), parameterTypeCombinations).generateExpressions(); } CucumberExpressionGenerator(ParameterTypeRegistry parameterTypeRegistry); List<GeneratedExpression> generateExpressions(String text); @Deprecated GeneratedExpression generateExpression(String text);  }