Skip to content

Commit

Permalink
Add MatchersTest
Browse files Browse the repository at this point in the history
  • Loading branch information
ygree committed Nov 18, 2023
1 parent b08c25a commit 0602486
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,11 @@
public final class GlobPattern {

public static Pattern globToRegexPattern(String globPattern) {
if (globPattern == null) {
return null;
}
String regex = globToRegex(globPattern);
if (regex == null) {
return null;
}
return Pattern.compile(regex);
}

private static String globToRegex(String globPattern) {
if ("*".equals(globPattern)) {
return null;
}
StringBuilder sb = new StringBuilder(64);
sb.append('^');
for (int i = 0; i < globPattern.length(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public static final Matcher compileGlob(String glob) {
}
}

public static final boolean matches(Matcher matcher, String str) {
public static boolean matches(Matcher matcher, String str) {
return (matcher == null) || matcher.matches(str);
}

public static final boolean matches(Matcher matcher, CharSequence charSeq) {
public static boolean matches(Matcher matcher, CharSequence charSeq) {
return (matcher == null) || matcher.matches(charSeq);
}

static final boolean isExact(String glob) {
static boolean isExact(String glob) {
return (glob.indexOf('*') == -1) && (glob.indexOf('?') == -1);
}

Expand All @@ -39,7 +39,7 @@ static final class ExactMatcher implements Matcher {
}

@Override
public final boolean matches(String str) {
public boolean matches(String str) {
return exact.equals(str);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ class GlobPatternTest extends DDSpecification {

where:
globPattern | expectedRegex
"*" | null
"*" | "^.*\$"
"?" | "^.\$"
"??" | "^..\$"
"Foo*" | "^Foo.*\$"
"abc" | "^abc\$"
"?" | "^.\$"
"F?o" | "^F.o\$"
"Bar*" | "^Bar.*\$"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package datadog.trace.core.util

import datadog.trace.test.util.DDSpecification

class MatchersTest extends DDSpecification {

def "match-all scenarios must return a null matcher"() {
expect:
Matchers.compileGlob(glob) == null

where:
glob << [null, "*"]
}

def "pattern without * or ? must be an ExactMatcher"() {
expect:
Matchers.compileGlob(glob) instanceof Matchers.ExactMatcher

where:
glob << ["a", "ogre", "bcoho34e2"]
}

def "pattern with either * or ? must be a PatternMatcher"() {
expect:
Matchers.compileGlob(glob) instanceof Matchers.PatternMatcher

where:
glob << ["?", "foo*", "*bar", "F?oB?r", "F?o*", "?*", "*?"]
}

def "an exact matcher is self matching"() {
expect:
Matchers.compileGlob(pattern).matches(pattern)

where:
pattern << ["", "a", "abc", "cde"]
}

def "a pattern matcher test"() {
when:
def matcher = Matchers.compileGlob(pattern)

then:
matcher.matches(matching)
!matcher.matches(nonMatching)

where:
pattern | matching | nonMatching
"Fo?" | "Foo" | "Fooo"
"Fo*" | "Fo" | "Fa"
"F*B?r" | "FooBar" | "FooFar"
"" | "" | "non-empty"
}
}

0 comments on commit 0602486

Please sign in to comment.