-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BeValidJson for checking strings (#29)
* Add BeValidJson for checking strings It also allows consecutive checks on the parsed JSON using the Which property. The check does not limit the valid values to objects and arrays because of reasons explained here: https://stackoverflow.com/questions/7487869/is-this-simple-string-considered-valid-json
- Loading branch information
1 parent
28404d9
commit 6958420
Showing
6 changed files
with
160 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using FluentAssertions.Execution; | ||
using FluentAssertions.Primitives; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace FluentAssertions.Json | ||
{ | ||
public static class StringAssertionsExtensions | ||
{ | ||
[CustomAssertionAttribute] | ||
public static AndWhichConstraint<StringAssertions, JToken> BeValidJson( | ||
this StringAssertions stringAssertions, | ||
string because = "", | ||
params object[] becauseArgs) | ||
{ | ||
JToken json = null; | ||
|
||
try | ||
{ | ||
json = JToken.Parse(stringAssertions.Subject); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Execute.Assertion.BecauseOf(because, becauseArgs) | ||
.FailWith("Expected {context:string} to be valid JSON{reason}, but parsing failed with {0}.", ex.Message); | ||
} | ||
|
||
return new AndWhichConstraint<StringAssertions, JToken>(stringAssertions, json); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
Tests/FluentAssertions.Json.Shared.Specs/StringAssertionsExtensionsSpecs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using System; | ||
using Newtonsoft.Json.Linq; | ||
using Xunit; | ||
using Xunit.Sdk; | ||
|
||
namespace FluentAssertions.Json.Net45.Specs | ||
{ | ||
// ReSharper disable ExpressionIsAlwaysNull | ||
public class StringAssertionsExtensionsSpecs | ||
{ | ||
#region BeValidJson | ||
|
||
[Fact] | ||
public void When_checking_valid_json_BeValidJson_should_succeed() | ||
{ | ||
//----------------------------------------------------------------------------------------------------------- | ||
// Arrange | ||
//----------------------------------------------------------------------------------------------------------- | ||
string subject = "{ id: 42, admin: true }"; | ||
|
||
//----------------------------------------------------------------------------------------------------------- | ||
// Act | ||
//----------------------------------------------------------------------------------------------------------- | ||
Action act = () => subject.Should().BeValidJson(); | ||
|
||
//----------------------------------------------------------------------------------------------------------- | ||
// Assert | ||
//----------------------------------------------------------------------------------------------------------- | ||
act.Should().NotThrow(); | ||
} | ||
|
||
[Fact] | ||
public void When_checking_valid_json_BeValidJson_should_enable_consecutive_jtoken_assertions() | ||
{ | ||
//----------------------------------------------------------------------------------------------------------- | ||
// Arrange | ||
//----------------------------------------------------------------------------------------------------------- | ||
string subject = "{ id: 42 }"; | ||
|
||
//----------------------------------------------------------------------------------------------------------- | ||
// Act | ||
//----------------------------------------------------------------------------------------------------------- | ||
object which = subject.Should().BeValidJson().Which; | ||
|
||
//----------------------------------------------------------------------------------------------------------- | ||
// Assert | ||
//----------------------------------------------------------------------------------------------------------- | ||
which.Should().BeAssignableTo<JToken>(); | ||
} | ||
|
||
[Fact] | ||
public void When_checking_null_BeValidJson_should_fail() | ||
{ | ||
//----------------------------------------------------------------------------------------------------------- | ||
// Arrange | ||
//----------------------------------------------------------------------------------------------------------- | ||
string subject = null; | ||
Exception caughtException = null; | ||
|
||
//----------------------------------------------------------------------------------------------------------- | ||
// Act | ||
//----------------------------------------------------------------------------------------------------------- | ||
try | ||
{ | ||
subject.Should().BeValidJson("null is not allowed"); | ||
} | ||
catch (Exception ex) | ||
{ | ||
caughtException = ex; | ||
} | ||
|
||
//----------------------------------------------------------------------------------------------------------- | ||
// Assert | ||
//----------------------------------------------------------------------------------------------------------- | ||
caughtException.Should() | ||
.BeOfType<XunitException>() | ||
.Which.Message.Should() | ||
.Match("Expected subject to be valid JSON because null is not allowed, but parsing failed with \"*\"."); | ||
} | ||
|
||
[Fact] | ||
public void When_checking_invalid_json_BeValidJson_should_fail() | ||
{ | ||
//----------------------------------------------------------------------------------------------------------- | ||
// Arrange | ||
//----------------------------------------------------------------------------------------------------------- | ||
string subject = "invalid json"; | ||
Exception caughtException = null; | ||
|
||
//----------------------------------------------------------------------------------------------------------- | ||
// Act | ||
//----------------------------------------------------------------------------------------------------------- | ||
try | ||
{ | ||
subject.Should().BeValidJson("we like {0}", "JSON"); | ||
} | ||
catch (Exception ex) | ||
{ | ||
caughtException = ex; | ||
} | ||
|
||
//----------------------------------------------------------------------------------------------------------- | ||
// Assert | ||
//----------------------------------------------------------------------------------------------------------- | ||
caughtException.Should() | ||
.BeOfType<XunitException>() | ||
.Which.Message.Should() | ||
.Match("Expected subject to be valid JSON because we like JSON, but parsing failed with \"*\"."); | ||
} | ||
|
||
#endregion | ||
|
||
} | ||
} |