Skip to content

Commit 50c002d

Browse files
author
sam.segers
committed
reuse JToken.Parse error handling
1 parent b197b46 commit 50c002d

File tree

2 files changed

+20
-40
lines changed

2 files changed

+20
-40
lines changed

Src/FluentAssertions.Json/JTokenAssertions.cs

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,7 @@ public JTokenAssertions(JToken subject, AssertionChain assertionChain)
5454
public AndConstraint<JTokenAssertions> BeEquivalentTo(string expected, string because = "",
5555
params object[] becauseArgs)
5656
{
57-
JToken parsedExpected;
58-
try
59-
{
60-
parsedExpected = JToken.Parse(expected);
61-
}
62-
catch (Exception ex)
63-
{
64-
throw new ArgumentException(
65-
$"Unable to parse expected JSON string:{Environment.NewLine}" +
66-
$"{expected}{Environment.NewLine}" +
67-
"Check inner exception for more details.",
68-
nameof(expected), ex);
69-
}
57+
JToken parsedExpected = Parse(expected, nameof(expected));
7058

7159
return BeEquivalentTo(parsedExpected, because, becauseArgs);
7260
}
@@ -150,19 +138,7 @@ private AndConstraint<JTokenAssertions> BeEquivalentTo(JToken expected, bool ign
150138
public AndConstraint<JTokenAssertions> NotBeEquivalentTo(string unexpected, string because = "",
151139
params object[] becauseArgs)
152140
{
153-
JToken parsedUnexpected;
154-
try
155-
{
156-
parsedUnexpected = JToken.Parse(unexpected);
157-
}
158-
catch (Exception ex)
159-
{
160-
throw new ArgumentException(
161-
$"Unable to parse unexpected JSON string:{Environment.NewLine}" +
162-
$"{unexpected}{Environment.NewLine}" +
163-
"Check inner exception for more details.",
164-
nameof(unexpected), ex);
165-
}
141+
JToken parsedUnexpected = Parse(unexpected, nameof(unexpected));
166142

167143
return NotBeEquivalentTo(parsedUnexpected, because, becauseArgs);
168144
}
@@ -448,19 +424,7 @@ public AndConstraint<JTokenAssertions> HaveCount(int expected, string because =
448424
/// </code>
449425
public AndConstraint<JTokenAssertions> ContainSubtree(string subtree, string because = "", params object[] becauseArgs)
450426
{
451-
JToken subtreeToken;
452-
try
453-
{
454-
subtreeToken = JToken.Parse(subtree);
455-
}
456-
catch (Exception ex)
457-
{
458-
throw new ArgumentException(
459-
$"Unable to parse expected JSON string:{Environment.NewLine}" +
460-
$"{subtree}{Environment.NewLine}" +
461-
"Check inner exception for more details.",
462-
nameof(subtree), ex);
463-
}
427+
JToken subtreeToken = Parse(subtree, nameof(subtree));
464428

465429
return ContainSubtree(subtreeToken, because, becauseArgs);
466430
}
@@ -496,6 +460,22 @@ public AndConstraint<JTokenAssertions> ContainSubtree(JToken subtree, string bec
496460
return BeEquivalentTo(subtree, true, options => options, because, becauseArgs);
497461
}
498462

463+
private static JToken Parse(string json, string paramName)
464+
{
465+
try
466+
{
467+
return JToken.Parse(json);
468+
}
469+
catch (Exception ex)
470+
{
471+
throw new ArgumentException(
472+
$"Unable to parse {paramName} JSON string:{Environment.NewLine}" +
473+
$"{json}{Environment.NewLine}" +
474+
"Check inner exception for more details.",
475+
paramName, ex);
476+
}
477+
}
478+
499479
#pragma warning disable CA1822 // Making this method static is a breaking chan
500480
public string Format(JToken value, bool useLineBreaks = false)
501481
{

Tests/FluentAssertions.Json.Specs/JTokenAssertionsSpecs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ public void When_checking_subtree_with_an_invalid_expected_string_it_should_prov
943943
// Act & Assert
944944
actualJson.Should().Invoking(x => x.ContainSubtree(invalidSubtree))
945945
.Should().Throw<ArgumentException>()
946-
.WithMessage($"Unable to parse expected JSON string:{invalidSubtree}*")
946+
.WithMessage($"Unable to parse subtree JSON string:{invalidSubtree}*")
947947
.WithInnerException<JsonReaderException>();
948948
}
949949

0 commit comments

Comments
 (0)