From bb5735ea0f145e83ade533ddd18bfb2dc6e25759 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 13 Jun 2024 12:26:22 +0100 Subject: [PATCH] Add highlight matcher --- .../internal/styles/HighlightMatcher.java | 16 ++++++++ .../tests/styles/HighlightMatcherTests.java | 38 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/main/java/org/zwobble/mammoth/internal/styles/HighlightMatcher.java create mode 100644 src/test/java/org/zwobble/mammoth/tests/styles/HighlightMatcherTests.java diff --git a/src/main/java/org/zwobble/mammoth/internal/styles/HighlightMatcher.java b/src/main/java/org/zwobble/mammoth/internal/styles/HighlightMatcher.java new file mode 100644 index 0000000..2e0481b --- /dev/null +++ b/src/main/java/org/zwobble/mammoth/internal/styles/HighlightMatcher.java @@ -0,0 +1,16 @@ +package org.zwobble.mammoth.internal.styles; + +import java.util.Optional; + +public class HighlightMatcher implements DocumentElementMatcher { + private final Optional color; + + public HighlightMatcher(Optional color) { + this.color = color; + } + + @Override + public boolean matches(String color) { + return !this.color.isPresent() || this.color.get().equals(color); + } +} diff --git a/src/test/java/org/zwobble/mammoth/tests/styles/HighlightMatcherTests.java b/src/test/java/org/zwobble/mammoth/tests/styles/HighlightMatcherTests.java new file mode 100644 index 0000000..4714f9a --- /dev/null +++ b/src/test/java/org/zwobble/mammoth/tests/styles/HighlightMatcherTests.java @@ -0,0 +1,38 @@ +package org.zwobble.mammoth.tests.styles; + +import org.junit.jupiter.api.Test; +import org.zwobble.mammoth.internal.styles.HighlightMatcher; + +import java.util.Optional; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +public class HighlightMatcherTests { + @Test + public void highlightMatcherWithoutColorMatchesAllHighlightElements() { + HighlightMatcher matcher = new HighlightMatcher(Optional.empty()); + + boolean result = matcher.matches("yellow"); + + assertThat(result, equalTo(true)); + } + + @Test + public void highlightMatcherWithColorMatchesHighlightWithThatColor() { + HighlightMatcher matcher = new HighlightMatcher(Optional.of("yellow")); + + boolean result = matcher.matches("yellow"); + + assertThat(result, equalTo(true)); + } + + @Test + public void highlightMatcherWithColorDoesNotMatchHighlightsWithOtherColors() { + HighlightMatcher matcher = new HighlightMatcher(Optional.of("yellow")); + + boolean result = matcher.matches("red"); + + assertThat(result, equalTo(false)); + } +}