Skip to content

Commit 771f370

Browse files
v1.5.0 (#21)
* - Add the support of FluentAssertions >= 6.0.0.
1 parent b779924 commit 771f370

File tree

6 files changed

+112
-25
lines changed

6 files changed

+112
-25
lines changed

.github/workflows/github-actions-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
type: string
88
description: The version of the library
99
required: true
10-
default: 1.4.0
10+
default: 1.5.0
1111
VersionSuffix:
1212
type: string
1313
description: The version suffix of the library (for example rc.1)

src/FluentAssertions.Json/FluentAssertions.Json.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
<PackageProjectUrl>https://github.com/PosInformatique/PosInformatique.FluentAssertions.Json</PackageProjectUrl>
1212
<PackageReadmeFile>README.md</PackageReadmeFile>
1313
<PackageReleaseNotes>
14+
1.5.0
15+
- Add the support of FluentAssertions 8.0.0.
16+
1417
1.4.0
1518
- Add new overload BeJsonDeserializableInto() method to test if a Stream contains a JSON serializable object.
1619

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright file="JsonAssertionFailedException.cs" company="P.O.S Informatique">
3+
// Copyright (c) P.O.S Informatique. All rights reserved.
4+
// </copyright>
5+
//-----------------------------------------------------------------------
6+
7+
namespace PosInformatique.FluentAssertions.Json
8+
{
9+
/// <summary>
10+
/// Occurs when an assertion related to JSON serialization / deserialization has been failed.
11+
/// </summary>
12+
public class JsonAssertionFailedException : Exception
13+
{
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="JsonAssertionFailedException"/> class.
16+
/// </summary>
17+
public JsonAssertionFailedException()
18+
: base()
19+
{
20+
}
21+
22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="JsonAssertionFailedException"/> class
24+
/// with the specified <paramref name="message"/>.
25+
/// </summary>
26+
/// <param name="message">Message of the exception.</param>
27+
public JsonAssertionFailedException(string message)
28+
: base(message)
29+
{
30+
}
31+
32+
/// <summary>
33+
/// Initializes a new instance of the <see cref="JsonAssertionFailedException"/> class
34+
/// with the specified <paramref name="message"/> and the <paramref name="innerException"/>.
35+
/// </summary>
36+
/// <param name="message">Message of the exception.</param>
37+
/// <param name="innerException">Inner exception related to the <see cref="JsonAssertionFailedException"/> to create.</param>
38+
public JsonAssertionFailedException(string message, Exception innerException)
39+
: base(message, innerException)
40+
{
41+
}
42+
}
43+
}

src/FluentAssertions.Json/JsonFluentAssertionsExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ private static void BeJsonSerializableIntoCore<TBase>(ObjectAssertions assertion
276276
{
277277
if (expectedJson != null)
278278
{
279-
Services.ThrowException("A JSON object was expected.");
279+
throw new JsonAssertionFailedException("A JSON object was expected.");
280280
}
281281
else
282282
{
@@ -288,7 +288,7 @@ private static void BeJsonSerializableIntoCore<TBase>(ObjectAssertions assertion
288288

289289
if (errors.Any())
290290
{
291-
Services.ThrowException(errors.First());
291+
throw new JsonAssertionFailedException(errors.First());
292292
}
293293
}
294294

@@ -329,7 +329,7 @@ private static void AreEquivalent<T>(T deserializedObject, T expectedObject)
329329

330330
if (errors.Any())
331331
{
332-
Services.ThrowException(errors.First());
332+
throw new JsonAssertionFailedException(errors.First());
333333
}
334334
}
335335
})
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright file="JsonAssertionFailedExceptionTest.cs" company="P.O.S Informatique">
3+
// Copyright (c) P.O.S Informatique. All rights reserved.
4+
// </copyright>
5+
//-----------------------------------------------------------------------
6+
7+
namespace PosInformatique.FluentAssertions.Json.Tests
8+
{
9+
using global::FluentAssertions;
10+
11+
public class JsonAssertionFailedExceptionTest
12+
{
13+
[Fact]
14+
public void Constructor()
15+
{
16+
var exception = new JsonAssertionFailedException();
17+
18+
exception.Message.Should().Be("Exception of type 'PosInformatique.FluentAssertions.Json.JsonAssertionFailedException' was thrown.");
19+
exception.InnerException.Should().BeNull();
20+
}
21+
22+
[Fact]
23+
public void Constructor_WithMessage()
24+
{
25+
var exception = new JsonAssertionFailedException("The message");
26+
27+
exception.Message.Should().Be("The message");
28+
exception.InnerException.Should().BeNull();
29+
}
30+
31+
[Fact]
32+
public void Constructor_WithMessageAndInnerException()
33+
{
34+
var innerException = new FormatException("The inner exception");
35+
var exception = new JsonAssertionFailedException("The message", innerException);
36+
37+
exception.Message.Should().Be("The message");
38+
exception.InnerException.Should().BeSameAs(innerException);
39+
}
40+
}
41+
}

tests/FluentAssertions.Json.Tests/JsonFluentAssertionsExtensionsTest.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void BeJsonSerializableInto_StringPropertyValueDifferent()
9999
});
100100
};
101101

102-
act.Should().ThrowExactly<XunitException>()
102+
act.Should().ThrowExactly<JsonAssertionFailedException>()
103103
.WithMessage("$.string_property: Expected 'Expected value' instead of 'Actual value'.");
104104
}
105105

@@ -121,7 +121,7 @@ public void BeJsonSerializableInto_Int32PropertyValueDifferent()
121121
});
122122
};
123123

124-
act.Should().ThrowExactly<XunitException>()
124+
act.Should().ThrowExactly<JsonAssertionFailedException>()
125125
.WithMessage("$.int32_property: Expected '100' instead of '1234'.");
126126
}
127127

@@ -147,7 +147,7 @@ public void BeJsonSerializableInto_BooleanPropertyValueDifferent(bool value, str
147147
});
148148
};
149149

150-
act.Should().ThrowExactly<XunitException>()
150+
act.Should().ThrowExactly<JsonAssertionFailedException>()
151151
.WithMessage($"$.boolean_property: Expected '{expectedValueString}' instead of '{actualValueString}'.");
152152
}
153153

@@ -173,7 +173,7 @@ public void BeJsonSerializableInto_NullPropertyValueDifferent()
173173
});
174174
};
175175

176-
act.Should().ThrowExactly<XunitException>()
176+
act.Should().ThrowExactly<JsonAssertionFailedException>()
177177
.WithMessage("$.null_property: Expected property to be 'String' type instead of 'Null' type.");
178178
}
179179

@@ -207,7 +207,7 @@ public void BeJsonSerializableInto_InnerObjectDifferentValue()
207207
});
208208
};
209209

210-
act.Should().ThrowExactly<XunitException>()
210+
act.Should().ThrowExactly<JsonAssertionFailedException>()
211211
.WithMessage("$.inner_object.inner_string_property: Expected 'Other inner string value' instead of 'Inner string value'.");
212212
}
213213

@@ -251,7 +251,7 @@ public void BeJsonSerializableInto_CollectionDifferentItemValue()
251251
});
252252
};
253253

254-
act.Should().ThrowExactly<XunitException>()
254+
act.Should().ThrowExactly<JsonAssertionFailedException>()
255255
.WithMessage("$.collection_int[1]: Expected '1234' instead of '20'.");
256256
}
257257

@@ -294,7 +294,7 @@ public void BeJsonSerializableInto_CollectionMissingItemDotNetClass()
294294
});
295295
};
296296

297-
act.Should().ThrowExactly<XunitException>()
297+
act.Should().ThrowExactly<JsonAssertionFailedException>()
298298
.WithMessage("$.collection_int: Expected 2 item(s) but found 1.");
299299
}
300300

@@ -337,7 +337,7 @@ public void BeJsonSerializableInto_CollectionMissingItemJson()
337337
});
338338
};
339339

340-
act.Should().ThrowExactly<XunitException>()
340+
act.Should().ThrowExactly<JsonAssertionFailedException>()
341341
.WithMessage("$.collection_int: Expected 1 item(s) but found 2.");
342342
}
343343

@@ -357,7 +357,7 @@ public void BeJsonSerializableInto_PropertyTypeDifferent_String()
357357
});
358358
};
359359

360-
act.Should().ThrowExactly<XunitException>()
360+
act.Should().ThrowExactly<JsonAssertionFailedException>()
361361
.WithMessage("$.string_property: Expected property to be 'Object' type instead of 'String' type.");
362362
}
363363

@@ -381,7 +381,7 @@ public void BeJsonSerializableInto_PropertyTypeDifferent_Number(object expectedV
381381
});
382382
};
383383

384-
act.Should().ThrowExactly<XunitException>()
384+
act.Should().ThrowExactly<JsonAssertionFailedException>()
385385
.WithMessage($"$.int32_property: Expected property to be '{expectedTypeMessage}' type instead of 'Number' type.");
386386
}
387387

@@ -407,7 +407,7 @@ public void BeJsonSerializableInto_PropertyTypeDifferent_Boolean(bool value, str
407407
});
408408
};
409409

410-
act.Should().ThrowExactly<XunitException>()
410+
act.Should().ThrowExactly<JsonAssertionFailedException>()
411411
.WithMessage($"$.boolean_property: Expected property to be 'Object' type instead of '{insteadOfMessageString}' type.");
412412
}
413413

@@ -441,7 +441,7 @@ public void BeJsonSerializableInto_PropertyTypeDifferent_Object(object value, st
441441
});
442442
};
443443

444-
act.Should().ThrowExactly<XunitException>()
444+
act.Should().ThrowExactly<JsonAssertionFailedException>()
445445
.WithMessage($"$.inner_object: Expected property to be '{expectedKindMessage}' type instead of 'Object' type.");
446446
}
447447

@@ -477,7 +477,7 @@ public void BeJsonSerializableInto_PropertyTypeDifferent_Array(object value, str
477477
});
478478
};
479479

480-
act.Should().ThrowExactly<XunitException>()
480+
act.Should().ThrowExactly<JsonAssertionFailedException>()
481481
.WithMessage($"$.collection_int: Expected property to be '{expectedKindMessage}' type instead of 'Array' type.");
482482
}
483483

@@ -497,7 +497,7 @@ public void BeJsonSerializableInto_PropertyNameDifferent()
497497
});
498498
};
499499

500-
act.Should().ThrowExactly<XunitException>()
500+
act.Should().ThrowExactly<JsonAssertionFailedException>()
501501
.WithMessage("$: Expected property with the 'string_property_other_name' name but found 'string_property' instead.");
502502
}
503503

@@ -518,7 +518,7 @@ public void BeJsonSerializableInto_DotNetPropertyMissing()
518518
});
519519
};
520520

521-
act.Should().ThrowExactly<XunitException>()
521+
act.Should().ThrowExactly<JsonAssertionFailedException>()
522522
.WithMessage("$: Expected 'new_property' property but found no property.");
523523
}
524524

@@ -537,7 +537,7 @@ public void BeJsonSerializableInto_JsonPropertyMissing()
537537
});
538538
};
539539

540-
act.Should().ThrowExactly<XunitException>()
540+
act.Should().ThrowExactly<JsonAssertionFailedException>()
541541
.WithMessage("$: Expected no property but found 'inner_string_property' property.");
542542
}
543543

@@ -551,7 +551,7 @@ public void BeJsonSerializableInto_NullSubject()
551551
json.Should().BeJsonSerializableInto(new { });
552552
};
553553

554-
act.Should().ThrowExactly<XunitException>()
554+
act.Should().ThrowExactly<JsonAssertionFailedException>()
555555
.WithMessage("A JSON object was expected.");
556556
}
557557

@@ -1221,7 +1221,7 @@ public void BeJsonDeserializableInto_WithObjectProperty_Number_Different(object
12211221
});
12221222
};
12231223

1224-
act.Should().ThrowExactly<XunitException>()
1224+
act.Should().ThrowExactly<JsonAssertionFailedException>()
12251225
.WithMessage($"$.inner.object_property: Expected '8888' instead of '{value}'.");
12261226
}
12271227

@@ -1249,7 +1249,7 @@ public void BeJsonDeserializableInto_WithObjectProperty_Number_WrongType(object
12491249
});
12501250
};
12511251

1252-
act.Should().ThrowExactly<XunitException>()
1252+
act.Should().ThrowExactly<JsonAssertionFailedException>()
12531253
.WithMessage($"$.inner.object_property: Expected property to be 'String' type instead of 'Number' type.");
12541254
}
12551255

@@ -1275,7 +1275,7 @@ public void BeJsonDeserializableInto_WithObjectProperty_String_Different()
12751275
});
12761276
};
12771277

1278-
act.Should().ThrowExactly<XunitException>()
1278+
act.Should().ThrowExactly<JsonAssertionFailedException>()
12791279
.WithMessage("$.inner.object_property: Expected 'The expected string' instead of 'The actual string'.");
12801280
}
12811281

@@ -1301,7 +1301,7 @@ public void BeJsonDeserializableInto_WithObjectProperty_String_WrongType()
13011301
});
13021302
};
13031303

1304-
act.Should().ThrowExactly<XunitException>()
1304+
act.Should().ThrowExactly<JsonAssertionFailedException>()
13051305
.WithMessage("$.inner.object_property: Expected property to be 'Number' type instead of 'String' type.");
13061306
}
13071307

0 commit comments

Comments
 (0)