@@ -712,6 +712,30 @@ def test_max2_overload2(self):
712
712
en = Enumerable (lst )
713
713
assert en .max2 (lambda x : x [0 ], object ) == 4
714
714
715
+
716
+ class TestMaxByMethod :
717
+ def test_overload1 (self ):
718
+ strings = ['aaa' , 'bb' , 'c' , 'dddd' ]
719
+ en = Enumerable (strings )
720
+ assert en .max_by (len ) == 'dddd'
721
+
722
+ def test_overload1_empty (self ):
723
+ strings : List [str ] = []
724
+ en = Enumerable (strings )
725
+ with pytest .raises (InvalidOperationError ):
726
+ en .max_by (len )
727
+
728
+ def test_dup_choose_first (self ):
729
+ strings = ['foo' , 'cheese' , 'baz' , 'spam' , 'orange' , 'string' ]
730
+ en = Enumerable (strings )
731
+ assert en .max_by (len ) == 'cheese'
732
+
733
+ def test_overload2 (self ):
734
+ lst = [['foo' ], ['cheese' ], ['baz' ], ['spam' ], ['string' ]]
735
+ en = Enumerable (lst )
736
+ assert en .max_by (lambda x : x [0 ], lambda x , y : len (x ) - len (y )) == ['cheese' ]
737
+
738
+
715
739
class TestMinMethod :
716
740
def test_min_overload1 (self ):
717
741
nums = (1 , 0.4 , 2.2 , 5 , 1 , 2 )
@@ -757,6 +781,32 @@ def test_min2_overload2(self):
757
781
assert en .min2 (lambda x : x [0 ], object ) == - 11
758
782
759
783
784
+ class TestMinByMethod :
785
+ def test_overload1 (self ):
786
+ strings = ['aaa' , 'bb' , 'c' , 'dddd' ]
787
+ en = Enumerable (strings )
788
+ assert en .min_by (len ) == 'c'
789
+
790
+ def test_overload1_empty (self ):
791
+ strings : List [str ] = []
792
+ en = Enumerable (strings )
793
+ with pytest .raises (InvalidOperationError ):
794
+ en .min_by (len )
795
+
796
+ def test_dup_choose_first (self ):
797
+ class MyType :
798
+ def __init__ (self , x : int ): self .x = x
799
+ lst = [MyType (2 ), MyType (7 ), MyType (19 ), MyType (1 ), MyType (7 ), MyType (1 )]
800
+ en = Enumerable (lst )
801
+ assert en .min_by (lambda x : x .x ) == lst [3 ]
802
+ assert en .min_by (lambda x : x .x ) != lst [- 1 ]
803
+
804
+ def test_overload2 (self ):
805
+ lst = [['foo' ], ['cheese' ], ['baz' ], ['spam' ], ['string' ]]
806
+ en = Enumerable (lst )
807
+ assert en .min_by (lambda x : x [0 ], lambda x , y : len (x ) - len (y )) == ['foo' ]
808
+
809
+
760
810
class TestOfTypeMethod :
761
811
def test_of_type (self ):
762
812
lst = [1 , 5 , 4.4 , object (), 5.6 , - 12.2 , [], 2.2 , False ]
0 commit comments