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 16cece8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
public final class GlobPattern {

public static Pattern globToRegexPattern(String globPattern) {
if (globPattern == null) {
return null;
}
String regex = globToRegex(globPattern);
if (regex == null) {
return null;
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 @@ -14,5 +14,7 @@ class GlobPatternTest extends DDSpecification {
"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 16cece8

Please sign in to comment.