@@ -484,6 +484,204 @@ func TestStream_GroupBy(t *testing.T) {
484
484
}
485
485
}
486
486
487
+ func TestStream_Count (t * testing.T ) {
488
+ tt := map [string ]struct {
489
+ stream chan int
490
+ want int
491
+ }{
492
+ "Should return 0 for a nil channel" : {
493
+ stream : nil ,
494
+ want : 0 ,
495
+ },
496
+ "Should return 0 for an empty closed channel" : {
497
+ stream : func () chan int {
498
+ c := make (chan int )
499
+ go func () {
500
+ defer close (c )
501
+ }()
502
+ return c
503
+ }(),
504
+ want : 0 ,
505
+ },
506
+ "Should return 3 for a size 3 closed channel" : {
507
+ stream : func () chan int {
508
+ c := make (chan int , 1 )
509
+ go func () {
510
+ defer close (c )
511
+ c <- 1
512
+ c <- 2
513
+ c <- 1
514
+ }()
515
+ return c
516
+ }(),
517
+ want : 3 ,
518
+ },
519
+ }
520
+
521
+ for name , tc := range tt {
522
+ tc := tc
523
+
524
+ t .Run (name , func (t * testing.T ) {
525
+ s := Stream [int ]{
526
+ stream : tc .stream ,
527
+ }
528
+ if got := s .Count (); got != tc .want {
529
+ t .Errorf ("Stream.Count() = %v, want %v" , got , tc .want )
530
+ }
531
+ })
532
+ }
533
+ }
534
+
535
+ func TestStream_AnyMatch (t * testing.T ) {
536
+ dataGenerator := func () chan any {
537
+ c := make (chan any , 2 )
538
+ go func () {
539
+ defer close (c )
540
+ c <- "a"
541
+ c <- false
542
+ c <- "b"
543
+ c <- - 17
544
+ c <- "c"
545
+ }()
546
+ return c
547
+ }
548
+
549
+ tt := map [string ]struct {
550
+ stream chan any
551
+ predicate Predicate [any ]
552
+ want bool
553
+ }{
554
+ "Should not match any when channel is nil" : {
555
+ stream : nil ,
556
+ predicate : True [any ](),
557
+ want : false ,
558
+ },
559
+ "Should not match any" : {
560
+ stream : dataGenerator (),
561
+ predicate : func (e any ) bool { return e == "not in here" },
562
+ want : false ,
563
+ },
564
+ "Should match any" : {
565
+ stream : dataGenerator (),
566
+ predicate : func (e any ) bool { return e == "b" },
567
+ want : true ,
568
+ },
569
+ }
570
+
571
+ for name , tc := range tt {
572
+ tc := tc
573
+
574
+ t .Run (name , func (t * testing.T ) {
575
+ s := Stream [any ]{
576
+ stream : tc .stream ,
577
+ }
578
+ if got := s .AnyMatch (tc .predicate ); got != tc .want {
579
+ t .Errorf ("Stream.AnyMatch() = %v, want %v" , got , tc .want )
580
+ }
581
+ })
582
+ }
583
+ }
584
+
585
+ func TestStream_NoneMatch (t * testing.T ) {
586
+ dataGenerator := func () chan any {
587
+ c := make (chan any , 2 )
588
+ go func () {
589
+ defer close (c )
590
+ c <- "a"
591
+ c <- false
592
+ c <- "b"
593
+ c <- - 17
594
+ c <- "c"
595
+ }()
596
+ return c
597
+ }
598
+
599
+ tt := map [string ]struct {
600
+ stream chan any
601
+ predicate Predicate [any ]
602
+ want bool
603
+ }{
604
+ "Should satisfy when channel is nil" : {
605
+ stream : nil ,
606
+ predicate : True [any ](),
607
+ want : true ,
608
+ },
609
+ "Should satisfy" : {
610
+ stream : dataGenerator (),
611
+ predicate : func (e any ) bool { return e == "not in here" },
612
+ want : true ,
613
+ },
614
+ "Should not satisfy" : {
615
+ stream : dataGenerator (),
616
+ predicate : func (e any ) bool { return e == "b" },
617
+ want : false ,
618
+ },
619
+ }
620
+
621
+ for name , tc := range tt {
622
+ tc := tc
623
+
624
+ t .Run (name , func (t * testing.T ) {
625
+ s := Stream [any ]{
626
+ stream : tc .stream ,
627
+ }
628
+ if got := s .NoneMatch (tc .predicate ); got != tc .want {
629
+ t .Errorf ("Stream.NoneMatch() = %v, want %v" , got , tc .want )
630
+ }
631
+ })
632
+ }
633
+ }
634
+
635
+ func TestStream_AllMatch (t * testing.T ) {
636
+ dataGenerator := func () chan any {
637
+ c := make (chan any , 2 )
638
+ go func () {
639
+ defer close (c )
640
+ c <- "a"
641
+ c <- false
642
+ c <- "b"
643
+ c <- - 17
644
+ c <- "c"
645
+ }()
646
+ return c
647
+ }
648
+
649
+ tt := map [string ]struct {
650
+ stream chan any
651
+ predicate Predicate [any ]
652
+ want bool
653
+ }{
654
+ "Should not match all when channel is nil" : {
655
+ stream : nil ,
656
+ predicate : True [any ](),
657
+ want : false ,
658
+ },
659
+ "Should match all" : {
660
+ stream : dataGenerator (),
661
+ predicate : func (e any ) bool { return e != "not in here" },
662
+ want : true ,
663
+ },
664
+ "Should not match all" : {
665
+ stream : dataGenerator (),
666
+ predicate : func (e any ) bool { return e == "b" },
667
+ want : false ,
668
+ },
669
+ }
670
+
671
+ for name , tc := range tt {
672
+ tc := tc
673
+
674
+ t .Run (name , func (t * testing.T ) {
675
+ s := Stream [any ]{
676
+ stream : tc .stream ,
677
+ }
678
+ if got := s .AllMatch (tc .predicate ); got != tc .want {
679
+ t .Errorf ("Stream.AllMatch() = %v, want %v" , got , tc .want )
680
+ }
681
+ })
682
+ }
683
+ }
684
+
487
685
func TestStream_ForEach (t * testing.T ) {
488
686
computeSumTotal := func (callCount , total * int ) Consumer [int ] {
489
687
return func (value int ) {
0 commit comments