Skip to content

Commit 33c9a0d

Browse files
authored
Add support for Fluent Assertions 6 (#55)
1 parent 7aef321 commit 33c9a0d

File tree

6 files changed

+57
-70
lines changed

6 files changed

+57
-70
lines changed

Src/FluentAssertions.Json/FluentAssertions.Json.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net451;netstandard1.4</TargetFrameworks>
3+
<TargetFrameworks>net47;netstandard2.0</TargetFrameworks>
44
<GenerateDocumentationFile>true</GenerateDocumentationFile>
55
<NoWarn>1701;1702;1705;1591;1574;1572;1573;419</NoWarn>
66
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
@@ -17,10 +17,10 @@
1717
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
1818
<PackageIcon>FluentAssertions.png</PackageIcon>
1919
<PackageReleaseNotes>See https://github.com/fluentassertions/fluentassertions.json/releases/</PackageReleaseNotes>
20-
<Copyright>Copyright Dennis Doomen 2010-2020</Copyright>
21-
<LangVersion>8.0</LangVersion>
20+
<Copyright>Copyright Dennis Doomen 2010-2021</Copyright>
21+
<LangVersion>9.0</LangVersion>
2222
</PropertyGroup>
23-
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net451|AnyCPU'">
23+
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net47|AnyCPU'">
2424
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
2525
</PropertyGroup>
2626
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
@@ -30,7 +30,7 @@
3030
<OutputPath>..\..\Artifacts\bin\</OutputPath>
3131
</PropertyGroup>
3232
<ItemGroup>
33-
<PackageReference Include="FluentAssertions" Version="5.10.2" />
33+
<PackageReference Include="FluentAssertions" Version="6.0.0" />
3434
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
3535
<None Include="..\FluentAssertions.png" Pack="true" Visible="false" PackagePath="" />
3636
</ItemGroup>

Src/FluentAssertions.Json/JTokenAssertions.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
using System.Diagnostics;
2-
using FluentAssertions.Collections;
1+
using FluentAssertions.Collections;
32
using FluentAssertions.Json.Common;
43
using FluentAssertions.Execution;
54
using FluentAssertions.Formatting;
65
using FluentAssertions.Primitives;
76
using Newtonsoft.Json.Linq;
8-
using System.Text.RegularExpressions;
97
using System;
8+
using System.Diagnostics;
9+
using System.Text.RegularExpressions;
1010

1111
namespace FluentAssertions.Json
1212
{
@@ -27,9 +27,8 @@ static JTokenAssertions()
2727
/// Initializes a new instance of the <see cref="JTokenAssertions" /> class.
2828
/// </summary>
2929
/// <param name="subject">The subject</param>
30-
public JTokenAssertions(JToken subject)
30+
public JTokenAssertions(JToken subject) : base(subject)
3131
{
32-
Subject = subject;
3332
EnumerableSubject = new GenericCollectionAssertions<JToken>(subject);
3433
}
3534

@@ -469,10 +468,16 @@ public AndConstraint<JTokenAssertions> ContainSubtree(JToken subtree, string bec
469468

470469
public string Format(JToken value, bool useLineBreaks = false)
471470
{
472-
return new JTokenFormatter().Format(value, new FormattingContext
471+
// SMELL: Why is this method necessary at all?
472+
// SMELL: Why aren't we using the Formatter class directly?
473+
var output = new FormattedObjectGraph(maxLines: 100);
474+
475+
new JTokenFormatter().Format(value, output, new FormattingContext
473476
{
474477
UseLineBreaks = useLineBreaks
475478
}, null);
479+
480+
return output.ToString();
476481
}
477482
}
478483
}
Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Collections.Generic;
21
using FluentAssertions.Formatting;
32
using FluentAssertions.Json.Common;
43
using Newtonsoft.Json.Linq;
@@ -22,26 +21,26 @@ public bool CanHandle(object value)
2221
return value is JToken;
2322
}
2423

25-
/// <summary>
26-
/// Returns a <see cref="string" /> that represents this instance.
27-
/// </summary>
28-
/// <param name="value">The value for which to create a <see cref="string"/>.</param>
29-
/// <param name="useLineBreaks"> </param>
30-
/// <param name="processedObjects">
31-
/// A collection of objects that
32-
/// </param>
33-
/// <param name="nestedPropertyLevel">
34-
/// The level of nesting for the supplied value. This is used for indenting the format string for objects that have
35-
/// no <see cref="object.ToString()"/> override.
36-
/// </param>
37-
/// <returns>
38-
/// A <see cref="string" /> that represents this instance.
39-
/// </returns>
40-
public string Format(object value, FormattingContext context, FormatChild formatChild)
24+
public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
4125
{
42-
var jToken = (JToken)value;
43-
string result = context.UseLineBreaks ? jToken?.ToString(Newtonsoft.Json.Formatting.Indented) : jToken?.ToString().RemoveNewLines();
44-
return result ?? "<null>";
26+
var jToken = value as JToken;
27+
28+
if (context.UseLineBreaks)
29+
{
30+
var result = jToken?.ToString(Newtonsoft.Json.Formatting.Indented);
31+
if (result is not null)
32+
{
33+
formattedGraph.AddFragmentOnNewLine(result);
34+
}
35+
else
36+
{
37+
formattedGraph.AddFragment("<null>");
38+
}
39+
}
40+
else
41+
{
42+
formattedGraph.AddFragment(jToken?.ToString().RemoveNewLines() ?? "<null>");
43+
}
4544
}
4645
}
4746
}

Tests/FluentAssertions.Json.Specs/FluentAssertions.Json.Specs.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="AutoFixture" Version="4.17.0" />
12-
<PackageReference Include="FluentAssertions" Version="5.10.3" />
1312
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
1413
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
15-
<PackageReference Include="xunit" Version="2.4.0" />
14+
<PackageReference Include="xunit" Version="2.4.1" />
1615
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
1716
<PackageReference Include="coverlet.collector" Version="1.0.1" />
1817
</ItemGroup>

Tests/FluentAssertions.Json.Specs/JTokenAssertionsSpecs.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1215,10 +1215,14 @@ public void When_checking_subtree_with_an_invalid_expected_string_it_should_prov
12151215

12161216
private static string Format(JToken value, bool useLineBreaks = false)
12171217
{
1218-
return new JTokenFormatter().Format(value, new FormattingContext
1218+
var output = new FormattedObjectGraph(100);
1219+
1220+
new JTokenFormatter().Format(value, output, new FormattingContext
12191221
{
12201222
UseLineBreaks = useLineBreaks
12211223
}, null);
1224+
1225+
return output.ToString();
12221226
}
12231227
}
12241228
}

Tests/FluentAssertions.Json.Specs/JTokenFormatterSpecs.cs

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,40 @@ namespace FluentAssertions.Json.Specs
77
// ReSharper disable InconsistentNaming
88
public class JTokenFormatterSpecs
99
{
10+
public JTokenFormatterSpecs()
11+
{
12+
Formatter.AddFormatter(new JTokenFormatter());
13+
}
14+
1015
[Fact]
1116
public void Should_Handle_JToken()
1217
{
1318
//-----------------------------------------------------------------------------------------------------------
14-
// Arrange
15-
//-----------------------------------------------------------------------------------------------------------
16-
var sut = new JTokenFormatter();
17-
19+
// Act / Arrange
1820
//-----------------------------------------------------------------------------------------------------------
19-
// Act
20-
//-----------------------------------------------------------------------------------------------------------
21-
var actual = sut.CanHandle(JToken.Parse("{}"));
21+
var actual = Formatter.ToString(JToken.Parse("{}"));
2222

2323
//-----------------------------------------------------------------------------------------------------------
2424
// Assert
2525
//-----------------------------------------------------------------------------------------------------------
26-
actual.Should().BeTrue();
26+
actual.Should().Be("{}");
2727
}
2828

29-
[Fact]
30-
public void Should_not_handle_anything_else()
31-
{
32-
//-----------------------------------------------------------------------------------------------------------
33-
// Arrange
34-
//-----------------------------------------------------------------------------------------------------------
35-
var testCases = new object[] { null, string.Empty };
36-
var sut = new JTokenFormatter();
37-
38-
foreach (var testCase in testCases)
39-
{
40-
//-----------------------------------------------------------------------------------------------------------
41-
// Act
42-
//-----------------------------------------------------------------------------------------------------------
43-
var actual = sut.CanHandle(testCase);
44-
45-
//-----------------------------------------------------------------------------------------------------------
46-
// Assert
47-
//-----------------------------------------------------------------------------------------------------------
48-
actual.Should().BeFalse();
49-
}
50-
}
51-
52-
5329
[Fact]
5430
public void Should_preserve_indenting()
5531
{
5632
//-----------------------------------------------------------------------------------------------------------
5733
// Arrange
5834
//-----------------------------------------------------------------------------------------------------------
5935
var json = JToken.Parse("{ \"id\":1 }");
60-
var sut = new JTokenFormatter();
6136

6237
//-----------------------------------------------------------------------------------------------------------
6338
// Act
6439
//-----------------------------------------------------------------------------------------------------------
65-
var actual = sut.Format(json, new FormattingContext{UseLineBreaks = true}, (path, value) => "");
40+
var actual = Formatter.ToString(json, new FormattingOptions
41+
{
42+
UseLineBreaks = true
43+
});
6644

6745
//-----------------------------------------------------------------------------------------------------------
6846
// Assert
@@ -77,13 +55,15 @@ public void Should_Remove_line_breaks_and_indenting()
7755
// Arrange
7856
//-----------------------------------------------------------------------------------------------------------
7957
var json = JToken.Parse("{ \"id\":1 }");
80-
var sut = new JTokenFormatter();
8158

8259
//-----------------------------------------------------------------------------------------------------------
8360
// Act
8461
//-----------------------------------------------------------------------------------------------------------
8562
// ReSharper disable once RedundantArgumentDefaultValue
86-
var actual = sut.Format(json, new FormattingContext{UseLineBreaks = false}, (path, value) => "");
63+
var actual = Formatter.ToString(json, new FormattingOptions
64+
{
65+
UseLineBreaks = false
66+
});
8767

8868
//-----------------------------------------------------------------------------------------------------------
8969
// Assert

0 commit comments

Comments
 (0)