Skip to content

Commit

Permalink
Merge pull request #617 from EDbarvinsky/dotall_custom_matcher_regexp
Browse files Browse the repository at this point in the history
Make custom matcher regexp DOTALL
  • Loading branch information
lukas-krecan authored Mar 23, 2023
2 parents 21b4a18 + b83fd47 commit 9f02156
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class Diff {
private static final Pattern ANY_STRING_PLACEHOLDER = Pattern.compile("[$#]\\{json-unit.any-string\\}");

private static final Pattern REGEX_PLACEHOLDER = Pattern.compile("[$#]\\{json-unit.regex\\}(.*)");
private static final Pattern MATCHER_PLACEHOLDER_PATTERN = Pattern.compile("[$#]\\{json-unit.matches:(.+?)\\}(.*)");
private static final Pattern MATCHER_PLACEHOLDER_PATTERN = Pattern.compile("[$#]\\{json-unit.matches:(.+?)\\}(.*)", Pattern.DOTALL);

private static final JsonUnitLogger DEFAULT_DIFF_LOGGER = createLogger("net.javacrumbs.jsonunit.difference.diff");
private static final JsonUnitLogger DEFAULT_VALUE_LOGGER = createLogger("net.javacrumbs.jsonunit.difference.values");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

import net.javacrumbs.jsonunit.core.Configuration;
import net.javacrumbs.jsonunit.core.Option;
import net.javacrumbs.jsonunit.core.ParametrizedMatcher;
import net.javacrumbs.jsonunit.core.listener.Difference;
import net.javacrumbs.jsonunit.core.listener.DifferenceContext;
import net.javacrumbs.jsonunit.core.listener.DifferenceListener;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
Expand All @@ -32,6 +35,7 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class DifferenceTest {
private final RecordingDifferenceListener listener = new RecordingDifferenceListener();
Expand Down Expand Up @@ -216,6 +220,13 @@ void shouldSeeExpectedSource() {
assertThat(listener.getExpectedSource(), equalTo(singletonMap("test", "1")));
}

@Test
void shouldMatchWithLineSeparatorCustomMatcher() {
Configuration cfg = commonConfig().withMatcher("equalTo", new EqualsMatcher());
Diff diff = Diff.create("{\"key\": \"${json-unit.matches:equalTo}separated \\n line\"}", "{\"key\": \"separated \\n line\"}", "", "", cfg);
assertTrue(diff.similar());
assertThat(listener.getDifferenceList(), hasSize(0));
}

private Configuration commonConfig() {
return Configuration.empty().withDifferenceListener(listener);
Expand Down Expand Up @@ -245,4 +256,23 @@ Object getExpectedSource() {
return expectedSource;
}
}

private static class EqualsMatcher extends BaseMatcher<Object> implements ParametrizedMatcher {
private String parameter;

@Override
public void setParameter(String parameter) {
this.parameter = parameter;
}

@Override
public boolean matches(Object o) {
return o.toString().equals(parameter);
}

@Override
public void describeTo(Description description) {
description.appendText("the same ").appendText(parameter);
}
}
}

0 comments on commit 9f02156

Please sign in to comment.