Skip to content

Commit

Permalink
add regex support
Browse files Browse the repository at this point in the history
Add ${json-unit.regex} support to strings where the remaining part of
the string is a regex pattern.  Ex
"test": "${json-unit.regex}[A-Z]+"
  • Loading branch information
andrewscode authored and lukas-krecan committed Dec 20, 2014
1 parent 20f1e20 commit fc82ca5
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@
* Comparison configuration. Immutable.
*/
public class Configuration {
private static final Configuration EMPTY_CONFIGURATION = new Configuration(null, Options.empty(), "${json-unit.ignore}");
private static final Configuration EMPTY_CONFIGURATION = new Configuration(null, Options.empty(), "${json-unit.ignore}", "${json-unit.regex}");
private final BigDecimal tolerance;
private final Options options;
private final String ignorePlaceholder;
private final String regexPlaceholder;

public Configuration(BigDecimal tolerance, Options options, String ignorePlaceholder) {
public Configuration(BigDecimal tolerance, Options options, String ignorePlaceholder, String regexPlaceholder) {
this.tolerance = tolerance;
this.options = options;
this.ignorePlaceholder = ignorePlaceholder;
this.regexPlaceholder = regexPlaceholder;
}

/**
Expand All @@ -50,7 +52,7 @@ public static Configuration empty() {
* @return
*/
public Configuration withTolerance(BigDecimal tolerance) {
return new Configuration(tolerance, options, ignorePlaceholder);
return new Configuration(tolerance, options, ignorePlaceholder, regexPlaceholder);
}

/**
Expand Down Expand Up @@ -82,7 +84,7 @@ public Configuration when(Option first, Option... next) {
* @return
*/
public Configuration withOptions(Option first, Option... next) {
return new Configuration(tolerance, options.with(first, next), ignorePlaceholder);
return new Configuration(tolerance, options.with(first, next), ignorePlaceholder, regexPlaceholder);
}

/**
Expand All @@ -92,7 +94,7 @@ public Configuration withOptions(Option first, Option... next) {
* @return
*/
public Configuration withOptions(Options options) {
return new Configuration(tolerance, options, ignorePlaceholder);
return new Configuration(tolerance, options, ignorePlaceholder, regexPlaceholder);
}

/**
Expand All @@ -102,7 +104,17 @@ public Configuration withOptions(Options options) {
* @return
*/
public Configuration withIgnorePlaceholder(String ignorePlaceholder) {
return new Configuration(tolerance, options, ignorePlaceholder);
return new Configuration(tolerance, options, ignorePlaceholder, regexPlaceholder);
}

/**
* Set the regex placeholder
*
* @param ignorePlaceholder
* @return
*/
public Configuration withRegexPlaceholder(String regexPlaceholder) {
return new Configuration(tolerance, options, ignorePlaceholder, regexPlaceholder);
}

public BigDecimal getTolerance() {
Expand All @@ -116,4 +128,8 @@ public Options getOptions() {
public String getIgnorePlaceholder() {
return ignorePlaceholder;
}

public String getRegexPlaceholder() {
return regexPlaceholder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ private static String appendKeysToPrefix(Iterable<String> keys, String prefix) {
return buffer.toString();
}


/**
* Compares two nodes.
*
Expand Down Expand Up @@ -214,7 +213,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 +240,21 @@ private void compareNodes(Node expectedNode, Node actualNode, String fieldPath)
}
}

private void compareStringValues(String expectedValue, String actualValue, String path) {
if (hasOption(IGNORING_VALUES)) {
return;
}

if (expectedValue.startsWith(configuration.getRegexPlaceholder())) {
String pattern = expectedValue.substring(configuration.getRegexPlaceholder().length());
if (!actualValue.matches(pattern)) {
valueDifferenceFound("Different value found in node \"%s\". Pattern %s did not match against %s.", path, quoteTextValue(pattern), quoteTextValue(actualValue));
}
} else {
compareValues(expectedValue, actualValue, path);
return;
}
}

private void compareValues(Object expectedValue, Object actualValue, String path) {
if (!hasOption(IGNORING_VALUES)) {
Expand Down
13 changes: 13 additions & 0 deletions json-unit/src/main/java/net/javacrumbs/jsonunit/JsonAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,19 @@ public static String getIgnorePlaceholder() {
return configuration.getIgnorePlaceholder();
}

/**
* Set's string that will be prefixed to a string marking it as a regex pattern. Default value is "${json-unit.regex}"
*
* @param ignorePlaceholder
*/
public static void setRegexPlaceholder(String regexPlaceholder) {
configuration = configuration.withRegexPlaceholder(regexPlaceholder);
}

public static String getRegexPlaceholder() {
return configuration.getRegexPlaceholder();
}

/**
* Sets the tolerance for floating number comparison. If set to null, requires exact match of the values.
* For example, if set to 0.01, ignores all differences lower than 0.01, so 1 and 0.9999 are considered equal.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/
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;

import java.io.IOException;
Expand Down Expand Up @@ -107,6 +109,28 @@ private void failIfNoException() {
fail("Exception expected");
}

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

@Test
public void testStructureNotEquals() {
try {
JsonAssert.assertJsonStructureEquals( "{\"test\": 123}", "{\"test\": {\"asd\": 23}}");

fail("Exception expected");
} catch (AssertionError e) {
assertEquals("JSON documents are different:\nDifferent value found in node \"test\". Expected '123', got '{\"asd\":23}'.\n", e.getMessage());
}

}

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

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

0 comments on commit fc82ca5

Please sign in to comment.