Skip to content

Commit 39a4049

Browse files
author
Ronald Kroon
committed
Add HaveCount assertion to JToken
1 parent 5048a87 commit 39a4049

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

Src/FluentAssertions.Json.Shared/JTokenAssertions.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,5 +313,28 @@ public AndWhichConstraint<JTokenAssertions, JToken> ContainSingleItem(string bec
313313
return new AndWhichConstraint<JTokenAssertions, JToken>(this, constraint.Which);
314314
}
315315
}
316+
317+
/// <summary>
318+
/// Asserts that the number of items in the current <see cref="JToken" /> matches the supplied <paramref name="expected" /> amount.
319+
/// </summary>
320+
/// <param name="expected">The expected number of items in the current <see cref="JToken" />.</param>
321+
/// <param name="because">
322+
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
323+
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
324+
/// </param>
325+
/// <param name="becauseArgs">
326+
/// Zero or more objects to format using the placeholders in <see cref="because" />.
327+
/// </param>
328+
public AndConstraint<JTokenAssertions> HaveCount(int expected, string because = "", params object[] becauseArgs)
329+
{
330+
var formatter = new JTokenFormatter();
331+
string formattedDocument = formatter.ToString(Subject).Replace("{", "{{").Replace("}", "}}");
332+
333+
using (new AssertionScope("JSON document " + formattedDocument))
334+
{
335+
EnumerableSubject.HaveCount(expected, because, becauseArgs);
336+
return new AndConstraint<JTokenAssertions>(this);
337+
}
338+
}
316339
}
317340
}

Tests/FluentAssertions.Json.Shared.Specs/JTokenAssertionsSpecs.cs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,5 +552,126 @@ public void When_jtoken_is_an_array_with_multiple_items_ContainSingleItem_should
552552
}
553553

554554
#endregion ContainSingleItem
555+
556+
#region HaveCount
557+
558+
[TestMethod]
559+
public void When_expecting_the_actual_number_of_elements_HaveCount_should_succeed()
560+
{
561+
//-----------------------------------------------------------------------------------------------------------
562+
// Arrange
563+
//-----------------------------------------------------------------------------------------------------------
564+
var subject = JToken.Parse("{ id: 42, admin: true }");
565+
566+
//-----------------------------------------------------------------------------------------------------------
567+
// Act
568+
//-----------------------------------------------------------------------------------------------------------
569+
Action act = () => subject.Should().HaveCount(2);
570+
571+
//-----------------------------------------------------------------------------------------------------------
572+
// Assert
573+
//-----------------------------------------------------------------------------------------------------------
574+
act.ShouldNotThrow();
575+
}
576+
577+
[TestMethod]
578+
public void When_expecting_the_actual_number_of_elements_HaveCount_should_enable_consecutive_assertions()
579+
{
580+
//-----------------------------------------------------------------------------------------------------------
581+
// Arrange
582+
//-----------------------------------------------------------------------------------------------------------
583+
var subject = JToken.Parse("{ id: 42 }");
584+
585+
//-----------------------------------------------------------------------------------------------------------
586+
// Act
587+
//-----------------------------------------------------------------------------------------------------------
588+
JTokenAssertions and = subject.Should().HaveCount(1).And;
589+
590+
//-----------------------------------------------------------------------------------------------------------
591+
// Assert
592+
//-----------------------------------------------------------------------------------------------------------
593+
and.Be(subject);
594+
}
595+
596+
[TestMethod]
597+
public void When_jtoken_is_null_HaveCount_should_fail()
598+
{
599+
//-----------------------------------------------------------------------------------------------------------
600+
// Arrange
601+
//-----------------------------------------------------------------------------------------------------------
602+
JToken subject = null;
603+
604+
//-----------------------------------------------------------------------------------------------------------
605+
// Act
606+
//-----------------------------------------------------------------------------------------------------------
607+
Action act = () => subject.Should().HaveCount(1, "null is not allowed");
608+
609+
//-----------------------------------------------------------------------------------------------------------
610+
// Assert
611+
//-----------------------------------------------------------------------------------------------------------
612+
act.ShouldThrow<AssertFailedException>()
613+
.WithMessage($"Expected JSON document <null> to contain 1 item(s) because null is not allowed, but found <null>.");
614+
}
615+
616+
[TestMethod]
617+
public void When_expecting_a_different_number_of_elements_than_the_actual_number_HaveCount_should_fail()
618+
{
619+
//-----------------------------------------------------------------------------------------------------------
620+
// Arrange
621+
//-----------------------------------------------------------------------------------------------------------
622+
var subject = JToken.Parse("{ }");
623+
624+
//-----------------------------------------------------------------------------------------------------------
625+
// Act
626+
//-----------------------------------------------------------------------------------------------------------
627+
Action act = () => subject.Should().HaveCount(1, "numbers matter");
628+
629+
//-----------------------------------------------------------------------------------------------------------
630+
// Assert
631+
//-----------------------------------------------------------------------------------------------------------
632+
act.ShouldThrow<AssertFailedException>()
633+
.WithMessage($"Expected JSON document * to contain 1 item(s) because numbers matter, but found 0.");
634+
}
635+
636+
[TestMethod]
637+
public void When_expecting_the_actual_number_of_array_items_HaveCount_should_succeed()
638+
{
639+
//-----------------------------------------------------------------------------------------------------------
640+
// Arrange
641+
//-----------------------------------------------------------------------------------------------------------
642+
var subject = JToken.Parse("[ 'Hello', 'World!' ]");
643+
644+
//-----------------------------------------------------------------------------------------------------------
645+
// Act
646+
//-----------------------------------------------------------------------------------------------------------
647+
Action act = () => subject.Should().HaveCount(2);
648+
649+
//-----------------------------------------------------------------------------------------------------------
650+
// Assert
651+
//-----------------------------------------------------------------------------------------------------------
652+
act.ShouldNotThrow();
653+
}
654+
655+
[TestMethod]
656+
public void When_expecting_a_different_number_of_array_items_than_the_actual_number_HaveCount_should_fail()
657+
{
658+
//-----------------------------------------------------------------------------------------------------------
659+
// Arrange
660+
//-----------------------------------------------------------------------------------------------------------
661+
var subject = JToken.Parse("[ 'Hello', 'World!' ]");
662+
663+
//-----------------------------------------------------------------------------------------------------------
664+
// Act
665+
//-----------------------------------------------------------------------------------------------------------
666+
Action act = () => subject.Should().HaveCount(3, "the more the better");
667+
668+
//-----------------------------------------------------------------------------------------------------------
669+
// Assert
670+
//-----------------------------------------------------------------------------------------------------------
671+
act.ShouldThrow<AssertFailedException>()
672+
.WithMessage($"Expected JSON document * to contain 3 item(s) because the more the better, but found 2.");
673+
}
674+
675+
#endregion HaveCount
555676
}
556677
}

0 commit comments

Comments
 (0)