Skip to content

Commit 13ee308

Browse files
authored
Merge pull request #10 from deanagan/master
Added tests and fixes for type hierarchy
2 parents e5e3795 + 3bed48e commit 13ee308

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-3
lines changed

fluentcheck/assertions/types.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ def is_subtype_of(check_obj, _type):
1212

1313
def is_not_subtype_of(check_obj, _type):
1414
try:
15-
assert issubclass(check_obj._val.__class__, _type)
16-
raise CheckError('{} is subtype of {}'.format(check_obj._val, _type))
17-
except AssertionError:
15+
assert not issubclass(check_obj._val.__class__, _type)
1816
return check_obj
17+
except AssertionError:
18+
raise CheckError('{} is subtype of {}'.format(check_obj._val, _type))
1919

2020

2121
def is_of_type(check_obj, _type):
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)