|
17 | 17 |
|
18 | 18 | import net.javacrumbs.jsonunit.core.Configuration;
|
19 | 19 | import net.javacrumbs.jsonunit.core.Option;
|
| 20 | +import net.javacrumbs.jsonunit.core.ParametrizedMatcher; |
20 | 21 | import net.javacrumbs.jsonunit.core.listener.Difference;
|
21 | 22 | import net.javacrumbs.jsonunit.core.listener.DifferenceContext;
|
22 | 23 | import net.javacrumbs.jsonunit.core.listener.DifferenceListener;
|
| 24 | +import org.hamcrest.BaseMatcher; |
| 25 | +import org.hamcrest.Description; |
23 | 26 | import org.junit.jupiter.api.Test;
|
24 | 27 |
|
25 | 28 | import java.math.BigDecimal;
|
|
32 | 35 | import static org.hamcrest.Matchers.equalTo;
|
33 | 36 | import static org.hamcrest.Matchers.hasSize;
|
34 | 37 | import static org.hamcrest.Matchers.nullValue;
|
| 38 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
35 | 39 |
|
36 | 40 | public class DifferenceTest {
|
37 | 41 | private final RecordingDifferenceListener listener = new RecordingDifferenceListener();
|
@@ -216,6 +220,13 @@ void shouldSeeExpectedSource() {
|
216 | 220 | assertThat(listener.getExpectedSource(), equalTo(singletonMap("test", "1")));
|
217 | 221 | }
|
218 | 222 |
|
| 223 | + @Test |
| 224 | + void shouldMatchWithLineSeparatorCustomMatcher() { |
| 225 | + Configuration cfg = commonConfig().withMatcher("equalTo", new EqualsMatcher()); |
| 226 | + Diff diff = Diff.create("{\"key\": \"${json-unit.matches:equalTo}separated \\n line\"}", "{\"key\": \"separated \\n line\"}", "", "", cfg); |
| 227 | + assertTrue(diff.similar()); |
| 228 | + assertThat(listener.getDifferenceList(), hasSize(0)); |
| 229 | + } |
219 | 230 |
|
220 | 231 | private Configuration commonConfig() {
|
221 | 232 | return Configuration.empty().withDifferenceListener(listener);
|
@@ -245,4 +256,23 @@ Object getExpectedSource() {
|
245 | 256 | return expectedSource;
|
246 | 257 | }
|
247 | 258 | }
|
| 259 | + |
| 260 | + private static class EqualsMatcher extends BaseMatcher<Object> implements ParametrizedMatcher { |
| 261 | + private String parameter; |
| 262 | + |
| 263 | + @Override |
| 264 | + public void setParameter(String parameter) { |
| 265 | + this.parameter = parameter; |
| 266 | + } |
| 267 | + |
| 268 | + @Override |
| 269 | + public boolean matches(Object o) { |
| 270 | + return o.toString().equals(parameter); |
| 271 | + } |
| 272 | + |
| 273 | + @Override |
| 274 | + public void describeTo(Description description) { |
| 275 | + description.appendText("the same ").appendText(parameter); |
| 276 | + } |
| 277 | + } |
248 | 278 | }
|
0 commit comments