@@ -435,176 +435,103 @@ void assertDoesNotContainNull(AssertionInfo info, Failures failures, Object arra
435
435
436
436
public <E > void assertAre (AssertionInfo info , Failures failures , Conditions conditions , Object array ,
437
437
Condition <E > condition ) {
438
- assertNotNull (info , array );
439
- conditions .assertIsNotNull (condition );
440
- try {
441
- List <E > notSatisfiesCondition = elementsNotSatisfyingCondition (array , condition );
442
- if (!notSatisfiesCondition .isEmpty ())
443
- throw failures .failure (info , elementsShouldBe (array , notSatisfiesCondition , condition ));
444
- } catch (ClassCastException e ) {
445
- throw failures .failure (info , shouldBeSameGenericBetweenIterableAndCondition (array , condition ));
438
+ List <E > notMatchingCondition = getElementsNotMatchingCondition (info , failures , conditions , array , condition );
439
+ if (!notMatchingCondition .isEmpty ())
440
+ throw failures .failure (info , elementsShouldBe (array , notMatchingCondition , condition ));
446
441
}
447
- }
448
442
449
443
public <E > void assertAreNot (AssertionInfo info , Failures failures , Conditions conditions , Object array ,
450
444
Condition <E > condition ) {
451
- assertNotNull (info , array );
452
- conditions .assertIsNotNull (condition );
453
- try {
454
- List <E > satisfiesCondition = elementsSatisfyingCondition (array , condition );
455
- if (satisfiesCondition .isEmpty ()) {
456
- return ;
457
- }
458
- throw failures .failure (info , elementsShouldNotBe (array , satisfiesCondition , condition ));
459
- } catch (ClassCastException e ) {
460
- throw failures .failure (info , shouldBeSameGenericBetweenIterableAndCondition (array , condition ));
445
+ List <E > matchingElements = getElementsMatchingCondition (info , failures , conditions , array , condition );
446
+ if (!matchingElements .isEmpty ())
447
+ throw failures .failure (info , elementsShouldNotBe (array , matchingElements , condition ));
461
448
}
462
- }
463
449
464
450
public <E > void assertHave (AssertionInfo info , Failures failures , Conditions conditions , Object array ,
465
451
Condition <E > condition ) {
466
- assertNotNull (info , array );
467
- conditions .assertIsNotNull (condition );
468
- try {
469
- List <E > notSatisfiesCondition = elementsNotSatisfyingCondition (array , condition );
470
- if (notSatisfiesCondition .isEmpty ()) {
471
- return ;
472
- }
473
- throw failures .failure (info , elementsShouldHave (array , notSatisfiesCondition , condition ));
474
- } catch (ClassCastException e ) {
475
- throw failures .failure (info , shouldBeSameGenericBetweenIterableAndCondition (array , condition ));
452
+ List <E > notMatchingCondition = getElementsNotMatchingCondition (info , failures , conditions , array , condition );
453
+ if (!notMatchingCondition .isEmpty ())
454
+ throw failures .failure (info , elementsShouldHave (array , notMatchingCondition , condition ));
476
455
}
477
- }
478
456
479
457
public <E > void assertHaveNot (AssertionInfo info , Failures failures , Conditions conditions , Object array ,
480
458
Condition <E > condition ) {
481
- assertNotNull (info , array );
482
- conditions .assertIsNotNull (condition );
483
- try {
484
- List <E > satisfiesCondition = elementsSatisfyingCondition (array , condition );
485
- if (satisfiesCondition .isEmpty ()) {
486
- return ;
487
- }
488
- throw failures .failure (info , elementsShouldNotHave (array , satisfiesCondition , condition ));
489
- } catch (ClassCastException e ) {
490
- throw failures .failure (info , shouldBeSameGenericBetweenIterableAndCondition (array , condition ));
491
- }
459
+ List <E > matchingElements = getElementsMatchingCondition (info , failures , conditions , array , condition );
460
+ if (!matchingElements .isEmpty ())
461
+ throw failures .failure (info , elementsShouldNotHave (array , matchingElements , condition ));
492
462
}
493
463
494
464
public <E > void assertAreAtLeast (AssertionInfo info , Failures failures , Conditions conditions , Object array ,
495
465
int times , Condition <E > condition ) {
496
- assertNotNull (info , array );
497
- conditions .assertIsNotNull (condition );
498
- try {
499
- List <E > satisfiesCondition = elementsSatisfyingCondition (array , condition );
500
- if (satisfiesCondition .size () >= times ) {
501
- return ;
502
- }
466
+ List <E > matchingElements = getElementsMatchingCondition (info , failures , conditions , array , condition );
467
+ if (matchingElements .size () < times )
503
468
throw failures .failure (info , elementsShouldBeAtLeast (array , times , condition ));
504
- } catch (ClassCastException e ) {
505
- throw failures .failure (info , shouldBeSameGenericBetweenIterableAndCondition (array , condition ));
506
469
}
507
- }
508
470
509
471
public <E > void assertAreAtMost (AssertionInfo info , Failures failures , Conditions conditions , Object array ,
510
472
int times , Condition <E > condition ) {
511
- assertNotNull (info , array );
512
- conditions .assertIsNotNull (condition );
513
- try {
514
- List <E > satisfiesCondition = elementsSatisfyingCondition (array , condition );
515
- if (satisfiesCondition .size () <= times ) {
516
- return ;
517
- }
518
- throw failures .failure (info , elementsShouldBeAtMost (array , times , condition ));
519
- } catch (ClassCastException e ) {
520
- throw failures .failure (info , shouldBeSameGenericBetweenIterableAndCondition (array , condition ));
473
+ List <E > matchingElements = getElementsMatchingCondition (info , failures , conditions , array , condition );
474
+ if (matchingElements .size () > times ) throw failures .failure (info , elementsShouldBeAtMost (array , times , condition ));
521
475
}
522
- }
523
476
524
477
public <E > void assertAreExactly (AssertionInfo info , Failures failures , Conditions conditions , Object array ,
525
478
int times , Condition <E > condition ) {
526
- assertNotNull (info , array );
527
- conditions .assertIsNotNull (condition );
528
- try {
529
- List <E > satisfiesCondition = elementsSatisfyingCondition (array , condition );
530
- if (satisfiesCondition .size () == times ) {
531
- return ;
532
- }
479
+ List <E > matchingElements = getElementsMatchingCondition (info , failures , conditions , array , condition );
480
+ if (matchingElements .size () != times )
533
481
throw failures .failure (info , elementsShouldBeExactly (array , times , condition ));
534
- } catch (ClassCastException e ) {
535
- throw failures .failure (info , shouldBeSameGenericBetweenIterableAndCondition (array , condition ));
536
482
}
537
- }
538
483
539
484
public <E > void assertHaveAtLeast (AssertionInfo info , Failures failures , Conditions conditions , Object array ,
540
485
int times , Condition <E > condition ) {
541
- assertNotNull (info , array );
542
- conditions .assertIsNotNull (condition );
543
- try {
544
- List <E > satisfiesCondition = elementsSatisfyingCondition (array , condition );
545
- if (satisfiesCondition .size () >= times ) {
546
- return ;
547
- }
486
+ List <E > matchingElements = getElementsMatchingCondition (info , failures , conditions , array , condition );
487
+ if (matchingElements .size () < times )
548
488
throw failures .failure (info , elementsShouldHaveAtLeast (array , times , condition ));
549
- } catch (ClassCastException e ) {
550
- throw failures .failure (info , shouldBeSameGenericBetweenIterableAndCondition (array , condition ));
551
- }
489
+
552
490
}
553
491
554
492
public <E > void assertHaveAtMost (AssertionInfo info , Failures failures , Conditions conditions , Object array ,
555
493
int times , Condition <E > condition ) {
556
- assertNotNull (info , array );
557
- conditions .assertIsNotNull (condition );
558
- try {
559
- List <E > satisfiesCondition = elementsSatisfyingCondition (array , condition );
560
- if (satisfiesCondition .size () <= times ) {
561
- return ;
562
- }
494
+ List <E > matchingElements = getElementsMatchingCondition (info , failures , conditions , array , condition );
495
+ if (matchingElements .size () > times )
563
496
throw failures .failure (info , elementsShouldHaveAtMost (array , times , condition ));
564
- } catch (ClassCastException e ) {
565
- throw failures .failure (info , shouldBeSameGenericBetweenIterableAndCondition (array , condition ));
566
- }
497
+
567
498
}
568
499
569
500
public <E > void assertHaveExactly (AssertionInfo info , Failures failures , Conditions conditions , Object array ,
570
501
int times , Condition <E > condition ) {
502
+ List <E > matchingElements = getElementsMatchingCondition (info , failures , conditions , array , condition );
503
+ if (matchingElements .size () != times )
504
+ throw failures .failure (info , elementsShouldHaveExactly (array , times , condition ));
505
+ }
506
+
507
+ private <E > List <E > getElementsMatchingCondition (AssertionInfo info , Failures failures , Conditions conditions ,
508
+ Object array , Condition <E > condition ) {
509
+ return filterElements (info , failures , conditions , array , condition , false );
510
+ }
511
+
512
+ private <E > List <E > getElementsNotMatchingCondition (AssertionInfo info , Failures failures , Conditions conditions ,
513
+ Object array , Condition <E > condition ) {
514
+ return filterElements (info , failures , conditions , array , condition , true );
515
+ }
516
+
517
+ @ SuppressWarnings ("unchecked" )
518
+ private <E > List <E > filterElements (AssertionInfo info , Failures failures , Conditions conditions , Object array ,
519
+ Condition <E > condition , boolean negateCondition ) throws AssertionError {
571
520
assertNotNull (info , array );
572
521
conditions .assertIsNotNull (condition );
573
522
try {
574
- List <E > satisfiesCondition = elementsSatisfyingCondition (array , condition );
575
- if (satisfiesCondition .size () == times ) {
576
- return ;
523
+ List <E > filteredElements = new LinkedList <E >();
524
+ int arraySize = sizeOf (array );
525
+ for (int i = 0 ; i < arraySize ; i ++) {
526
+ E element = (E ) Array .get (array , i );
527
+ if (negateCondition ? !condition .matches (element ) : condition .matches (element )) filteredElements .add (element );
577
528
}
578
- throw failures . failure ( info , elementsShouldHaveExactly ( array , times , condition )) ;
529
+ return filteredElements ;
579
530
} catch (ClassCastException e ) {
580
531
throw failures .failure (info , shouldBeSameGenericBetweenIterableAndCondition (array , condition ));
581
532
}
582
533
}
583
534
584
- @ SuppressWarnings ("unchecked" )
585
- private <E > List <E > elementsNotSatisfyingCondition (Object array , Condition <E > condition ) {
586
- List <E > elementsNotSatisfyingCondition = new LinkedList <E >();
587
- int arraySize = sizeOf (array );
588
- for (int i = 0 ; i < arraySize ; i ++) {
589
- Object o = Array .get (array , i );
590
- if (!condition .matches ((E ) o ))
591
- elementsNotSatisfyingCondition .add ((E ) o );
592
- }
593
- return elementsNotSatisfyingCondition ;
594
- }
595
-
596
- @ SuppressWarnings ("unchecked" )
597
- private <E > List <E > elementsSatisfyingCondition (Object array , Condition <E > condition ) {
598
- List <E > elementsSatisfyingCondition = new LinkedList <E >();
599
- int arraySize = sizeOf (array );
600
- for (int i = 0 ; i < arraySize ; i ++) {
601
- Object o = Array .get (array , i );
602
- if (condition .matches ((E ) o ))
603
- elementsSatisfyingCondition .add ((E ) o );
604
- }
605
- return elementsSatisfyingCondition ;
606
- }
607
-
608
535
void assertIsSorted (AssertionInfo info , Failures failures , Object array ) {
609
536
assertNotNull (info , array );
610
537
if (comparisonStrategy instanceof ComparatorBasedComparisonStrategy ) {
0 commit comments