Skip to content

Commit fd01bcb

Browse files
authored
Upgraded dependency to Fluent Assertions 5.0 RC2 (#14)
1 parent 15850d8 commit fd01bcb

13 files changed

Lines changed: 169 additions & 54 deletions

FluentAssertions.Json.sln

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
77
ProjectSection(SolutionItems) = preProject
88
Tests\Default.testsettings = Tests\Default.testsettings
99
Src\FluentAssertions.nuspec = Src\FluentAssertions.nuspec
10-
.nuget\nuget.config = .nuget\nuget.config
1110
README.md = README.md
12-
Settings.StyleCop = Settings.StyleCop
13-
Src\SolutionInfo.cs = Src\SolutionInfo.cs
1411
EndProjectSection
1512
EndProject
1613
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Json.Shared.Specs", "Tests\FluentAssertions.Json.Shared.Specs\Json.Shared.Specs.shproj", "{845ED06B-1F6D-4442-BCCC-52483561F447}"

FluentAssertions.Json.sln.DotSettings

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
<s:String x:Key="/Default/CodeStyle/CodeCleanup/Profiles/=Safe_0020Cleanup/@EntryIndexedValue">&lt;?xml version="1.0" encoding="utf-16"?&gt;&lt;Profile name="Safe Cleanup"&gt;&lt;CSArrangeThisQualifier&gt;True&lt;/CSArrangeThisQualifier&gt;&lt;RemoveCodeRedundancies&gt;True&lt;/RemoveCodeRedundancies&gt;&lt;CSUseAutoProperty&gt;True&lt;/CSUseAutoProperty&gt;&lt;CSMakeFieldReadonly&gt;True&lt;/CSMakeFieldReadonly&gt;&lt;CSOptimizeUsings&gt;&lt;OptimizeUsings&gt;True&lt;/OptimizeUsings&gt;&lt;EmbraceInRegion&gt;False&lt;/EmbraceInRegion&gt;&lt;RegionName&gt;&lt;/RegionName&gt;&lt;/CSOptimizeUsings&gt;&lt;CSShortenReferences&gt;True&lt;/CSShortenReferences&gt;&lt;CSReformatCode&gt;True&lt;/CSReformatCode&gt;&lt;/Profile&gt;</s:String>
1010
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_CODE/@EntryValue">1</s:Int64>
1111
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_BLANK_LINES_IN_DECLARATIONS/@EntryValue">1</s:Int64>
12+
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_ACCESSORHOLDER_ATTRIBUTE_ON_SAME_LINE_EX/@EntryValue">NEVER</s:String>
1213
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_FIELD_ATTRIBUTE_ON_SAME_LINE/@EntryValue">False</s:Boolean>
14+
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_FIELD_ATTRIBUTE_ON_SAME_LINE_EX/@EntryValue">NEVER</s:String>
1315
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_WHILE_ON_NEW_LINE/@EntryValue">True</s:Boolean>
1416
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_AFTER_TYPECAST_PARENTHESES/@EntryValue">False</s:Boolean>
1517
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_LIMIT/@EntryValue">130</s:Int64>
@@ -57,7 +59,11 @@
5759
<s:String x:Key="/Default/Environment/Editor/MatchingBraceHighlighting/Style/@EntryValue">OUTLINE</s:String>
5860
<s:String x:Key="/Default/Environment/Hierarchy/PsiConfigurationSettingsKey/LocationType/@EntryValue">SOLUTION_FOLDER</s:String>
5961
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpAttributeForSingleLineMethodUpgrade/@EntryIndexedValue">True</s:Boolean>
62+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
63+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
64+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpRenamePlacementToArrangementMigration/@EntryIndexedValue">True</s:Boolean>
6065
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAddAccessorOwnerDeclarationBracesMigration/@EntryIndexedValue">True</s:Boolean>
66+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002ECSharpPlaceAttributeOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
6167
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
6268
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean>
6369
<s:String x:Key="/Default/Environment/UnitTesting/MsTestProvider/RunConfigurationFilename/@EntryValue">D:\Workspaces\FluentAssertions\Default.testsettings</s:String>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System;
2+
using System.Linq;
3+
using FluentAssertions.Formatting;
4+
5+
namespace FluentAssertions.Json.Common
6+
{
7+
internal static class StringExtensions
8+
{
9+
/// <summary>
10+
/// Finds the first index at which the <paramref name="value"/> does not match the <paramref name="expected"/>
11+
/// string anymore, including the exact casing.
12+
/// </summary>
13+
public static int IndexOfFirstMismatch(this string value, string expected)
14+
{
15+
return IndexOfFirstMismatch(value, expected, StringComparison.CurrentCulture);
16+
}
17+
18+
/// <summary>
19+
/// Finds the first index at which the <paramref name="value"/> does not match the <paramref name="expected"/>
20+
/// string anymore, accounting for the specified <paramref name="stringComparison"/>.
21+
/// </summary>
22+
public static int IndexOfFirstMismatch(this string value, string expected, StringComparison stringComparison)
23+
{
24+
for (int index = 0; index < value.Length; index++)
25+
{
26+
if ((index >= expected.Length) || !value[index].ToString().Equals(expected[index].ToString(), stringComparison))
27+
{
28+
return index;
29+
}
30+
}
31+
32+
return -1;
33+
}
34+
35+
/// <summary>
36+
/// Gets the quoted three characters at the specified index of a string, including the index itself.
37+
/// </summary>
38+
public static string IndexedSegmentAt(this string value, int index)
39+
{
40+
int length = Math.Min(value.Length - index, 3);
41+
42+
return $"{Formatter.ToString(value.Substring(index, length))} (index {index})".Replace("{", "{{").Replace("}", "}}");
43+
}
44+
45+
/// <summary>
46+
/// Replaces all characters that might conflict with formatting placeholders and newlines with their escaped counterparts.
47+
/// </summary>
48+
public static string Escape(this string value, bool escapePlaceholders = false)
49+
{
50+
value = value.Replace("\"", "\\\"").Replace("\n", @"\n").Replace("\r", @"\r");
51+
if (escapePlaceholders)
52+
{
53+
value = value.Replace("{", "{{").Replace("}", "}}");
54+
}
55+
56+
return value;
57+
}
58+
59+
/// <summary>
60+
/// Joins a string with one or more other strings using a specified separator.
61+
/// </summary>
62+
/// <remarks>
63+
/// Any string that is empty (including the original string) is ignored.
64+
/// </remarks>
65+
public static string Combine(this string @this, string other, string separator = ".")
66+
{
67+
var strings = new[] { @this }.Concat(new[] {other}).Where(s => s.Length > 0).ToArray();
68+
return string.Join(separator, strings);
69+
}
70+
71+
/// <summary>
72+
/// Changes the first character of a string to uppercase.
73+
/// </summary>
74+
public static string Capitalize(this string @this)
75+
{
76+
char[] charArray = @this.ToCharArray();
77+
charArray[0] = char.ToUpper(charArray[0]);
78+
return new string(charArray);
79+
}
80+
81+
public static string RemoveNewLines(this string @this)
82+
{
83+
return @this.Replace("\n", "").Replace("\r", "").Replace("\\r\\n", "");
84+
}
85+
}
86+
}

Src/FluentAssertions.Json/FluentAssertions.Json.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
1313
</PropertyGroup>
1414
<ItemGroup>
15-
<PackageReference Include="FluentAssertions" Version="5.0.0-beta0001" />
15+
<PackageReference Include="FluentAssertions" Version="5.0.0-rc0002" />
1616
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
1717
</ItemGroup>
1818
</Project>

Src/FluentAssertions.Json/JTokenAssertions.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Diagnostics;
22
using FluentAssertions.Collections;
3-
using FluentAssertions.Common;
3+
using FluentAssertions.Json.Common;
44
using FluentAssertions.Execution;
55
using FluentAssertions.Formatting;
66
using FluentAssertions.Primitives;
@@ -34,7 +34,7 @@ public JTokenAssertions(JToken subject)
3434
/// <summary>
3535
/// Returns the type of the subject the assertion applies on.
3636
/// </summary>
37-
protected override string Context => nameof(JToken);
37+
protected override string Identifier => nameof(JToken);
3838

3939
/// <summary>
4040
/// Asserts that the current <see cref="JToken" /> equals the <paramref name="expected" /> element.
@@ -131,10 +131,10 @@ public AndConstraint<JTokenAssertions> BeEquivalentTo(JToken expected, string be
131131
{
132132
ObjectDiffPatchResult diff = ObjectDiffPatch.GenerateDiff(Subject, expected);
133133
JToken firstDifferingToken = diff.NewValues?.First ?? diff.OldValues?.First;
134-
JTokenFormatter formatter = new JTokenFormatter();
135-
136-
var message = $"Expected JSON document {formatter.ToString(Subject, true).Replace("{", "{{").Replace("}", "}}")}" +
137-
$" to be equivalent to {formatter.ToString(expected, true).Replace("{", "{{").Replace("}", "}}")}" +
134+
135+
136+
var message = $"Expected JSON document {Format(Subject, true).Replace("{", "{{").Replace("}", "}}")}" +
137+
$" to be equivalent to {Format(expected, true).Replace("{", "{{").Replace("}", "}}")}" +
138138
$"{{reason}}, but differs at {firstDifferingToken?.ToString().Replace("{", "{{").Replace("}", "}}")}.";
139139

140140
Execute.Assertion
@@ -304,7 +304,7 @@ public AndWhichConstraint<JTokenAssertions, JToken> NotHaveElement(string unexpe
304304
public AndWhichConstraint<JTokenAssertions, JToken> ContainSingleItem(string because = "", params object[] becauseArgs)
305305
{
306306
var formatter = new JTokenFormatter();
307-
string formattedDocument = formatter.ToString(Subject).Replace("{", "{{").Replace("}", "}}");
307+
string formattedDocument = Format(Subject).Replace("{", "{{").Replace("}", "}}");
308308

309309
using (new AssertionScope("JSON document " + formattedDocument))
310310
{
@@ -327,13 +327,22 @@ public AndWhichConstraint<JTokenAssertions, JToken> ContainSingleItem(string bec
327327
public AndConstraint<JTokenAssertions> HaveCount(int expected, string because = "", params object[] becauseArgs)
328328
{
329329
var formatter = new JTokenFormatter();
330-
string formattedDocument = formatter.ToString(Subject).Replace("{", "{{").Replace("}", "}}");
330+
string formattedDocument = Format(Subject).Replace("{", "{{").Replace("}", "}}");
331331

332332
using (new AssertionScope("JSON document " + formattedDocument))
333333
{
334334
EnumerableSubject.HaveCount(expected, because, becauseArgs);
335335
return new AndConstraint<JTokenAssertions>(this);
336336
}
337337
}
338+
339+
public string Format(JToken value, bool useLineBreaks = false)
340+
{
341+
return new JTokenFormatter().Format(value, new FormattingContext
342+
{
343+
UseLineBreaks = useLineBreaks
344+
}, null);
345+
}
346+
338347
}
339348
}

Src/FluentAssertions.Json/JTokenFormatter.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Collections.Generic;
2-
using FluentAssertions.Common;
32
using FluentAssertions.Formatting;
3+
using FluentAssertions.Json.Common;
44
using Newtonsoft.Json.Linq;
55

66
namespace FluentAssertions.Json
@@ -37,11 +37,10 @@ public bool CanHandle(object value)
3737
/// <returns>
3838
/// A <see cref="string" /> that represents this instance.
3939
/// </returns>
40-
public string ToString(object value, bool useLineBreaks = false, IList<object> processedObjects = null,
41-
int nestedPropertyLevel = 0)
40+
public string Format(object value, FormattingContext context, FormatChild formatChild)
4241
{
4342
var jToken = (JToken)value;
44-
string result = useLineBreaks ? jToken?.ToString(Newtonsoft.Json.Formatting.Indented) : jToken?.ToString().RemoveNewLines();
43+
string result = context.UseLineBreaks ? jToken?.ToString(Newtonsoft.Json.Formatting.Indented) : jToken?.ToString().RemoveNewLines();
4544
return result ?? "<null>";
4645
}
4746
}

Src/FluentAssertions.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<requireLicenseAcceptance>false</requireLicenseAcceptance>
1111
<description>Fluent Assertions extensions for Newtonsoft.Json</description>
1212
<summary>Fluent Assertions extensions for Newtonsoft.Json</summary>
13-
<copyright>Copyright Dennis Doomen 2010-2017</copyright>
13+
<copyright>Copyright Dennis Doomen 2010-2018</copyright>
1414
<releaseNotes>
1515
See https://github.com/fluentassertions/fluentassertions/releases/
1616
</releaseNotes>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
<SignAssembly>false</SignAssembly>
3838
</PropertyGroup>
3939
<ItemGroup>
40-
<Reference Include="FluentAssertions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL">
41-
<HintPath>..\..\packages\FluentAssertions.5.0.0-beta0001\lib\net451\FluentAssertions.dll</HintPath>
40+
<Reference Include="FluentAssertions, Version=5.0.0.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a">
41+
<HintPath>..\..\packages\FluentAssertions.5.0.0-rc0002\lib\net45\FluentAssertions.dll</HintPath>
4242
</Reference>
4343
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
4444
<HintPath>..\..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>

Tests/FluentAssertions.Json.Net45.Specs/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="FluentAssertions" version="5.0.0-beta0001" targetFramework="net451" />
3+
<package id="FluentAssertions" version="5.0.0-rc0002" targetFramework="net451" />
44
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net451" />
55
<package id="System.Collections" version="4.0.11" targetFramework="net451" />
66
<package id="System.Collections.Concurrent" version="4.0.12" targetFramework="net451" />

0 commit comments

Comments
 (0)