Skip to content

Commit ee092da

Browse files
authored
Add verify json (VerifyTests#333)
1 parent 4170cf9 commit ee092da

19 files changed

+317
-31
lines changed

docs/named-tuples.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static (bool Member1, string Member2, string Member3) MethodWithNamedTuple()
2121
return (true, "A", "B");
2222
}
2323
```
24-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L541-L548' title='Snippet source file'>snippet source</a> | <a href='#snippet-methodwithnamedtuple' title='Start of snippet'>anchor</a></sup>
24+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L542-L549' title='Snippet source file'>snippet source</a> | <a href='#snippet-methodwithnamedtuple' title='Start of snippet'>anchor</a></sup>
2525
<!-- endSnippet -->
2626

2727
Can be verified:
@@ -31,7 +31,7 @@ Can be verified:
3131
```cs
3232
await Verifier.Verify(() => MethodWithNamedTuple());
3333
```
34-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L534-L538' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifytuple' title='Start of snippet'>anchor</a></sup>
34+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L535-L539' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifytuple' title='Start of snippet'>anchor</a></sup>
3535
<!-- endSnippet -->
3636

3737
Resulting in:

docs/scrubbers.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ For example remove lines containing `text`:
5656
```cs
5757
verifySettings.ScrubLines(line => line.Contains("text"));
5858
```
59-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L424-L428' title='Snippet source file'>snippet source</a> | <a href='#snippet-scrublines' title='Start of snippet'>anchor</a></sup>
59+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L425-L429' title='Snippet source file'>snippet source</a> | <a href='#snippet-scrublines' title='Start of snippet'>anchor</a></sup>
6060
<!-- endSnippet -->
6161

6262

@@ -71,7 +71,7 @@ For example remove lines containing `text1` or `text2`
7171
```cs
7272
verifySettings.ScrubLinesContaining("text1", "text2");
7373
```
74-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L430-L434' title='Snippet source file'>snippet source</a> | <a href='#snippet-scrublinescontaining' title='Start of snippet'>anchor</a></sup>
74+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L431-L435' title='Snippet source file'>snippet source</a> | <a href='#snippet-scrublinescontaining' title='Start of snippet'>anchor</a></sup>
7575
<!-- endSnippet -->
7676

7777
Case insensitive by default (StringComparison.OrdinalIgnoreCase).
@@ -83,7 +83,7 @@ Case insensitive by default (StringComparison.OrdinalIgnoreCase).
8383
```cs
8484
verifySettings.ScrubLinesContaining(StringComparison.Ordinal, "text1", "text2");
8585
```
86-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L436-L440' title='Snippet source file'>snippet source</a> | <a href='#snippet-scrublinescontainingordinal' title='Start of snippet'>anchor</a></sup>
86+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L437-L441' title='Snippet source file'>snippet source</a> | <a href='#snippet-scrublinescontainingordinal' title='Start of snippet'>anchor</a></sup>
8787
<!-- endSnippet -->
8888

8989

@@ -98,7 +98,7 @@ For example converts lines to upper case:
9898
```cs
9999
verifySettings.ScrubLinesWithReplace(line => line.ToUpper());
100100
```
101-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L442-L446' title='Snippet source file'>snippet source</a> | <a href='#snippet-scrublineswithreplace' title='Start of snippet'>anchor</a></sup>
101+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L443-L447' title='Snippet source file'>snippet source</a> | <a href='#snippet-scrublineswithreplace' title='Start of snippet'>anchor</a></sup>
102102
<!-- endSnippet -->
103103

104104

@@ -111,7 +111,7 @@ Replaces `Environment.MachineName` with `TheMachineName`.
111111
```cs
112112
verifySettings.ScrubMachineName();
113113
```
114-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L448-L452' title='Snippet source file'>snippet source</a> | <a href='#snippet-scrubmachinename' title='Start of snippet'>anchor</a></sup>
114+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L449-L453' title='Snippet source file'>snippet source</a> | <a href='#snippet-scrubmachinename' title='Start of snippet'>anchor</a></sup>
115115
<!-- endSnippet -->
116116

117117

docs/serializer-settings.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ TheTarget target = new()
5454
};
5555
await Verifier.Verify(target);
5656
```
57-
<sup><a href='/src/StrictJsonTests/Tests.cs#L34-L42' title='Snippet source file'>snippet source</a> | <a href='#snippet-usestrictjsonverify' title='Start of snippet'>anchor</a></sup>
57+
<sup><a href='/src/StrictJsonTests/Tests.cs#L41-L49' title='Snippet source file'>snippet source</a> | <a href='#snippet-usestrictjsonverify' title='Start of snippet'>anchor</a></sup>
5858
<!-- endSnippet -->
5959

6060
The resulting file will be:
@@ -104,7 +104,7 @@ To disable this behavior globally use:
104104
```cs
105105
VerifierSettings.ModifySerialization(_ => _.DontIgnoreEmptyCollections());
106106
```
107-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L371-L375' title='Snippet source file'>snippet source</a> | <a href='#snippet-dontignoreemptycollections' title='Start of snippet'>anchor</a></sup>
107+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L372-L376' title='Snippet source file'>snippet source</a> | <a href='#snippet-dontignoreemptycollections' title='Start of snippet'>anchor</a></sup>
108108
<!-- endSnippet -->
109109

110110

@@ -126,7 +126,7 @@ GuidTarget target = new()
126126

127127
await Verifier.Verify(target);
128128
```
129-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L577-L590' title='Snippet source file'>snippet source</a> | <a href='#snippet-guid' title='Start of snippet'>anchor</a></sup>
129+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L578-L591' title='Snippet source file'>snippet source</a> | <a href='#snippet-guid' title='Start of snippet'>anchor</a></sup>
130130
<!-- endSnippet -->
131131

132132
Results in the following:
@@ -151,7 +151,7 @@ To disable this behavior globally use:
151151
```cs
152152
VerifierSettings.ModifySerialization(_ => _.DontScrubGuids());
153153
```
154-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L380-L384' title='Snippet source file'>snippet source</a> | <a href='#snippet-dontscrubguids' title='Start of snippet'>anchor</a></sup>
154+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L381-L385' title='Snippet source file'>snippet source</a> | <a href='#snippet-dontscrubguids' title='Start of snippet'>anchor</a></sup>
155155
<!-- endSnippet -->
156156

157157
Strings containing inline Guids can also be scrubbed. To enable this behavior, use:
@@ -161,7 +161,7 @@ Strings containing inline Guids can also be scrubbed. To enable this behavior, u
161161
```cs
162162
VerifierSettings.ScrubInlineGuids();
163163
```
164-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L389-L393' title='Snippet source file'>snippet source</a> | <a href='#snippet-scrubinlineguids' title='Start of snippet'>anchor</a></sup>
164+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L390-L394' title='Snippet source file'>snippet source</a> | <a href='#snippet-scrubinlineguids' title='Start of snippet'>anchor</a></sup>
165165
<!-- endSnippet -->
166166

167167

@@ -186,7 +186,7 @@ DateTimeTarget target = new()
186186

187187
await Verifier.Verify(target);
188188
```
189-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L34-L50' title='Snippet source file'>snippet source</a> | <a href='#snippet-date' title='Start of snippet'>anchor</a></sup>
189+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L35-L51' title='Snippet source file'>snippet source</a> | <a href='#snippet-date' title='Start of snippet'>anchor</a></sup>
190190
<!-- endSnippet -->
191191

192192
Results in the following:
@@ -213,7 +213,7 @@ To disable this behavior globally use:
213213
```cs
214214
VerifierSettings.ModifySerialization(_ => _.DontScrubDateTimes());
215215
```
216-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L398-L402' title='Snippet source file'>snippet source</a> | <a href='#snippet-dontscrubdatetimes' title='Start of snippet'>anchor</a></sup>
216+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L399-L403' title='Snippet source file'>snippet source</a> | <a href='#snippet-dontscrubdatetimes' title='Start of snippet'>anchor</a></sup>
217217
<!-- endSnippet -->
218218

219219

@@ -228,7 +228,7 @@ To disable this behavior globally use:
228228
```cs
229229
VerifierSettings.ModifySerialization(_ => _.DontIgnoreFalse());
230230
```
231-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L407-L411' title='Snippet source file'>snippet source</a> | <a href='#snippet-dontignorefalse' title='Start of snippet'>anchor</a></sup>
231+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L408-L412' title='Snippet source file'>snippet source</a> | <a href='#snippet-dontignorefalse' title='Start of snippet'>anchor</a></sup>
232232
<!-- endSnippet -->
233233

234234

@@ -382,7 +382,7 @@ public Task ScopedSerializerFluent()
382382
_ => { _.TypeNameHandling = TypeNameHandling.All; });
383383
}
384384
```
385-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1346-L1375' title='Snippet source file'>snippet source</a> | <a href='#snippet-scopedserializer' title='Start of snippet'>anchor</a></sup>
385+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1374-L1403' title='Snippet source file'>snippet source</a> | <a href='#snippet-scopedserializer' title='Start of snippet'>anchor</a></sup>
386386
<!-- endSnippet -->
387387

388388
Result:
@@ -445,7 +445,7 @@ public async Task IgnoreTypeFluent()
445445

446446
}
447447
```
448-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L851-L891' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoretype' title='Start of snippet'>anchor</a></sup>
448+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L852-L892' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoretype' title='Start of snippet'>anchor</a></sup>
449449
<!-- endSnippet -->
450450

451451
Result:
@@ -509,7 +509,7 @@ public async Task AddIgnoreInstanceFluent()
509509
_ => { _.IgnoreInstance<Instance>(x => x.Property == "Ignore"); });
510510
}
511511
```
512-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L797-L838' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoreinstance' title='Start of snippet'>anchor</a></sup>
512+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L798-L839' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoreinstance' title='Start of snippet'>anchor</a></sup>
513513
<!-- endSnippet -->
514514

515515
Result:
@@ -553,7 +553,7 @@ public Task WithObsoleteProp()
553553
return Verifier.Verify(target);
554554
}
555555
```
556-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1307-L1328' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoleteprop' title='Start of snippet'>anchor</a></sup>
556+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1335-L1356' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoleteprop' title='Start of snippet'>anchor</a></sup>
557557
<!-- endSnippet -->
558558

559559
Result:
@@ -601,7 +601,7 @@ public Task WithObsoletePropIncludedFluent()
601601
.ModifySerialization(_ => { _.IncludeObsoletes(); });
602602
}
603603
```
604-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1278-L1305' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoletepropincluded' title='Start of snippet'>anchor</a></sup>
604+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1306-L1333' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoletepropincluded' title='Start of snippet'>anchor</a></sup>
605605
<!-- endSnippet -->
606606

607607
Result:
@@ -666,7 +666,7 @@ public async Task IgnoreMemberByExpressionFluent()
666666
});
667667
}
668668
```
669-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L979-L1022' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbyexpression' title='Start of snippet'>anchor</a></sup>
669+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L980-L1023' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbyexpression' title='Start of snippet'>anchor</a></sup>
670670
<!-- endSnippet -->
671671

672672
Result:
@@ -734,7 +734,7 @@ public async Task IgnoreMemberByNameFluent()
734734
});
735735
}
736736
```
737-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1072-L1119' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbyname' title='Start of snippet'>anchor</a></sup>
737+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1073-L1120' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbyname' title='Start of snippet'>anchor</a></sup>
738738
<!-- endSnippet -->
739739

740740
Result:
@@ -780,7 +780,7 @@ public async Task CustomExceptionPropFluent()
780780
.ModifySerialization(_ => _.IgnoreMembersThatThrow<CustomException>());
781781
}
782782
```
783-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1178-L1197' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignoremembersthatthrow' title='Start of snippet'>anchor</a></sup>
783+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1206-L1225' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignoremembersthatthrow' title='Start of snippet'>anchor</a></sup>
784784
<!-- endSnippet -->
785785

786786
Result:
@@ -820,7 +820,7 @@ public async Task ExceptionMessagePropFluent()
820820
_ => _.IgnoreMembersThatThrow<Exception>(x => x.Message == "Ignore"));
821821
}
822822
```
823-
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L743-L767' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignoremembersthatthrowexpression' title='Start of snippet'>anchor</a></sup>
823+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L744-L768' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignoremembersthatthrowexpression' title='Start of snippet'>anchor</a></sup>
824824
<!-- endSnippet -->
825825

826826
Result:

readme.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,58 @@ The clipboard will again contain the following:
248248
See also: [Clipboard](/docs/clipboard.md)
249249

250250

251+
### VerifyJson
252+
253+
`VerifyJson` performs the following actions
254+
255+
* Convert to `JToken` (if necessary).
256+
* Apply [ignore member by name](serializer-settings.md#ignore-member-by-name) for keys.
257+
* PrettyPrint the resulting text.
258+
259+
<!-- snippet: VerifyJson -->
260+
<a id='snippet-verifyjson'></a>
261+
```cs
262+
[Fact]
263+
public Task VerifyJsonString()
264+
{
265+
var json = @"{'key': {'msg': 'No action taken'}}";
266+
return Verifier.VerifyJson(json);
267+
}
268+
269+
[Fact]
270+
public Task VerifyJsonStream()
271+
{
272+
var json = @"{'key': {'msg': 'No action taken'}}";
273+
var stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
274+
return Verifier.VerifyJson(stream);
275+
}
276+
277+
[Fact]
278+
public Task VerifyJsonJToken()
279+
{
280+
var json = @"{'key': {'msg': 'No action taken'}}";
281+
var target = JToken.Parse(json);
282+
return Verifier.VerifyJson(target);
283+
}
284+
```
285+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1145-L1170' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyjson' title='Start of snippet'>anchor</a></sup>
286+
<!-- endSnippet -->
287+
288+
Results in:
289+
290+
<!-- snippet: SerializationTests.VerifyJsonString.verified.txt -->
291+
<a id='snippet-SerializationTests.VerifyJsonString.verified.txt'></a>
292+
```txt
293+
{
294+
key: {
295+
msg: No action taken
296+
}
297+
}
298+
```
299+
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.VerifyJsonString.verified.txt#L1-L5' title='Snippet source file'>snippet source</a> | <a href='#snippet-SerializationTests.VerifyJsonString.verified.txt' title='Start of snippet'>anchor</a></sup>
300+
<!-- endSnippet -->
301+
302+
251303
#### The [Diff Tool](https://github.com/VerifyTests/DiffEngine) is will display the diff:
252304

253305
![SecondDiff](/docs/SecondDiff.png)

readme.source.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@ The clipboard will again contain the following:
116116
See also: [Clipboard](/docs/clipboard.md)
117117

118118

119+
### VerifyJson
120+
121+
`VerifyJson` performs the following actions
122+
123+
* Convert to `JToken` (if necessary).
124+
* Apply [ignore member by name](serializer-settings.md#ignore-member-by-name) for keys.
125+
* PrettyPrint the resulting text.
126+
127+
snippet: VerifyJson
128+
129+
Results in:
130+
131+
snippet: SerializationTests.VerifyJsonString.verified.txt
132+
133+
119134
#### The [Diff Tool](https://github.com/VerifyTests/DiffEngine) is will display the diff:
120135

121136
![SecondDiff](/docs/SecondDiff.png)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"key": {
3+
"msg": "No action taken"
4+
}
5+
}

src/StrictJsonTests/Tests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ public Task String()
2222
return Verifier.Verify("Foo");
2323
}
2424

25+
[Fact]
26+
public Task VerifyJsonString()
27+
{
28+
var json = @"{'key': {'msg': 'No action taken'}}";
29+
return Verifier.VerifyJson(json);
30+
}
31+
2532
[Fact]
2633
public Task Dynamic()
2734
{

src/Verify.MSTest/VerifyBase_Json.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Collections.Generic;
2+
using System.IO;
23
using System.Runtime.CompilerServices;
34
using System.Threading.Tasks;
5+
using Newtonsoft.Json.Linq;
46
using VerifyTests;
57

68
namespace VerifyMSTest
@@ -38,5 +40,29 @@ public SettingsTask Verify<T>(
3840
{
3941
return Verify(settings, sourceFile, _ => _.Verify(target));
4042
}
43+
44+
public SettingsTask VerifyJson(
45+
string target,
46+
VerifySettings? settings = null,
47+
[CallerFilePath] string sourceFile = "")
48+
{
49+
return Verify(settings, sourceFile, _ => _.VerifyJson(target));
50+
}
51+
52+
public SettingsTask VerifyJson(
53+
JToken target,
54+
VerifySettings? settings = null,
55+
[CallerFilePath] string sourceFile = "")
56+
{
57+
return Verify(settings, sourceFile, _ => _.VerifyJson(target));
58+
}
59+
60+
public SettingsTask VerifyJson(
61+
Stream target,
62+
VerifySettings? settings = null,
63+
[CallerFilePath] string sourceFile = "")
64+
{
65+
return Verify(settings, sourceFile, _ => _.VerifyJson(target));
66+
}
4167
}
4268
}

src/Verify.NUnit/Verifier_Json.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Collections.Generic;
2+
using System.IO;
23
using System.Runtime.CompilerServices;
34
using System.Threading.Tasks;
5+
using Newtonsoft.Json.Linq;
46
using VerifyTests;
57

68
namespace VerifyNUnit
@@ -38,5 +40,29 @@ public static SettingsTask Verify<T>(
3840
{
3941
return Verify(settings, sourceFile, _ => _.Verify(target));
4042
}
43+
44+
public static SettingsTask VerifyJson(
45+
string target,
46+
VerifySettings? settings = null,
47+
[CallerFilePath] string sourceFile = "")
48+
{
49+
return Verify(settings, sourceFile, _ => _.VerifyJson(target));
50+
}
51+
52+
public static SettingsTask VerifyJson(
53+
JToken target,
54+
VerifySettings? settings = null,
55+
[CallerFilePath] string sourceFile = "")
56+
{
57+
return Verify(settings, sourceFile, _ => _.VerifyJson(target));
58+
}
59+
60+
public static SettingsTask VerifyJson(
61+
Stream target,
62+
VerifySettings? settings = null,
63+
[CallerFilePath] string sourceFile = "")
64+
{
65+
return Verify(settings, sourceFile, _ => _.VerifyJson(target));
66+
}
4167
}
4268
}

0 commit comments

Comments
 (0)