Skip to content

Commit

Permalink
Match expected logs case-insensitively
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwalker committed Jul 15, 2024
1 parent 04b79e3 commit 0d1b880
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.neo4j.gds.compat;

import org.intellij.lang.annotations.Language;
import org.neo4j.gds.annotation.SuppressForbidden;
import org.neo4j.logging.Log;

Expand All @@ -32,6 +33,8 @@ public interface TestLog extends Log {

void assertContainsMessage(String level, String fragment);

void assertMessageMatches(String level, @Language("RegExp") String pattern);

boolean containsMessage(String level, String fragment);

boolean hasMessages(String level);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
import java.util.regex.Pattern;

public class TestLogImpl implements TestLog {

Expand All @@ -50,6 +51,23 @@ public void assertContainsMessage(String level, String fragment) {
}
}

@Override
public void assertMessageMatches(String level, String pattern) {
var compiled = Pattern.compile(pattern);
var messageList = messages.getOrDefault(level, new ConcurrentLinkedQueue<>());
if (messageList.stream().noneMatch(compiled.asPredicate())) {
throw new RuntimeException(
String.format(
Locale.US,
"Expected log output to match `%s` for log level `%s`%nLog messages:%n%s",
pattern,
level,
String.join("\n", messageList)
)
);
}
}

@Override
public boolean containsMessage(String level, String fragment) {
ConcurrentLinkedQueue<String> messageList = messages.getOrDefault(level, new ConcurrentLinkedQueue<>());
Expand Down

0 comments on commit 0d1b880

Please sign in to comment.