Skip to content

Commit

Permalink
Merge pull request #1194 from Pfoerd/support_doublestar_matcher_in_globs
Browse files Browse the repository at this point in the history
Support glob pattern **. to match zero or more packages
  • Loading branch information
hcoles authored May 2, 2023
2 parents 6fc727c + 7df3e7e commit 90c936e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pitest/src/main/java/org/pitest/util/Glob.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class Glob implements Predicate<String> {

private final Pattern regex;

private static final String ZERO_OR_MORE_PACKAGES = "(?:.*\\.)*";
private static final String DOUBLE_STAR_PACKAGE_MARKER = "#%#%#";

public Glob(final String glob) {
String rectifiedGlob;
if (glob.startsWith("~")) {
Expand All @@ -50,9 +53,10 @@ public static Collection<Predicate<String>> toGlobPredicates(
}

private static String convertGlobToRegex(final String glob) {
final String preparedGlob = glob.replace("**.", DOUBLE_STAR_PACKAGE_MARKER);
final StringBuilder out = new StringBuilder("^");
for (int i = 0; i < glob.length(); ++i) {
final char c = glob.charAt(i);
for (int i = 0; i < preparedGlob.length(); ++i) {
final char c = preparedGlob.charAt(i);
switch (c) {
case '$':
out.append("\\$");
Expand All @@ -74,7 +78,8 @@ private static String convertGlobToRegex(final String glob) {
}
}
out.append('$');
return out.toString();
return out.toString()
.replace(DOUBLE_STAR_PACKAGE_MARKER, ZERO_OR_MORE_PACKAGES);
}

@Override
Expand Down
13 changes: 13 additions & 0 deletions pitest/src/test/java/org/pitest/util/GlobTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,17 @@ public void matchesStringsWithPlusSign() {
assertFalse(glob.matches("foo-Bar-car"));
}

@Test
public void shouldSupportDoubleStarPackageMatcher() {
final Glob glob = new Glob("**.databinding.**.Foo");
assertTrue(glob.matches("databinding.Foo"));
assertTrue(glob.matches("databinding.bar.Foo"));
assertTrue(glob.matches("databinding.bar.car.Foo"));
assertTrue(glob.matches("foo.databinding.Foo"));
assertTrue(glob.matches("foo.car.databinding.bar.Foo"));
assertTrue(glob.matches(".databinding.Foo"));
assertFalse(glob.matches("databindingfoo.Foo"));
assertFalse(glob.matches("foodatabinding.Foo"));
assertFalse(glob.matches("databinding.fooFoo"));
}
}

0 comments on commit 90c936e

Please sign in to comment.