1
+ import unittest
2
+ from fluentcheck .check import Check , CheckError
3
+
4
+ class ParentA :
5
+ def __init__ (self ):
6
+ pass
7
+
8
+ class Child (ParentA ):
9
+ def __init__ (self ):
10
+ pass
11
+
12
+ class GrandChild (Child ):
13
+ def __init__ (self ):
14
+ pass
15
+
16
+ class ParentB :
17
+ def __init__ (self ):
18
+ pass
19
+
20
+ class ChildOfMultipleParents (ParentA , ParentB ):
21
+ def __init__ (self ):
22
+ pass
23
+
24
+ class TestTypeHierarchy (unittest .TestCase ):
25
+
26
+ def test_is_subtype_of (self ):
27
+ res = Check (Child ()).is_subtype_of (ParentA )
28
+ self .assertIsInstance (res , Check )
29
+ try :
30
+ Check (ParentA ()).is_subtype_of (Child )
31
+ self .fail ()
32
+ except CheckError :
33
+ pass
34
+
35
+ def test_is_subtype_of_when_grandchild_is_subtype_of_parent (self ):
36
+ res = Check (GrandChild ()).is_subtype_of (ParentA )
37
+ self .assertIsInstance (res , Check )
38
+ try :
39
+ Check (ParentA ()).is_subtype_of (GrandChild )
40
+ self .fail ()
41
+ except CheckError :
42
+ pass
43
+
44
+ def test_is_subtype_of_when_multiple_inheritance (self ):
45
+ res = Check (ChildOfMultipleParents ()).is_subtype_of ((ParentA , ParentB ))
46
+ self .assertIsInstance (res , Check )
47
+ try :
48
+ Check (ParentA ()).is_subtype_of ((ChildOfMultipleParents , ParentB ))
49
+ self .fail ()
50
+ except CheckError :
51
+ pass
52
+
53
+ def test_is_not_subtype_of (self ):
54
+ res = Check (ParentA ()).is_not_subtype_of (ParentB )
55
+ self .assertIsInstance (res , Check )
56
+ try :
57
+ Check (Child ()).is_not_subtype_of (ParentA )
58
+ self .fail ()
59
+ except CheckError :
60
+ pass
61
+
62
+ def test_is_subtype_of_itself (self ):
63
+ res = Check (Child ()).is_subtype_of (Child )
64
+ self .assertIsInstance (res , Check )
65
+ try :
66
+ Check (Child ()).is_not_subtype_of (Child )
67
+ self .fail ()
68
+ except CheckError :
69
+ pass
70
+
71
+ def test_is_subtype_of_atleast_one_parent (self ):
72
+ res = Check (Child ()).is_subtype_of ((ParentA , ParentB ))
73
+ self .assertIsInstance (res , Check )
74
+ try :
75
+ Check (Child ()).is_not_subtype_of ((ParentA , ParentB ))
76
+ self .fail ()
77
+ except CheckError :
78
+ pass
0 commit comments