Skip to content

Commit

Permalink
- F verifyJson() accepts GsonBuilder
Browse files Browse the repository at this point in the history
Fixes #479
  • Loading branch information
ScottBob committed Jul 11, 2024
1 parent 3d2c129 commit 4214e81
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@
* CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L291-L299) (Function7, Object[], Object[], Object[], Object[], Object[], Object[], Object[], $\color{#AAA}{\textsf{Options}}$)
* CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L309-L317) (Function8, Object[], Object[], Object[], Object[], Object[], Object[], Object[], Object[], $\color{#AAA}{\textsf{Options}}$)
* CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L326-L333) (Function9, Object[], Object[], Object[], Object[], Object[], Object[], Object[], Object[], Object[], $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L44-L47) (Object, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L39-L42) (Object, Function1, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L18-L21) (String, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L26-L30) (String, boolean, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L53-L56) (Object, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L48-L51) (Object, Function1, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L17-L20) (String, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L35-L39) (String, boolean, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L29-L34) (String, boolean, Function1, $\color{#AAA}{\textsf{Options}}$)
* JsonJacksonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonJacksonApprovals.java#L14-L17) (Object, $\color{#AAA}{\textsf{Options}}$)
* JsonXstreamApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonXstreamApprovals.java#L18-L21) (Object, $\color{#AAA}{\textsf{Options}}$)
* VelocityApprovals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/velocity/VelocityApprovals.java#L16-L24) (ContextAware, $\color{#AAA}{\textsf{Options}}$)
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,11 @@ public void testJsonFieldOrdering()
JsonApprovals.verifyJson(JsonUtils.reorderFields(json1));
JsonApprovals.verifyJson(JsonUtils.reorderFields(json2));
}
@Test
public void testJsonFieldOrderingWithNulls()
{
Approvals.settings().allowMultipleVerifyCallsForThisMethod();
String json1 = "{\"infos\":{\"address\":null,\"phone\":\"my phone\"},\"insurance\":{\"forks\":[14,53,123],\"prices\":[5,8,\"3%\"]}}";
JsonApprovals.verifyJson(json1, true, g -> g.serializeNulls());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"infos": {
"address": null,
"phone": "my phone"
},
"insurance": {
"forks": [
14,
53,
123
],
"prices": [
5,
8,
"3%"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ private static GsonBuilder addHandlingForDateObjects(GsonBuilder builder)
return builder;
}
public static String reorderFields(String json)
{
return reorderFields(json, g -> g);
}
public static String reorderFields(String json, Function1<GsonBuilder, GsonBuilder> gsonBuilder)
{
JsonObject sortedJsonObject = sortJsonObject(json);
return asJson(sortedJsonObject);
return asJson(sortedJsonObject, gsonBuilder);
}
public static JsonObject sortJsonObject(String json)
{
Expand Down
11 changes: 10 additions & 1 deletion approvaltests/src/main/java/org/approvaltests/JsonApprovals.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.approvaltests;

import com.google.gson.GsonBuilder;
import com.spun.util.FormattedException;
import com.spun.util.JsonUtils;
import org.approvaltests.core.Options;
import org.lambda.functions.Function1;
Expand All @@ -23,6 +22,16 @@ public static void verifyJson(String json, boolean reorderJson)
{
verifyJson(json, reorderJson, new Options());
}
public static void verifyJson(String json, boolean reorderJson, Function1<GsonBuilder, GsonBuilder> gsonBuilder)
{
verifyJson(json, reorderJson, gsonBuilder, new Options());
}
public static void verifyJson(String json, boolean reorderJson, Function1<GsonBuilder, GsonBuilder> gsonBuilder,
Options options)
{
String formattedJson = reorderJson ? JsonUtils.reorderFields(json, gsonBuilder) : JsonUtils.prettyPrint(json);
Approvals.verify(formattedJson, options.forFile().withExtension(".json"));
}
public static void verifyJson(String json, boolean reorderJson, Options options)
{
String formattedJson = reorderJson ? JsonUtils.reorderFields(json) : JsonUtils.prettyPrint(json);
Expand Down

0 comments on commit 4214e81

Please sign in to comment.