Skip to content

Commit

Permalink
Merge pull request #90 from witspirit/1.65.0
Browse files Browse the repository at this point in the history
1.65.0
  • Loading branch information
picimako committed Jul 4, 2024
2 parents 84ab31f + c645dff commit 31b53df
Show file tree
Hide file tree
Showing 17 changed files with 592 additions and 199 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

## [1.65.0]
### Changed
- Added caching for the JBehave step reference resolution and Given-When-Then step annotation lookup.

## [1.64.0]
### Changed
- New supported IDE version range: 2023.1.6-2024.2-EAP
Expand Down
7 changes: 7 additions & 0 deletions NOTICE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This product includes software developed in the JBehave project. (https://github.com/jbehave/jbehave-core).

The classes 'com.github.kumaraman21.intellijbehave.jbehave.core.steps.PatternVariantBuilder' and
'com.github.kumaraman21.intellijbehave.jbehave.core.steps.PatternVariantBuilderTest' are the simplified
and modified versions of 'org.jbehave.core.steps.PatternVariantBuilder' and 'org.jbehave.core.steps.PatternVariantBuilderBehaviour'.

Those classes are licensed under the BSD-3-Clause license.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ tasks {
//Required for running tests in 2021.3 due to it not finding test classes properly.
//See https://app.slack.com/client/T5P9YATH9/C5U8BM1MK/thread/C5U8BM1MK-1639934273.054400
isScanForTestClasses = false
include("**/codeInspector/*Test.class", "**/resolver/*Test.class", "**/utility/*Test.class", "**/service/*Test.class")
include("**/codeInspector/*Test.class", "**/resolver/*Test.class", "**/utility/*Test.class", "**/service/*Test.class", "**/jbehave/core/steps/*Test.class")
exclude("**/highlighter/*Test.class", "**/parser/*Test.class", "**/spellchecker/*Test.class", "**/structure/*Test.class")
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pluginGroup = com.github.kumaraman21.intellijbehave
pluginName = JBehave Support
pluginRepositoryUrl = https://github.com/witspirit/IntelliJBehave
# SemVer format -> https://semver.org
pluginVersion = 1.64.0
pluginVersion = 1.65.0

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 231.9414.13
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package com.github.kumaraman21.intellijbehave.jbehave.core.steps;

import static java.util.Arrays.asList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* <p>
* Builds a set of pattern variants of given pattern input, supporting a custom
* directives. Depending on the directives present, one or more resulting
* variants are created.
* </p>
* <p>
* Currently supported directives are
* </p>
* <table border="1">
* <thead>
* <tr>
* <td>Pattern</td>
* <td>Result</td>
* </tr>
* </thead> <tbody>
* <tr>
* <td>..A {x|y} B..</td>
* <td>
* <ul>
* <li>..A x B..</li>
* <li>..A y B..</li>
* </ul>
* </td>
* </tr>
* <tr>
* <td>..A {x|y|} B..</td>
* <td>
* <ul>
* <li>..A x B..</li>
* <li>..A y B..</li>
* <li>..A B..</li>
* </ul>
* </td>
* </tr>
* <tr>
* <td>..A {x} B..</td>
* <td>
* <ul>
* <li>..A x B..</li>
* </ul>
* </td>
* </tr>
* </table>
* <p>
* These directives can be used to conveniently create several variants of a
* step pattern, without having to repeat it as a whole as one or more aliases.
* </p>
* <p>
* Examples:
* </p>
* <ul>
* <li>
* <p>
* <code>@Then("the result {must |has to |}be $x")<br> public void checkResult(int x)...</code>
* </p>
* <p>
* Would match any of these variants from a story file:
* <ul>
* <li>Then the result must be 42</li>
* <li>Then the result has to be 42</li>
* <li>Then the result be 42</li>
* </ul>
* </p>
* </li>
* <li>
* <p>
* <code>@When("$A {+|plus|is added to} $B")<br> public void add(int A, int B)...</code>
* </p>
* <p>
* Would match any of these variants from a story file:
* <ul>
* <li>When 42 + 23</li>
* <li>When 42 plus 23</li>
* <li>When 42 is added to 23</li>
* </ul>
* </p>
* </li>
* </ul>
* <p>
* This is the modified and optimized version of {@link org.jbehave.core.steps.PatternVariantBuilder}.
*
* @author Daniel Schneller
*/
public class PatternVariantBuilder {

/**
* Regular expression that locates patterns to be evaluated in the input
* pattern.
*/
private static final Pattern REGEX = Pattern.compile("([^\\n{]*+)(\\{(([^|}]++)(\\|)?+)*+\\})([^\\n]*+)");

private final Set<String> variants;

private final String input;

/**
* Creates a builder and calculates all variants for given input. When there
* are no variants found in the input, it will itself be the only result.
*
* @param input to be evaluated
*/
public PatternVariantBuilder(String input) {
this.input = input;
this.variants = variantsFor(input);
}

public String getInput() {
return input;
}

/**
* <p>
* Parses the {@link #input} received at construction and generates the
* variants. When there are multiple patterns in the input, the method will
* recurse on itself to generate the variants for the tailing end after the
* first matched pattern.
* </p>
* <p>
* Generated variants are stored in a {@link Set}, so there will never be
* any duplicates, even if the input's patterns were to result in such.
* </p>
*/
private Set<String> variantsFor(String input) {
Matcher m = REGEX.matcher(input);

if (!m.matches()) {
// if the regex does not find any patterns,
// simply add the input as is
return Collections.singleton(input);
}

// isolate the pattern itself, removing its wrapping {}
String patternGroup = m.group(2).replaceAll("[\\{\\}]", "");

// split the pattern into its options and add an empty
// string if it ends with a separator
List<String> patternParts = new ArrayList<>(asList(patternGroup.split("\\|")));
if (patternGroup.endsWith("|")) {
patternParts.add("");
}

// Store current invocation's results
Set<String> variants = new HashSet<>(8);

if (!patternParts.isEmpty()) {
// isolate the part before the first pattern
String head = m.group(1);

// isolate the remaining part of the input
String tail = m.group(6);

var variantsForTail = variantsFor(tail);

// Iterate over the current pattern's
// variants and construct the result.
for (String part : patternParts) {
var partString = head != null ? head + part : part;

// recurse on the tail of the input
// to handle the next pattern

// append all variants of the tail end
// and add each of them to the part we have
// built up so far.
for (String tailVariant : variantsForTail) {
variants.add(partString + tailVariant);
}
}
}
return variants;
}

/**
* Returns a new copy set of all variants with no whitespace compression.
*
* @return a {@link Set} of all variants without whitespace compression
*/
public Set<String> allVariants() {
return variants.size() == 1 ? Collections.singleton(variants.iterator().next()) : new HashSet<>(variants);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/
package com.github.kumaraman21.intellijbehave.resolver;

import com.github.kumaraman21.intellijbehave.jbehave.core.steps.PatternVariantBuilder;
import com.intellij.psi.*;
import org.jbehave.core.annotations.Alias;
import org.jbehave.core.annotations.Aliases;
import org.jbehave.core.steps.PatternVariantBuilder;
import org.jbehave.core.steps.StepType;

import java.util.Collections;
Expand Down
Loading

0 comments on commit 31b53df

Please sign in to comment.