@@ -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