@@ -720,3 +720,156 @@ class C failed to implement interface I:
720
720
)
721
721
722
722
assert actual_message == expected_message
723
+
724
+
725
+ @pytest .mark .parametrize ('name' , ['MyInterface' , None ])
726
+ def test_interface_from_class (name ):
727
+
728
+ class MyClass (object ): # pragma: nocover
729
+ def method1 (self , x ):
730
+ raise AssertionError ("method1 called" )
731
+
732
+ def method2 (self , y ):
733
+ raise AssertionError ("method2 called" )
734
+
735
+ iface = Interface .from_class (MyClass , name = name )
736
+
737
+ if name is None :
738
+ expected_name = 'MyClassInterface'
739
+ else :
740
+ expected_name = name
741
+
742
+ assert iface .__name__ == expected_name
743
+
744
+ with pytest .raises (InvalidImplementation ) as e :
745
+ class C (implements (iface )): # pragma: nocover
746
+ pass
747
+
748
+ actual_message = str (e .value )
749
+ expected_message = dedent (
750
+ """
751
+ class C failed to implement interface {iface}:
752
+
753
+ The following methods of {iface} were not implemented:
754
+ - method1(self, x)
755
+ - method2(self, y)"""
756
+ ).format (iface = expected_name )
757
+
758
+ assert actual_message == expected_message
759
+
760
+
761
+ def test_interface_from_class_method_subset ():
762
+
763
+ class C (object ): # pragma: nocover
764
+
765
+ def method1 (self , x ):
766
+ pass
767
+
768
+ def method2 (self , y ):
769
+ pass
770
+
771
+ iface = Interface .from_class (C , subset = ['method1' ])
772
+
773
+ class Impl (implements (iface )): # pragma: nocover
774
+ def method1 (self , x ):
775
+ pass
776
+
777
+ with pytest .raises (InvalidImplementation ) as e :
778
+
779
+ class BadImpl (implements (iface )): # pragma: nocover
780
+ def method2 (self , y ):
781
+ pass
782
+
783
+ actual_message = str (e .value )
784
+ expected_message = dedent (
785
+ """
786
+ class BadImpl failed to implement interface CInterface:
787
+
788
+ The following methods of CInterface were not implemented:
789
+ - method1(self, x)"""
790
+ )
791
+
792
+ assert actual_message == expected_message
793
+
794
+
795
+ def test_interface_from_class_inherited_methods ():
796
+
797
+ class Base (object ): # pragma: nocover
798
+ def base_method (self , x ):
799
+ pass
800
+
801
+ class Derived (Base ): # pragma: nocover
802
+ def derived_method (self , y ):
803
+ pass
804
+
805
+ iface = Interface .from_class (Derived )
806
+
807
+ # Should be fine
808
+ class Impl (implements (iface )): # pragma: nocover
809
+ def base_method (self , x ):
810
+ pass
811
+
812
+ def derived_method (self , y ):
813
+ pass
814
+
815
+ with pytest .raises (InvalidImplementation ) as e :
816
+
817
+ class BadImpl (implements (iface )): # pragma: nocover
818
+ def derived_method (self , y ):
819
+ pass
820
+
821
+ actual_message = str (e .value )
822
+ expected_message = dedent (
823
+ """
824
+ class BadImpl failed to implement interface DerivedInterface:
825
+
826
+ The following methods of DerivedInterface were not implemented:
827
+ - base_method(self, x)"""
828
+ )
829
+ assert actual_message == expected_message
830
+
831
+ with pytest .raises (InvalidImplementation ) as e :
832
+
833
+ class BadImpl (implements (iface )): # pragma: nocover
834
+ def base_method (self , x ):
835
+ pass
836
+
837
+ actual_message = str (e .value )
838
+ expected_message = dedent (
839
+ """
840
+ class BadImpl failed to implement interface DerivedInterface:
841
+
842
+ The following methods of DerivedInterface were not implemented:
843
+ - derived_method(self, y)"""
844
+ )
845
+
846
+ assert actual_message == expected_message
847
+
848
+
849
+ def test_interface_from_class_magic_methods ():
850
+
851
+ class HasMagicMethods (object ): # pragma: nocover
852
+ def __getitem__ (self , key ):
853
+ return key
854
+
855
+ iface = Interface .from_class (HasMagicMethods )
856
+
857
+ # Should be fine
858
+ class Impl (implements (iface )): # pragma: nocover
859
+ def __getitem__ (self , key ):
860
+ return key
861
+
862
+ with pytest .raises (InvalidImplementation ) as e :
863
+
864
+ class BadImpl (implements (iface )): # pragma: nocover
865
+ pass
866
+
867
+ actual_message = str (e .value )
868
+ expected_message = dedent (
869
+ """
870
+ class BadImpl failed to implement interface HasMagicMethodsInterface:
871
+
872
+ The following methods of HasMagicMethodsInterface were not implemented:
873
+ - __getitem__(self, key)"""
874
+ )
875
+ assert actual_message == expected_message
0 commit comments