Skip to content

Commit

Permalink
Merge pull request #13 from lukas-krecan/regex
Browse files Browse the repository at this point in the history
Add regex support
  • Loading branch information
lukas-krecan committed Dec 21, 2014
2 parents 20f1e20 + e7f1861 commit 3d66d05
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
3 changes: 3 additions & 0 deletions RELEASENOTES.md
@@ -1,3 +1,6 @@
## 1.5.2
* Added support for regular expressions

## 1.5.1
* isStringEqualTo() added to fluent assertions
* isArray added to fluent assertions
Expand Down
Expand Up @@ -20,6 +20,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.String;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -49,6 +50,7 @@
* @author Lukas Krecan
*/
public class Diff {
private static final String REGEX_PLACEHOLDER = "${json-unit.regex}";
private final Node expectedRoot;
private final Node actualRoot;
private final Differences differences = new Differences();
Expand Down Expand Up @@ -186,7 +188,6 @@ private static String appendKeysToPrefix(Iterable<String> keys, String prefix) {
return buffer.toString();
}


/**
* Compares two nodes.
*
Expand Down Expand Up @@ -214,7 +215,7 @@ private void compareNodes(Node expectedNode, Node actualNode, String fieldPath)
compareArrayNodes(expectedNode, actualNode, fieldPath);
break;
case STRING:
compareValues(expectedNode.asText(), actualNode.asText(), fieldPath);
compareStringValues(expectedNode.asText(), actualNode.asText(), fieldPath);
break;
case NUMBER:
BigDecimal actualValue = actualNode.decimalValue();
Expand All @@ -241,6 +242,27 @@ private void compareNodes(Node expectedNode, Node actualNode, String fieldPath)
}
}

private void compareStringValues(String expectedValue, String actualValue, String path) {
if (hasOption(IGNORING_VALUES)) {
return;
}
if (isRegexExpected(expectedValue)) {
String pattern = getRegexPattern(expectedValue);
if (!actualValue.matches(pattern)) {
valueDifferenceFound("Different value found in node \"%s\". Pattern %s did not match %s.", path, quoteTextValue(pattern), quoteTextValue(actualValue));
}
} else {
compareValues(expectedValue, actualValue, path);
}
}

private String getRegexPattern(String expectedValue) {
return expectedValue.substring(REGEX_PLACEHOLDER.length());
}

private boolean isRegexExpected(String expectedValue) {
return expectedValue.startsWith(REGEX_PLACEHOLDER);
}

private void compareValues(Object expectedValue, Object actualValue, String path) {
if (!hasOption(IGNORING_VALUES)) {
Expand Down
Expand Up @@ -15,6 +15,7 @@
*/
package net.javacrumbs.jsonunit.test.all;

import net.javacrumbs.jsonunit.JsonAssert;
import net.javacrumbs.jsonunit.test.base.AbstractJsonAssertTest;
import net.javacrumbs.jsonunit.test.base.JsonTestUtils;
import org.junit.Test;
Expand Down Expand Up @@ -103,11 +104,65 @@ public void testEqualsNodeStringFail() throws IOException {
}
}

private void failIfNoException() {
fail("Exception expected");
@Test
public void testStructureEquals() {
JsonAssert.assertJsonStructureEquals("{\"test\": 123}", "{\"test\": 412}");
}

@Test
public void testRegex() {
assertJsonEquals("{\"test\": \"${json-unit.regex}[A-Z]+\"}", "{\"test\": \"ABCD\"}");
}

@Test
public void regexShouldFail() {
try {
assertJsonEquals("{\"test\": \"${json-unit.regex}[A-Z]+\"}", "{\"test\": \"123\"}");
failIfNoException();
} catch (AssertionError e) {
assertEquals("JSON documents are different:\n" +
"Different value found in node \"test\". Pattern \"[A-Z]+\" did not match \"123\".\n", e.getMessage());
}
}

@Test
public void regexShouldFailOnNullGracefully() {
try {
assertJsonEquals("{\"test\": \"${json-unit.regex}[A-Z]+\"}", "{\"test\": null}");
failIfNoException();
} catch (AssertionError e) {
assertEquals("JSON documents are different:\n" +
"Different value found in node \"test\". Expected '\"${json-unit.regex}[A-Z]+\"', got 'null'.\n", e.getMessage());
}
}

@Test
public void regexShouldFailOnNumberGracefully() {
try {
assertJsonEquals("{\"test\": \"${json-unit.regex}[A-Z]+\"}", "{\"test\": 123}");
failIfNoException();
} catch (AssertionError e) {
assertEquals("JSON documents are different:\n" +
"Different value found in node \"test\". Expected '\"${json-unit.regex}[A-Z]+\"', got '123'.\n", e.getMessage());
}
}

@Test
public void regexShouldFailOnNonexistingGracefully() {
try {
assertJsonEquals("{\"test\": \"${json-unit.regex}[A-Z]+\"}", "{\"test2\": 123}");
failIfNoException();
} catch (AssertionError e) {
assertEquals("JSON documents are different:\n" +
"Different keys found in node \"\". Expected [test], got [test2]. Missing: \"test\" Extra: \"test2\"\n", e.getMessage());
}
}

protected Object readValue(String value) {
return JsonTestUtils.readByJackson1(value);
}

private void failIfNoException() {
fail("Exception expected");
}
}

0 comments on commit 3d66d05

Please sign in to comment.