Skip to content

Commit

Permalink
Add support for Fluent Assertions 6 (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisdoomen committed Aug 14, 2021
1 parent 7aef321 commit 33c9a0d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 70 deletions.
10 changes: 5 additions & 5 deletions Src/FluentAssertions.Json/FluentAssertions.Json.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net451;netstandard1.4</TargetFrameworks>
<TargetFrameworks>net47;netstandard2.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>1701;1702;1705;1591;1574;1572;1573;419</NoWarn>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
Expand All @@ -17,10 +17,10 @@
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageIcon>FluentAssertions.png</PackageIcon>
<PackageReleaseNotes>See https://github.com/fluentassertions/fluentassertions.json/releases/</PackageReleaseNotes>
<Copyright>Copyright Dennis Doomen 2010-2020</Copyright>
<LangVersion>8.0</LangVersion>
<Copyright>Copyright Dennis Doomen 2010-2021</Copyright>
<LangVersion>9.0</LangVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net451|AnyCPU'">
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net47|AnyCPU'">
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
Expand All @@ -30,7 +30,7 @@
<OutputPath>..\..\Artifacts\bin\</OutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.2" />
<PackageReference Include="FluentAssertions" Version="6.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<None Include="..\FluentAssertions.png" Pack="true" Visible="false" PackagePath="" />
</ItemGroup>
Expand Down
17 changes: 11 additions & 6 deletions Src/FluentAssertions.Json/JTokenAssertions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Diagnostics;
using FluentAssertions.Collections;
using FluentAssertions.Collections;
using FluentAssertions.Json.Common;
using FluentAssertions.Execution;
using FluentAssertions.Formatting;
using FluentAssertions.Primitives;
using Newtonsoft.Json.Linq;
using System.Text.RegularExpressions;
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace FluentAssertions.Json
{
Expand All @@ -27,9 +27,8 @@ static JTokenAssertions()
/// Initializes a new instance of the <see cref="JTokenAssertions" /> class.
/// </summary>
/// <param name="subject">The subject</param>
public JTokenAssertions(JToken subject)
public JTokenAssertions(JToken subject) : base(subject)
{
Subject = subject;
EnumerableSubject = new GenericCollectionAssertions<JToken>(subject);
}

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

public string Format(JToken value, bool useLineBreaks = false)
{
return new JTokenFormatter().Format(value, new FormattingContext
// SMELL: Why is this method necessary at all?
// SMELL: Why aren't we using the Formatter class directly?
var output = new FormattedObjectGraph(maxLines: 100);

new JTokenFormatter().Format(value, output, new FormattingContext
{
UseLineBreaks = useLineBreaks
}, null);

return output.ToString();
}
}
}
39 changes: 19 additions & 20 deletions Src/FluentAssertions.Json/JTokenFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using FluentAssertions.Formatting;
using FluentAssertions.Json.Common;
using Newtonsoft.Json.Linq;
Expand All @@ -22,26 +21,26 @@ public bool CanHandle(object value)
return value is JToken;
}

/// <summary>
/// Returns a <see cref="string" /> that represents this instance.
/// </summary>
/// <param name="value">The value for which to create a <see cref="string"/>.</param>
/// <param name="useLineBreaks"> </param>
/// <param name="processedObjects">
/// A collection of objects that
/// </param>
/// <param name="nestedPropertyLevel">
/// The level of nesting for the supplied value. This is used for indenting the format string for objects that have
/// no <see cref="object.ToString()"/> override.
/// </param>
/// <returns>
/// A <see cref="string" /> that represents this instance.
/// </returns>
public string Format(object value, FormattingContext context, FormatChild formatChild)
public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
{
var jToken = (JToken)value;
string result = context.UseLineBreaks ? jToken?.ToString(Newtonsoft.Json.Formatting.Indented) : jToken?.ToString().RemoveNewLines();
return result ?? "<null>";
var jToken = value as JToken;

if (context.UseLineBreaks)
{
var result = jToken?.ToString(Newtonsoft.Json.Formatting.Indented);
if (result is not null)
{
formattedGraph.AddFragmentOnNewLine(result);
}
else
{
formattedGraph.AddFragment("<null>");
}
}
else
{
formattedGraph.AddFragment(jToken?.ToString().RemoveNewLines() ?? "<null>");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@

<ItemGroup>
<PackageReference Include="AutoFixture" Version="4.17.0" />
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>
Expand Down
6 changes: 5 additions & 1 deletion Tests/FluentAssertions.Json.Specs/JTokenAssertionsSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1215,10 +1215,14 @@ public void When_checking_subtree_with_an_invalid_expected_string_it_should_prov

private static string Format(JToken value, bool useLineBreaks = false)
{
return new JTokenFormatter().Format(value, new FormattingContext
var output = new FormattedObjectGraph(100);

new JTokenFormatter().Format(value, output, new FormattingContext
{
UseLineBreaks = useLineBreaks
}, null);

return output.ToString();
}
}
}
52 changes: 16 additions & 36 deletions Tests/FluentAssertions.Json.Specs/JTokenFormatterSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,40 @@ namespace FluentAssertions.Json.Specs
// ReSharper disable InconsistentNaming
public class JTokenFormatterSpecs
{
public JTokenFormatterSpecs()
{
Formatter.AddFormatter(new JTokenFormatter());
}

[Fact]
public void Should_Handle_JToken()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var sut = new JTokenFormatter();

// Act / Arrange
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
var actual = sut.CanHandle(JToken.Parse("{}"));
var actual = Formatter.ToString(JToken.Parse("{}"));

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
actual.Should().BeTrue();
actual.Should().Be("{}");
}

[Fact]
public void Should_not_handle_anything_else()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var testCases = new object[] { null, string.Empty };
var sut = new JTokenFormatter();

foreach (var testCase in testCases)
{
//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
var actual = sut.CanHandle(testCase);

//-----------------------------------------------------------------------------------------------------------
// Assert
//-----------------------------------------------------------------------------------------------------------
actual.Should().BeFalse();
}
}


[Fact]
public void Should_preserve_indenting()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var json = JToken.Parse("{ \"id\":1 }");
var sut = new JTokenFormatter();

//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
var actual = sut.Format(json, new FormattingContext{UseLineBreaks = true}, (path, value) => "");
var actual = Formatter.ToString(json, new FormattingOptions
{
UseLineBreaks = true
});

//-----------------------------------------------------------------------------------------------------------
// Assert
Expand All @@ -77,13 +55,15 @@ public void Should_Remove_line_breaks_and_indenting()
// Arrange
//-----------------------------------------------------------------------------------------------------------
var json = JToken.Parse("{ \"id\":1 }");
var sut = new JTokenFormatter();

//-----------------------------------------------------------------------------------------------------------
// Act
//-----------------------------------------------------------------------------------------------------------
// ReSharper disable once RedundantArgumentDefaultValue
var actual = sut.Format(json, new FormattingContext{UseLineBreaks = false}, (path, value) => "");
var actual = Formatter.ToString(json, new FormattingOptions
{
UseLineBreaks = false
});

//-----------------------------------------------------------------------------------------------------------
// Assert
Expand Down

0 comments on commit 33c9a0d

Please sign in to comment.